Merge branch 'master_MDL-67980' of https://github.com/golenkovm/moodle
authorJun Pataleta <jun@moodle.com>
Wed, 26 Feb 2020 06:30:49 +0000 (14:30 +0800)
committerJun Pataleta <jun@moodle.com>
Wed, 26 Feb 2020 06:30:49 +0000 (14:30 +0800)
lib/classes/task/manager.php
lib/tests/adhoc_task_test.php

index e0ab0e4..c7f5033 100644 (file)
@@ -561,7 +561,7 @@ class manager {
 
         $where = '(nextruntime IS NULL OR nextruntime < :timestart1)';
         $params = array('timestart1' => $timestart);
-        $records = $DB->get_records_select('task_adhoc', $where, $params);
+        $records = $DB->get_records_select('task_adhoc', $where, $params, 'nextruntime ASC, id ASC');
 
         $records = self::ensure_adhoc_task_qos($records);
 
index f90f238..b0fdbb5 100644 (file)
@@ -396,4 +396,50 @@ class core_adhoc_task_testcase extends advanced_testcase {
         $concurrencylimit = $task->get_concurrency_limit();
         $this->assertEquals(5, $concurrencylimit);
     }
+
+    /**
+     * Test adhoc task sorting.
+     */
+    public function test_get_next_adhoc_task_sorting() {
+        $this->resetAfterTest(true);
+
+        // Create adhoc tasks.
+        $task1 = new \core\task\adhoc_test_task();
+        $task1->set_next_run_time(1510000000);
+        $task1->set_custom_data_as_string('Task 1');
+        \core\task\manager::queue_adhoc_task($task1);
+
+        $task2 = new \core\task\adhoc_test_task();
+        $task2->set_next_run_time(1520000000);
+        $task2->set_custom_data_as_string('Task 2');
+        \core\task\manager::queue_adhoc_task($task2);
+
+        $task3 = new \core\task\adhoc_test_task();
+        $task3->set_next_run_time(1520000000);
+        $task3->set_custom_data_as_string('Task 3');
+        \core\task\manager::queue_adhoc_task($task3);
+
+        // Shuffle tasks.
+        $task1->set_next_run_time(1540000000);
+        \core\task\manager::reschedule_or_queue_adhoc_task($task1);
+
+        $task3->set_next_run_time(1530000000);
+        \core\task\manager::reschedule_or_queue_adhoc_task($task3);
+
+        $task2->set_next_run_time(1530000000);
+        \core\task\manager::reschedule_or_queue_adhoc_task($task2);
+
+        // Confirm, that tasks are sorted by nextruntime and then by id (ascending).
+        $task = \core\task\manager::get_next_adhoc_task(time());
+        $this->assertEquals('Task 2', $task->get_custom_data_as_string());
+        \core\task\manager::adhoc_task_complete($task);
+
+        $task = \core\task\manager::get_next_adhoc_task(time());
+        $this->assertEquals('Task 3', $task->get_custom_data_as_string());
+        \core\task\manager::adhoc_task_complete($task);
+
+        $task = \core\task\manager::get_next_adhoc_task(time());
+        $this->assertEquals('Task 1', $task->get_custom_data_as_string());
+        \core\task\manager::adhoc_task_complete($task);
+    }
 }