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