You are here

protected function AssertQueueTrait::assertQueueRun in Helper 8

Assert running a queue.

Parameters

string $queue_name: The queue name.

int $assert_count_before: The number of items to assert are in the queue before running. Use NULL to skip this assertion.

int $assert_count_after: The number of items to assert are in the queue after running. Use NULL to skip this assertion. Defaults to zero items (an empty queue).

int $time_limit: The number of seconds to limit running the queue. Defaults to 5 minutes.

File

src/Tests/AssertQueueTrait.php, line 63

Class

AssertQueueTrait
Provides assertions for running queues.

Namespace

Drupal\helper\Tests

Code

protected function assertQueueRun($queue_name, $assert_count_before = NULL, $assert_count_after = 0, $time_limit = 300) {
  $queue = \Drupal::queue($queue_name);
  if (isset($assert_count_before)) {
    $this
      ->assertQueueSize($queue_name, $assert_count_before);
  }

  /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
  $queue_worker = \Drupal::service('plugin.manager.queue_worker')
    ->createInstance($queue_name);
  $end = time() + $time_limit;
  $lease_time = 1;
  while (time() < $end && ($item = $queue
    ->claimItem($lease_time))) {
    try {
      $queue_worker
        ->processItem($item->data);
      $queue
        ->deleteItem($item);
    } catch (RequeueException $e) {
      $queue
        ->releaseItem($item);
    } catch (SuspendQueueException $e) {
      $queue
        ->releaseItem($item);
      $this
        ->markTestSkipped($e
        ->getMessage());
      break;
    }
  }
  if (isset($assert_count_after)) {
    if ($this
      ->assertQueueSize($queue_name, $assert_count_after) != $assert_count_after) {

      // If the count does not match, then assume something failed and mark
      // the test skipped.
      $this
        ->markTestSkipped("Could not complete running the {$queue_name} queue.");
    }
  }
}