3 namespace Drupal\KernelTests\Core\Lock;
5 use Drupal\Core\Lock\DatabaseLockBackend;
6 use Drupal\KernelTests\KernelTestBase;
9 * Tests the Database lock backend.
13 class LockTest extends KernelTestBase {
16 * Database lock backend to test.
18 * @var \Drupal\Core\Lock\DatabaseLockBackend
22 protected function setUp() {
24 $this->lock = new DatabaseLockBackend($this->container->get('database'));
28 * Tests backend release functionality.
30 public function testBackendLockRelease() {
31 $success = $this->lock->acquire('lock_a');
32 $this->assertTrue($success, 'Could acquire first lock.');
34 // This function is not part of the backend, but the default database
35 // backend implement it, we can here use it safely.
36 $is_free = $this->lock->lockMayBeAvailable('lock_a');
37 $this->assertFalse($is_free, 'First lock is unavailable.');
39 $this->lock->release('lock_a');
40 $is_free = $this->lock->lockMayBeAvailable('lock_a');
41 $this->assertTrue($is_free, 'First lock has been released.');
43 $success = $this->lock->acquire('lock_b');
44 $this->assertTrue($success, 'Could acquire second lock.');
46 $success = $this->lock->acquire('lock_b');
47 $this->assertTrue($success, 'Could acquire second lock a second time within the same request.');
49 $this->lock->release('lock_b');
53 * Tests backend release functionality.
55 public function testBackendLockReleaseAll() {
56 $success = $this->lock->acquire('lock_a');
57 $this->assertTrue($success, 'Could acquire first lock.');
59 $success = $this->lock->acquire('lock_b');
60 $this->assertTrue($success, 'Could acquire second lock.');
62 $this->lock->releaseAll();
64 $is_free = $this->lock->lockMayBeAvailable('lock_a');
65 $this->assertTrue($is_free, 'First lock has been released.');
67 $is_free = $this->lock->lockMayBeAvailable('lock_b');
68 $this->assertTrue($is_free, 'Second lock has been released.');