Clean up code for batch 19
[civicrm-core.git] / tests / phpunit / CRM / Queue / QueueTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06a1bc01 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
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
29require_once 'CiviTest/CiviUnitTestCase.php';
30
31/**
32 * Ensure that various queue implementations comply with the interface
33 */
34class CRM_Queue_QueueTest extends CiviUnitTestCase {
6a488035
TO
35
36 /* ----------------------- Queue providers ----------------------- */
37
38 /* Define a list of queue providers which should be tested */
39
40 /**
41 * Return a list of persistent and transient queue providers
42 */
00be9182 43 public function getQueueSpecs() {
6a488035
TO
44 $queueSpecs = array();
45 $queueSpecs[] = array(
46 array(
47 'type' => 'Sql',
48 'name' => 'test-queue',
28a04ea9 49 ),
92915c55 50 );
6a488035
TO
51 $queueSpecs[] = array(
52 array(
53 'type' => 'Memory',
54 'name' => 'test-queue',
28a04ea9 55 ),
92915c55 56 );
6a488035
TO
57 return $queueSpecs;
58 }
59
28a04ea9 60 /**
61 * Per-provider tests
62 */
00be9182 63 public function setUp() {
6a488035
TO
64 parent::setUp();
65 $this->queueService = CRM_Queue_Service::singleton(TRUE);
66 }
67
00be9182 68 public function tearDown() {
6a488035
TO
69 CRM_Utils_Time::resetTime();
70
71 $tablesToTruncate = array('civicrm_queue_item');
72 $this->quickCleanup($tablesToTruncate);
73 }
74
75 /**
76 * Create a few queue items; alternately enqueue and dequeue various
77 *
78 * @dataProvider getQueueSpecs
1e1fdcf6 79 * @param $queueSpec
6a488035 80 */
00be9182 81 public function testBasicUsage($queueSpec) {
6a488035
TO
82 $this->queue = $this->queueService->create($queueSpec);
83 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
84
85 $this->queue->createItem(array(
92915c55
TO
86 'test-key' => 'a',
87 ));
6a488035 88 $this->queue->createItem(array(
92915c55
TO
89 'test-key' => 'b',
90 ));
6a488035 91 $this->queue->createItem(array(
92915c55
TO
92 'test-key' => 'c',
93 ));
6a488035
TO
94
95 $this->assertEquals(3, $this->queue->numberOfItems());
96 $item = $this->queue->claimItem();
97 $this->assertEquals('a', $item->data['test-key']);
98 $this->queue->deleteItem($item);
99
100 $this->assertEquals(2, $this->queue->numberOfItems());
101 $item = $this->queue->claimItem();
102 $this->assertEquals('b', $item->data['test-key']);
103 $this->queue->deleteItem($item);
104
105 $this->queue->createItem(array(
92915c55
TO
106 'test-key' => 'd',
107 ));
6a488035
TO
108
109 $this->assertEquals(2, $this->queue->numberOfItems());
110 $item = $this->queue->claimItem();
111 $this->assertEquals('c', $item->data['test-key']);
112 $this->queue->deleteItem($item);
113
114 $this->assertEquals(1, $this->queue->numberOfItems());
115 $item = $this->queue->claimItem();
116 $this->assertEquals('d', $item->data['test-key']);
117 $this->queue->deleteItem($item);
118
119 $this->assertEquals(0, $this->queue->numberOfItems());
120 }
121
122 /**
123 * Claim an item from the queue and release it back for subsequent processing
124 *
125 * @dataProvider getQueueSpecs
1e1fdcf6 126 * @param $queueSpec
6a488035 127 */
00be9182 128 public function testManualRelease($queueSpec) {
6a488035
TO
129 $this->queue = $this->queueService->create($queueSpec);
130 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
131
132 $this->queue->createItem(array(
92915c55
TO
133 'test-key' => 'a',
134 ));
6a488035
TO
135
136 $item = $this->queue->claimItem();
137 $this->assertEquals('a', $item->data['test-key']);
138 $this->assertEquals(1, $this->queue->numberOfItems());
139 $this->queue->releaseItem($item);
140
141 $this->assertEquals(1, $this->queue->numberOfItems());
142 $item = $this->queue->claimItem();
143 $this->assertEquals('a', $item->data['test-key']);
144 $this->queue->deleteItem($item);
145
146 $this->assertEquals(0, $this->queue->numberOfItems());
147 }
148
149 /**
150 * Test that item leases expire at the expected time
151 *
152 * @dataProvider getQueueSpecs
1e1fdcf6 153 * @param $queueSpec
6a488035 154 */
00be9182 155 public function testTimeoutRelease($queueSpec) {
6a488035
TO
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(
92915c55
TO
161 'test-key' => 'a',
162 ));
6a488035
TO
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
1e1fdcf6 188 * @param $queueSpec
6a488035 189 */
00be9182 190 public function testStealItem($queueSpec) {
6a488035
TO
191 $this->queue = $this->queueService->create($queueSpec);
192 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
193
194 require_once 'CRM/Utils/Time.php';
195 CRM_Utils_Time::setTime('2012-04-01 1:00:00');
196 $this->queue->createItem(array(
92915c55
TO
197 'test-key' => 'a',
198 ));
6a488035
TO
199
200 $item = $this->queue->claimItem();
201 $this->assertEquals('a', $item->data['test-key']);
202 $this->assertEquals(1, $this->queue->numberOfItems());
203 // forget to release
204
205 // haven't reached expiration yet, so claimItem fails
206 CRM_Utils_Time::setTime('2012-04-01 1:59:00');
207 $item2 = $this->queue->claimItem();
208 $this->assertEquals(FALSE, $item2);
209
210 // but stealItem works
211 $item3 = $this->queue->stealItem();
212 $this->assertEquals('a', $item3->data['test-key']);
213 $this->assertEquals(1, $this->queue->numberOfItems());
214 $this->queue->deleteItem($item3);
215
216 $this->assertEquals(0, $this->queue->numberOfItems());
217 }
218
219 /**
220 * Test that queue content is reset when reset=>TRUE
221 *
222 * @dataProvider getQueueSpecs
1e1fdcf6 223 * @param $queueSpec
6a488035 224 */
00be9182 225 public function testCreateResetTrue($queueSpec) {
6a488035
TO
226 $this->queue = $this->queueService->create($queueSpec);
227 $this->queue->createItem(array(
92915c55
TO
228 'test-key' => 'a',
229 ));
6a488035 230 $this->queue->createItem(array(
92915c55
TO
231 'test-key' => 'b',
232 ));
6a488035
TO
233 $this->assertEquals(2, $this->queue->numberOfItems());
234 unset($this->queue);
235
236 $queue2 = $this->queueService->create(
237 $queueSpec + array('reset' => TRUE)
238 );
239 $this->assertEquals(0, $queue2->numberOfItems());
240 }
241
242 /**
243 * Test that queue content is not reset when reset is omitted
244 *
245 * @dataProvider getQueueSpecs
1e1fdcf6 246 * @param $queueSpec
6a488035 247 */
00be9182 248 public function testCreateResetFalse($queueSpec) {
6a488035
TO
249 $this->queue = $this->queueService->create($queueSpec);
250 $this->queue->createItem(array(
92915c55
TO
251 'test-key' => 'a',
252 ));
6a488035 253 $this->queue->createItem(array(
92915c55
TO
254 'test-key' => 'b',
255 ));
6a488035
TO
256 $this->assertEquals(2, $this->queue->numberOfItems());
257 unset($this->queue);
258
259 $queue2 = $this->queueService->create($queueSpec);
260 $this->assertEquals(2, $queue2->numberOfItems());
261
262 $item = $queue2->claimItem();
263 $this->assertEquals('a', $item->data['test-key']);
264 $queue2->releaseItem($item);
265 }
266
267 /**
268 * Test that queue content is not reset when using load()
269 *
270 * @dataProvider getQueueSpecs
1e1fdcf6 271 * @param $queueSpec
6a488035 272 */
00be9182 273 public function testLoad($queueSpec) {
6a488035
TO
274 $this->queue = $this->queueService->create($queueSpec);
275 $this->queue->createItem(array(
92915c55
TO
276 'test-key' => 'a',
277 ));
6a488035 278 $this->queue->createItem(array(
92915c55
TO
279 'test-key' => 'b',
280 ));
6a488035
TO
281 $this->assertEquals(2, $this->queue->numberOfItems());
282 unset($this->queue);
283
284 $queue2 = $this->queueService->create($queueSpec);
285 $this->assertEquals(2, $queue2->numberOfItems());
286
287 $item = $queue2->claimItem();
288 $this->assertEquals('a', $item->data['test-key']);
289 $queue2->releaseItem($item);
290 }
291}