From 8f078a291108c06d112f0a6301a8ead2da8ae401 Mon Sep 17 00:00:00 2001 From: Simey Lameze Date: Wed, 10 Aug 2022 13:52:44 +0800 Subject: [PATCH] MDL-75231 files: make bucket calculations more precise This commit changes changes the unit test calculations to keep track of the time the bucket starts/finishes bursting and use milliseconds instead of seconds to have a precise time to sleep. --- lib/tests/filelib_test.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/tests/filelib_test.php b/lib/tests/filelib_test.php index faaa198a801..5c9cc9b6fcb 100644 --- a/lib/tests/filelib_test.php +++ b/lib/tests/filelib_test.php @@ -1850,6 +1850,7 @@ EOF; } // Burst up to the capacity and make sure that the bucket allows it. + $burststart = microtime(); for ($i = 0; $i < $capacity; $i++) { if ($i) { sleep(1); // A little delay so we have different timemodified value for files. @@ -1864,9 +1865,16 @@ EOF; // The bucket should be full after bursting. $this->assertTrue(file_is_draft_areas_limit_reached($user->id)); + // Calculate the time taken to burst up the bucket capacity. + $timetaken = microtime_diff($burststart, microtime()); + // The bucket leaks so it shouldn't be full after a certain time. - // Reiterating that this test could have been faster if MDL-37327 was implemented. - sleep(ceil(1 / $leak) - ($capacity - 1)); + // Items are added into the bucket at the rate of 1 item per second. + // One item leaks from the bucket every 1/$leak seconds. + // So it takes 1/$leak - ($capacity-1) seconds for the bucket to leak one item and not be full anymore. + $milliseconds = ceil(1000000 * ((1 / $leak) - ($capacity - 1)) - ($timetaken * 1000)); + usleep($milliseconds); + $this->assertFalse(file_is_draft_areas_limit_reached($user->id)); // Only one item was leaked from the bucket. So the bucket should become full again if we add a single item to it.