Same name in this branch
  1. 10 core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageTest.php \Drupal\KernelTests\Core\KeyValueStore\DatabaseStorageTest
  2. 10 core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php \Drupal\KernelTests\Core\Config\Storage\DatabaseStorageTest
Same name and namespace in other branches
  1. 8.9.x core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageTest.php \Drupal\KernelTests\Core\KeyValueStore\DatabaseStorageTest
  2. 9 core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageTest.php \Drupal\KernelTests\Core\KeyValueStore\DatabaseStorageTest

Tests the key-value database storage.

@group KeyValueStore

Hierarchy

Expanded class hierarchy of DatabaseStorageTest

File

core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageTest.php, line 17

Namespace

Drupal\KernelTests\Core\KeyValueStore
View source
class DatabaseStorageTest extends StorageTestBase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->factory = 'keyvalue.database';
  }

  /**
   * {@inheritdoc}
   */
  public function register(ContainerBuilder $container) {
    parent::register($container);
    $parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.database';
    $container
      ->setParameter('factory.keyvalue', $parameter);
  }

  /**
   * Tests asynchronous table creation.
   */
  public function testConcurrent() {
    $this
      ->markTestSkipped("Skipped due to frequent random test failures. See https://www.drupal.org/project/drupal/issues/3398063");
    if (!function_exists('pcntl_fork')) {
      $this
        ->markTestSkipped('Requires the pcntl_fork() function');
    }
    $functions = [];
    for ($i = 1; $i <= 10; $i++) {
      $functions[] = 'set';
      $functions[] = 'getAll';
    }
    $default_connection = Database::getConnectionInfo();
    Database::removeConnection('default');
    $time_to_start = microtime(TRUE) + 0.1;

    // This loop creates a new fork to set or get key values keys.
    foreach ($functions as $i => $function) {
      $pid = pcntl_fork();
      if ($pid == -1) {
        $this
          ->fail("Error forking");
      }
      elseif ($pid == 0) {
        Database::addConnectionInfo('default' . $i, 'default', $default_connection['default']);
        Database::setActiveConnection('default' . $i);

        // Create a new factory using the new connection to avoid problems with
        // forks closing the database connections.
        $factory = new KeyValueDatabaseFactory($this->container
          ->get('serialization.phpserialize'), Database::getConnection());
        $store = $factory
          ->get('test');

        // Sleep so that all the forks start at the same time.
        usleep((int) (($time_to_start - microtime(TRUE)) * 1000000));
        if ($function === 'getAll') {
          $this
            ->assertIsArray($store
            ->getAll());
        }
        else {
          $this
            ->assertNull($store
            ->set('foo' . $i, 'bar'));
        }
        exit;
      }
    }

    // This while loop holds the parent process until all the child threads
    // are complete - at which point the script continues to execute.
    while (pcntl_waitpid(0, $status) != -1) {
    }
    Database::addConnectionInfo('default', 'default', $default_connection['default']);
    $factory = new KeyValueDatabaseFactory($this->container
      ->get('serialization.phpserialize'), Database::getConnection());
    $store = $factory
      ->get('test');
    $this
      ->assertCount(10, $store
      ->getAll());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseStorageTest::register public function
DatabaseStorageTest::setUp protected function Overrides StorageTestBase::setUp
DatabaseStorageTest::testConcurrent public function Tests asynchronous table creation.
StorageTestBase::$collections protected property An array of data collection labels.
StorageTestBase::$factory protected property Whether we are using an expirable key/value store.
StorageTestBase::$objects protected property An array of random stdClass objects.
StorageTestBase::createStorage protected function Creates storage objects for each collection defined for this class.
StorageTestBase::testCRUD public function Tests CRUD operations.
StorageTestBase::testNonExistingKeys public function Tests expected behavior for non-existing keys.
StorageTestBase::testRename public function Tests the rename operation.
StorageTestBase::testRenameNoChange public function Tests the rename operation.
StorageTestBase::testSetIfNotExists public function Tests the setIfNotExists() method.