a4f2ec13233608904dfadee71c0cd2c71d6880d0
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Ensure that various queue implementations comply with the interface
34 class CRM_Queue_RunnerTest
extends CiviUnitTestCase
{
37 'name' => 'SQL Queue',
38 'description' => 'Test SQL-backed queue items',
45 require_once 'CRM/Queue/Service.php';
46 $this->queueService
= CRM_Queue_Service
::singleton(TRUE);
47 $this->queue
= $this->queueService
->create(array(
49 'name' => 'test-queue',
51 self
::$_recordedValues = array();
56 unset($this->queueService
);
58 CRM_Utils_Time
::resetTime();
60 $tablesToTruncate = array('civicrm_queue_item');
61 $this->quickCleanup($tablesToTruncate);
64 function testRunAllNormal() {
65 // prepare a list of tasks with an error in the middle
66 $this->queue
->createItem(new CRM_Queue_Task(
67 array('CRM_Queue_RunnerTest', '_recordValue'),
71 $this->queue
->createItem(new CRM_Queue_Task(
72 array('CRM_Queue_RunnerTest', '_recordValue'),
76 $this->queue
->createItem(new CRM_Queue_Task(
77 array('CRM_Queue_RunnerTest', '_recordValue'),
82 // run the list of tasks
83 $runner = new CRM_Queue_Runner(array(
84 'queue' => $this->queue
,
85 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
87 $this->assertEquals(self
::$_recordedValues, array());
88 $this->assertEquals(3, $this->queue
->numberOfItems());
89 $result = $runner->runAll();
90 $this->assertEquals(TRUE, $result);
91 $this->assertEquals(self
::$_recordedValues, array('a', 'b', 'c'));
92 $this->assertEquals(0, $this->queue
->numberOfItems());
96 * Run a series of tasks; one of the tasks will insert more
97 * TODOs at the start of the list
99 function testRunAll_AddMore() {
100 // prepare a list of tasks with an error in the middle
101 $this->queue
->createItem(new CRM_Queue_Task(
102 array('CRM_Queue_RunnerTest', '_recordValue'),
106 $this->queue
->createItem(new CRM_Queue_Task(
107 array('CRM_Queue_RunnerTest', '_enqueueNumbers'),
111 $this->queue
->createItem(new CRM_Queue_Task(
112 array('CRM_Queue_RunnerTest', '_recordValue'),
117 // run the list of tasks
118 $runner = new CRM_Queue_Runner(array(
119 'queue' => $this->queue
,
120 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
122 $this->assertEquals(self
::$_recordedValues, array());
123 $this->assertEquals(3, $this->queue
->numberOfItems());
124 $result = $runner->runAll();
125 $this->assertEquals(TRUE, $result);
126 $this->assertEquals(self
::$_recordedValues, array('a', 1, 2, 3, 'b'));
127 $this->assertEquals(0, $this->queue
->numberOfItems());
131 * Run a series of tasks; when one throws an
132 * exception, ignore it and continue
134 function testRunAll_Continue_Exception() {
135 // prepare a list of tasks with an error in the middle
136 $this->queue
->createItem(new CRM_Queue_Task(
137 array('CRM_Queue_RunnerTest', '_recordValue'),
141 $this->queue
->createItem(new CRM_Queue_Task(
142 array('CRM_Queue_RunnerTest', '_throwException'),
146 $this->queue
->createItem(new CRM_Queue_Task(
147 array('CRM_Queue_RunnerTest', '_recordValue'),
152 // run the list of tasks
153 $runner = new CRM_Queue_Runner(array(
154 'queue' => $this->queue
,
155 'errorMode' => CRM_Queue_Runner
::ERROR_CONTINUE
,
157 $this->assertEquals(self
::$_recordedValues, array());
158 $this->assertEquals(3, $this->queue
->numberOfItems());
159 $result = $runner->runAll();
160 // FIXME useless return
161 $this->assertEquals(TRUE, $result);
162 $this->assertEquals(self
::$_recordedValues, array('a', 'c'));
163 $this->assertEquals(0, $this->queue
->numberOfItems());
167 * Run a series of tasks; when one throws an exception,
168 * abort processing and return it to the queue.
170 function testRunAll_Abort_Exception() {
171 // prepare a list of tasks with an error in the middle
172 $this->queue
->createItem(new CRM_Queue_Task(
173 array('CRM_Queue_RunnerTest', '_recordValue'),
177 $this->queue
->createItem(new CRM_Queue_Task(
178 array('CRM_Queue_RunnerTest', '_throwException'),
182 $this->queue
->createItem(new CRM_Queue_Task(
183 array('CRM_Queue_RunnerTest', '_recordValue'),
188 // run the list of tasks
189 $runner = new CRM_Queue_Runner(array(
190 'queue' => $this->queue
,
191 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
193 $this->assertEquals(self
::$_recordedValues, array());
194 $this->assertEquals(3, $this->queue
->numberOfItems());
195 $result = $runner->runAll();
196 $this->assertEquals(1, $result['is_error']);
198 $this->assertEquals(self
::$_recordedValues, array('a'));
199 // 'b' and 'c' remain
200 $this->assertEquals(2, $this->queue
->numberOfItems());
204 * Run a series of tasks; when one returns false,
205 * abort processing and return it to the queue.
207 function testRunAll_Abort_False() {
208 // prepare a list of tasks with an error in the middle
209 $this->queue
->createItem(new CRM_Queue_Task(
210 array('CRM_Queue_RunnerTest', '_recordValue'),
214 $this->queue
->createItem(new CRM_Queue_Task(
215 array('CRM_Queue_RunnerTest', '_returnFalse'),
219 $this->queue
->createItem(new CRM_Queue_Task(
220 array('CRM_Queue_RunnerTest', '_recordValue'),
225 // run the list of tasks
226 $runner = new CRM_Queue_Runner(array(
227 'queue' => $this->queue
,
228 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
230 $this->assertEquals(self
::$_recordedValues, array());
231 $this->assertEquals(3, $this->queue
->numberOfItems());
232 $result = $runner->runAll();
233 $this->assertEquals(1, $result['is_error']);
235 $this->assertEquals(self
::$_recordedValues, array('a'));
236 // 'b' and 'c' remain
237 $this->assertEquals(2, $this->queue
->numberOfItems());
240 /* **** Queue tasks **** */
243 static $_recordedValues;
246 function _recordValue($taskCtx, $value) {
247 self
::$_recordedValues[] = $value;
252 function _returnFalse($taskCtx) {
257 function _throwException($taskCtx, $value) {
258 throw new Exception("Manufactured error: $value");
262 function _enqueueNumbers($taskCtx, $low, $high) {
263 for ($i = $low; $i <= $high; $i++
) {
264 $taskCtx->queue
->createItem(new CRM_Queue_Task(
265 array('CRM_Queue_RunnerTest', '_recordValue'),
267 sprintf('Add number "%d"', $i)