3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Ensure that various queue implementations comply with the interface
32 class CRM_Queue_RunnerTest
extends CiviUnitTestCase
{
34 public function setUp() {
36 $this->queueService
= CRM_Queue_Service
::singleton(TRUE);
37 $this->queue
= $this->queueService
->create(array(
39 'name' => 'test-queue',
41 self
::$_recordedValues = array();
44 public function tearDown() {
46 unset($this->queueService
);
48 CRM_Utils_Time
::resetTime();
50 $tablesToTruncate = array('civicrm_queue_item');
51 $this->quickCleanup($tablesToTruncate);
54 public function testRunAllNormal() {
55 // prepare a list of tasks with an error in the middle
56 $this->queue
->createItem(new CRM_Queue_Task(
57 array('CRM_Queue_RunnerTest', '_recordValue'),
61 $this->queue
->createItem(new CRM_Queue_Task(
62 array('CRM_Queue_RunnerTest', '_recordValue'),
66 $this->queue
->createItem(new CRM_Queue_Task(
67 array('CRM_Queue_RunnerTest', '_recordValue'),
72 // run the list of tasks
73 $runner = new CRM_Queue_Runner(array(
74 'queue' => $this->queue
,
75 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
77 $this->assertEquals(self
::$_recordedValues, array());
78 $this->assertEquals(3, $this->queue
->numberOfItems());
79 $result = $runner->runAll();
80 $this->assertEquals(TRUE, $result);
81 $this->assertEquals(self
::$_recordedValues, array('a', 'b', 'c'));
82 $this->assertEquals(0, $this->queue
->numberOfItems());
86 * Run a series of tasks.
88 * One of the tasks will insert more TODOs at the start of the list.
90 public function testRunAll_AddMore() {
91 // Prepare a list of tasks with an error in the middle.
92 $this->queue
->createItem(new CRM_Queue_Task(
93 array('CRM_Queue_RunnerTest', '_recordValue'),
97 $this->queue
->createItem(new CRM_Queue_Task(
98 array('CRM_Queue_RunnerTest', '_enqueueNumbers'),
102 $this->queue
->createItem(new CRM_Queue_Task(
103 array('CRM_Queue_RunnerTest', '_recordValue'),
108 // run the list of tasks
109 $runner = new CRM_Queue_Runner(array(
110 'queue' => $this->queue
,
111 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
113 $this->assertEquals(self
::$_recordedValues, array());
114 $this->assertEquals(3, $this->queue
->numberOfItems());
115 $result = $runner->runAll();
116 $this->assertEquals(TRUE, $result);
117 $this->assertEquals(self
::$_recordedValues, array('a', 1, 2, 3, 'b'));
118 $this->assertEquals(0, $this->queue
->numberOfItems());
122 * Run a series of tasks; when one throws an
123 * exception, ignore it and continue
125 public function testRunAll_Continue_Exception() {
126 // prepare a list of tasks with an error in the middle
127 $this->queue
->createItem(new CRM_Queue_Task(
128 array('CRM_Queue_RunnerTest', '_recordValue'),
132 $this->queue
->createItem(new CRM_Queue_Task(
133 array('CRM_Queue_RunnerTest', '_throwException'),
137 $this->queue
->createItem(new CRM_Queue_Task(
138 array('CRM_Queue_RunnerTest', '_recordValue'),
143 // run the list of tasks
144 $runner = new CRM_Queue_Runner(array(
145 'queue' => $this->queue
,
146 'errorMode' => CRM_Queue_Runner
::ERROR_CONTINUE
,
148 $this->assertEquals(self
::$_recordedValues, array());
149 $this->assertEquals(3, $this->queue
->numberOfItems());
150 $result = $runner->runAll();
151 // FIXME useless return
152 $this->assertEquals(TRUE, $result);
153 $this->assertEquals(self
::$_recordedValues, array('a', 'c'));
154 $this->assertEquals(0, $this->queue
->numberOfItems());
158 * Run a series of tasks; when one throws an exception,
159 * abort processing and return it to the queue.
161 public function testRunAll_Abort_Exception() {
162 // prepare a list of tasks with an error in the middle
163 $this->queue
->createItem(new CRM_Queue_Task(
164 array('CRM_Queue_RunnerTest', '_recordValue'),
168 $this->queue
->createItem(new CRM_Queue_Task(
169 array('CRM_Queue_RunnerTest', '_throwException'),
173 $this->queue
->createItem(new CRM_Queue_Task(
174 array('CRM_Queue_RunnerTest', '_recordValue'),
179 // run the list of tasks
180 $runner = new CRM_Queue_Runner(array(
181 'queue' => $this->queue
,
182 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
184 $this->assertEquals(self
::$_recordedValues, array());
185 $this->assertEquals(3, $this->queue
->numberOfItems());
186 $result = $runner->runAll();
187 $this->assertEquals(1, $result['is_error']);
189 $this->assertEquals(self
::$_recordedValues, array('a'));
190 // 'b' and 'c' remain
191 $this->assertEquals(2, $this->queue
->numberOfItems());
195 * Run a series of tasks; when one returns false,
196 * abort processing and return it to the queue.
198 public function testRunAll_Abort_False() {
199 // prepare a list of tasks with an error in the middle
200 $this->queue
->createItem(new CRM_Queue_Task(
201 array('CRM_Queue_RunnerTest', '_recordValue'),
205 $this->queue
->createItem(new CRM_Queue_Task(
206 array('CRM_Queue_RunnerTest', '_returnFalse'),
210 $this->queue
->createItem(new CRM_Queue_Task(
211 array('CRM_Queue_RunnerTest', '_recordValue'),
216 // run the list of tasks
217 $runner = new CRM_Queue_Runner(array(
218 'queue' => $this->queue
,
219 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
221 $this->assertEquals(self
::$_recordedValues, array());
222 $this->assertEquals(3, $this->queue
->numberOfItems());
223 $result = $runner->runAll();
224 $this->assertEquals(1, $result['is_error']);
226 $this->assertEquals(self
::$_recordedValues, array('a'));
227 // 'b' and 'c' remain
228 $this->assertEquals(2, $this->queue
->numberOfItems());
231 /* **** Queue tasks **** */
234 static $_recordedValues;
242 static public function _recordValue($taskCtx, $value) {
243 self
::$_recordedValues[] = $value;
252 static public function _returnFalse($taskCtx) {
262 static public function _throwException($taskCtx, $value) {
263 throw new Exception("Manufactured error: $value");
273 static public function _enqueueNumbers($taskCtx, $low, $high) {
274 for ($i = $low; $i <= $high; $i++
) {
275 $taskCtx->queue
->createItem(new CRM_Queue_Task(
276 array('CRM_Queue_RunnerTest', '_recordValue'),
278 sprintf('Add number "%d"', $i)