LockFunctionalTest.php in Drupal 8
File
core/modules/system/tests/src/Functional/Lock/LockFunctionalTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\Lock;
use Drupal\Tests\BrowserTestBase;
class LockFunctionalTest extends BrowserTestBase {
public static $modules = [
'system_test',
];
protected $defaultTheme = 'stark';
public function testLockAcquire() {
$lock = $this->container
->get('lock');
$lock_acquired = 'TRUE: Lock successfully acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockAcquire()';
$lock_not_acquired = 'FALSE: Lock not acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockAcquire()';
$this
->assertTrue($lock
->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock');
$this
->assertTrue($lock
->acquire('system_test_lock_acquire'), 'Lock extended by this request.', 'Lock');
$lock
->release('system_test_lock_acquire');
$this
->drupalGet('system-test/lock-acquire');
$this
->assertText($lock_acquired, 'Lock acquired by the other request.', 'Lock');
$this
->assertTrue($lock
->acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock');
$this
->drupalGet('system-test/lock-acquire');
$this
->assertText($lock_not_acquired, 'Lock not acquired by the other request.', 'Lock');
$lock
->release('system_test_lock_acquire');
$this
->assertTrue($lock
->acquire('system_test_lock_acquire', 0.5), 'Lock acquired by this request.', 'Lock');
sleep(1);
$this
->drupalGet('system-test/lock-acquire');
$this
->assertText($lock_acquired, 'Lock acquired by the other request, breaking our lock.', 'Lock');
$this
->assertFalse($lock
->acquire('system_test_lock_acquire'), 'Lock cannot be extended by this request.', 'Lock');
$lock_acquired_exit = 'TRUE: Lock successfully acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockExit()';
$this
->drupalGet('system-test/lock-exit');
$this
->assertText($lock_acquired_exit, 'Lock acquired by the other request before exit.', 'Lock');
$this
->assertTrue($lock
->acquire('system_test_lock_exit'), 'Lock acquired by this request after the other request exits.', 'Lock');
}
public function testPersistentLock() {
$persistent_lock = $this->container
->get('lock.persistent');
$this
->drupalGet('system-test/lock-persist/lock1');
$this
->assertText('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
$this
->assertFalse($persistent_lock
->lockMayBeAvailable('lock1'));
$this
->drupalGet('system-test/lock-persist/lock1');
$this
->assertText('FALSE: Lock not acquired in SystemTestController::lockPersist()');
$this
->drupalGet('system-test/lock-persist/lock2');
$this
->assertText('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
$this
->assertFalse($persistent_lock
->lockMayBeAvailable('lock2'));
$persistent_lock
->release('lock1');
$this
->drupalGet('system-test/lock-persist/lock1');
$this
->assertText('TRUE: Lock successfully acquired in SystemTestController::lockPersist()');
}
}