Skip to content

Commit 5e1d0b6

Browse files
committed
Enhance integration tests
1 parent c0413d4 commit 5e1d0b6

File tree

5 files changed

+106
-42
lines changed

5 files changed

+106
-42
lines changed

tests/Integration/QueueTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class QueueTest extends TestCase
1919
{
2020
/**
2121
* @dataProvider provideTaskData
22+
* @lua create_tube('%tube_name%', '%tube_type%')
2223
*
2324
* @param mixed $data
2425
*/
@@ -44,6 +45,7 @@ public function provideTaskData() : iterable
4445
}
4546

4647
/**
48+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
4749
* @lua tube:put('peek_0')
4850
*/
4951
public function testPeek() : void
@@ -54,6 +56,7 @@ public function testPeek() : void
5456
}
5557

5658
/**
59+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
5760
* @lua tube:put('take')
5861
*/
5962
public function testTake() : void
@@ -63,6 +66,9 @@ public function testTake() : void
6366
self::assertTask($task, 0, States::TAKEN, 'take');
6467
}
6568

69+
/**
70+
* @lua create_tube('%tube_name%', '%tube_type%')
71+
*/
6672
public function testTakeNone() : void
6773
{
6874
$time = time();
@@ -73,6 +79,7 @@ public function testTakeNone() : void
7379
}
7480

7581
/**
82+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
7683
* @lua tube:put('release_0')
7784
* @lua tube:take()
7885
*/
@@ -84,6 +91,7 @@ public function testRelease() : void
8491
}
8592

8693
/**
94+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
8795
* @lua tube:put('ack_0')
8896
* @lua tube:take()
8997
*/
@@ -95,6 +103,7 @@ public function testAck() : void
95103
}
96104

97105
/**
106+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
98107
* @lua tube:put('bury_0')
99108
*/
100109
public function testBury() : void
@@ -105,6 +114,7 @@ public function testBury() : void
105114
}
106115

107116
/**
117+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
108118
* @lua tube:put('kick_1')
109119
* @lua tube:bury(0)
110120
*/
@@ -116,6 +126,7 @@ public function testKickOne() : void
116126
}
117127

118128
/**
129+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
119130
* @lua tube:put('kick_1')
120131
* @lua tube:put('kick_2')
121132
* @lua tube:put('kick_3')
@@ -131,6 +142,7 @@ public function testKickMany() : void
131142
}
132143

133144
/**
145+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
134146
* @lua tube:put('delete_0')
135147
*/
136148
public function testDelete() : void
@@ -141,6 +153,7 @@ public function testDelete() : void
141153
}
142154

143155
/**
156+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
144157
* @lua tube:put('truncate_0')
145158
* @lua tube:put('truncate_1')
146159
*/
@@ -153,6 +166,9 @@ public function testTruncate() : void
153166
self::assertSame(0, $this->queue->stats('tasks.total'));
154167
}
155168

169+
/**
170+
* @lua create_tube('%tube_name%', '%tube_type%')
171+
*/
156172
public function testTruncateEmpty() : void
157173
{
158174
self::assertSame(0, $this->queue->stats('tasks.total'));
@@ -163,6 +179,7 @@ public function testTruncateEmpty() : void
163179
}
164180

165181
/**
182+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
166183
* @lua tube:put('stat_0')
167184
* @lua tube:put('stat_1')
168185
* @lua tube:put('stat_2')
@@ -207,6 +224,9 @@ public function testStats() : void
207224
], $stats);
208225
}
209226

227+
/**
228+
* @lua create_tube('%tube_name%', '%tube_type%')
229+
*/
210230
public function testEmptyStats() : void
211231
{
212232
$stats = $this->queue->stats();
@@ -237,6 +257,7 @@ public function testEmptyStats() : void
237257
}
238258

