MDL-67594 core_lock: Remove supports_recursion() usages
[moodle.git] / lib / tests / lock_test.php
CommitLineData
9843e5ec
DW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * lock unit tests
19 *
20 * @package core
21 * @category test
22 * @copyright 2013 Damyon Wiese
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28
29/**
30 * Unit tests for our locking implementations.
31 *
32 * @package core
33 * @category test
34 * @copyright 2013 Damyon Wiese
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class lock_testcase extends advanced_testcase {
38
39 /**
40 * Some lock types will store data in the database.
41 */
42 protected function setUp() {
43 $this->resetAfterTest(true);
44 }
45
46 /**
7e086935
BH
47 * Run a suite of tests on a lock factory class.
48 *
49 * @param class $lockfactoryclass - A lock factory class to test
9843e5ec 50 */
7e086935 51 protected function run_on_lock_factory($lockfactoryclass) {
9843e5ec 52
7e086935
BH
53 $modassignfactory = new $lockfactoryclass('mod_assign');
54 $tooltaskfactory = new $lockfactoryclass('tool_task');
55
56 // Test for lock clashes between lock stores.
57 $assignlock = $modassignfactory->get_lock('abc', 0);
58 $this->assertNotEmpty($assignlock, 'Get a lock "abc" from store "mod_assign"');
59
60 $tasklock = $tooltaskfactory->get_lock('abc', 0);
61 $this->assertNotEmpty($tasklock, 'Get a lock "abc" from store "tool_task"');
62
63 $assignlock->release();
64 $tasklock->release();
65
66 $lockfactory = new $lockfactoryclass('default');
9843e5ec
DW
67 if ($lockfactory->is_available()) {
68 // This should work.
69 $lock1 = $lockfactory->get_lock('abc', 2);
70 $this->assertNotEmpty($lock1, 'Get a lock');
71
72 if ($lockfactory->supports_timeout()) {
41bc63db
MG
73 // Attempt to obtain a lock within a 2 sec timeout.
74 $durationlock2 = -microtime(true);
75 $lock2 = $lockfactory->get_lock('abc', 2);
76 $durationlock2 += microtime(true);
77
78 if (!$lock2) { // If the lock was not obtained.
79 $this->assertFalse($lock2, 'Cannot get a stacked lock');
80 // This should timeout after 2 seconds.
81 $this->assertTrue($durationlock2 < 2.5, 'Lock should timeout after no more than 2 seconds');
82 } else {
9843e5ec
DW
83 $this->assertNotEmpty($lock2, 'Get a stacked lock');
84 $this->assertTrue($lock2->release(), 'Release a stacked lock');
41bc63db
MG
85 }
86
87 // Attempt to obtain a lock within a 0 sec timeout.
88 $durationlock2 = -microtime(true);
89 $lock2 = $lockfactory->get_lock('abc', 0);
90 $durationlock2 += microtime(true);
93ee7082 91
41bc63db
MG
92 if (!$lock2) { // If the lock was not obtained.
93 // This should timeout almost instantly.
94 $this->assertTrue($durationlock2 < 0.100, 'Lock should timeout almost instantly < 100ms');
95 } else {
93ee7082 96 // This stacked lock should be gained almost instantly.
41bc63db
MG
97 $this->assertTrue($durationlock2 < 0.100, 'Lock should be gained almost instantly');
98 $lock2->release();
93ee7082
BH
99
100 // We should also assert that locks fail instantly if locked
101 // from another process but this is hard to unit test.
9843e5ec
DW
102 }
103 }
104 // Release the lock.
105 $this->assertTrue($lock1->release(), 'Release a lock');
106 // Get it again.
107 $lock3 = $lockfactory->get_lock('abc', 2);
108
109 $this->assertNotEmpty($lock3, 'Get a lock again');
110 // Release the lock again.
111 $this->assertTrue($lock3->release(), 'Release a lock again');
112 // Release the lock again (shouldn't hurt).
113 $this->assertFalse($lock3->release(), 'Release a lock that is not held');
114 if (!$lockfactory->supports_auto_release()) {
115 // Test that a lock can be claimed after the timeout period.
116 $lock4 = $lockfactory->get_lock('abc', 2, 2);
117 $this->assertNotEmpty($lock4, 'Get a lock');
118 sleep(3);
119
120 $lock5 = $lockfactory->get_lock('abc', 2, 2);
121 $this->assertNotEmpty($lock5, 'Get another lock after a timeout');
122 $this->assertTrue($lock5->release(), 'Release the lock');
123 $this->assertTrue($lock4->release(), 'Release the lock');
124 }
125 }
126 }
127
128 /**
7e086935 129 * Tests the testable lock factories classes.
9843e5ec
DW
130 * @return void
131 */
132 public function test_locks() {
133 // Run the suite on the current configured default (may be non-core).
7e086935 134 $this->run_on_lock_factory(\core\lock\lock_config::get_lock_factory_class());
9843e5ec
DW
135
136 // Manually create the core no-configuration factories.
7e086935
BH
137 $this->run_on_lock_factory(\core\lock\db_record_lock_factory::class);
138 $this->run_on_lock_factory(\core\lock\file_lock_factory::class);
9843e5ec
DW
139
140 }
141
142}
143