Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11 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\lock;
18
 
19
/**
20
 * Unit tests for the postgres lock factory.
21
 *
22
 * @covers \core\lock\postgres_lock_factory
23
 * @package core
24
 * @copyright 2024 Martin Gauk <martin.gauk@tu-berlin.de>
25
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
final class postgres_lock_factory_test extends \advanced_testcase {
28
    /**
29
     * Set up.
30
     */
31
    public function setUp(): void {
32
        global $DB;
33
        parent::setUp();
34
        // Skip tests if not using Postgres.
35
        if (!($DB instanceof \pgsql_native_moodle_database)) {
36
            $this->markTestSkipped('Postgres-only test');
37
        }
38
    }
39
 
40
    /**
41
     * Test locking resources that lead to the same token computed by postgres_lock_factory::get_index_from_key.
42
     *
43
     * It is known that get_index_from_key computes the same token for the strings 'cron_core_cron' and
44
     * 'cron_adhoc_82795762'.
45
     */
46
    public function test_resources_with_same_hash(): void {
47
        $cronlockfactory = new postgres_lock_factory('cron');
48
        $lock1 = $cronlockfactory->get_lock('core_cron', 0);
49
        $this->assertNotFalse($lock1);
50
        $lock2 = $cronlockfactory->get_lock('adhoc_82795762', 0);
51
        $this->assertNotFalse($lock2);
52
 
53
        $lock2->release();
54
        $lock1->release();
55
    }
56
 
57
    /**
58
     * Test auto_release() method.
59
     *
60
     * Check that all locks were released after calling auto_release().
61
     */
62
    public function test_auto_release(): void {
63
        $cronlockfactory = new postgres_lock_factory('test');
64
        $lock1 = $cronlockfactory->get_lock('1', 0);
65
        $this->assertNotFalse($lock1);
66
        $lock2 = $cronlockfactory->get_lock('2', 0);
67
        $this->assertNotFalse($lock2);
68
 
69
        // Trying to get the lock again should fail as the lock is already held.
70
        $this->assertFalse($cronlockfactory->get_lock('1', 0));
71
 
72
        $cronlockfactory->auto_release();
73
 
74
        // We can now lock the resources again.
75
        $lock1again = $cronlockfactory->get_lock('1', 0);
76
        $this->assertNotFalse($lock1again);
77
        $lock2again = $cronlockfactory->get_lock('2', 0);
78
        $this->assertNotFalse($lock2again);
79
 
80
        $lock1again->release();
81
        $lock2again->release();
82
 
83
        // Need to explicitly release the locks (although they were already released) to avoid the debug message.
84
        $lock1->release();
85
        $lock2->release();
86
    }
87
}