3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * Ensure that various queue implementations comply with the interface
16 class CRM_Queue_RunnerTest
extends CiviUnitTestCase
{
18 public function setUp(): void
{
20 $this->queueService
= CRM_Queue_Service
::singleton(TRUE);
21 $this->queue
= $this->queueService
->create([
23 'name' => 'test-queue',
25 self
::$_recordedValues = [];
28 public function tearDown(): void
{
30 unset($this->queueService
);
32 CRM_Utils_Time
::resetTime();
34 $tablesToTruncate = ['civicrm_queue_item'];
35 $this->quickCleanup($tablesToTruncate);
38 public function testRunAllNormal() {
39 // prepare a list of tasks with an error in the middle
40 $this->queue
->createItem(new CRM_Queue_Task(
41 ['CRM_Queue_RunnerTest', '_recordValue'],
45 $this->queue
->createItem(new CRM_Queue_Task(
46 ['CRM_Queue_RunnerTest', '_recordValue'],
50 $this->queue
->createItem(new CRM_Queue_Task(
51 ['CRM_Queue_RunnerTest', '_recordValue'],
56 // run the list of tasks
57 $runner = new CRM_Queue_Runner([
58 'queue' => $this->queue
,
59 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
61 $this->assertEquals(self
::$_recordedValues, []);
62 $this->assertEquals(3, $this->queue
->numberOfItems());
63 $result = $runner->runAll();
64 $this->assertEquals(TRUE, $result);
65 $this->assertEquals(self
::$_recordedValues, ['a', 'b', 'c']);
66 $this->assertEquals(0, $this->queue
->numberOfItems());
70 * Run a series of tasks.
72 * One of the tasks will insert more TODOs at the start of the list.
74 public function testRunAll_AddMore() {
75 // Prepare a list of tasks with an error in the middle.
76 $this->queue
->createItem(new CRM_Queue_Task(
77 ['CRM_Queue_RunnerTest', '_recordValue'],
81 $this->queue
->createItem(new CRM_Queue_Task(
82 ['CRM_Queue_RunnerTest', '_enqueueNumbers'],
86 $this->queue
->createItem(new CRM_Queue_Task(
87 ['CRM_Queue_RunnerTest', '_recordValue'],
92 // run the list of tasks
93 $runner = new CRM_Queue_Runner([
94 'queue' => $this->queue
,
95 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
97 $this->assertEquals(self
::$_recordedValues, []);
98 $this->assertEquals(3, $this->queue
->numberOfItems());
99 $result = $runner->runAll();
100 $this->assertEquals(TRUE, $result);
101 $this->assertEquals(self
::$_recordedValues, ['a', 1, 2, 3, 'b']);
102 $this->assertEquals(0, $this->queue
->numberOfItems());
106 * Run a series of tasks; when one throws an
107 * exception, ignore it and continue
109 public function testRunAll_Continue_Exception() {
110 // prepare a list of tasks with an error in the middle
111 $this->queue
->createItem(new CRM_Queue_Task(
112 ['CRM_Queue_RunnerTest', '_recordValue'],
116 $this->queue
->createItem(new CRM_Queue_Task(
117 ['CRM_Queue_RunnerTest', '_throwException'],
121 $this->queue
->createItem(new CRM_Queue_Task(
122 ['CRM_Queue_RunnerTest', '_recordValue'],
127 // run the list of tasks
128 $runner = new CRM_Queue_Runner([
129 'queue' => $this->queue
,
130 'errorMode' => CRM_Queue_Runner
::ERROR_CONTINUE
,
132 $this->assertEquals(self
::$_recordedValues, []);
133 $this->assertEquals(3, $this->queue
->numberOfItems());
134 $result = $runner->runAll();
135 // FIXME useless return
136 $this->assertEquals(TRUE, $result);
137 $this->assertEquals(self
::$_recordedValues, ['a', 'c']);
138 $this->assertEquals(0, $this->queue
->numberOfItems());
142 * Run a series of tasks; when one throws an exception,
143 * abort processing and return it to the queue.
145 public function testRunAll_Abort_Exception() {
146 // prepare a list of tasks with an error in the middle
147 $this->queue
->createItem(new CRM_Queue_Task(
148 ['CRM_Queue_RunnerTest', '_recordValue'],
152 $this->queue
->createItem(new CRM_Queue_Task(
153 ['CRM_Queue_RunnerTest', '_throwException'],
157 $this->queue
->createItem(new CRM_Queue_Task(
158 ['CRM_Queue_RunnerTest', '_recordValue'],
163 // run the list of tasks
164 $runner = new CRM_Queue_Runner([
165 'queue' => $this->queue
,
166 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
168 $this->assertEquals(self
::$_recordedValues, []);
169 $this->assertEquals(3, $this->queue
->numberOfItems());
170 $result = $runner->runAll();
171 $this->assertEquals(1, $result['is_error']);
173 $this->assertEquals(self
::$_recordedValues, ['a']);
174 // 'b' and 'c' remain
175 $this->assertEquals(2, $this->queue
->numberOfItems());
179 * Run a series of tasks; when one returns false,
180 * abort processing and return it to the queue.
182 public function testRunAll_Abort_False() {
183 // prepare a list of tasks with an error in the middle
184 $this->queue
->createItem(new CRM_Queue_Task(
185 ['CRM_Queue_RunnerTest', '_recordValue'],
189 $this->queue
->createItem(new CRM_Queue_Task(
190 ['CRM_Queue_RunnerTest', '_returnFalse'],
194 $this->queue
->createItem(new CRM_Queue_Task(
195 ['CRM_Queue_RunnerTest', '_recordValue'],
200 // run the list of tasks
201 $runner = new CRM_Queue_Runner([
202 'queue' => $this->queue
,
203 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
205 $this->assertEquals(self
::$_recordedValues, []);
206 $this->assertEquals(3, $this->queue
->numberOfItems());
207 $result = $runner->runAll();
208 $this->assertEquals(1, $result['is_error']);
210 $this->assertEquals(self
::$_recordedValues, ['a']);
211 // 'b' and 'c' remain
212 $this->assertEquals(2, $this->queue
->numberOfItems());
219 protected static $_recordedValues;
227 public static function _recordValue($taskCtx, $value) {
228 self
::$_recordedValues[] = $value;
237 public static function _returnFalse($taskCtx) {
247 public static function _throwException($taskCtx, $value) {
248 throw new Exception("Manufactured error: $value");
258 public static function _enqueueNumbers($taskCtx, $low, $high) {
259 for ($i = $low; $i <= $high; $i++
) {
260 $taskCtx->queue
->createItem(new CRM_Queue_Task(
261 ['CRM_Queue_RunnerTest', '_recordValue'],
263 sprintf('Add number "%d"', $i)