aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lock/db_test.php
blob: e96920ed1ff1a25003cfb60606f356e37344d9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

class phpbb_lock_db_test extends phpbb_database_test_case
{
	private $db;
	private $config;
	private $lock;

	public function getDataSet()
	{
		return $this->createXMLDataSet(dirname(__FILE__).'/fixtures/config.xml');
	}

	public function setUp(): void
	{
		global $db, $config;

		$db = $this->db = $this->new_dbal();
		$config = $this->config = new \phpbb\config\config(array('rand_seed' => '', 'rand_seed_last_update' => '0'));
		$this->lock = new \phpbb\lock\db('test_lock', $this->config, $this->db);
	}

	public function test_new_lock()
	{
		$this->assertFalse($this->lock->owns_lock());

		$this->assertTrue($this->lock->acquire());
		$this->assertTrue($this->lock->owns_lock());
		$this->assertTrue(isset($this->config['test_lock']), 'Lock was created');

		$lock2 = new \phpbb\lock\db('test_lock', $this->config, $this->db);
		$this->assertFalse($lock2->acquire());
		$this->assertFalse($lock2->owns_lock());

		$this->lock->release();
		$this->assertFalse($this->lock->owns_lock());
		$this->assertEquals('0', $this->config['test_lock'], 'Lock was released');
	}

	public function test_expire_lock()
	{
		$lock = new \phpbb\lock\db('foo_lock', $this->config, $this->db);
		$this->assertTrue($lock->acquire());
	}

	public function test_double_lock()
	{
		$this->assertFalse($this->lock->owns_lock());

		$this->assertTrue($this->lock->acquire());
		$this->assertTrue($this->lock->owns_lock());
		$this->assertTrue(isset($this->config['test_lock']), 'Lock was created');

		$value = $this->config['test_lock'];

		$this->assertFalse($this->lock->acquire());
		$this->assertTrue($this->lock->owns_lock());
		$this->assertEquals($value, $this->config['test_lock'], 'Second lock failed');

		$this->lock->release();
		$this->assertFalse($this->lock->owns_lock());
		$this->assertEquals('0', $this->config['test_lock'], 'Lock was released');
	}

	public function test_double_unlock()
	{
		$this->assertTrue($this->lock->acquire());
		$this->assertTrue($this->lock->owns_lock());
		$this->assertFalse(empty($this->config['test_lock']), 'First lock is acquired');

		$this->lock->release();
		$this->assertFalse($this->lock->owns_lock());
		$this->assertEquals('0', $this->config['test_lock'], 'First lock is released');

		$lock2 = new \phpbb\lock\db('test_lock', $this->config, $this->db);
		$this->assertTrue($lock2->acquire());
		$this->assertTrue($lock2->owns_lock());
		$this->assertFalse(empty($this->config['test_lock']), 'Second lock is acquired');

		$this->lock->release();
		$this->assertTrue($lock2->owns_lock());
		$this->assertFalse(empty($this->config['test_lock']), 'Double release of first lock is ignored');

		$lock2->release();
		$this->assertEquals('0', $this->config['test_lock'], 'Second lock is released');
	}
}