1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the tarantool/queue package. |
5
|
|
|
* |
6
|
|
|
* (c) Eugene Leonovich <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Tarantool\Queue\Tests\Integration; |
15
|
|
|
|
16
|
|
|
use Tarantool\Queue\States; |
17
|
|
|
|
18
|
|
|
abstract class QueueTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @dataProvider provideTaskData |
22
|
|
|
* @lua create_tube('%tube_name%', '%tube_type%') |
23
|
|
|
* |
24
|
|
|
* @param mixed $data |
25
|
|
|
*/ |
26
|
|
|
public function testPut($data) : void |
27
|
|
|
{ |
28
|
|
|
$task = $this->queue->put($data); |
29
|
|
|
|
30
|
|
|
self::assertTask($task, 0, States::READY, $data); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function provideTaskData() : iterable |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
[null], |
37
|
|
|
[true], |
38
|
|
|
['foo'], |
39
|
|
|
["\x04\x00\xa0\x00\x00"], |
40
|
|
|
[42], |
41
|
|
|
[-42], |
42
|
|
|
[4.2], |
43
|
|
|
[['foo' => 'bar', 'baz' => ['qux' => false, -4.2]]], |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
49
|
|
|
* @lua tube:put('peek_0') |
50
|
|
|
*/ |
51
|
|
|
public function testPeek() : void |
52
|
|
|
{ |
53
|
|
|
$task = $this->queue->peek(0); |
54
|
|
|
|
55
|
|
|
self::assertTask($task, 0, States::READY, 'peek_0'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
60
|
|
|
* @lua tube:put('take') |
61
|
|
|
*/ |
62
|
|
|
public function testTake() : void |
63
|
|
|
{ |
64
|
|
|
$task = $this->queue->take(); |
65
|
|
|
|
66
|
|
|
self::assertTask($task, 0, States::TAKEN, 'take'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @lua create_tube('%tube_name%', '%tube_type%') |
71
|
|
|
*/ |
72
|
|
|
public function testTakeNone() : void |
73
|
|
|
{ |
74
|
|
|
$time = time(); |
75
|
|
|
$task = $this->queue->take(2); |
76
|
|
|
|
77
|
|
|
self::assertGreaterThanOrEqual(2, time() - $time); |
78
|
|
|
self::assertNull($task); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
83
|
|
|
* @lua tube:put('release_0') |
84
|
|
|
* @lua tube:take() |
85
|
|
|
*/ |
86
|
|
|
public function testRelease() : void |
87
|
|
|
{ |
88
|
|
|
$task = $this->queue->release(0); |
89
|
|
|
|
90
|
|
|
self::assertTask($task, 0, States::READY, 'release_0'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
95
|
|
|
* @lua tube:put('ack_0') |
96
|
|
|
* @lua tube:take() |
97
|
|
|
*/ |
98
|
|
|
public function testAck() : void |
99
|
|
|
{ |
100
|
|
|
$task = $this->queue->ack(0); |
101
|
|
|
|
102
|
|
|
self::assertTask($task, 0, States::DONE, 'ack_0'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
107
|
|
|
* @lua tube:put('bury_0') |
108
|
|
|
*/ |
109
|
|
|
public function testBury() : void |
110
|
|
|
{ |
111
|
|
|
$task = $this->queue->bury(0); |
112
|
|
|
|
113
|
|
|
self::assertTask($task, 0, States::BURIED, 'bury_0'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
118
|
|
|
* @lua tube:put('kick_1') |
119
|
|
|
* @lua tube:bury(0) |
120
|
|
|
*/ |
121
|
|
|
public function testKickOne() : void |
122
|
|
|
{ |
123
|
|
|
$count = $this->queue->kick(1); |
124
|
|
|
|
125
|
|
|
self::assertSame(1, $count); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
130
|
|
|
* @lua tube:put('kick_1') |
131
|
|
|
* @lua tube:put('kick_2') |
132
|
|
|
* @lua tube:put('kick_3') |
133
|
|
|
* @lua tube:bury(0) |
134
|
|
|
* @lua tube:bury(1) |
135
|
|
|
* @lua tube:bury(2) |
136
|
|
|
*/ |
137
|
|
|
public function testKickMany() : void |
138
|
|
|
{ |
139
|
|
|
$count = $this->queue->kick(3); |
140
|
|
|
|
141
|
|
|
self::assertSame(3, $count); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
146
|
|
|
* @lua tube:put('delete_0') |
147
|
|
|
*/ |
148
|
|
|
public function testDelete() : void |
149
|
|
|
{ |
150
|
|
|
$task = $this->queue->delete(0); |
151
|
|
|
|
152
|
|
|
self::assertTask($task, 0, States::DONE, 'delete_0'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
157
|
|
|
* @lua tube:put('truncate_0') |
158
|
|
|
* @lua tube:put('truncate_1') |
159
|
|
|
*/ |
160
|
|
|
public function testTruncate() : void |
161
|
|
|
{ |
162
|
|
|
self::assertSame(2, $this->queue->stats('tasks.total')); |
163
|
|
|
|
164
|
|
|
$this->queue->truncate(); |
165
|
|
|
|
166
|
|
|
self::assertSame(0, $this->queue->stats('tasks.total')); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @lua create_tube('%tube_name%', '%tube_type%') |
171
|
|
|
*/ |
172
|
|
|
public function testTruncateEmpty() : void |
173
|
|
|
{ |
174
|
|
|
self::assertSame(0, $this->queue->stats('tasks.total')); |
175
|
|
|
|
176
|
|
|
$this->queue->truncate(); |
177
|
|
|
|
178
|
|
|
self::assertSame(0, $this->queue->stats('tasks.total')); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
183
|
|
|
* @lua tube:put('stat_0') |
184
|
|
|
* @lua tube:put('stat_1') |
185
|
|
|
* @lua tube:put('stat_2') |
186
|
|
|
* @lua tube:put('stat_3') |
187
|
|
|
* @lua tube:put('stat_4') |
188
|
|
|
* @lua tube:delete(4) |
189
|
|
|
* @lua tube:take(.001) |
190
|
|
|
* @lua tube:release(0) |
191
|
|
|
* @lua tube:take(.001) |
192
|
|
|
* @lua tube:ack(0) |
193
|
|
|
* @lua tube:bury(1) |
194
|
|
|
* @lua tube:bury(2) |
195
|
|
|
* @lua tube:kick(1) |
196
|
|
|
* @lua tube:take(.001) |
197
|
|
|
*/ |
198
|
|
|
public function testStats() : void |
199
|
|
|
{ |
200
|
|
|
$stats = $this->queue->stats(); |
201
|
|
|
|
202
|
|
|
self::assertEquals([ |
203
|
|
|
'tasks' => [ |
204
|
|
|
'taken' => 1, |
205
|
|
|
'buried' => 1, |
206
|
|
|
'ready' => 1, |
207
|
|
|
'done' => 2, |
208
|
|
|
'delayed' => 0, |
209
|
|
|
'total' => 3, |
210
|
|
|
], |
211
|
|
|
'calls' => [ |
212
|
|
|
'ack' => 1, |
213
|
|
|
'delete' => 1, |
214
|
|
|
'take' => 3, |
215
|
|
|
'kick' => 1, |
216
|
|
|
'release' => 1, |
217
|
|
|
'touch' => 0, |
218
|
|
|
'put' => 5, |
219
|
|
|
'bury' => 2, |
220
|
|
|
'ttr' => 0, |
221
|
|
|
'delay' => 0, |
222
|
|
|
'ttl' => 0, |
223
|
|
|
], |
224
|
|
|
], $stats); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @lua create_tube('%tube_name%', '%tube_type%') |
229
|
|
|
*/ |
230
|
|
|
public function testEmptyStats() : void |
231
|
|
|
{ |
232
|
|
|
$stats = $this->queue->stats(); |
233
|
|
|
|
234
|
|
|
self::assertEquals([ |
235
|
|
|
'tasks' => [ |
236
|
|
|
'taken' => 0, |
237
|
|
|
'buried' => 0, |
238
|
|
|
'ready' => 0, |
239
|
|
|
'done' => 0, |
240
|
|
|
'delayed' => 0, |
241
|
|
|
'total' => 0, |
242
|
|
|
], |
243
|
|
|
'calls' => [ |
244
|
|
|
'ack' => 0, |
245
|
|
|
'bury' => 0, |
246
|
|
|
'delete' => 0, |
247
|
|
|
'kick' => 0, |
248
|
|
|
'put' => 0, |
249
|
|
|
'release' => 0, |
250
|
|
|
'take' => 0, |
251
|
|
|
'touch' => 0, |
252
|
|
|
'ttr' => 0, |
253
|
|
|
'delay' => 0, |
254
|
|
|
'ttl' => 0, |
255
|
|
|
], |
256
|
|
|
], $stats); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
261
|
|
|
* @lua tube:put('stat_0') |
262
|
|
|
* @lua tube:put('stat_1') |
263
|
|
|
* @lua tube:put('stat_2') |
264
|
|
|
* @lua tube:put('stat_3') |
265
|
|
|
* @lua tube:put('stat_4') |
266
|
|
|
* @lua tube:put('stat_5') |
267
|
|
|
* @lua tube:put('stat_6') |
268
|
|
|
* @lua tube:put('stat_7') |
269
|
|
|
* @lua tube:put('stat_8') |
270
|
|
|
* @lua tube:put('stat_9') |
271
|
|
|
* @lua tube:take(.001) |
272
|
|
|
* @lua tube:release(0) |
273
|
|
|
* @lua tube:take(.001) |
274
|
|
|
* @lua tube:ack(0) |
275
|
|
|
* @lua tube:bury(1) |
276
|
|
|
* @lua tube:bury(2) |
277
|
|
|
* @lua tube:bury(3) |
278
|
|
|
* @lua tube:kick(1) |
279
|
|
|
* @lua tube:delete(6) |
280
|
|
|
* @lua tube:delete(7) |
281
|
|
|
* @lua tube:delete(8) |
282
|
|
|
* @lua tube:take(.001) |
283
|
|
|
*/ |
284
|
|
|
public function testStatsPath() : void |
285
|
|
|
{ |
286
|
|
|
self::assertEquals([ |
287
|
|
|
'taken' => 1, |
288
|
|
|
'buried' => 2, |
289
|
|
|
'ready' => 3, |
290
|
|
|
'done' => 4, |
291
|
|
|
'delayed' => 0, |
292
|
|
|
'total' => 6, |
293
|
|
|
], $this->queue->stats('tasks')); |
294
|
|
|
|
295
|
|
|
self::assertSame(1, $this->queue->stats('tasks.taken')); |
296
|
|
|
self::assertSame(2, $this->queue->stats('tasks.buried')); |
297
|
|
|
self::assertSame(3, $this->queue->stats('tasks.ready')); |
298
|
|
|
self::assertSame(4, $this->queue->stats('tasks.done')); |
299
|
|
|
self::assertSame(0, $this->queue->stats('tasks.delayed')); |
300
|
|
|
self::assertSame(6, $this->queue->stats('tasks.total')); |
301
|
|
|
|
302
|
|
|
self::assertEquals([ |
303
|
|
|
'ack' => 1, |
304
|
|
|
'delete' => 3, |
305
|
|
|
'take' => 3, |
306
|
|
|
'kick' => 1, |
307
|
|
|
'release' => 1, |
308
|
|
|
'touch' => 0, |
309
|
|
|
'put' => 10, |
310
|
|
|
'bury' => 3, |
311
|
|
|
'ttr' => 0, |
312
|
|
|
'delay' => 0, |
313
|
|
|
'ttl' => 0, |
314
|
|
|
], $this->queue->stats('calls')); |
315
|
|
|
|
316
|
|
|
self::assertSame(1, $this->queue->stats('calls.ack')); |
317
|
|
|
self::assertSame(3, $this->queue->stats('calls.delete')); |
318
|
|
|
self::assertSame(3, $this->queue->stats('calls.take')); |
319
|
|
|
self::assertSame(1, $this->queue->stats('calls.kick')); |
320
|
|
|
self::assertSame(1, $this->queue->stats('calls.release')); |
321
|
|
|
self::assertSame(0, $this->queue->stats('calls.touch')); |
322
|
|
|
self::assertSame(10, $this->queue->stats('calls.put')); |
323
|
|
|
self::assertSame(3, $this->queue->stats('calls.bury')); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @dataProvider provideStatsInvalidPathData |
328
|
|
|
* @lua create_tube('%tube_name%', '%tube_type%') |
329
|
|
|
*/ |
330
|
|
|
public function testStatsInvalidPath(?string $path) : void |
331
|
|
|
{ |
332
|
|
|
$this->expectException(\InvalidArgumentException::class); |
333
|
|
|
$this->expectExceptionMessageMatches('/^Invalid path ".*?"$/'); |
334
|
|
|
|
335
|
|
|
$this->queue->stats($path); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function provideStatsInvalidPathData() : iterable |
339
|
|
|
{ |
340
|
|
|
return [ |
341
|
|
|
[''], |
342
|
|
|
['.'], |
343
|
|
|
['foo'], |
344
|
|
|
['tasks.foo'], |
345
|
|
|
['.tasks'], |
346
|
|
|
['tasks.'], |
347
|
|
|
['calls.foo'], |
348
|
|
|
['.calls'], |
349
|
|
|
['calls.'], |
350
|
|
|
]; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @lua tube = create_tube('%tube_name%', '%tube_type%') |
355
|
|
|
* @lua tube.pow = function(self, base, exp) return math.pow(base, exp) end |
356
|
|
|
*/ |
357
|
|
|
public function testCall() : void |
358
|
|
|
{ |
359
|
|
|
$result = $this->queue->call('pow', 2, 8); |
360
|
|
|
|
361
|
|
|
self::assertSame(256, $result[0]); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @dataProvider provideFailureCallbackData |
366
|
|
|
*/ |
367
|
|
|
public function testThrowException(string $methodName, array $args) : void |
368
|
|
|
{ |
369
|
|
|
$this->expectException(\Throwable::class); |
370
|
|
|
|
371
|
|
|
$this->queue->$methodName(...$args); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function provideFailureCallbackData() : iterable |
375
|
|
|
{ |
376
|
|
|
return [ |
377
|
|
|
['ack', [42]], |
378
|
|
|
['release', [42]], |
379
|
|
|
['peek', [42]], |
380
|
|
|
['bury', [42]], |
381
|
|
|
['kick', ['foo']], |
382
|
|
|
['delete', [42]], |
383
|
|
|
]; |
384
|
|
|
} |
385
|
|
|
} |
386
|
|
|
|