From 41bc63dbf368b4c7df5a68be8ffa0a8bd5e45976 Mon Sep 17 00:00:00 2001 From: Mihail Geshoski Date: Wed, 1 Apr 2020 16:03:57 +0800 Subject: [PATCH] MDL-67594 core_lock: Remove supports_recursion() usages --- lib/tests/lock_test.php | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/tests/lock_test.php b/lib/tests/lock_test.php index 47a9fece862..2064621b2d3 100644 --- a/lib/tests/lock_test.php +++ b/lib/tests/lock_test.php @@ -70,35 +70,35 @@ class lock_testcase extends advanced_testcase { $this->assertNotEmpty($lock1, 'Get a lock'); if ($lockfactory->supports_timeout()) { - if ($lockfactory->supports_recursion()) { - $lock2 = $lockfactory->get_lock('abc', 2); + // Attempt to obtain a lock within a 2 sec timeout. + $durationlock2 = -microtime(true); + $lock2 = $lockfactory->get_lock('abc', 2); + $durationlock2 += microtime(true); + + if (!$lock2) { // If the lock was not obtained. + $this->assertFalse($lock2, 'Cannot get a stacked lock'); + // This should timeout after 2 seconds. + $this->assertTrue($durationlock2 < 2.5, 'Lock should timeout after no more than 2 seconds'); + } else { $this->assertNotEmpty($lock2, 'Get a stacked lock'); $this->assertTrue($lock2->release(), 'Release a stacked lock'); + } + + // Attempt to obtain a lock within a 0 sec timeout. + $durationlock2 = -microtime(true); + $lock2 = $lockfactory->get_lock('abc', 0); + $durationlock2 += microtime(true); + if (!$lock2) { // If the lock was not obtained. + // This should timeout almost instantly. + $this->assertTrue($durationlock2 < 0.100, 'Lock should timeout almost instantly < 100ms'); + } else { // This stacked lock should be gained almost instantly. - $duration = -microtime(true); - $lock3 = $lockfactory->get_lock('abc', 0); - $duration += microtime(true); - $lock3->release(); - $this->assertTrue($duration < 0.100, 'Lock should be gained almost instantly'); + $this->assertTrue($durationlock2 < 0.100, 'Lock should be gained almost instantly'); + $lock2->release(); // We should also assert that locks fail instantly if locked // from another process but this is hard to unit test. - - } else { - // This should timeout after 2 seconds. - $duration = -microtime(true); - $lock2 = $lockfactory->get_lock('abc', 2); - $duration += microtime(true); - $this->assertFalse($lock2, 'Cannot get a stacked lock'); - $this->assertTrue($duration < 2.5, 'Lock should timeout after no more than 2 seconds'); - - // This should timeout almost instantly. - $duration = -microtime(true); - $lock2 = $lockfactory->get_lock('abc', 0); - $duration += microtime(true); - $this->assertFalse($lock2, 'Cannot get a stacked lock'); - $this->assertTrue($duration < 0.100, 'Lock should timeout almost instantly < 100ms'); } } // Release the lock. -- 2.43.0