From 401710883b593c732e4fc30cbe931594a8b0fd84 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 6 Nov 2017 11:50:43 +0000 Subject: [PATCH] MDL-60705 core_search: Unit tests get time wrong by factor 1 million --- search/tests/fixtures/mock_search_engine.php | 4 ++-- search/tests/manager_test.php | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/search/tests/fixtures/mock_search_engine.php b/search/tests/fixtures/mock_search_engine.php index 35ea923f9a3..235082f8990 100644 --- a/search/tests/fixtures/mock_search_engine.php +++ b/search/tests/fixtures/mock_search_engine.php @@ -31,7 +31,7 @@ defined('MOODLE_INTERNAL') || die; class engine extends \core_search\engine { - /** @var int If set, waits when adding each document (microseconds) */ + /** @var float If set, waits when adding each document (seconds) */ protected $adddelay = 0; /** @var \core_search\document[] Documents added */ @@ -83,7 +83,7 @@ class engine extends \core_search\engine { * @param float $seconds Delay in seconds for each document */ public function set_add_delay($seconds) { - $this->adddelay = (int)($seconds * 1000000); + $this->adddelay = $seconds; } /** diff --git a/search/tests/manager_test.php b/search/tests/manager_test.php index 6ad9121f498..607c6413b11 100644 --- a/search/tests/manager_test.php +++ b/search/tests/manager_test.php @@ -218,6 +218,9 @@ class search_manager_testcase extends advanced_testcase { $generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id, 'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now + 2, 'name' => 'Zombie']); + $generator->get_plugin_generator('mod_forum')->create_discussion(['course' => $course->id, + 'forum' => $forum->id, 'userid' => $USER->id, 'timemodified' => $now + 2, + 'name' => 'Werewolf']); time_sleep_until($now + 3); // Clear the count of added documents. @@ -253,15 +256,18 @@ class search_manager_testcase extends advanced_testcase { $this->assertCount(1, $added); $this->assertEquals('Vampire', $added[0]->get('title')); - // Index again with a 2 second limit - it will redo last post for safety (because of other + // Index again with a 3 second limit - it will redo last post for safety (because of other // things possibly having the same time second), and then do the remaining one. (Note: // because it always does more than one second worth of items, it would actually index 2 - // posts even if the limit were less than 2.) - $search->index(false, 2); + // posts even if the limit were less than 2, we are testing it does 3 posts to make sure + // the time limiting is actually working with the specified time.) + $search->index(false, 3); $added = $search->get_engine()->get_and_clear_added_documents(); - $this->assertCount(2, $added); + $this->assertCount(3, $added); $this->assertEquals('Toad', $added[0]->get('title')); - $this->assertEquals('Zombie', $added[1]->get('title')); + $remainingtitles = [$added[1]->get('title'), $added[2]->get('title')]; + sort($remainingtitles); + $this->assertEquals(['Werewolf', 'Zombie'], $remainingtitles); $this->assertFalse(get_config($componentname, $varname . '_partial')); // Index again - there should be nothing to index this time.