| Línea 25... |
Línea 25... |
| 25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
25 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
| 26 |
*/
|
26 |
*/
|
| 27 |
final class running_test extends \advanced_testcase {
|
27 |
final class running_test extends \advanced_testcase {
|
| 28 |
public static function setUpBeforeClass(): void {
|
28 |
public static function setUpBeforeClass(): void {
|
| 29 |
require_once(__DIR__ . '/../fixtures/task_fixtures.php');
|
29 |
require_once(__DIR__ . '/../fixtures/task_fixtures.php');
|
| - |
|
30 |
parent::setUpBeforeClass();
|
| 30 |
}
|
31 |
}
|
| Línea 31... |
Línea 32... |
| 31 |
|
32 |
|
| 32 |
/**
|
33 |
/**
|
| 33 |
* Test for ad-hoc tasks.
|
34 |
* Test for ad-hoc tasks.
|
| Línea 153... |
Línea 154... |
| 153 |
// Other task fails.
|
154 |
// Other task fails.
|
| 154 |
manager::scheduled_task_failed($next1);
|
155 |
manager::scheduled_task_failed($next1);
|
| 155 |
$running = manager::get_running_tasks();
|
156 |
$running = manager::get_running_tasks();
|
| 156 |
$this->assertCount(0, $running);
|
157 |
$this->assertCount(0, $running);
|
| 157 |
}
|
158 |
}
|
| - |
|
159 |
|
| - |
|
160 |
/**
|
| - |
|
161 |
* Test for adhoc task cleanup.
|
| - |
|
162 |
*
|
| - |
|
163 |
* @covers \core\task\manager::cleanup_metadata()
|
| - |
|
164 |
*/
|
| - |
|
165 |
public function test_adhoc_cleanup_metadata(): void {
|
| - |
|
166 |
global $DB;
|
| - |
|
167 |
$this->resetAfterTest(true);
|
| - |
|
168 |
|
| - |
|
169 |
$clock = $this->mock_clock_with_frozen();
|
| - |
|
170 |
|
| - |
|
171 |
// Specify lock factory to avoid previously mentioned issues with Postgres locks.
|
| - |
|
172 |
set_config('lock_factory', '\core\lock\db_record_lock_factory');
|
| - |
|
173 |
|
| - |
|
174 |
// Disable all scheduled tasks except the cleanup task.
|
| - |
|
175 |
$classname = 'core\task\task_lock_cleanup_task';
|
| - |
|
176 |
$DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ?', ["\\$classname"]);
|
| - |
|
177 |
$DB->set_field('task_scheduled', 'nextruntime', 1, ['classname' => "\\$classname"]);
|
| - |
|
178 |
|
| - |
|
179 |
// Create an adhoc task.
|
| - |
|
180 |
$task = new adhoc_test_task();
|
| - |
|
181 |
$task->set_next_run_time($clock->time() - MINSECS);
|
| - |
|
182 |
|
| - |
|
183 |
// Queue and start the adhoc task.
|
| - |
|
184 |
manager::queue_adhoc_task($task);
|
| - |
|
185 |
$task = manager::get_next_adhoc_task($clock->time());
|
| - |
|
186 |
manager::adhoc_task_starting($task);
|
| - |
|
187 |
|
| - |
|
188 |
// Release the lock to simulate an adhoc task that has been destroyed but hasn't been cleaned up.
|
| - |
|
189 |
$task->get_lock()->release();
|
| - |
|
190 |
$this->assertCount(1, manager::get_running_tasks());
|
| - |
|
191 |
|
| - |
|
192 |
// Run the cleanup scheduled task one hour later.
|
| - |
|
193 |
$clock->bump(HOURSECS);
|
| - |
|
194 |
$cleanuptask = manager::get_next_scheduled_task($clock->time());
|
| - |
|
195 |
manager::scheduled_task_starting($cleanuptask);
|
| - |
|
196 |
logmanager::start_logging($cleanuptask);
|
| - |
|
197 |
$this->assertCount(2, manager::get_running_tasks());
|
| - |
|
198 |
$cleanuptask->execute();
|
| - |
|
199 |
|
| - |
|
200 |
// Confirm the task has been cleaned up.
|
| - |
|
201 |
$this->assertCount(1, manager::get_running_tasks());
|
| - |
|
202 |
|
| - |
|
203 |
// Check the task log hasn't been finalised for the cleanup task.
|
| - |
|
204 |
$record = $DB->get_record('task_log', ['classname' => $classname]);
|
| - |
|
205 |
$this->assertEmpty($record);
|
| - |
|
206 |
|
| - |
|
207 |
// Now complete the task and make sure it was successful (0 = success, 1 = fail).
|
| - |
|
208 |
manager::scheduled_task_complete($cleanuptask);
|
| - |
|
209 |
$record = $DB->get_record('task_log', ['classname' => $classname]);
|
| - |
|
210 |
$this->assertEquals(0, $record->result);
|
| - |
|
211 |
}
|
| - |
|
212 |
|
| - |
|
213 |
/**
|
| - |
|
214 |
* Test for scheduled task cleanup.
|
| - |
|
215 |
*
|
| - |
|
216 |
* @covers \core\task\manager::cleanup_metadata()
|
| - |
|
217 |
*/
|
| - |
|
218 |
public function test_scheduled_cleanup_metadata(): void {
|
| - |
|
219 |
global $DB;
|
| - |
|
220 |
$this->resetAfterTest(true);
|
| - |
|
221 |
|
| - |
|
222 |
$clock = $this->mock_clock_with_frozen();
|
| - |
|
223 |
|
| - |
|
224 |
// Specify lock factory to avoid previously mentioned issues with Postgres locks.
|
| - |
|
225 |
set_config('lock_factory', '\core\lock\db_record_lock_factory');
|
| - |
|
226 |
|
| - |
|
227 |
// Disable all scheduled tasks except the cleanup task.
|
| - |
|
228 |
$classname = 'core\task\task_lock_cleanup_task';
|
| - |
|
229 |
$DB->set_field_select('task_scheduled', 'disabled', 1, 'classname != ?', ["\\$classname"]);
|
| - |
|
230 |
$DB->set_field('task_scheduled', 'nextruntime', $clock->time() + MINSECS, ['classname' => "\\$classname"]);
|
| - |
|
231 |
|
| - |
|
232 |
// Create a new scheduled task.
|
| - |
|
233 |
$task = new scheduled_test_task();
|
| - |
|
234 |
$task->set_next_run_time($clock->time() - MINSECS);
|
| - |
|
235 |
|
| - |
|
236 |
// Insert and start the test scheduled task.
|
| - |
|
237 |
$DB->insert_record('task_scheduled', manager::record_from_scheduled_task($task));
|
| - |
|
238 |
$task = manager::get_next_scheduled_task($clock->time());
|
| - |
|
239 |
manager::scheduled_task_starting($task);
|
| - |
|
240 |
|
| - |
|
241 |
// Release the lock to simulate a scheduled task that has been destroyed but hasn't been cleaned up.
|
| - |
|
242 |
$task->get_lock()->release();
|
| - |
|
243 |
$this->assertCount(1, manager::get_running_tasks());
|
| - |
|
244 |
|
| - |
|
245 |
// Run the cleanup scheduled task one hour later.
|
| - |
|
246 |
$clock->bump(HOURSECS);
|
| - |
|
247 |
$cleanuptask = manager::get_next_scheduled_task($clock->time());
|
| - |
|
248 |
manager::scheduled_task_starting($cleanuptask);
|
| - |
|
249 |
logmanager::start_logging($cleanuptask);
|
| - |
|
250 |
$this->assertCount(2, manager::get_running_tasks());
|
| - |
|
251 |
$cleanuptask->execute();
|
| - |
|
252 |
|
| - |
|
253 |
// Confirm the task has been cleaned up.
|
| - |
|
254 |
$this->assertCount(1, manager::get_running_tasks());
|
| - |
|
255 |
|
| - |
|
256 |
// Check the task log hasn't been finalised for the cleanup task.
|
| - |
|
257 |
$record = $DB->get_record('task_log', ['classname' => $classname]);
|
| - |
|
258 |
$this->assertEmpty($record);
|
| - |
|
259 |
|
| - |
|
260 |
// Now complete the task and make sure it was successful (0 = success, 1 = fail).
|
| - |
|
261 |
manager::scheduled_task_complete($cleanuptask);
|
| - |
|
262 |
$record = $DB->get_record('task_log', ['classname' => $classname]);
|
| - |
|
263 |
$this->assertEquals(0, $record->result);
|
| - |
|
264 |
}
|
| 158 |
}
|
265 |
}
|