You are here

public function CronQueueTest::testDelayException in Drupal 9

Tests that DelayedRequeueException behaves as expected when running cron.

File

core/modules/system/tests/src/Kernel/System/CronQueueTest.php, line 81

Class

CronQueueTest
Tests the Cron Queue runner.

Namespace

Drupal\Tests\system\Kernel\System

Code

public function testDelayException() {
  $database = $this->container
    ->get('queue')
    ->get('cron_queue_test_database_delay_exception');
  $memory = $this->container
    ->get('queue')
    ->get('cron_queue_test_memory_delay_exception');

  // Ensure that the queues are of the correct type for this test.
  $this
    ->assertInstanceOf('Drupal\\Core\\Queue\\DelayableQueueInterface', $database);
  $this
    ->assertNotInstanceOf('Drupal\\Core\\Queue\\DelayableQueueInterface', $memory);

  // Get the queue worker plugin manager.
  $manager = $this->container
    ->get('plugin.manager.queue_worker');
  $definitions = $manager
    ->getDefinitions();
  $this
    ->assertNotEmpty($database_lease_time = $definitions['cron_queue_test_database_delay_exception']['cron']['time']);
  $this
    ->assertNotEmpty($memory_lease_time = $definitions['cron_queue_test_memory_delay_exception']['cron']['time']);

  // Create the necessary test data and run cron.
  $database
    ->createItem('test');
  $memory
    ->createItem('test');
  $this->cron
    ->run();

  // Fetch the expiry time for the database queue.
  $query = $this->connection
    ->select('queue');
  $query
    ->condition('name', 'cron_queue_test_database_delay_exception');
  $query
    ->addField('queue', 'expire');
  $query
    ->range(0, 1);
  $expire = $query
    ->execute()
    ->fetchField();

  // Assert that the delay interval is greater than the lease interval. This
  // allows us to assume that (if updated) the new expiry time will be greater
  // than the initial expiry time. We can then also assume that the new expiry
  // time offset will be identical to the delay interval.
  $this
    ->assertGreaterThan($database_lease_time, CronQueueTestDatabaseDelayException::DELAY_INTERVAL);
  $this
    ->assertGreaterThan($this->currentTime + $database_lease_time, $expire);
  $this
    ->assertEquals(CronQueueTestDatabaseDelayException::DELAY_INTERVAL, $expire - $this->currentTime);

  // Ensure that the memory queue expiry time is unchanged after the
  // DelayedRequeueException has been thrown.
  $property = (new \ReflectionClass($memory))
    ->getProperty('queue');
  $property
    ->setAccessible(TRUE);
  $memory_queue_internal = $property
    ->getValue($memory);
  $this
    ->assertEquals($this->currentTime + $memory_lease_time, reset($memory_queue_internal)->expire);
}