Merge branch 'MDL-55944' of https://github.com/mr-russ/moodle
authorDan Poltawski <dan@moodle.com>
Mon, 19 Sep 2016 10:25:12 +0000 (11:25 +0100)
committerDan Poltawski <dan@moodle.com>
Mon, 19 Sep 2016 10:25:12 +0000 (11:25 +0100)
admin/tool/monitor/tests/eventobservers_test.php
lib/phpunit/classes/advanced_testcase.php
lib/phpunit/tests/advanced_test.php
lib/tests/coursecatlib_test.php
lib/tests/progress_display_test.php
mod/assign/tests/lib_test.php
mod/assign/tests/locallib_test.php

index 9239e16..a1cb384 100644 (file)
@@ -384,7 +384,7 @@ class tool_monitor_eventobservers_testcase extends advanced_testcase {
             // Now let us trigger 7 instances of the event.
             $event = \mod_book\event\course_module_instance_list_viewed::create_from_course($course);
             $event->trigger();
-            sleep(1); // Add a second delay, to prevent time collisions.
+            $this->waitForSecond(); // Add a second delay, to prevent time collisions.
         }
         $this->run_adhock_tasks();
         $messages = $messagesink->get_messages();
index 1c0bd81..6a9964b 100644 (file)
@@ -645,4 +645,17 @@ abstract class advanced_testcase extends base_testcase {
             }
         }
     }
+
+    /**
+     * Wait for a second to roll over, ensures future calls to time() return a different result.
+     *
+     * This is implemented instead of sleep() as we do not need to wait a full second. In some cases
+     * due to calls we may wait more than sleep() would have, on average it will be less.
+     */
+    public function waitForSecond() {
+        $starttime = time();
+        while (time() == $starttime) {
+            usleep(50000);
+        }
+    }
 }
index 36fde30..ebd4a55 100644 (file)
@@ -358,7 +358,7 @@ class core_phpunit_advanced_testcase extends advanced_testcase {
 
         $this->setCurrentTimeStart();
         $this->assertTimeCurrent(time());
-        sleep(2);
+        $this->waitForSecond();
         $this->assertTimeCurrent(time());
         $this->assertTimeCurrent(time()-1);
 
index 2c855dc..b219753 100644 (file)
@@ -236,7 +236,7 @@ class core_coursecatlib_testcase extends advanced_testcase {
         $timecreated = $category1->timemodified;
         $this->assertSame('Cat1', $category1->name);
         $this->assertTrue(empty($category1->description));
-        sleep(2);
+        $this->waitForSecond();
         $testdescription = 'This is cat 1 а также русский текст';
         $category1->update(array('description' => $testdescription));
         $this->assertSame($testdescription, $category1->description);
index 8da0e78..278c7ea 100644 (file)
@@ -36,7 +36,7 @@ class progress_display_test extends \advanced_testcase {
         $this->assertEquals(1, $progress->get_direction());
         $this->assertTimeCurrent($progress->get_last_wibble());
         // Wait 1 second to ensure that all code in update_progress is run.
-        sleep(1);
+        $this->waitForSecond();
         $progress->update_progress();
         $this->assertEquals(2, $progress->get_current_state());
         $this->assertEquals(1, $progress->get_direction());
@@ -58,14 +58,14 @@ class progress_display_test extends \advanced_testcase {
 
         // Set wibbler to final state and progress to check that it reverses direction.
         $progress->set_current_state(core_mock_progress_display::WIBBLE_STATES);
-        sleep(1);
+        $this->waitForSecond();
         $progress->update_progress();
         $this->assertEquals(core_mock_progress_display::WIBBLE_STATES - 1, $progress->get_current_state());
         $this->assertEquals(-1, $progress->get_direction());
 
         // Set wibbler to beginning and progress to check that it reverses direction.
         $progress->set_current_state(0);
-        sleep(1);
+        $this->waitForSecond();
         $progress->update_progress();
         $this->assertEquals(1, $progress->get_current_state());
         $this->assertEquals(1, $progress->get_direction());
index 66cabaf..34e5219 100644 (file)
@@ -72,7 +72,7 @@ class mod_assign_lib_testcase extends mod_assign_base_testcase {
         $assign->testable_apply_grade_to_user($data, $this->students[0]->id, 0);
 
         // This is required so that the submissions timemodified > the grade timemodified.
-        sleep(2);
+        $this->waitForSecond();
 
         // Edit the submission again.
         $this->setUser($this->students[0]);
@@ -80,7 +80,7 @@ class mod_assign_lib_testcase extends mod_assign_base_testcase {
         $assign->testable_update_submission($submission, $this->students[0]->id, true, false);
 
         // This is required so that the submissions timemodified > the grade timemodified.
-        sleep(2);
+        $this->waitForSecond();
 
         // Allow the student another attempt.
         $this->teachers[0]->ignoresesskey = true;
index b41a354..269f1b7 100644 (file)
@@ -852,7 +852,7 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase {
 
         // This is to make sure the grade happens after the submission because
         // we have no control over the timemodified values.
-        sleep(1);
+        $this->waitForSecond();
         // Grade the submission.
         $this->setUser($this->teachers[0]);
 
@@ -1016,7 +1016,7 @@ class mod_assign_locallib_testcase extends mod_assign_base_testcase {
         $plugin->save($submission, $data);
 
         // Wait 1 second so the submission and grade do not have the same timemodified.
-        sleep(1);
+        $this->waitForSecond();
         // Simulate adding a grade.
         $this->setUser($this->editingteachers[0]);
         $data = new stdClass();