Skip to content

Added 'QueueTrait' with Drupal core queue management and assertion steps. #601

@AlexSkrypnyk

Description

@AlexSkrypnyk

Summary

Add a new QueueTrait for managing and asserting Drupal core queue state
during BDD tests.

Motivation

Queue-driven processing is common in Drupal: batch operations, email
sending, search indexing, data sync, webhook processing. Drupal core's
Queue API (\Drupal\Core\Queue\QueueInterface) provides the foundation,
and contrib modules like AdvancedQueue build on top of it.

DrupalExtension provides no queue steps.

Proposed steps

Given the "my_queue" queue is empty

When I process 5 items from the "my_queue" queue
When I process all items from the "my_queue" queue

Then the "my_queue" queue should have 3 items
Then the "my_queue" queue should be empty

Implementation notes

  • New file: src/Drupal/QueueTrait.php.
  • Use \Drupal::queue($queue_name) to get QueueInterface instances.
  • Given queue is empty: call $queue->deleteQueue() then
    $queue->createQueue() to reset.
  • When I process N items: get queue worker via
    \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name)
    and call $worker->processItem($item->data) in a loop with
    $queue->claimItem() / $queue->deleteItem().
  • When I process all items: same loop until $queue->numberOfItems() === 0
    or claimItem() returns FALSE, with a safety limit to prevent infinite
    loops.
  • Then queue should have N items: call $queue->numberOfItems() and
    compare.
  • Method naming: queueEmpty, queueProcessItems, queueProcessAll,
    queueAssertItemCount, queueAssertEmpty.
  • Add @AfterScenario hook (tagged @queue) to clean up queues created
    during the scenario.

Configurable defaults (protected methods)

All limits and settings should be exposed as protected methods so that
consumer FeatureContext classes can override them:

// Maximum items to process in "process all" to prevent infinite loops.
protected function queueGetProcessLimit(): int {
  return 1000;
}

// Lease time in seconds when claiming queue items.
protected function queueGetLeaseTime(): int {
  return 30;
}

Scope boundaries

  • This trait targets Drupal core Queue API (DatabaseQueue, Batch,
    Memory).
  • AdvancedQueue has a fundamentally different API (entity-based jobs with
    states, not claim/release). If needed, it should be a separate
    AdvancedQueueTrait in a future issue.
  • No queue creation steps — queues are created by modules, not by tests.

Acceptance criteria

  • New trait in src/Drupal/QueueTrait.php.
  • Steps use #[Given(...)] / #[When(...)] / #[Then(...)] tuple annotations.
  • Feature tests in tests/behat/features/queue.feature.
  • @trait:QueueTrait negative tests.
  • Safety limit on processAll to prevent infinite loops.
  • @AfterScenario cleanup hook for @queue-tagged scenarios.
  • ahoy update-docs run to regenerate STEPS.md.
  • ahoy lint passes.
  • Add use QueueTrait; to FeatureContext.php.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions