function CronQueueTest::testLeaseTime

Same name in other branches
  1. 10 core/modules/system/tests/src/Kernel/System/CronQueueTest.php \Drupal\Tests\system\Kernel\System\CronQueueTest::testLeaseTime()
  2. 11.x core/modules/system/tests/src/Kernel/System/CronQueueTest.php \Drupal\Tests\system\Kernel\System\CronQueueTest::testLeaseTime()

Tests that leases are expiring correctly, also within the same request.

File

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

Class

CronQueueTest
Tests the Cron Queue runner.

Namespace

Drupal\Tests\system\Kernel\System

Code

public function testLeaseTime() {
    $queue = $this->container
        ->get('queue')
        ->get('cron_queue_test_lease_time');
    $queue->createItem([
        $this->randomMachineName() => $this->randomMachineName(),
    ]);
    // Run initial queue job and ensure lease time variable is initialized.
    $this->cron
        ->run();
    static::assertEquals(1, \Drupal::state()->get('cron_queue_test_lease_time'));
    // Ensure the same queue job is not picked up due to the extended lease.
    $this->cron
        ->run();
    static::assertEquals(1, \Drupal::state()->get('cron_queue_test_lease_time'));
    // Set the expiration time to 3 seconds ago, so the lease should
    // automatically expire.
    \Drupal::database()->update(DatabaseQueue::TABLE_NAME)
        ->fields([
        'expire' => $this->currentTime - 3,
    ])
        ->execute();
    // The queue job should now be picked back up since it's lease has expired,
    // and the state variable should be consequently incremented.
    $this->cron
        ->run();
    static::assertEquals(2, \Drupal::state()->get('cron_queue_test_lease_time'));
    // Ensure the same queue job is not picked up again due to the extended
    // lease.
    $this->cron
        ->run();
    static::assertEquals(2, \Drupal::state()->get('cron_queue_test_lease_time'));
}

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