You are here

public function ConnectionUnitTest::testOpenClose in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php \Drupal\KernelTests\Core\Database\ConnectionUnitTest::testOpenClose()
  2. 9 core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php \Drupal\KernelTests\Core\Database\ConnectionUnitTest::testOpenClose()

Tests Database::closeConnection() without query.

@todo getConnectionId() executes a query.

File

core/tests/Drupal/KernelTests/Core/Database/ConnectionUnitTest.php, line 124

Class

ConnectionUnitTest
Tests management of database connections.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testOpenClose() {

  // Do not run this test for an SQLite database.
  $database_type = $this->connection
    ->databaseType();
  if ($database_type != 'mysql' && $database_type != 'pgsql') {
    $this
      ->markTestSkipped("This tests only runs on MySQL and PostgreSQL");
  }

  // Add and open a new connection.
  $this
    ->addConnection();
  $id = $this
    ->getConnectionId();
  Database::getConnection(static::TEST_TARGET_CONNECTION);

  // Verify that there is a new connection.
  $this
    ->assertConnection($id);

  // Close the connection.
  Database::closeConnection(static::TEST_TARGET_CONNECTION);

  // Wait 20ms to give the database engine sufficient time to react.
  usleep(20000);

  // Verify that we are back to the original connection count.
  $this
    ->assertNoConnection($id);
}