Merge pull request #14639 from colemanw/i18nCleanup
[civicrm-core.git] / tests / phpunit / CRM / Queue / QueueTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Ensure that various queue implementations comply with the interface
acb109b7 30 * @group headless
6a488035
TO
31 */
32class CRM_Queue_QueueTest extends CiviUnitTestCase {
6a488035
TO
33
34 /* ----------------------- Queue providers ----------------------- */
35
36 /* Define a list of queue providers which should be tested */
37
38 /**
eceb18cc 39 * Return a list of persistent and transient queue providers.
6a488035 40 */
00be9182 41 public function getQueueSpecs() {
6a488035
TO
42 $queueSpecs = array();
43 $queueSpecs[] = array(
44 array(
45 'type' => 'Sql',
46 'name' => 'test-queue',
28a04ea9 47 ),
92915c55 48 );
6a488035
TO
49 $queueSpecs[] = array(
50 array(
51 'type' => 'Memory',
52 'name' => 'test-queue',
28a04ea9 53 ),
92915c55 54 );
6a488035
TO
55 return $queueSpecs;
56 }
57
28a04ea9 58 /**
59 * Per-provider tests
60 */
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 /**
eceb18cc 121 * Claim an item from the queue and release it back for subsequent processing.
6a488035
TO
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 /**
eceb18cc 148 * Test that item leases expire at the expected time.
6a488035
TO
149 *
150 * @dataProvider getQueueSpecs
fe482240 151 *
1e1fdcf6 152 * @param $queueSpec
6a488035 153 */
00be9182 154 public function testTimeoutRelease($queueSpec) {
6a488035
TO
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(
92915c55
TO
160 'test-key' => 'a',
161 ));
6a488035
TO
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 /**
eceb18cc 184 * Test that item leases can be ignored.
6a488035
TO
185 *
186 * @dataProvider getQueueSpecs
1e1fdcf6 187 * @param $queueSpec
6a488035 188 */
00be9182 189 public function testStealItem($queueSpec) {
6a488035
TO
190 $this->queue = $this->queueService->create($queueSpec);
191 $this->assertTrue($this->queue instanceof CRM_Queue_Queue);
192
6a488035
TO
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 /**
eceb18cc 241 * Test that queue content is not reset when reset is omitted.
6a488035
TO
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 }
96025800 289
6a488035 290}