MDL-67594 core_lock: Remove supports_recursion() usages
authorMihail Geshoski <mihail@moodle.com>
Wed, 1 Apr 2020 08:03:57 +0000 (16:03 +0800)
committerMihail Geshoski <mihail@moodle.com>
Mon, 6 Jul 2020 05:01:20 +0000 (13:01 +0800)
lib/tests/lock_test.php

index 47a9fec..2064621 100644 (file)
@@ -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.