239259
/**
260+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
240261
* @lua tube:put('stat_0')
241262
* @lua tube:put('stat_1')
242263
* @lua tube:put('stat_2')
@@ -304,6 +325,7 @@ public function testStatsPath() : void
304325

305326
/**
306327
* @dataProvider provideStatsInvalidPathData
328+
* @lua create_tube('%tube_name%', '%tube_type%')
307329
*/
308330
public function testStatsInvalidPath(?string $path) : void
309331
{
@@ -329,6 +351,7 @@ public function provideStatsInvalidPathData() : iterable
329351
}
330352

331353
/**
354+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
332355
* @lua tube.pow = function(self, base, exp) return math.pow(base, exp) end
333356
*/
334357
public function testCall() : void

tests/Integration/TestCase.php

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313

1414
namespace Tarantool\Queue\Tests\Integration;
1515

16-
use PHPUnit\Framework\TestCase as BaseTestCase;
16+
use PHPUnitExtras\Annotation\AnnotationProcessorBuilder;
1717
use Tarantool\Client\Client;
18-
use Tarantool\PhpUnit\Annotation\Annotations;
18+
use Tarantool\PhpUnit\TestCase as BaseTestCase;
1919
use Tarantool\Queue\Queue;
2020
use Tarantool\Queue\Task;
2121
use Tarantool\Queue\Tests\PhpUnitCompat;
2222

2323
abstract class TestCase extends BaseTestCase
2424
{
25-
use Annotations;
2625
use PhpUnitCompat;
2726

2827
/** @var Client|null */
@@ -31,6 +30,24 @@ abstract class TestCase extends BaseTestCase
3130
/** @var Queue */
3231
protected $queue;
3332

33+
public function getQueueName() : string
34+
{
35+
$methodName = $this->getName(false);
36+
if (0 === strpos($methodName, 'test')) {
37+
$methodName = substr($methodName, 4);
38+
}
39+
40+
return sprintf('t_%s_%s', $this->getQueueType(), strtolower($methodName));
41+
}
42+
43+
public function getQueueType() : string
44+
{
45+
$class = new \ReflectionClass($this);
46+
$type = str_replace('QueueTest', '', $class->getShortName());
47+
48+
return strtolower($type);
49+
}
50+
3451
final protected function getClient() : Client
3552
{
3653
if ($this->client) {
@@ -54,54 +71,25 @@ final protected function getClient() : Client
5471
return $this->client = Client::fromDsn($dsn);
5572
}
5673

57-
/**
58-
* @before
59-
*/
60-
final protected function initQueue() : void
74+
final protected function createAnnotationProcessorBuilder() : AnnotationProcessorBuilder
6175
{
62-
$queueName = $this->getQueueName();
63-
$client = $this->getClient();
64-
$this->queue = new Queue($client, $queueName);
65-
66-
$client->evaluate(sprintf(
67-
"tube = create_tube('%s', '%s')",
68-
$queueName,
69-
$this->getQueueType()
70-
));
71-
72-
$this->processAnnotations(static::class, $this->getName(false) ?? '');
76+
return parent::createAnnotationProcessorBuilder()
77+
->addPlaceholderResolver(new TubePlaceholderResolver($this));
7378
}
7479

7580
/**
76-
* @after
81+
* @before
7782
*/
78-
final protected function destroyQueue() : void
83+
final protected function initQueue() : void
7984
{
80-
$this->getClient()->evaluate('tube = nil');
81-
$this->queue = null;
85+
$this->queue = new Queue($this->getClient(), $this->getQueueName());
8286
}
8387

8488
final protected function getQueue() : Queue
8589
{
8690
return $this->queue;
8791
}
8892

89-
protected function getQueueName() : string
90-
{
91-
$testName = preg_replace('/^test(\S+).*$/', '\1', $this->getName());
92-
$testName = strtolower($testName);
93-
94-
return sprintf('t_%s_%s', $this->getQueueType(), $testName);
95-
}
96-
97-
protected function getQueueType() : string
98-
{
99-
$class = new \ReflectionClass($this);
100-
$type = str_replace('QueueTest', '', $class->getShortName());
101-
102-
return strtolower($type);
103-
}
104-
10593
final protected static function assertTaskInstance($task) : void
10694
{
10795
self::assertInstanceOf(Task::class, $task);

tests/Integration/Ttl.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
trait Ttl
2020
{
2121
/**
22+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
2223
* @lua tube:put('ttr_1', {ttr = 1})
2324
*/
2425
public function testTimeToRun() : void
@@ -35,6 +36,7 @@ public function testTimeToRun() : void
3536
}
3637

3738
/**
39+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
3840
* @lua tube:put('ttl_1', {ttl = 1})
3941
*/
4042
public function testTimeToLive() : void
@@ -46,6 +48,7 @@ public function testTimeToLive() : void
4648
}
4749

4850
/**
51+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
4952
* @lua tube:put('touch_ttr_1', {ttr = 1})
5053
*/
5154
public function testTouchTimeToRun() : void
@@ -64,6 +67,7 @@ public function testTouchTimeToRun() : void
6467
}
6568

6669
/**
70+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
6771
* @lua tube:put('touch_ttl_1', {ttl = 1})
6872
*/
6973
public function testTouchTimeToLive() : void
@@ -83,6 +87,7 @@ public function testTouchTimeToLive() : void
8387
}
8488

