Newer
Older
<?php
namespace Drupal\system\Tests\Lock;
use Drupal\Core\Lock\DatabaseLockBackend;
Alex Pott
committed
use Drupal\simpletest\KernelTestBase;
* Tests the Database lock backend.
*
* @group Lock
Alex Pott
committed
class LockUnitTest extends KernelTestBase {
/**
* Database lock backend to test.
*
* @var \Drupal\Core\Lock\DatabaseLockBackend
*/
protected $lock;
Alex Pott
committed
protected function setUp() {
parent::setUp();
Alex Pott
committed
$this->lock = new DatabaseLockBackend($this->container->get('database'));
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
}
/**
* Tests backend release functionality.
*/
public function testBackendLockRelease() {
$success = $this->lock->acquire('lock_a');
$this->assertTrue($success, 'Could acquire first lock.');
// This function is not part of the backend, but the default database
// backend implement it, we can here use it safely.
$is_free = $this->lock->lockMayBeAvailable('lock_a');
$this->assertFalse($is_free, 'First lock is unavailable.');
$this->lock->release('lock_a');
$is_free = $this->lock->lockMayBeAvailable('lock_a');
$this->assertTrue($is_free, 'First lock has been released.');
$success = $this->lock->acquire('lock_b');
$this->assertTrue($success, 'Could acquire second lock.');
$success = $this->lock->acquire('lock_b');
$this->assertTrue($success, 'Could acquire second lock a second time within the same request.');
$this->lock->release('lock_b');
}
/**
* Tests backend release functionality.
*/
public function testBackendLockReleaseAll() {
$success = $this->lock->acquire('lock_a');
$this->assertTrue($success, 'Could acquire first lock.');
$success = $this->lock->acquire('lock_b');
$this->assertTrue($success, 'Could acquire second lock.');
$this->lock->releaseAll();
$is_free = $this->lock->lockMayBeAvailable('lock_a');
$this->assertTrue($is_free, 'First lock has been released.');
$is_free = $this->lock->lockMayBeAvailable('lock_b');
$this->assertTrue($is_free, 'Second lock has been released.');
}