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);
39 public function testRunAllNormal() {
40 // prepare a list of tasks with an error in the middle
41 $this->queue
->createItem(new CRM_Queue_Task(
42 ['CRM_Queue_RunnerTest', '_recordValue'],
46 $this->queue
->createItem(new CRM_Queue_Task(
47 ['CRM_Queue_RunnerTest', '_recordValue'],
51 $this->queue
->createItem(new CRM_Queue_Task(
52 ['CRM_Queue_RunnerTest', '_recordValue'],
57 // run the list of tasks
58 $runner = new CRM_Queue_Runner([
59 'queue' => $this->queue
,
60 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
62 $this->assertEquals(self
::$_recordedValues, []);
63 $this->assertEquals(3, $this->queue
->numberOfItems());
64 $result = $runner->runAll();
65 $this->assertEquals(TRUE, $result);
66 $this->assertEquals(self
::$_recordedValues, ['a', 'b', 'c']);
67 $this->assertEquals(0, $this->queue
->numberOfItems());
71 * Run a series of tasks.
73 * One of the tasks will insert more TODOs at the start of the list.
75 public function testRunAll_AddMore() {
76 // Prepare a list of tasks with an error in the middle.
77 $this->queue
->createItem(new CRM_Queue_Task(
78 ['CRM_Queue_RunnerTest', '_recordValue'],
82 $this->queue
->createItem(new CRM_Queue_Task(
83 ['CRM_Queue_RunnerTest', '_enqueueNumbers'],
87 $this->queue
->createItem(new CRM_Queue_Task(
88 ['CRM_Queue_RunnerTest', '_recordValue'],
93 // run the list of tasks
94 $runner = new CRM_Queue_Runner([
95 'queue' => $this->queue
,
96 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
98 $this->assertEquals(self
::$_recordedValues, []);
99 $this->assertEquals(3, $this->queue
->numberOfItems());
100 $result = $runner->runAll();
101 $this->assertEquals(TRUE, $result);
102 $this->assertEquals(self
::$_recordedValues, ['a', 1, 2, 3, 'b']);
103 $this->assertEquals(0, $this->queue
->numberOfItems());
107 * Run a series of tasks; when one throws an
108 * exception, ignore it and continue
110 public function testRunAll_Continue_Exception() {
111 // prepare a list of tasks with an error in the middle
112 $this->queue
->createItem(new CRM_Queue_Task(
113 ['CRM_Queue_RunnerTest', '_recordValue'],
117 $this->queue
->createItem(new CRM_Queue_Task(
118 ['CRM_Queue_RunnerTest', '_throwException'],
122 $this->queue
->createItem(new CRM_Queue_Task(
123 ['CRM_Queue_RunnerTest', '_recordValue'],
128 // run the list of tasks
129 $runner = new CRM_Queue_Runner([
130 'queue' => $this->queue
,
131 'errorMode' => CRM_Queue_Runner
::ERROR_CONTINUE
,
133 $this->assertEquals(self
::$_recordedValues, []);
134 $this->assertEquals(3, $this->queue
->numberOfItems());
135 $result = $runner->runAll();
136 // FIXME useless return
137 $this->assertEquals(TRUE, $result);
138 $this->assertEquals(self
::$_recordedValues, ['a', 'c']);
139 $this->assertEquals(0, $this->queue
->numberOfItems());
143 * Run a series of tasks; when one throws an exception,
144 * abort processing and return it to the queue.
146 public function testRunAll_Abort_Exception() {
147 // prepare a list of tasks with an error in the middle
148 $this->queue
->createItem(new CRM_Queue_Task(
149 ['CRM_Queue_RunnerTest', '_recordValue'],
153 $this->queue
->createItem(new CRM_Queue_Task(
154 ['CRM_Queue_RunnerTest', '_throwException'],
158 $this->queue
->createItem(new CRM_Queue_Task(
159 ['CRM_Queue_RunnerTest', '_recordValue'],
164 // run the list of tasks
165 $runner = new CRM_Queue_Runner([
166 'queue' => $this->queue
,
167 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
169 $this->assertEquals(self
::$_recordedValues, []);
170 $this->assertEquals(3, $this->queue
->numberOfItems());
171 $result = $runner->runAll();
172 $this->assertEquals(1, $result['is_error']);
174 $this->assertEquals(self
::$_recordedValues, ['a']);
175 // 'b' and 'c' remain
176 $this->assertEquals(2, $this->queue
->numberOfItems());
180 * Run a series of tasks; when one returns false,
181 * abort processing and return it to the queue.
183 public function testRunAll_Abort_False() {
184 // prepare a list of tasks with an error in the middle
185 $this->queue
->createItem(new CRM_Queue_Task(
186 ['CRM_Queue_RunnerTest', '_recordValue'],
190 $this->queue
->createItem(new CRM_Queue_Task(
191 ['CRM_Queue_RunnerTest', '_returnFalse'],
195 $this->queue
->createItem(new CRM_Queue_Task(
196 ['CRM_Queue_RunnerTest', '_recordValue'],
201 // run the list of tasks
202 $runner = new CRM_Queue_Runner([
203 'queue' => $this->queue
,
204 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
206 $this->assertEquals(self
::$_recordedValues, []);
207 $this->assertEquals(3, $this->queue
->numberOfItems());
208 $result = $runner->runAll();
209 $this->assertEquals(1, $result['is_error']);
211 $this->assertEquals(self
::$_recordedValues, ['a']);
212 // 'b' and 'c' remain
213 $this->assertEquals(2, $this->queue
->numberOfItems());
220 protected static $_recordedValues;
228 public static function _recordValue($taskCtx, $value) {
229 self
::$_recordedValues[] = $value;
238 public static function _returnFalse($taskCtx) {
248 public static function _throwException($taskCtx, $value) {
249 throw new Exception("Manufactured error: $value");
259 public static function _enqueueNumbers($taskCtx, $low, $high) {
260 for ($i = $low; $i <= $high; $i++
) {
261 $taskCtx->queue
->createItem(new CRM_Queue_Task(
262 ['CRM_Queue_RunnerTest', '_recordValue'],
264 sprintf('Add number "%d"', $i)