Skip to content

Commit

Permalink
Merge branch 'MDL-52385-master' of git://github.com/andrewnicols/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Apr 11, 2016
2 parents 2a84fae + dfb9dae commit 468ef66
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/classes/task/manager.php
Expand Up @@ -135,8 +135,10 @@ public static function queue_adhoc_task(adhoc_task $task) {
global $DB;

$record = self::record_from_adhoc_task($task);
// Schedule it immediately.
$record->nextruntime = time() - 1;
// Schedule it immediately if nextruntime not explicitly set.
if (!$task->get_next_run_time()) {
$record->nextruntime = time() - 1;
}
$result = $DB->insert_record('task_adhoc', $record);

return $result;
Expand Down
60 changes: 51 additions & 9 deletions lib/tests/adhoc_task_test.php
Expand Up @@ -37,34 +37,76 @@
*/
class core_adhoc_task_testcase extends advanced_testcase {

public function test_get_next_adhoc_task() {
/**
* Test basic adhoc task execution.
*/
public function test_get_next_adhoc_task_now() {
$this->resetAfterTest(true);

// Create an adhoc task.
$task = new \core\task\adhoc_test_task();

// Queue it.
$task = \core\task\manager::queue_adhoc_task($task);
\core\task\manager::queue_adhoc_task($task);

$now = time();
// Get it from the scheduler.
$task = \core\task\manager::get_next_adhoc_task($now);
$this->assertNotNull($task);
$this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
$task->execute();
\core\task\manager::adhoc_task_complete($task);
}

\core\task\manager::adhoc_task_failed($task);
// Should not get any task.
/**
* Test adhoc task failure retry backoff.
*/
public function test_get_next_adhoc_task_fail_retry() {
$this->resetAfterTest(true);

// Create an adhoc task.
$task = new \core\task\adhoc_test_task();
\core\task\manager::queue_adhoc_task($task);

$now = time();

// Get it from the scheduler, execute it, and mark it as failed.
$task = \core\task\manager::get_next_adhoc_task($now);
$this->assertNull($task);
$task->execute();
\core\task\manager::adhoc_task_failed($task);

// The task will not be returned immediately.
$this->assertNull(\core\task\manager::get_next_adhoc_task($now));

// Should get the adhoc task (retry after delay).
$task = \core\task\manager::get_next_adhoc_task($now + 120);
$this->assertNotNull($task);
$this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
$task->execute();

\core\task\manager::adhoc_task_complete($task);

// Should not get any task.
$task = \core\task\manager::get_next_adhoc_task($now);
$this->assertNull($task);
$this->assertNull(\core\task\manager::get_next_adhoc_task($now));
}

/**
* Test future adhoc task execution.
*/
public function test_get_next_adhoc_task_future() {
$this->resetAfterTest(true);

$now = time();
// Create an adhoc task in future.
$task = new \core\task\adhoc_test_task();
$task->set_next_run_time($now + 1000);
\core\task\manager::queue_adhoc_task($task);

// Fetching the next task should not return anything.
$this->assertNull(\core\task\manager::get_next_adhoc_task($now));

// Fetching in the future should return the task.
$task = \core\task\manager::get_next_adhoc_task($now + 1020);
$this->assertInstanceOf('\\core\\task\\adhoc_test_task', $task);
$task->execute();
\core\task\manager::adhoc_task_complete($task);
}
}

0 comments on commit 468ef66

Please sign in to comment.