Merge remote-tracking branch 'upstream/4.3' into 4.3-master-2013-09-25-01-46-57
[civicrm-core.git] / tests / phpunit / CRM / Queue / QueueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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_QueueTest 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 /* ----------------------- Queue providers ----------------------- */
44
45 /* Define a list of queue providers which should be tested */
46
47 /**
48 * Return a list of persistent and transient queue providers
49 */
50 function getQueueSpecs() {
51 $queueSpecs = array();
52 $queueSpecs[] = array(
53 array(
54 'type' => 'Sql',
55 'name' => 'test-queue',
56 ));
57 $queueSpecs[] = array(
58 array(
59 'type' => 'Memory',
60 'name' => 'test-queue',
61 ));
62 return $queueSpecs;
63 }
64
65 /* ----------------------- Per-provider tests ----------------------- */
66 function setUp() {
67 parent::setUp();
68 $this->queueService = CRM_Queue_Service::singleton(TRUE);
69 }
70
71 function tearDown() {
72 CRM_Utils_Time::resetTime();
73
74 $tablesToTruncate = array('civicrm_queue_item');
75 $this->quickCleanup($tablesToTruncate);
76 }
77
78 /**
79 * Create a few queue items; alternately enqueue and dequeue various
80 *
81 * @dataProvider getQueueSpecs
82 */
83 function testBasicUsage($queueSpec) {
84 $this->queue = $this->queueService->create($queueSpec);
85 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
86
87 $this->queue->createItem(array(
88 'test-key' => 'a',
89 ));
90 $this->queue->createItem(array(
91 'test-key' => 'b',
92 ));
93 $this->queue->createItem(array(
94 'test-key' => 'c',
95 ));
96
97 $this->assertEquals(3, $this->queue->numberOfItems());
98 $item = $this->queue->claimItem();
99 $this->assertEquals('a', $item->data['test-key']);
100 $this->queue->deleteItem($item);
101
102 $this->assertEquals(2, $this->queue->numberOfItems());
103 $item = $this->queue->claimItem();
104 $this->assertEquals('b', $item->data['test-key']);
105 $this->queue->deleteItem($item);
106
107 $this->queue->createItem(array(
108 'test-key' => 'd',
109 ));
110
111 $this->assertEquals(2, $this->queue->numberOfItems());
112 $item = $this->queue->claimItem();
113 $this->assertEquals('c', $item->data['test-key']);
114 $this->queue->deleteItem($item);
115
116 $this->assertEquals(1, $this->queue->numberOfItems());
117 $item = $this->queue->claimItem();
118 $this->assertEquals('d', $item->data['test-key']);
119 $this->queue->deleteItem($item);
120
121 $this->assertEquals(0, $this->queue->numberOfItems());
122 }
123
124 /**
125 * Claim an item from the queue and release it back for subsequent processing
126 *
127 * @dataProvider getQueueSpecs
128 */
129 function testManualRelease($queueSpec) {
130 $this->queue = $this->queueService->create($queueSpec);
131 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
132
133 $this->queue->createItem(array(
134 'test-key' => 'a',
135 ));
136
137 $item = $this->queue->claimItem();
138 $this->assertEquals('a', $item->data['test-key']);
139 $this->assertEquals(1, $this->queue->numberOfItems());
140 $this->queue->releaseItem($item);
141
142 $this->assertEquals(1, $this->queue->numberOfItems());
143 $item = $this->queue->claimItem();
144 $this->assertEquals('a', $item->data['test-key']);
145 $this->queue->deleteItem($item);
146
147 $this->assertEquals(0, $this->queue->numberOfItems());
148 }
149
150 /**
151 * Test that item leases expire at the expected time
152 *
153 * @dataProvider getQueueSpecs
154 */
155 function testTimeoutRelease($queueSpec) {
156 $this->queue = $this->queueService->create($queueSpec);
157 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
158
159 CRM_Utils_Time::setTime('2012-04-01 1:00:00');
160 $this->queue->createItem(array(
161 'test-key' => 'a',
162 ));
163
164 $item = $this->queue->claimItem();
165 $this->assertEquals('a', $item->data['test-key']);
166 $this->assertEquals(1, $this->queue->numberOfItems());
167 // forget to release
168
169 // haven't reach expiration yet
170 CRM_Utils_Time::setTime('2012-04-01 1:59:00');
171 $item2 = $this->queue->claimItem();
172 $this->assertEquals(FALSE, $item2);
173
174 // pass expiration mark
175 CRM_Utils_Time::setTime('2012-04-01 2:00:01');
176 $item3 = $this->queue->claimItem();
177 $this->assertEquals('a', $item3->data['test-key']);
178 $this->assertEquals(1, $this->queue->numberOfItems());
179 $this->queue->deleteItem($item3);
180
181 $this->assertEquals(0, $this->queue->numberOfItems());
182 }
183
184 /**
185 * Test that item leases can be ignored
186 *
187 * @dataProvider getQueueSpecs
188 */
189 function testStealItem($queueSpec) {
190 $this->queue = $this->queueService->create($queueSpec);
191 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
192
193 require_once 'CRM/Utils/Time.php';
194 CRM_Utils_Time::setTime('2012-04-01 1:00:00');
195 $this->queue->createItem(array(
196 'test-key' => 'a',
197 ));
198
199 $item = $this->queue->claimItem();
200 $this->assertEquals('a', $item->data['test-key']);
201 $this->assertEquals(1, $this->queue->numberOfItems());
202 // forget to release
203
204 // haven't reached expiration yet, so claimItem fails
205 CRM_Utils_Time::setTime('2012-04-01 1:59:00');
206 $item2 = $this->queue->claimItem();
207 $this->assertEquals(FALSE, $item2);
208
209 // but stealItem works
210 $item3 = $this->queue->stealItem();
211 $this->assertEquals('a', $item3->data['test-key']);
212 $this->assertEquals(1, $this->queue->numberOfItems());
213 $this->queue->deleteItem($item3);
214
215 $this->assertEquals(0, $this->queue->numberOfItems());
216 }
217
218 /**
219 * Test that queue content is reset when reset=>TRUE
220 *
221 * @dataProvider getQueueSpecs
222 */
223 function testCreateResetTrue($queueSpec) {
224 $this->queue = $this->queueService->create($queueSpec);
225 $this->queue->createItem(array(
226 'test-key' => 'a',
227 ));
228 $this->queue->createItem(array(
229 'test-key' => 'b',
230 ));
231 $this->assertEquals(2, $this->queue->numberOfItems());
232 unset($this->queue);
233
234 $queue2 = $this->queueService->create(
235 $queueSpec + array('reset' => TRUE)
236 );
237 $this->assertEquals(0, $queue2->numberOfItems());
238 }
239
240 /**
241 * Test that queue content is not reset when reset is omitted
242 *
243 * @dataProvider getQueueSpecs
244 */
245 function testCreateResetFalse($queueSpec) {
246 $this->queue = $this->queueService->create($queueSpec);
247 $this->queue->createItem(array(
248 'test-key' => 'a',
249 ));
250 $this->queue->createItem(array(
251 'test-key' => 'b',
252 ));
253 $this->assertEquals(2, $this->queue->numberOfItems());
254 unset($this->queue);
255
256 $queue2 = $this->queueService->create($queueSpec);
257 $this->assertEquals(2, $queue2->numberOfItems());
258
259 $item = $queue2->claimItem();
260 $this->assertEquals('a', $item->data['test-key']);
261 $queue2->releaseItem($item);
262 }
263
264 /**
265 * Test that queue content is not reset when using load()
266 *
267 * @dataProvider getQueueSpecs
268 */
269 function testLoad($queueSpec) {
270 $this->queue = $this->queueService->create($queueSpec);
271 $this->queue->createItem(array(
272 'test-key' => 'a',
273 ));
274 $this->queue->createItem(array(
275 'test-key' => 'b',
276 ));
277 $this->assertEquals(2, $this->queue->numberOfItems());
278 unset($this->queue);
279
280 $queue2 = $this->queueService->create($queueSpec);
281 $this->assertEquals(2, $queue2->numberOfItems());
282
283 $item = $queue2->claimItem();
284 $this->assertEquals('a', $item->data['test-key']);
285 $queue2->releaseItem($item);
286 }
287 }
288