function DatabaseBackendTest::testBinCreatedAfterQueryFailure

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php \Drupal\KernelTests\Core\Cache\DatabaseBackendTest::testBinCreatedAfterQueryFailure()

Tests write operations when another process makes the bin table.

The other process makes the table after the first query fails.

Attributes

#[TestWith([ 'setMultiple', ])] #[TestWith([ 'invalidateMultiple', ])] #[TestWith([ 'deleteMultiple', ])] #[TestWith([ 'deleteAll', ])]

Parameters

string $method: The name of the cache backend method to call.

File

core/tests/Drupal/KernelTests/Core/Cache/DatabaseBackendTest.php, line 117

Class

DatabaseBackendTest
Unit test of the database backend using the generic cache unit test base.

Namespace

Drupal\KernelTests\Core\Cache

Code

public function testBinCreatedAfterQueryFailure(string $method) : void {
  $backend = $this->createCacheBackend('created_concurrently');
  $connection = $this->container
    ->get('database');
  $table = 'cache_created_concurrently';
  $this->assertFalse($connection->schema()
    ->tableExists($table));
  // Make the table after the first query fails and before the backend
  // catches the exception. Another process can do this.
  $created = FALSE;
  $connection->enableEvents([
    StatementExecutionStartEvent::class,
    StatementExecutionFailureEvent::class,
  ]);
  $this->container
    ->get('event_dispatcher')
    ->addListener(StatementExecutionFailureEvent::class, function () use ($backend, $connection, $table, &$created) : void {
    if (!$created) {
      $created = TRUE;
      $connection->schema()
        ->createTable($table, $backend->schemaDefinition());
    }
  });
  match ($method) {  'setMultiple' => $backend->setMultiple([
      'test' => [
        'data' => 'value',
      ],
    ]),
    'deleteAll' => $backend->deleteAll(),
    default => $backend->{$method}([
      'test',
    ]),
  
  };
  $this->assertTrue($created);
  if ($method === 'setMultiple') {
    $this->assertSame('value', $backend->get('test')->data);
  }
}

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