From b0e5b3b63b53cd53ff07f156e91c28564c68aa8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 28 Aug 2012 22:52:31 +0200 Subject: [PATCH 1/2] MDL-35109 Improve unittests for cron based fetching of available updates The expected behaviour is as follows: * If the recently fetched data is older than 48 hours, it is considered as outdated and the new fetch is executed * else, if the recently fetched data is younger than 24 hours, it is considered as fresh enough and no fetch is executed * else, if the current time is after 01:00 AM plus a certain offset (which is randomly generated for each site), the fetch is executed. --- lib/tests/pluginlib_test.php | 47 +++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/lib/tests/pluginlib_test.php b/lib/tests/pluginlib_test.php index eb29268a55b..2ec4d0dd2f8 100644 --- a/lib/tests/pluginlib_test.php +++ b/lib/tests/pluginlib_test.php @@ -103,7 +103,7 @@ class available_update_checker_test extends advanced_testcase { */ public function test_cron_has_fresh_fetch() { $provider = testable_available_update_checker::instance(); - $provider->fakerecentfetch = time() - 59 * MINSECS; // fetched an hour ago + $provider->fakerecentfetch = time() - 23 * HOURSECS; // fetched 23 hours ago $provider->fakecurrenttimestamp = -1; $provider->cron(); $this->assertTrue(true); // we should get here with no exception thrown @@ -127,23 +127,52 @@ class available_update_checker_test extends advanced_testcase { */ public function test_cron_offset_execution_not_yet() { $provider = testable_available_update_checker::instance(); - $provider->fakerecentfetch = time() - 24 * HOURSECS; - $provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM + $provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today + $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS; $provider->cron(); $this->assertTrue(true); // we should get here with no exception thrown } /** - * The first cron after 01:42 AM today should fetch the data + * The first cron after 01:42 AM today should fetch the data and then + * it is supposed to wait next 24 hours. * * @see testable_available_update_checker::cron_execution_offset() */ public function test_cron_offset_execution() { $provider = testable_available_update_checker::instance(); - $provider->fakerecentfetch = time() - 24 * HOURSECS; - $provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM - $this->setExpectedException('testable_available_update_checker_cron_executed'); - $provider->cron(); + + // the cron at 01:45 should fetch the data + $provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today + $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS - 1; + $executed = false; + try { + $provider->cron(); + } catch (testable_available_update_checker_cron_executed $e) { + $executed = true; + } + $this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.'); + + // another cron at 06:45 should still consider data as fresh enough + $provider->fakerecentfetch = $provider->fakecurrenttimestamp; + $provider->fakecurrenttimestamp = mktime(6, 45, 03); // 06:45:03 AM + $executed = false; + try { + $provider->cron(); + } catch (testable_available_update_checker_cron_executed $e) { + $executed = true; + } + $this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.'); + + // the next scheduled execution should happen the next day + $provider->fakecurrenttimestamp = $provider->fakerecentfetch + 24 * HOURSECS + 1; + $executed = false; + try { + $provider->cron(); + } catch (testable_available_update_checker_cron_executed $e) { + $executed = true; + } + $this->assertTrue($executed, 'Cron should be executed the next night but it was not.'); } public function test_compare_responses_both_empty() { @@ -503,7 +532,7 @@ class testable_available_update_checker extends available_update_checker { } protected function cron_execute() { - throw new testable_available_update_checker_cron_executed('Cron executed but it should not!'); + throw new testable_available_update_checker_cron_executed('Cron executed!'); } } From 903d13c0c28148d8327bfc5857f6de655b727098 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Mudr=C3=A1k?= Date: Tue, 28 Aug 2012 22:56:18 +0200 Subject: [PATCH 2/2] MDL-35109 Fix available_update_checker::cron_has_fresh_fetch() For the purpose of cron based fetching, recently fetched data are valid for 24 hours. --- lib/pluginlib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pluginlib.php b/lib/pluginlib.php index 43de5106d6a..65e1c35623a 100644 --- a/lib/pluginlib.php +++ b/lib/pluginlib.php @@ -1064,7 +1064,7 @@ class available_update_checker { return true; } - if ($now - $recent > HOURSECS) { + if ($now - $recent > 24 * HOURSECS) { return false; }