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