You are here

public function SocialDrupalContext::iWaitForTheQueueToBeEmpty in Open Social 8

Same name and namespace in other branches
  1. 8.2 tests/behat/features/bootstrap/SocialDrupalContext.php \SocialDrupalContext::iWaitForTheQueueToBeEmpty()

@When I wait for the queue to be empty

File

tests/behat/features/bootstrap/SocialDrupalContext.php, line 105

Class

SocialDrupalContext
Provides pre-built step definitions for interacting with Open Social.

Code

public function iWaitForTheQueueToBeEmpty() {
  $workerManager = \Drupal::service('plugin.manager.queue_worker');

  /** @var Drupal\Core\Queue\QueueFactory; $queue */
  $queue = \Drupal::service('queue');
  for ($i = 0; $i < 20; $i++) {
    foreach ($workerManager
      ->getDefinitions() as $name => $info) {

      /** @var Drupal\Core\Queue\QueueInterface $worker */
      $worker = $queue
        ->get($name);

      /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
      $queue_worker = $workerManager
        ->createInstance($name);
      if ($worker
        ->numberOfItems() > 0) {
        while ($item = $worker
          ->claimItem()) {
          $queue_worker
            ->processItem($item->data);
          $worker
            ->deleteItem($item);
        }
      }
    }
  }
}