8589
/**
90+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
8691
* @lua tube:put('touch_invalid_interval')
8792
*/
8893
public function testTouchInvalidInterval() : void
@@ -96,6 +101,7 @@ public function testTouchInvalidInterval() : void
96101
}
97102

98103
/**
104+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
99105
* @lua tube:put('pri_low', {pri = 2})
100106
* @lua tube:put('pri_high', {pri = 1})
101107
*/
@@ -114,6 +120,7 @@ public function testPriority() : void
114120
}
115121

116122
/**
123+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
117124
* @lua tube:put('delay_1', {delay = 1})
118125
*/
119126
public function testDelay() : void
@@ -130,6 +137,7 @@ public function testDelay() : void
130137
}
131138

132139
/**
140+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
133141
* @lua tube:put('release_0')
134142
* @lua tube:take()
135143
*/
@@ -149,6 +157,7 @@ public function testDelayedRelease() : void
149157
}
150158

151159
/**
160+
* @lua tube = create_tube('%tube_name%', '%tube_type%')
152161
* @lua tube:put('stat_delayed_0', {delay = 9999})
153162
* @lua tube:put('stat_delayed_1', {delay = 9999})
154163
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the tarantool/queue package.
5+
*
6+
* (c) Eugene Leonovich <gen.work@gmail.com>
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 PHPUnitExtras\Annotation\PlaceholderResolver\PlaceholderResolver;
17+
use PHPUnitExtras\Annotation\Target;
18+
19+
final class TubePlaceholderResolver implements PlaceholderResolver
20+
{
21+
private $testCase;
22+
23+
public function __construct(TestCase $testCase)
24+
{
25+
$this->testCase = $testCase;
26+
}
27+
28+
public function getName() : string
29+
{
30+
return 'tube';
31+
}
32+
33+
public function resolve(string $value, Target $target) : string
34+
{
35+
return strtr($value, [
36+
'%tube_name%' => $this->testCase->getQueueName(),
37+
'%tube_type%' => $this->testCase->getQueueType(),
38+
]);
39+
}
40+
}

tests/Integration/queues.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ box.schema.user.grant('guest', 'read,write,execute,create,drop,alter', 'universe
1313

1414
queue = require('queue')
1515

16-
function create_tube(tube_name, tube_type, opts)
17-
if queue.tube[tube_name] then
18-
queue.tube[tube_name]:drop()
16+
function try_drop_tube(name)
17+
if queue.tube[name] then
18+
queue.tube[name]:drop()
1919
end
20+
end
2021

21-
return queue.create_tube(tube_name, tube_type, opts)
22+
function create_tube(name, type, opts)
23+
try_drop_tube(name)
24+
return queue.create_tube(name, type, opts)
2225
end
26+

0 commit comments

Comments
 (0)