Skip to content

Commit

Permalink
MDL-55944 testing: Reduce sleep usage.
Browse files Browse the repository at this point in the history
Remove sleep() and replace with waiting for a second to roll over,
this results in a simpler call the guarantee time() has moved forward
  • Loading branch information
mr-russ committed Sep 13, 2016
1 parent 0344082 commit 74ee9d2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion admin/tool/monitor/tests/eventobservers_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ public function test_multiple_notification_not_sent() {
// 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();
Expand Down
13 changes: 13 additions & 0 deletions lib/phpunit/classes/advanced_testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,17 @@ public function recurseFolders($path, $callback, $fileregexp = '/.*/', $exclude
}
}
}

/**
* 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);
}
}
}
2 changes: 1 addition & 1 deletion lib/phpunit/tests/advanced_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function test_assert_time_current() {

$this->setCurrentTimeStart();
$this->assertTimeCurrent(time());
sleep(2);
$this->waitForSecond();
$this->assertTimeCurrent(time());
$this->assertTimeCurrent(time()-1);

Expand Down
2 changes: 1 addition & 1 deletion lib/tests/coursecatlib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public function test_update() {
$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);
Expand Down
6 changes: 3 additions & 3 deletions lib/tests/progress_display_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_progress_display_update() {
$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());
Expand All @@ -58,14 +58,14 @@ public function test_progress_display_wibbler() {

// 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());
Expand Down
4 changes: 2 additions & 2 deletions mod/assign/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ protected function setUp() {
$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]);
$submission = $assign->get_user_submission($this->students[0]->id, true);
$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;
Expand Down
4 changes: 2 additions & 2 deletions mod/assign/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ public function test_get_participant_with_graded_submission() {

// 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]);

Expand Down Expand Up @@ -1016,7 +1016,7 @@ public function test_count_submissions() {
$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();
Expand Down

0 comments on commit 74ee9d2

Please sign in to comment.