Skip to content

Commit

Permalink
Merge branch 'MDL-66475' of git://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Apr 19, 2021
2 parents d688af7 + 2d6cde2 commit 5459965
Showing 1 changed file with 17 additions and 17 deletions.
34 changes: 17 additions & 17 deletions lib/tests/task_database_logger_test.php
Expand Up @@ -433,11 +433,12 @@ public function test_cleanup_combined() {

$this->resetAfterTest();

// Create sample log data - 2 tasks, once per hour for 3 days.
// Calculate date to be used for logs, starting from current time rounded down to nearest hour.
$date = new DateTime();
$date->setTime($date->format('G'), 0);
$baselogtime = $date->getTimestamp();

// Create sample log data - 2 tasks, once per hour for 3 days.
for ($i = 0; $i < 3 * 24; $i++) {
$task = new \core\task\cache_cron_task();
$logpath = __FILE__;
Expand All @@ -456,36 +457,35 @@ public function test_cleanup_combined() {
$this->assertEquals(72, $DB->count_records('task_log', ['classname' => \core\task\badges_cron_task::class]));

// Note: We set the retention time to a period like DAYSECS minus an adjustment.
// The adjustment is to account for the time taken during setup.
// The adjustment is to account for the difference between current time and baselogtime.

// With a retention period of 2 * DAYSECS, there should only be 94-96 left.
// With a retention period of 2 * DAYSECS, there should only be 96 left.
// The run count is a higher number so it will have no effect.
set_config('task_logretention', (2 * DAYSECS) - (time() - $baselogtime));
set_config('task_logretention', time() - ($baselogtime - (2 * DAYSECS)) - 1);
set_config('task_logretainruns', 50);
\core\task\database_logger::cleanup();
$this->assertGreaterThanOrEqual(94, $DB->count_records('task_log'));
$this->assertLessThanOrEqual(96, $DB->count_records('task_log'));
$this->assertGreaterThanOrEqual(47, $DB->count_records('task_log', ['classname' => \core\task\cache_cron_task::class]));
$this->assertLessThanOrEqual(48, $DB->count_records('task_log', ['classname' => \core\task\cache_cron_task::class]));
$this->assertGreaterThanOrEqual(47, $DB->count_records('task_log', ['classname' => \core\task\badges_cron_task::class]));
$this->assertLessThanOrEqual(48, $DB->count_records('task_log', ['classname' => \core\task\badges_cron_task::class]));

// We should retain the most recent 48 so the oldest will be no more than 48 hours old.
$oldest = $DB->get_records('task_log', [], 'timestart DESC', 'timestart', 0, 1);

$this->assertEquals(96, $DB->count_records('task_log'));
$this->assertEquals(48, $DB->count_records('task_log', ['classname' => \core\task\cache_cron_task::class]));
$this->assertEquals(48, $DB->count_records('task_log', ['classname' => \core\task\badges_cron_task::class]));

// We should retain the most recent 48 of each task, so the oldest will be 47 hours old.
$oldest = $DB->get_records('task_log', [], 'timestart ASC', 'timestart', 0, 1);
$oldest = reset($oldest);
$this->assertGreaterThan(time() - (48 * DAYSECS), $oldest->timestart);
$this->assertEquals($baselogtime - (47 * HOURSECS), $oldest->timestart);

// Reducing the retain runs count to 10 should reduce the total logs to 20, overriding the time constraint.
set_config('task_logretainruns', 10);
\core\task\database_logger::cleanup();

$this->assertEquals(20, $DB->count_records('task_log'));
$this->assertEquals(10, $DB->count_records('task_log', ['classname' => \core\task\cache_cron_task::class]));
$this->assertEquals(10, $DB->count_records('task_log', ['classname' => \core\task\badges_cron_task::class]));

// We should retain the most recent 10 so the oldeste will be no more than 10 hours old.
$oldest = $DB->get_records('task_log', [], 'timestart DESC', 'timestart', 0, 1);
// We should retain the most recent 10 of each task, so the oldest will be 9 hours old.
$oldest = $DB->get_records('task_log', [], 'timestart ASC', 'timestart', 0, 1);
$oldest = reset($oldest);
$this->assertGreaterThan(time() - (10 * DAYSECS), $oldest->timestart);
$this->assertEquals($baselogtime - (9 * HOURSECS), $oldest->timestart);
}

/**
Expand Down

0 comments on commit 5459965

Please sign in to comment.