From 7f83e99e5fa0e0ce82a41e7658b3c6a42b48081f Mon Sep 17 00:00:00 2001 From: Adam Olley Date: Tue, 7 Nov 2017 00:24:51 +1030 Subject: [PATCH] MDL-60707 core_search: In tests, use faked time for indexing length Without this change its possible that the unit tests will fail at any time. Before this change the indexing time is measured by real-time, not fake time, making all index timings 0. This happens as PHP offers no guarantee around the sort-order of an array for any given two members that equate as equal. It just happens to pass for the current array of search areas in vanilla Moodle. --- search/classes/manager.php | 4 ++-- search/tests/manager_test.php | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/search/classes/manager.php b/search/classes/manager.php index 8760bf46d9f..fc5a7188e10 100644 --- a/search/classes/manager.php +++ b/search/classes/manager.php @@ -686,7 +686,7 @@ class manager { // Notify the engine that an area is starting. $this->engine->area_index_starting($searcharea, $fullindex); - $indexingstart = time(); + $indexingstart = (int)self::get_current_time(); $elapsed = self::get_current_time(); // This is used to store this component config. @@ -751,7 +751,7 @@ class manager { // Store last index run once documents have been committed to the search engine. set_config($varname . '_indexingstart', $indexingstart, $componentconfigname); - set_config($varname . '_indexingend', time(), $componentconfigname); + set_config($varname . '_indexingend', (int)self::get_current_time(), $componentconfigname); set_config($varname . '_docsignored', $numdocsignored, $componentconfigname); set_config($varname . '_docsprocessed', $numdocs, $componentconfigname); set_config($varname . '_recordsprocessed', $numrecords, $componentconfigname); diff --git a/search/tests/manager_test.php b/search/tests/manager_test.php index 607c6413b11..95fa82c1f47 100644 --- a/search/tests/manager_test.php +++ b/search/tests/manager_test.php @@ -240,6 +240,9 @@ class search_manager_testcase extends advanced_testcase { $this->assertEquals('Frog', $added[0]->get('title')); $this->assertEquals('Toad', $added[1]->get('title')); $this->assertEquals(1, get_config($componentname, $varname . '_partial')); + // Whilst 2.4 seconds of "time" have elapsed, the indexing duration is + // measured in seconds, so should be 2. + $this->assertEquals(2, $searcharea->get_last_indexing_duration()); // Add a label. $generator->create_module('label', ['course' => $course->id, 'intro' => 'Vampire']);