Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace core;
18
 
1441 ariadna 19
use core\exception\coding_exception;
20
 
1 efrain 21
/**
22
 * Unit tests for our locking implementations.
23
 *
24
 * @package    core
25
 * @category   test
26
 * @copyright  2013 Damyon Wiese
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 28
 * @covers \core\lock\db_record_lock_factory
29
 * @covers \core\lock\file_lock_factory
30
 * @covers \core\lock\lock
1 efrain 31
 */
1441 ariadna 32
final class lock_test extends \advanced_testcase {
1 efrain 33
 
34
    /**
35
     * Some lock types will store data in the database.
36
     */
37
    protected function setUp(): void {
1441 ariadna 38
        parent::setUp();
1 efrain 39
        $this->resetAfterTest(true);
40
    }
41
 
42
    /**
43
     * Run a suite of tests on a lock factory class.
44
     *
1441 ariadna 45
     * @param string $lockfactoryclass - name of a lock factory class to test.
1 efrain 46
     */
1441 ariadna 47
    protected function run_on_lock_factory(string $lockfactoryclass): void {
1 efrain 48
 
49
        $modassignfactory = new $lockfactoryclass('mod_assign');
50
        $tooltaskfactory = new $lockfactoryclass('tool_task');
51
 
52
        // Test for lock clashes between lock stores.
53
        $assignlock = $modassignfactory->get_lock('abc', 0);
54
        $this->assertNotEmpty($assignlock, 'Get a lock "abc" from store "mod_assign"');
55
 
56
        $tasklock = $tooltaskfactory->get_lock('abc', 0);
57
        $this->assertNotEmpty($tasklock, 'Get a lock "abc" from store "tool_task"');
58
 
59
        $assignlock->release();
60
        $tasklock->release();
61
 
62
        $lockfactory = new $lockfactoryclass('default');
63
        if ($lockfactory->is_available()) {
64
            // This should work.
65
            $lock1 = $lockfactory->get_lock('abc', 2);
66
            $this->assertNotEmpty($lock1, 'Get a lock');
67
 
68
            if ($lockfactory->supports_timeout()) {
69
                // Attempt to obtain a lock within a 2 sec timeout.
70
                $durationlock2 = -microtime(true);
71
                $lock2 = $lockfactory->get_lock('abc', 2);
72
                $durationlock2 += microtime(true);
73
 
74
                if (!$lock2) { // If the lock was not obtained.
75
                    $this->assertFalse($lock2, 'Cannot get a stacked lock');
76
                    // This should timeout after 2 seconds.
77
                    $this->assertTrue($durationlock2 < 2.5, 'Lock should timeout after no more than 2 seconds');
78
                } else {
79
                    $this->assertNotEmpty($lock2, 'Get a stacked lock');
80
                    $this->assertTrue($lock2->release(), 'Release a stacked lock');
81
                }
82
 
83
                // Attempt to obtain a lock within a 0 sec timeout.
84
                $durationlock2 = -microtime(true);
85
                $lock2 = $lockfactory->get_lock('abc', 0);
86
                $durationlock2 += microtime(true);
87
 
88
                if (!$lock2) { // If the lock was not obtained.
89
                    // This should timeout almost instantly.
90
                    $this->assertTrue($durationlock2 < 0.100, 'Lock should timeout almost instantly < 100ms');
91
                } else {
92
                    // This stacked lock should be gained almost instantly.
93
                    $this->assertTrue($durationlock2 < 0.100, 'Lock should be gained almost instantly');
94
                    $lock2->release();
95
 
96
                    // We should also assert that locks fail instantly if locked
97
                    // from another process but this is hard to unit test.
98
                }
99
            }
100
            // Release the lock.
101
            $this->assertTrue($lock1->release(), 'Release a lock');
102
            // Get it again.
103
            $lock3 = $lockfactory->get_lock('abc', 2);
104
 
105
            $this->assertNotEmpty($lock3, 'Get a lock again');
106
            // Release the lock again.
107
            $this->assertTrue($lock3->release(), 'Release a lock again');
108
            // Release the lock again (shouldn't hurt).
109
            $this->assertFalse($lock3->release(), 'Release a lock that is not held');
110
            if (!$lockfactory->supports_auto_release()) {
111
                // Test that a lock can be claimed after the timeout period.
112
                $lock4 = $lockfactory->get_lock('abc', 2, 2);
113
                $this->assertNotEmpty($lock4, 'Get a lock');
114
                sleep(3);
115
 
116
                $lock5 = $lockfactory->get_lock('abc', 2, 2);
117
                $this->assertNotEmpty($lock5, 'Get another lock after a timeout');
118
                $this->assertTrue($lock5->release(), 'Release the lock');
119
                $this->assertTrue($lock4->release(), 'Release the lock');
120
            }
121
        }
122
    }
123
 
124
    /**
125
     * Tests the testable lock factories classes.
126
     * @return void
127
     */
11 efrain 128
    public function test_locks(): void {
1 efrain 129
        // Run the suite on the current configured default (may be non-core).
130
        $this->run_on_lock_factory(\core\lock\lock_config::get_lock_factory_class());
131
 
132
        // Manually create the core no-configuration factories.
133
        $this->run_on_lock_factory(\core\lock\db_record_lock_factory::class);
134
        $this->run_on_lock_factory(\core\lock\file_lock_factory::class);
1441 ariadna 135
    }
1 efrain 136
 
1441 ariadna 137
    public function test_exception_for_unreleased_locks(): void {
138
        $factory = new \core\lock\db_record_lock_factory('mod_assign');
139
        $lock = $factory->get_lock('abc', 0);
140
 
141
        // Using try/catch, not expectException, because I want to verify a few things.
142
        try {
143
            $lock->release_if_not_released(true);
144
        } catch (coding_exception $e) {
145
            $this->assertStringContainsString('A lock was created but not released at:', $e->getMessage());
146
            $this->assertStringContainsString('call to core\lock\db_record_lock_factory->get_lock()', $e->getMessage());
147
            $this->assertStringContainsString('call to core\lock_test->test_exception_for_unreleased_locks()', $e->getMessage());
148
            return;
149
        }
150
 
151
        // Now use expectException to get the standard failure message.
152
        $this->expectException(coding_exception::class);
1 efrain 153
    }
154
}
155