function BuildTestBase::findAvailablePort

Same name in other branches
  1. 8.9.x core/tests/Drupal/BuildTests/Framework/BuildTestBase.php \Drupal\BuildTests\Framework\BuildTestBase::findAvailablePort()
  2. 10 core/tests/Drupal/BuildTests/Framework/BuildTestBase.php \Drupal\BuildTests\Framework\BuildTestBase::findAvailablePort()
  3. 11.x core/tests/Drupal/BuildTests/Framework/BuildTestBase.php \Drupal\BuildTests\Framework\BuildTestBase::findAvailablePort()

Discover an available port number.

Return value

int The available port number that we discovered.

Throws

\RuntimeException Thrown when there are no available ports within the range.

2 calls to BuildTestBase::findAvailablePort()
BuildTestBase::getPortNumber in core/tests/Drupal/BuildTests/Framework/BuildTestBase.php
Get the port number for requests.
BuildTestTest::testPortMany in core/tests/Drupal/BuildTests/Framework/Tests/BuildTestTest.php
@covers ::findAvailablePort

File

core/tests/Drupal/BuildTests/Framework/BuildTestBase.php, line 478

Class

BuildTestBase
Provides a workspace to test build processes.

Namespace

Drupal\BuildTests\Framework

Code

protected function findAvailablePort() {
    $store = new FlockStore(DrupalFilesystem::getOsTemporaryDirectory());
    $lock_factory = new LockFactory($store);
    $counter = 100;
    while ($counter--) {
        // Limit to 9999 as higher ports cause random fails on DrupalCI.
        $port = random_int(1024, 9999);
        if (isset($this->portLocks[$port])) {
            continue;
        }
        // Take a lock so that no other process can use the same port number even
        // if the server is yet to start.
        $lock = $lock_factory->createLock('drupal-build-test-port-' . $port);
        if ($lock->acquire()) {
            if ($this->checkPortIsAvailable($port)) {
                $this->portLocks[$port] = $lock;
                return $port;
            }
            else {
                $lock->release();
            }
        }
    }
    throw new \RuntimeException('Unable to find a port available to run the web server.');
}

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