tests/phpunit - Declare `@group headless`
[civicrm-core.git] / tests / phpunit / CRM / Queue / QueueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * Ensure that various queue implementations comply with the interface
30 * @group headless
31 */
32 class CRM_Queue_QueueTest extends CiviUnitTestCase {
33
34 /* ----------------------- Queue providers ----------------------- */
35
36 /* Define a list of queue providers which should be tested */
37
38 /**
39 * Return a list of persistent and transient queue providers.
40 */
41 public function getQueueSpecs() {
42 $queueSpecs = array();
43 $queueSpecs[] = array(
44 array(
45 'type' => 'Sql',
46 'name' => 'test-queue',
47 ),
48 );
49 $queueSpecs[] = array(
50 array(
51 'type' => 'Memory',
52 'name' => 'test-queue',
53 ),
54 );
55 return $queueSpecs;
56 }
57
58 /**
59 * Per-provider tests
60 */
61 public function setUp() {
62 parent::setUp();
63 $this->queueService = CRM_Queue_Service::singleton(TRUE);
64 }
65
66 public function tearDown() {
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
77 * @param $queueSpec
78 */
79 public function testBasicUsage($queueSpec) {
80 $this->queue = $this->queueService->create($queueSpec);
81 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
82
83 $this->queue->createItem(array(
84 'test-key' => 'a',
85 ));
86 $this->queue->createItem(array(
87 'test-key' => 'b',
88 ));
89 $this->queue->createItem(array(
90 'test-key' => 'c',
91 ));
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(
104 'test-key' => 'd',
105 ));
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
124 * @param $queueSpec
125 */
126 public function testManualRelease($queueSpec) {
127 $this->queue = $this->queueService->create($queueSpec);
128 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
129
130 $this->queue->createItem(array(
131 'test-key' => 'a',
132 ));
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
151 *
152 * @param $queueSpec
153 */
154 public function testTimeoutRelease($queueSpec) {
155 $this->queue = $this->queueService->create($queueSpec);
156 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
157
158 CRM_Utils_Time::setTime('2012-04-01 1:00:00');
159 $this->queue->createItem(array(
160 'test-key' => 'a',
161 ));
162
163 $item = $this->queue->claimItem();
164 $this->assertEquals('a', $item->data['test-key']);
165 $this->assertEquals(1, $this->queue->numberOfItems());
166 // forget to release
167
168 // haven't reach expiration yet
169 CRM_Utils_Time::setTime('2012-04-01 1:59:00');
170 $item2 = $this->queue->claimItem();
171 $this->assertEquals(FALSE, $item2);
172
173 // pass expiration mark
174 CRM_Utils_Time::setTime('2012-04-01 2:00:01');
175 $item3 = $this->queue->claimItem();
176 $this->assertEquals('a', $item3->data['test-key']);
177 $this->assertEquals(1, $this->queue->numberOfItems());
178 $this->queue->deleteItem($item3);
179
180 $this->assertEquals(0, $this->queue->numberOfItems());
181 }
182
183 /**
184 * Test that item leases can be ignored.
185 *
186 * @dataProvider getQueueSpecs
187 * @param $queueSpec
188 */
189 public 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 * @param $queueSpec
223 */
224 public function testCreateResetTrue($queueSpec) {
225 $this->queue = $this->queueService->create($queueSpec);
226 $this->queue->createItem(array(
227 'test-key' => 'a',
228 ));
229 $this->queue->createItem(array(
230 'test-key' => 'b',
231 ));
232 $this->assertEquals(2, $this->queue->numberOfItems());
233 unset($this->queue);
234
235 $queue2 = $this->queueService->create(
236 $queueSpec + array('reset' => TRUE)
237 );
238 $this->assertEquals(0, $queue2->numberOfItems());
239 }
240
241 /**
242 * Test that queue content is not reset when reset is omitted.
243 *
244 * @dataProvider getQueueSpecs
245 * @param $queueSpec
246 */
247 public function testCreateResetFalse($queueSpec) {
248 $this->queue = $this->queueService->create($queueSpec);
249 $this->queue->createItem(array(
250 'test-key' => 'a',
251 ));
252 $this->queue->createItem(array(
253 'test-key' => 'b',
254 ));
255 $this->assertEquals(2, $this->queue->numberOfItems());
256 unset($this->queue);
257
258 $queue2 = $this->queueService->create($queueSpec);
259 $this->assertEquals(2, $queue2->numberOfItems());
260
261 $item = $queue2->claimItem();
262 $this->assertEquals('a', $item->data['test-key']);
263 $queue2->releaseItem($item);
264 }
265
266 /**
267 * Test that queue content is not reset when using load()
268 *
269 * @dataProvider getQueueSpecs
270 * @param $queueSpec
271 */
272 public function testLoad($queueSpec) {
273 $this->queue = $this->queueService->create($queueSpec);
274 $this->queue->createItem(array(
275 'test-key' => 'a',
276 ));
277 $this->queue->createItem(array(
278 'test-key' => 'b',
279 ));
280 $this->assertEquals(2, $this->queue->numberOfItems());
281 unset($this->queue);
282
283 $queue2 = $this->queueService->create($queueSpec);
284 $this->assertEquals(2, $queue2->numberOfItems());
285
286 $item = $queue2->claimItem();
287 $this->assertEquals('a', $item->data['test-key']);
288 $queue2->releaseItem($item);
289 }
290
291 }