function TestDatabase::getTestLock

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Test/TestDatabase.php \Drupal\Core\Test\TestDatabase::getTestLock()
  2. 10 core/lib/Drupal/Core/Test/TestDatabase.php \Drupal\Core\Test\TestDatabase::getTestLock()
  3. 11.x core/lib/Drupal/Core/Test/TestDatabase.php \Drupal\Core\Test\TestDatabase::getTestLock()

Generates a unique lock ID for the test method.

Parameters

bool $create_lock: (optional) Whether or not to create a lock file. Defaults to FALSE.

Return value

int The unique lock ID for the test method.

1 call to TestDatabase::getTestLock()
TestDatabase::__construct in core/lib/Drupal/Core/Test/TestDatabase.php
TestDatabase constructor.
1 method overrides TestDatabase::getTestLock()
TestTestDatabase::getTestLock in core/tests/Drupal/Tests/Core/Test/TestDatabaseTest.php
Generates a unique lock ID for the test method.

File

core/lib/Drupal/Core/Test/TestDatabase.php, line 120

Class

TestDatabase
Provides helper methods for interacting with the fixture database.

Namespace

Drupal\Core\Test

Code

protected function getTestLock($create_lock = FALSE) {
    // There is a risk that the generated random number is a duplicate. This
    // would cause different tests to try to use the same database prefix.
    // Therefore, if running with a concurrency of greater than 1, we need to
    // create a lock.
    if (getenv('RUN_TESTS_CONCURRENCY') > 1) {
        $create_lock = TRUE;
    }
    do {
        $lock_id = mt_rand(10000000, 99999999);
        if ($create_lock && @symlink(__FILE__, $this->getLockFile($lock_id)) === FALSE) {
            // If we can't create a symlink, the lock ID is in use. Generate another
            // one. Symlinks are used because they are atomic and reliable.
            $lock_id = NULL;
        }
    } while ($lock_id === NULL);
    return $lock_id;
}

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