Merge pull request #22888 from civicrm/5.47
[civicrm-core.git] / tests / phpunit / CRM / Queue / RunnerTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035 11
6a488035
TO
12/**
13 * Ensure that various queue implementations comply with the interface
acb109b7 14 * @group headless
60f014d5 15 * @group queue
6a488035
TO
16 */
17class CRM_Queue_RunnerTest extends CiviUnitTestCase {
6a488035 18
0b49aa04 19 public function setUp(): void {
6a488035 20 parent::setUp();
6a488035 21 $this->queueService = CRM_Queue_Service::singleton(TRUE);
9099cab3 22 $this->queue = $this->queueService->create([
92915c55
TO
23 'type' => 'Sql',
24 'name' => 'test-queue',
9099cab3
CW
25 ]);
26 self::$_recordedValues = [];
6a488035
TO
27 }
28
dd09ee0c 29 public function tearDown(): void {
6a488035
TO
30 unset($this->queue);
31 unset($this->queueService);
b6708aeb 32
6a488035
TO
33 CRM_Utils_Time::resetTime();
34
9099cab3 35 $tablesToTruncate = ['civicrm_queue_item'];
6a488035 36 $this->quickCleanup($tablesToTruncate);
bc4d424d 37 parent::tearDown();
6a488035
TO
38 }
39
00be9182 40 public function testRunAllNormal() {
6a488035
TO
41 // prepare a list of tasks with an error in the middle
42 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
43 ['CRM_Queue_RunnerTest', '_recordValue'],
44 ['a'],
92915c55
TO
45 'Add "a"'
46 ));
6a488035 47 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
48 ['CRM_Queue_RunnerTest', '_recordValue'],
49 ['b'],
92915c55
TO
50 'Add "b"'
51 ));
6a488035 52 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
53 ['CRM_Queue_RunnerTest', '_recordValue'],
54 ['c'],
92915c55
TO
55 'Add "c"'
56 ));
6a488035
TO
57
58 // run the list of tasks
9099cab3 59 $runner = new CRM_Queue_Runner([
92915c55
TO
60 'queue' => $this->queue,
61 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
9099cab3
CW
62 ]);
63 $this->assertEquals(self::$_recordedValues, []);
6a488035
TO
64 $this->assertEquals(3, $this->queue->numberOfItems());
65 $result = $runner->runAll();
66 $this->assertEquals(TRUE, $result);
9099cab3 67 $this->assertEquals(self::$_recordedValues, ['a', 'b', 'c']);
6a488035
TO
68 $this->assertEquals(0, $this->queue->numberOfItems());
69 }
70
71 /**
fe482240
EM
72 * Run a series of tasks.
73 *
74 * One of the tasks will insert more TODOs at the start of the list.
6a488035 75 */
00be9182 76 public function testRunAll_AddMore() {
fe482240 77 // Prepare a list of tasks with an error in the middle.
6a488035 78 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
79 ['CRM_Queue_RunnerTest', '_recordValue'],
80 ['a'],
92915c55
TO
81 'Add "a"'
82 ));
6a488035 83 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
84 ['CRM_Queue_RunnerTest', '_enqueueNumbers'],
85 [1, 3],
92915c55
TO
86 'Add more'
87 ));
6a488035 88 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
89 ['CRM_Queue_RunnerTest', '_recordValue'],
90 ['b'],
92915c55
TO
91 'Add "b"'
92 ));
6a488035
TO
93
94 // run the list of tasks
9099cab3 95 $runner = new CRM_Queue_Runner([
92915c55
TO
96 'queue' => $this->queue,
97 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
9099cab3
CW
98 ]);
99 $this->assertEquals(self::$_recordedValues, []);
6a488035
TO
100 $this->assertEquals(3, $this->queue->numberOfItems());
101 $result = $runner->runAll();
102 $this->assertEquals(TRUE, $result);
9099cab3 103 $this->assertEquals(self::$_recordedValues, ['a', 1, 2, 3, 'b']);
6a488035
TO
104 $this->assertEquals(0, $this->queue->numberOfItems());
105 }
106
107 /**
108 * Run a series of tasks; when one throws an
109 * exception, ignore it and continue
110 */
00be9182 111 public function testRunAll_Continue_Exception() {
6a488035
TO
112 // prepare a list of tasks with an error in the middle
113 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
114 ['CRM_Queue_RunnerTest', '_recordValue'],
115 ['a'],
92915c55
TO
116 'Add "a"'
117 ));
6a488035 118 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
119 ['CRM_Queue_RunnerTest', '_throwException'],
120 ['b'],
92915c55
TO
121 'Throw exception'
122 ));
6a488035 123 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
124 ['CRM_Queue_RunnerTest', '_recordValue'],
125 ['c'],
92915c55
TO
126 'Add "c"'
127 ));
6a488035
TO
128
129 // run the list of tasks
9099cab3 130 $runner = new CRM_Queue_Runner([
92915c55
TO
131 'queue' => $this->queue,
132 'errorMode' => CRM_Queue_Runner::ERROR_CONTINUE,
9099cab3
CW
133 ]);
134 $this->assertEquals(self::$_recordedValues, []);
6a488035
TO
135 $this->assertEquals(3, $this->queue->numberOfItems());
136 $result = $runner->runAll();
137 // FIXME useless return
138 $this->assertEquals(TRUE, $result);
9099cab3 139 $this->assertEquals(self::$_recordedValues, ['a', 'c']);
6a488035
TO
140 $this->assertEquals(0, $this->queue->numberOfItems());
141 }
142
143 /**
144 * Run a series of tasks; when one throws an exception,
145 * abort processing and return it to the queue.
146 */
00be9182 147 public function testRunAll_Abort_Exception() {
6a488035
TO
148 // prepare a list of tasks with an error in the middle
149 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
150 ['CRM_Queue_RunnerTest', '_recordValue'],
151 ['a'],
92915c55
TO
152 'Add "a"'
153 ));
6a488035 154 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
155 ['CRM_Queue_RunnerTest', '_throwException'],
156 ['b'],
92915c55
TO
157 'Throw exception'
158 ));
6a488035 159 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
160 ['CRM_Queue_RunnerTest', '_recordValue'],
161 ['c'],
92915c55
TO
162 'Add "c"'
163 ));
6a488035
TO
164
165 // run the list of tasks
9099cab3 166 $runner = new CRM_Queue_Runner([
92915c55
TO
167 'queue' => $this->queue,
168 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
9099cab3
CW
169 ]);
170 $this->assertEquals(self::$_recordedValues, []);
6a488035
TO
171 $this->assertEquals(3, $this->queue->numberOfItems());
172 $result = $runner->runAll();
173 $this->assertEquals(1, $result['is_error']);
174 // nothing from 'c'
9099cab3 175 $this->assertEquals(self::$_recordedValues, ['a']);
6a488035
TO
176 // 'b' and 'c' remain
177 $this->assertEquals(2, $this->queue->numberOfItems());
178 }
179
180 /**
181 * Run a series of tasks; when one returns false,
182 * abort processing and return it to the queue.
183 */
00be9182 184 public function testRunAll_Abort_False() {
6a488035
TO
185 // prepare a list of tasks with an error in the middle
186 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
187 ['CRM_Queue_RunnerTest', '_recordValue'],
188 ['a'],
92915c55
TO
189 'Add "a"'
190 ));
6a488035 191 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
192 ['CRM_Queue_RunnerTest', '_returnFalse'],
193 [],
92915c55
TO
194 'Return false'
195 ));
6a488035 196 $this->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
197 ['CRM_Queue_RunnerTest', '_recordValue'],
198 ['c'],
92915c55
TO
199 'Add "c"'
200 ));
6a488035
TO
201
202 // run the list of tasks
9099cab3 203 $runner = new CRM_Queue_Runner([
92915c55
TO
204 'queue' => $this->queue,
205 'errorMode' => CRM_Queue_Runner::ERROR_ABORT,
9099cab3
CW
206 ]);
207 $this->assertEquals(self::$_recordedValues, []);
6a488035
TO
208 $this->assertEquals(3, $this->queue->numberOfItems());
209 $result = $runner->runAll();
210 $this->assertEquals(1, $result['is_error']);
211 // nothing from 'c'
9099cab3 212 $this->assertEquals(self::$_recordedValues, ['a']);
6a488035
TO
213 // 'b' and 'c' remain
214 $this->assertEquals(2, $this->queue->numberOfItems());
215 }
216
39b959db
SL
217 /**
218 * Queue tasks
219 * @var array
220 */
221 protected static $_recordedValues;
6a488035 222
4cbe18b8
EM
223 /**
224 * @param $taskCtx
225 * @param $value
226 *
227 * @return bool
228 */
39b959db 229 public static function _recordValue($taskCtx, $value) {
6a488035
TO
230 self::$_recordedValues[] = $value;
231 return TRUE;
232 }
233
4cbe18b8
EM
234 /**
235 * @param $taskCtx
236 *
237 * @return bool
238 */
39b959db 239 public static function _returnFalse($taskCtx) {
6a488035
TO
240 return FALSE;
241 }
242
4cbe18b8
EM
243 /**
244 * @param $taskCtx
245 * @param $value
246 *
247 * @throws Exception
248 */
39b959db 249 public static function _throwException($taskCtx, $value) {
6a488035
TO
250 throw new Exception("Manufactured error: $value");
251 }
b6708aeb 252
4cbe18b8
EM
253 /**
254 * @param $taskCtx
255 * @param $low
256 * @param $high
257 *
258 * @return bool
259 */
39b959db 260 public static function _enqueueNumbers($taskCtx, $low, $high) {
6a488035
TO
261 for ($i = $low; $i <= $high; $i++) {
262 $taskCtx->queue->createItem(new CRM_Queue_Task(
9099cab3
CW
263 ['CRM_Queue_RunnerTest', '_recordValue'],
264 [$i],
6a488035 265 sprintf('Add number "%d"', $i)
9099cab3 266 ), [
6a488035 267 'weight' => -1,
9099cab3 268 ]);
6a488035
TO
269 }
270 return TRUE;
271 }
96025800 272
6a488035 273}