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');
51 // Test acquiring an releasing a lock with a long key (over 255 chars).
52 $long_key = 'long_key:BZoMiSf9IIPULsJ98po18TxJ6T4usd3MZrLE0d3qMgG6iAgDlOi1G3oMap7zI5df84l7LtJBg4bOj6XvpO6vDRmP5h5QbA0Bj9rVFiPIPAIQZ9qFvJqTALiK1OR3GpOkWQ4vgEA4LkY0UfznrWBeuK7IWZfv1um6DLosnVXd1z1cJjvbEUqYGJj92rwHfhYihLm8IO9t3P2gAvEkH5Mhc8GBoiTsIDnP01Te1kxGFHO3RuvJIxPnHmZtSdBggmuVN7x9';
54 $success = $this->lock->acquire($long_key);
55 $this->assertTrue($success, 'Could acquire long key lock.');
57 // This function is not part of the backend, but the default database
58 // backend implement it, we can here use it safely.
59 $is_free = $this->lock->lockMayBeAvailable($long_key);
60 $this->assertFalse($is_free, 'Long key lock is unavailable.');
62 $this->lock->release($long_key);
63 $is_free = $this->lock->lockMayBeAvailable($long_key);
64 $this->assertTrue($is_free, 'Long key lock has been released.');
68 * Tests backend release functionality.
70 public function testBackendLockReleaseAll() {
71 $success = $this->lock->acquire('lock_a');
72 $this->assertTrue($success, 'Could acquire first lock.');
74 $success = $this->lock->acquire('lock_b');
75 $this->assertTrue($success, 'Could acquire second lock.');
77 $this->lock->releaseAll();
79 $is_free = $this->lock->lockMayBeAvailable('lock_a');
80 $this->assertTrue($is_free, 'First lock has been released.');
82 $is_free = $this->lock->lockMayBeAvailable('lock_b');
83 $this->assertTrue($is_free, 'Second lock has been released.');