forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QueueTest.php
101 lines (84 loc) · 4.52 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
<?php
namespace Unish;
use Webmozart\PathUtil\Path;
/**
* @group commands
*/
class QueueTest extends CommandUnishTestCase
{
use TestModuleHelperTrait;
/**
* 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.
$this->drush('queue:list', [], ['format' => 'json']);
$output = $this->getOutputFromJSON('woot_requeue_exception');
$this->assertStringContainsString(1, $output['items'], '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' => 'json']);
$output = $this->getOutputFromJSON('woot_requeue_exception');
$this->assertStringContainsString(0, $output['items'], 'Queue item processed after being requeued.');
}
/**
* Tests that CustomExceptions do not hold up the queue. Also queue:run, queue:list, queue:delete
*/
public function testCustomExceptionAndCommands()
{
$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.
$this->drush('queue-list', [], ['format' => 'json']);
$output = $this->getOutputFromJSON('woot_custom_exception');
$this->assertStringContainsString(2, $output['items'], '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' => 'json']);
$output = $this->getOutputFromJSON('woot_custom_exception');
$this->assertStringContainsString(1, $output['items'], 'Last queue item processed after first threw custom exception.');
$this->drush('queue:delete', ['woot_custom_exception']);
$this->drush('queue:list', [], ['format' => 'json']);
$output = $this->getOutputFromJSON('woot_custom_exception');
$this->assertEquals(0, $output['items'], 'Queue was successfully deleted.');
}
}