3 namespace Drupal\KernelTests\Core\Queue;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Queue\DatabaseQueue;
7 use Drupal\Core\Queue\Memory;
8 use Drupal\KernelTests\KernelTestBase;
11 * Queues and dequeues a set of items to check the basic queue functionality.
15 class QueueTest extends KernelTestBase {
18 * Tests the System queue.
20 public function testSystemQueue() {
22 $queue1 = new DatabaseQueue($this->randomMachineName(), Database::getConnection());
23 $queue1->createQueue();
24 $queue2 = new DatabaseQueue($this->randomMachineName(), Database::getConnection());
25 $queue2->createQueue();
27 $this->runQueueTest($queue1, $queue2);
31 * Tests the Memory queue.
33 public function testMemoryQueue() {
35 $queue1 = new Memory($this->randomMachineName());
36 $queue1->createQueue();
37 $queue2 = new Memory($this->randomMachineName());
38 $queue2->createQueue();
40 $this->runQueueTest($queue1, $queue2);
44 * Queues and dequeues a set of items to check the basic queue functionality.
46 * @param \Drupal\Core\Queue\QueueInterface $queue1
47 * An instantiated queue object.
48 * @param \Drupal\Core\Queue\QueueInterface $queue2
49 * An instantiated queue object.
51 protected function runQueueTest($queue1, $queue2) {
54 for ($i = 0; $i < 4; $i++) {
55 $data[] = [$this->randomMachineName() => $this->randomMachineName()];
58 // Queue items 1 and 2 in the queue1.
59 $queue1->createItem($data[0]);
60 $queue1->createItem($data[1]);
62 // Retrieve two items from queue1.
66 $items[] = $item = $queue1->claimItem();
67 $new_items[] = $item->data;
69 $items[] = $item = $queue1->claimItem();
70 $new_items[] = $item->data;
72 // First two dequeued items should match the first two items we queued.
73 $this->assertEqual($this->queueScore($data, $new_items), 2, 'Two items matched');
75 // Add two more items.
76 $queue1->createItem($data[2]);
77 $queue1->createItem($data[3]);
79 $this->assertTrue($queue1->numberOfItems(), 'Queue 1 is not empty after adding items.');
80 $this->assertFalse($queue2->numberOfItems(), 'Queue 2 is empty while Queue 1 has items');
82 $items[] = $item = $queue1->claimItem();
83 $new_items[] = $item->data;
85 $items[] = $item = $queue1->claimItem();
86 $new_items[] = $item->data;
88 // All dequeued items should match the items we queued exactly once,
89 // therefore the score must be exactly 4.
90 $this->assertEqual($this->queueScore($data, $new_items), 4, 'Four items matched');
92 // There should be no duplicate items.
93 $this->assertEqual($this->queueScore($new_items, $new_items), 4, 'Four items matched');
95 // Delete all items from queue1.
96 foreach ($items as $item) {
97 $queue1->deleteItem($item);
100 // Check that both queues are empty.
101 $this->assertFalse($queue1->numberOfItems(), 'Queue 1 is empty');
102 $this->assertFalse($queue2->numberOfItems(), 'Queue 2 is empty');
106 * Returns the number of equal items in two arrays.
108 protected function queueScore($items, $new_items) {
110 foreach ($items as $item) {
111 foreach ($new_items as $new_item) {
112 if ($item === $new_item) {