3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 require_once 'CRM/Queue/Service.php';
37 $this->queueService
= CRM_Queue_Service
::singleton(TRUE);
38 $this->queue
= $this->queueService
->create(array(
40 'name' => 'test-queue',
42 self
::$_recordedValues = array();
45 public function tearDown() {
47 unset($this->queueService
);
49 CRM_Utils_Time
::resetTime();
51 $tablesToTruncate = array('civicrm_queue_item');
52 $this->quickCleanup($tablesToTruncate);
55 public function testRunAllNormal() {
56 // prepare a list of tasks with an error in the middle
57 $this->queue
->createItem(new CRM_Queue_Task(
58 array('CRM_Queue_RunnerTest', '_recordValue'),
62 $this->queue
->createItem(new CRM_Queue_Task(
63 array('CRM_Queue_RunnerTest', '_recordValue'),
67 $this->queue
->createItem(new CRM_Queue_Task(
68 array('CRM_Queue_RunnerTest', '_recordValue'),
73 // run the list of tasks
74 $runner = new CRM_Queue_Runner(array(
75 'queue' => $this->queue
,
76 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
78 $this->assertEquals(self
::$_recordedValues, array());
79 $this->assertEquals(3, $this->queue
->numberOfItems());
80 $result = $runner->runAll();
81 $this->assertEquals(TRUE, $result);
82 $this->assertEquals(self
::$_recordedValues, array('a', 'b', 'c'));
83 $this->assertEquals(0, $this->queue
->numberOfItems());
87 * Run a series of tasks.
89 * One of the tasks will insert more TODOs at the start of the list.
91 public function testRunAll_AddMore() {
92 // Prepare a list of tasks with an error in the middle.
93 $this->queue
->createItem(new CRM_Queue_Task(
94 array('CRM_Queue_RunnerTest', '_recordValue'),
98 $this->queue
->createItem(new CRM_Queue_Task(
99 array('CRM_Queue_RunnerTest', '_enqueueNumbers'),
103 $this->queue
->createItem(new CRM_Queue_Task(
104 array('CRM_Queue_RunnerTest', '_recordValue'),
109 // run the list of tasks
110 $runner = new CRM_Queue_Runner(array(
111 'queue' => $this->queue
,
112 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
114 $this->assertEquals(self
::$_recordedValues, array());
115 $this->assertEquals(3, $this->queue
->numberOfItems());
116 $result = $runner->runAll();
117 $this->assertEquals(TRUE, $result);
118 $this->assertEquals(self
::$_recordedValues, array('a', 1, 2, 3, 'b'));
119 $this->assertEquals(0, $this->queue
->numberOfItems());
123 * Run a series of tasks; when one throws an
124 * exception, ignore it and continue
126 public function testRunAll_Continue_Exception() {
127 // prepare a list of tasks with an error in the middle
128 $this->queue
->createItem(new CRM_Queue_Task(
129 array('CRM_Queue_RunnerTest', '_recordValue'),
133 $this->queue
->createItem(new CRM_Queue_Task(
134 array('CRM_Queue_RunnerTest', '_throwException'),
138 $this->queue
->createItem(new CRM_Queue_Task(
139 array('CRM_Queue_RunnerTest', '_recordValue'),
144 // run the list of tasks
145 $runner = new CRM_Queue_Runner(array(
146 'queue' => $this->queue
,
147 'errorMode' => CRM_Queue_Runner
::ERROR_CONTINUE
,
149 $this->assertEquals(self
::$_recordedValues, array());
150 $this->assertEquals(3, $this->queue
->numberOfItems());
151 $result = $runner->runAll();
152 // FIXME useless return
153 $this->assertEquals(TRUE, $result);
154 $this->assertEquals(self
::$_recordedValues, array('a', 'c'));
155 $this->assertEquals(0, $this->queue
->numberOfItems());
159 * Run a series of tasks; when one throws an exception,
160 * abort processing and return it to the queue.
162 public function testRunAll_Abort_Exception() {
163 // prepare a list of tasks with an error in the middle
164 $this->queue
->createItem(new CRM_Queue_Task(
165 array('CRM_Queue_RunnerTest', '_recordValue'),
169 $this->queue
->createItem(new CRM_Queue_Task(
170 array('CRM_Queue_RunnerTest', '_throwException'),
174 $this->queue
->createItem(new CRM_Queue_Task(
175 array('CRM_Queue_RunnerTest', '_recordValue'),
180 // run the list of tasks
181 $runner = new CRM_Queue_Runner(array(
182 'queue' => $this->queue
,
183 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
185 $this->assertEquals(self
::$_recordedValues, array());
186 $this->assertEquals(3, $this->queue
->numberOfItems());
187 $result = $runner->runAll();
188 $this->assertEquals(1, $result['is_error']);
190 $this->assertEquals(self
::$_recordedValues, array('a'));
191 // 'b' and 'c' remain
192 $this->assertEquals(2, $this->queue
->numberOfItems());
196 * Run a series of tasks; when one returns false,
197 * abort processing and return it to the queue.
199 public function testRunAll_Abort_False() {
200 // prepare a list of tasks with an error in the middle
201 $this->queue
->createItem(new CRM_Queue_Task(
202 array('CRM_Queue_RunnerTest', '_recordValue'),
206 $this->queue
->createItem(new CRM_Queue_Task(
207 array('CRM_Queue_RunnerTest', '_returnFalse'),
211 $this->queue
->createItem(new CRM_Queue_Task(
212 array('CRM_Queue_RunnerTest', '_recordValue'),
217 // run the list of tasks
218 $runner = new CRM_Queue_Runner(array(
219 'queue' => $this->queue
,
220 'errorMode' => CRM_Queue_Runner
::ERROR_ABORT
,
222 $this->assertEquals(self
::$_recordedValues, array());
223 $this->assertEquals(3, $this->queue
->numberOfItems());
224 $result = $runner->runAll();
225 $this->assertEquals(1, $result['is_error']);
227 $this->assertEquals(self
::$_recordedValues, array('a'));
228 // 'b' and 'c' remain
229 $this->assertEquals(2, $this->queue
->numberOfItems());
232 /* **** Queue tasks **** */
235 static $_recordedValues;
243 static public function _recordValue($taskCtx, $value) {
244 self
::$_recordedValues[] = $value;
253 static public function _returnFalse($taskCtx) {
263 static public function _throwException($taskCtx, $value) {
264 throw new Exception("Manufactured error: $value");
274 static public function _enqueueNumbers($taskCtx, $low, $high) {
275 for ($i = $low; $i <= $high; $i++
) {
276 $taskCtx->queue
->createItem(new CRM_Queue_Task(
277 array('CRM_Queue_RunnerTest', '_recordValue'),
279 sprintf('Add number "%d"', $i)