function CronSuspendQueueDelayTest::testSuspendQueue

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Cron/CronSuspendQueueDelayTest.php \Drupal\Tests\Core\Cron\CronSuspendQueueDelayTest::testSuspendQueue()

Tests a queue is reprocessed again after other queues.

Two queues are created:

  • test_worker_a.
  • test_worker_b.

Queues and items are processed:

  • test_worker_a:

    • item throws SuspendQueueException with 2.0 delay.
  • test_worker_b:
    • item executes normally.
  • test_worker_a:
    • item throws SuspendQueueException with 3.0 delay.
  • test_worker_a:
    • no items remaining, quits.

File

core/tests/Drupal/Tests/Core/Cron/CronSuspendQueueDelayTest.php, line 126

Class

CronSuspendQueueDelayTest
Test Cron handling of suspended queues with a delay.

Namespace

Drupal\Tests\Core\Cron

Code

public function testSuspendQueue() : void {
  [
    'queue_factory' => $queueFactory,
    'queue_manager' => $queueManager,
    'time' => $time,
  ] = $this->cronConstructorArguments;
  $cron = $this->getMockBuilder(Cron::class)
    ->onlyMethods([
    'usleep',
  ])
    ->setConstructorArgs($this->cronConstructorArguments)
    ->getMock();
  $delays = [
    2000000,
    3000000,
  ];
  $cron->expects($this->exactly(count($delays)))
    ->method('usleep')
    ->with($this->callback(function (int $delay) use (&$delays) : bool {
    return array_shift($delays) === $delay;
  }));
  $queueManager->expects($this->once())
    ->method('getDefinitions')
    ->willReturn([
    'test_worker_a' => [
      'id' => 'test_worker_a',
      'cron' => [
        'time' => 300,
      ],
    ],
    'test_worker_b' => [
      'id' => 'test_worker_b',
      'cron' => [
        'time' => 300,
      ],
    ],
  ]);
  $queueA = $this->createMock(QueueInterface::class);
  $queueB = $this->createMock(QueueInterface::class);
  $queueFactory->expects($this->exactly(2))
    ->method('get')
    ->willReturnMap([
    [
      'test_worker_a',
      FALSE,
      $queueA,
    ],
    [
      'test_worker_b',
      FALSE,
      $queueB,
    ],
  ]);
  // Expect this queue to be processed twice.
  $queueA->expects($this->exactly(3))
    ->method('claimItem')
    ->willReturnOnConsecutiveCalls((object) [
    'data' => 'test_data_a1',
  ], (object) [
    'data' => 'test_data_a2',
  ], FALSE);
  // Expect this queue to be processed once.
  $queueB->expects($this->exactly(2))
    ->method('claimItem')
    ->willReturnOnConsecutiveCalls((object) [
    'data' => 'test_data_b1',
  ], FALSE);
  $queueManager->expects($this->any())
    ->method('createInstance')
    ->willReturnMap([
    [
      'test_worker_a',
      [],
      $this->workerA,
    ],
    [
      'test_worker_b',
      [],
      $this->workerB,
    ],
  ]);
  $this->workerA
    ->expects($this->exactly(2))
    ->method('processItem')
    ->with($this->anything())
    ->willReturnOnConsecutiveCalls($this->throwException(new SuspendQueueException('', 0, NULL, 2.0)), $this->throwException(new SuspendQueueException('', 0, NULL, 3.0)));
  $this->workerB
    ->expects($this->once())
    ->method('processItem')
    ->with('test_data_b1');
  $time->expects($this->any())
    ->method('getCurrentTime')
    ->willReturn(60);
  $cron->run();
}

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