function LockFunctionalTest::testLockAcquire

Same name in other branches
  1. 7.x modules/simpletest/tests/lock.test \LockFunctionalTest::testLockAcquire()
  2. 9 core/modules/system/tests/src/Functional/Lock/LockFunctionalTest.php \Drupal\Tests\system\Functional\Lock\LockFunctionalTest::testLockAcquire()
  3. 8.9.x core/modules/system/tests/src/Functional/Lock/LockFunctionalTest.php \Drupal\Tests\system\Functional\Lock\LockFunctionalTest::testLockAcquire()
  4. 11.x core/modules/system/tests/src/Functional/Lock/LockFunctionalTest.php \Drupal\Tests\system\Functional\Lock\LockFunctionalTest::testLockAcquire()

Confirms that we can acquire and release locks in two parallel requests.

File

core/modules/system/tests/src/Functional/Lock/LockFunctionalTest.php, line 29

Class

LockFunctionalTest
Confirm locking works between two separate requests.

Namespace

Drupal\Tests\system\Functional\Lock

Code

public function testLockAcquire() : void {
    $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.');
    $this->assertTrue($lock->acquire('system_test_lock_acquire'), 'Lock extended by this request.');
    $lock->release('system_test_lock_acquire');
    // Cause another request to acquire the lock.
    $this->drupalGet('system-test/lock-acquire');
    $this->assertSession()
        ->pageTextContains($lock_acquired);
    // The other request has finished, thus it should have released its lock.
    $this->assertTrue($lock->acquire('system_test_lock_acquire'), 'Lock acquired by this request.');
    // This request holds the lock, so the other request cannot acquire it.
    $this->drupalGet('system-test/lock-acquire');
    $this->assertSession()
        ->pageTextContains($lock_not_acquired);
    $lock->release('system_test_lock_acquire');
    // Try a very short timeout and lock breaking.
    $this->assertTrue($lock->acquire('system_test_lock_acquire', 0.5), 'Lock acquired by this request.');
    sleep(1);
    // The other request should break our lock.
    $this->drupalGet('system-test/lock-acquire');
    $this->assertSession()
        ->pageTextContains($lock_acquired);
    // We cannot renew it, since the other thread took it.
    $this->assertFalse($lock->acquire('system_test_lock_acquire'), 'Lock cannot be extended by this request.');
    // Check the shut-down function.
    $lock_acquired_exit = 'TRUE: Lock successfully acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockExit()';
    $this->drupalGet('system-test/lock-exit');
    $this->assertSession()
        ->pageTextContains($lock_acquired_exit);
    $this->assertTrue($lock->acquire('system_test_lock_exit'), 'Lock acquired by this request after the other request exits.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.