1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * This file contains unit tests for the 'task running' data. 19 * 20 * @package core 21 * @copyright 2019 The Open University 22 * @copyright 2020 Mikhail Golenkov <golenkovm@gmail.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core\task; 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 require_once (__DIR__ . '/fixtures/task_fixtures.php'); 31 32 /** 33 * This file contains unit tests for the 'task running' data. 34 * 35 * @copyright 2019 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class task_running_testcase extends \advanced_testcase { 39 40 /** 41 * Test for ad-hoc tasks. 42 */ 43 public function test_adhoc_task_running() { 44 $this->resetAfterTest(); 45 46 // Specify lock factory. The reason is that Postgres locks don't work within a single 47 // process (i.e. if you try to get a lock that you already locked, it will just let you) 48 // which is usually OK but not here where we are simulating running two tasks at once in 49 // the same process. 50 set_config('lock_factory', '\core\lock\db_record_lock_factory'); 51 52 // Create and queue 2 new ad-hoc tasks. 53 $task1 = new adhoc_test_task(); 54 $task1->set_next_run_time(time() - 20); 55 manager::queue_adhoc_task($task1); 56 $task2 = new adhoc_test2_task(); 57 $task2->set_next_run_time(time() - 10); 58 manager::queue_adhoc_task($task2); 59 60 // Check no tasks are marked running. 61 $running = manager::get_running_tasks(); 62 $this->assertEmpty($running); 63 64 // Mark the first task running and check results. 65 $before = time(); 66 $next1 = manager::get_next_adhoc_task(time()); 67 manager::adhoc_task_starting($next1); 68 $after = time(); 69 $running = manager::get_running_tasks(); 70 $this->assertCount(1, $running); 71 foreach ($running as $item) { 72 $this->assertEquals('adhoc', $item->type); 73 $this->assertLessThanOrEqual($after, $item->timestarted); 74 $this->assertGreaterThanOrEqual($before, $item->timestarted); 75 } 76 77 // Mark the second task running and check results. 78 $next2 = manager::get_next_adhoc_task(time()); 79 manager::adhoc_task_starting($next2); 80 $running = manager::get_running_tasks(); 81 $this->assertCount(2, $running); 82 83 // Second task completes successfully. 84 manager::adhoc_task_complete($next2); 85 $running = manager::get_running_tasks(); 86 $this->assertCount(1, $running); 87 88 // First task fails. 89 manager::adhoc_task_failed($next1); 90 $running = manager::get_running_tasks(); 91 $this->assertCount(0, $running); 92 } 93 94 /** 95 * Test for scheduled tasks. 96 */ 97 public function test_scheduled_task_running() { 98 global $DB; 99 $this->resetAfterTest(); 100 101 // Check no tasks are marked running. 102 $running = manager::get_running_tasks(); 103 $this->assertEmpty($running); 104 105 // Disable all the tasks, except two, and set those two due to run. 106 $DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ? AND classname != ?', 107 ['\core\task\session_cleanup_task', '\core\task\file_trash_cleanup_task']); 108 $DB->set_field('task_scheduled', 'nextruntime', 1, 109 ['classname' => '\core\task\session_cleanup_task']); 110 $DB->set_field('task_scheduled', 'nextruntime', 1, 111 ['classname' => '\core\task\file_trash_cleanup_task']); 112 $DB->set_field('task_scheduled', 'lastruntime', time() - 1000, 113 ['classname' => '\core\task\session_cleanup_task']); 114 $DB->set_field('task_scheduled', 'lastruntime', time() - 500, 115 ['classname' => '\core\task\file_trash_cleanup_task']); 116 117 // Get the first task and start it off. 118 $next1 = manager::get_next_scheduled_task(time()); 119 $before = time(); 120 manager::scheduled_task_starting($next1); 121 $after = time(); 122 $running = manager::get_running_tasks(); 123 $this->assertCount(1, $running); 124 foreach ($running as $item) { 125 $this->assertLessThanOrEqual($after, $item->timestarted); 126 $this->assertGreaterThanOrEqual($before, $item->timestarted); 127 $this->assertEquals('\core\task\session_cleanup_task', $item->classname); 128 } 129 130 // Mark the second task running and check results. We have to change the times so the other 131 // one comes up first, otherwise it repeats the same one. 132 $DB->set_field('task_scheduled', 'lastruntime', time() - 1500, 133 ['classname' => '\core\task\file_trash_cleanup_task']); 134 135 // Make sure that there is a time gap between task to sort them as expected. 136 sleep(1); 137 $next2 = manager::get_next_scheduled_task(time()); 138 manager::scheduled_task_starting($next2); 139 140 // Check default sorting by timestarted. 141 $running = manager::get_running_tasks(); 142 $this->assertCount(2, $running); 143 $item = array_shift($running); 144 $this->assertEquals('\core\task\session_cleanup_task', $item->classname); 145 $item = array_shift($running); 146 $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname); 147 148 // Check sorting by time ASC. 149 $running = manager::get_running_tasks('time ASC'); 150 $this->assertCount(2, $running); 151 $item = array_shift($running); 152 $this->assertEquals('\core\task\file_trash_cleanup_task', $item->classname); 153 $item = array_shift($running); 154 $this->assertEquals('\core\task\session_cleanup_task', $item->classname); 155 156 // Complete the file trash one. 157 manager::scheduled_task_complete($next2); 158 $running = manager::get_running_tasks(); 159 $this->assertCount(1, $running); 160 161 // Other task fails. 162 manager::scheduled_task_failed($next1); 163 $running = manager::get_running_tasks(); 164 $this->assertCount(0, $running); 165 } 166 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body