Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
5 | +--------------------------------------------------------------------+ |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | ||
31 | /** | |
32 | * Ensure that various queue implementations comply with the interface | |
33 | */ | |
34 | class CRM_Queue_RunnerTest extends CiviUnitTestCase { | |
35 | function get_info() { | |
36 | return array( | |
37 | 'name' => 'SQL Queue', | |
38 | 'description' => 'Test SQL-backed queue items', | |
39 | 'group' => 'Queue', | |
40 | ); | |
41 | } | |
42 | ||
43 | function setUp() { | |
44 | parent::setUp(); | |
45 | require_once 'CRM/Queue/Service.php'; | |
46 | $this->queueService = CRM_Queue_Service::singleton(TRUE); | |
47 | $this->queue = $this->queueService->create(array( | |
48 | 'type' => 'Sql', | |
49 | 'name' => 'test-queue', | |
50 | )); | |
51 | self::$_recordedValues = array(); | |
52 | } | |
53 | ||
54 | function tearDown() { | |
55 | unset($this->queue); | |
56 | unset($this->queueService); | |
b6708aeb | 57 | |
6a488035 TO |
58 | CRM_Utils_Time::resetTime(); |
59 | ||
60 | $tablesToTruncate = array('civicrm_queue_item'); | |
61 | $this->quickCleanup($tablesToTruncate); | |
62 | } | |
63 | ||
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'), | |
68 | array('a'), | |
69 | 'Add "a"' | |
70 | )); | |
71 | $this->queue->createItem(new CRM_Queue_Task( | |
72 | array('CRM_Queue_RunnerTest', '_recordValue'), | |
73 | array('b'), | |
74 | 'Add "b"' | |
75 | )); | |
76 | $this->queue->createItem(new CRM_Queue_Task( | |
77 | array('CRM_Queue_RunnerTest', '_recordValue'), | |
78 | array('c'), | |
79 | 'Add "c"' | |
80 | )); | |
81 | ||
82 | // run the list of tasks | |
83 | $runner = new CRM_Queue_Runner(array( | |
84 | 'queue' => $this->queue, | |
85 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, | |
86 | )); | |
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()); | |
93 | } | |
94 | ||
95 | /** | |
96 | * Run a series of tasks; one of the tasks will insert more | |
97 | * TODOs at the start of the list | |
98 | */ | |
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'), | |
103 | array('a'), | |
104 | 'Add "a"' | |
105 | )); | |
106 | $this->queue->createItem(new CRM_Queue_Task( | |
107 | array('CRM_Queue_RunnerTest', '_enqueueNumbers'), | |
108 | array(1, 3), | |
109 | 'Add more' | |
110 | )); | |
111 | $this->queue->createItem(new CRM_Queue_Task( | |
112 | array('CRM_Queue_RunnerTest', '_recordValue'), | |
113 | array('b'), | |
114 | 'Add "b"' | |
115 | )); | |
116 | ||
117 | // run the list of tasks | |
118 | $runner = new CRM_Queue_Runner(array( | |
119 | 'queue' => $this->queue, | |
120 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, | |
121 | )); | |
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()); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Run a series of tasks; when one throws an | |
132 | * exception, ignore it and continue | |
133 | */ | |
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'), | |
138 | array('a'), | |
139 | 'Add "a"' | |
140 | )); | |
141 | $this->queue->createItem(new CRM_Queue_Task( | |
142 | array('CRM_Queue_RunnerTest', '_throwException'), | |
143 | array('b'), | |
144 | 'Throw exception' | |
145 | )); | |
146 | $this->queue->createItem(new CRM_Queue_Task( | |
147 | array('CRM_Queue_RunnerTest', '_recordValue'), | |
148 | array('c'), | |
149 | 'Add "c"' | |
150 | )); | |
151 | ||
152 | // run the list of tasks | |
153 | $runner = new CRM_Queue_Runner(array( | |
154 | 'queue' => $this->queue, | |
155 | 'errorMode' => CRM_Queue_Runner::ERROR_CONTINUE, | |
156 | )); | |
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()); | |
164 | } | |
165 | ||
166 | /** | |
167 | * Run a series of tasks; when one throws an exception, | |
168 | * abort processing and return it to the queue. | |
169 | */ | |
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'), | |
174 | array('a'), | |
175 | 'Add "a"' | |
176 | )); | |
177 | $this->queue->createItem(new CRM_Queue_Task( | |
178 | array('CRM_Queue_RunnerTest', '_throwException'), | |
179 | array('b'), | |
180 | 'Throw exception' | |
181 | )); | |
182 | $this->queue->createItem(new CRM_Queue_Task( | |
183 | array('CRM_Queue_RunnerTest', '_recordValue'), | |
184 | array('c'), | |
185 | 'Add "c"' | |
186 | )); | |
187 | ||
188 | // run the list of tasks | |
189 | $runner = new CRM_Queue_Runner(array( | |
190 | 'queue' => $this->queue, | |
191 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, | |
192 | )); | |
193 | $this->assertEquals(self::$_recordedValues, array()); | |
194 | $this->assertEquals(3, $this->queue->numberOfItems()); | |
195 | $result = $runner->runAll(); | |
196 | $this->assertEquals(1, $result['is_error']); | |
197 | // nothing from 'c' | |
198 | $this->assertEquals(self::$_recordedValues, array('a')); | |
199 | // 'b' and 'c' remain | |
200 | $this->assertEquals(2, $this->queue->numberOfItems()); | |
201 | } | |
202 | ||
203 | /** | |
204 | * Run a series of tasks; when one returns false, | |
205 | * abort processing and return it to the queue. | |
206 | */ | |
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'), | |
211 | array('a'), | |
212 | 'Add "a"' | |
213 | )); | |
214 | $this->queue->createItem(new CRM_Queue_Task( | |
215 | array('CRM_Queue_RunnerTest', '_returnFalse'), | |
216 | array(), | |
217 | 'Return false' | |
218 | )); | |
219 | $this->queue->createItem(new CRM_Queue_Task( | |
220 | array('CRM_Queue_RunnerTest', '_recordValue'), | |
221 | array('c'), | |
222 | 'Add "c"' | |
223 | )); | |
224 | ||
225 | // run the list of tasks | |
226 | $runner = new CRM_Queue_Runner(array( | |
227 | 'queue' => $this->queue, | |
228 | 'errorMode' => CRM_Queue_Runner::ERROR_ABORT, | |
229 | )); | |
230 | $this->assertEquals(self::$_recordedValues, array()); | |
231 | $this->assertEquals(3, $this->queue->numberOfItems()); | |
232 | $result = $runner->runAll(); | |
233 | $this->assertEquals(1, $result['is_error']); | |
234 | // nothing from 'c' | |
235 | $this->assertEquals(self::$_recordedValues, array('a')); | |
236 | // 'b' and 'c' remain | |
237 | $this->assertEquals(2, $this->queue->numberOfItems()); | |
238 | } | |
239 | ||
240 | /* **** Queue tasks **** */ | |
241 | ||
242 | ||
243 | static $_recordedValues; | |
244 | ||
245 | static | |
246 | function _recordValue($taskCtx, $value) { | |
247 | self::$_recordedValues[] = $value; | |
248 | return TRUE; | |
249 | } | |
250 | ||
251 | static | |
252 | function _returnFalse($taskCtx) { | |
253 | return FALSE; | |
254 | } | |
255 | ||
256 | static | |
257 | function _throwException($taskCtx, $value) { | |
258 | throw new Exception("Manufactured error: $value"); | |
259 | } | |
b6708aeb | 260 | |
6a488035 TO |
261 | static |
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'), | |
266 | array($i), | |
267 | sprintf('Add number "%d"', $i) | |
268 | ), array( | |
269 | 'weight' => -1, | |
270 | )); | |
271 | } | |
272 | return TRUE; | |
273 | } | |
274 | } | |
275 |