You are here

protected function BuildTestBase::findAvailablePort in Drupal 8

Same name and namespace in other branches
  1. 9 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 466

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 Factory($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.');
}