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