forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueTest.php
148 lines (121 loc) · 6.55 KB
/
QueueTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Unish;
use Webmozart\PathUtil\Path;
/**
* @group commands
*/
class QueueTest extends CommandUnishTestCase
{
use TestModuleHelperTrait;
public function testQueue()
{
$expected = 'aggregator_feeds,%items,"Drupal\Core\Queue\DatabaseQueue"';
$sites = $this->setUpDrupal(1, true);
// Enable aggregator since it declares a queue.
$this->drush('pm-install', ['aggregator']);
$this->drush('queue-list');
$output = $this->getOutput();
$this->assertStringContainsString('aggregator_feeds', $output, 'Queue list shows the declared queue.');
// We need user to own to the feed.
$this->drush('user-create', ['example'], ['password' => 'password', 'mail' => "example@example.com"]);
$this->drush('php-script', ['queue_script'], ['script-path' => __DIR__ . '/resources']);
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutputAsList();
$this->assertEquals(str_replace('%items', 1, $expected), array_pop($output), 'Item was successfully added to the queue.');
$this->drush('queue-run', ['aggregator_feeds']);
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutputAsList();
$this->assertEquals(str_replace('%items', 0, $expected), array_pop($output), 'Queue item processed.');
}
/**
* Tests the queue-delete command.
*/
public function testQueueDelete()
{
$expected = 'aggregator_feeds,%items,"Drupal\Core\Queue\DatabaseQueue"';
$sites = $this->setUpDrupal(1, true);
// Enable aggregator since it declares a queue.
$this->drush('pm-install', ['aggregator']);
// Add another item to the queue and make sure it was deleted.
$this->drush('php-script', ['queue_script'], ['script-path' => __DIR__ . '/resources']);
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutputAsList();
$this->assertEquals(str_replace('%items', 1, $expected), array_pop($output), 'Item was successfully added to the queue.');
$this->drush('queue-delete', ['aggregator_feeds']);
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutputAsList();
$this->assertEquals(str_replace('%items', 0, $expected), array_pop($output), 'Queue was successfully deleted.');
}
/**
* Tests the RequeueException.
*/
public function testRequeueException()
{
$sites = $this->setUpDrupal(1, true);
// Copy the 'woot' module over to the Drupal site we just set up.
$this->setupModulesForTests(['woot'], Path::join(__DIR__, '/../fixtures/modules'));
// Enable woot module, which contains a queue worker that throws a
// RequeueException.
$this->drush('pm-install', ['woot'], [], null, null, self::EXIT_SUCCESS);
// Add an item to the queue.
$this->drush('php-script', ['requeue_script'], ['script-path' => __DIR__ . '/resources']);
// Check that the queue exists and it has one item in it.
$expected = 'woot_requeue_exception,%items,"Drupal\Core\Queue\DatabaseQueue"';
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutput();
$this->assertStringContainsString(str_replace('%items', 1, $expected), $output, 'Item was successfully added to the queue.');
// Process the queue.
$this->drush('queue-run', ['woot_requeue_exception']);
// Check that the item was processed after being requeued once.
// Here is the detailed workflow of what the above command did.
// 1. Drush calls drush queue-run woot_requeue_exception.
// 2. Drush claims the item. The worker sets a state variable (see below)
// and throws the RequeueException.
// 3. Drush catches the exception and puts it back in the queue.
// 4. Drush claims the next item, which is the one that we just requeued.
// 5. The worker finds the state variable, so it does not throw the
// RequeueException this time (see below).
// 6. Drush removes the item from the queue.
// 7. Command finishes. The queue is empty.
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutput();
$this->assertStringContainsString(str_replace('%items', 0, $expected), $output, 'Queue item processed after being requeued.');
}
/**
* Tests that CustomExceptions do not hold up the queue.
*/
public function testCustomException()
{
$this->setUpDrupal(1, true);
// Copy the 'woot' module over to the Drupal site we just set up.
$this->setupModulesForTests(['woot'], Path::join(__DIR__, '/../fixtures/modules'));
// Enable woot module, which contains a queue worker that throws a
// custom exception.
$this->drush('pm-install', ['woot'], [], null, null, self::EXIT_SUCCESS);
// Add a couple of items to the queue.
$this->drush('php-script', ['queue_custom_exception_script'], ['script-path' => __DIR__ . '/resources']);
// Check that the queue exists and it has two items in it.
$expected = 'woot_custom_exception,%items,"Drupal\Core\Queue\DatabaseQueue"';
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutput();
$this->assertStringContainsString(str_replace('%items', 2, $expected), $output, 'Items were successfully added to the queue.');
// Process the queue.
$this->drush('queue-run', ['woot_custom_exception']);
// Check that the item was processed after being requeued once.
// Here is the detailed workflow of what the above command did. Note
// there are two items in the queue when we begin.
// 1. Drush calls drush queue-run woot_custom_exception.
// 2. Drush claims the item. The worker sets a state variable (see below)
// and throws a CustomException.
// 3. Drush catches the exception and skips the item.
// 4. Drush claims the second item.
// 5. The worker finds the state variable, so it does not throw the
// CustomException this time (see below).
// 6. Drush removes the second item from the queue.
// 7. Command finishes. The queue is left with the first item, which was
// skipped.
$this->drush('queue-list', [], ['format' => 'csv']);
$output = $this->getOutput();
$this->assertStringContainsString(str_replace('%items', 1, $expected), $output, 'Last queue item processed after first threw custom exception.');
}
}