forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctionalTest.php
179 lines (134 loc) · 5.1 KB
/
FunctionalTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
namespace Clue\Tests\React\Redis;
use Clue\React\Redis\RedisClient;
use React\EventLoop\Loop;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use function React\Async\await;
class FunctionalTest extends TestCase
{
/** @var string */
private $uri;
public function setUp(): void
{
$this->uri = getenv('REDIS_URI') ?: '';
if ($this->uri === '') {
$this->markTestSkipped('No REDIS_URI environment variable given');
}
}
public function testPing(): void
{
$redis = new RedisClient($this->uri);
/** @var PromiseInterface<string> */
$promise = $redis->ping();
$ret = await($promise);
$this->assertEquals('PONG', $ret);
}
public function testPingLazy(): void
{
$redis = new RedisClient($this->uri);
/** @var PromiseInterface<string> */
$promise = $redis->ping();
$ret = await($promise);
$this->assertEquals('PONG', $ret);
}
/**
* @doesNotPerformAssertions
*/
public function testPingLazyWillNotBlockLoop(): void
{
$redis = new RedisClient($this->uri);
$redis->ping();
Loop::run();
}
/**
* @doesNotPerformAssertions
*/
public function testLazyClientWithoutCommandsWillNotBlockLoop(): void
{
$redis = new RedisClient($this->uri);
Loop::run();
unset($redis);
}
public function testMgetIsNotInterpretedAsSubMessage(): void
{
$redis = new RedisClient($this->uri);
$redis->mset('message', 'message', 'channel', 'channel', 'payload', 'payload');
/** @var PromiseInterface<never> */
$promise = $redis->mget('message', 'channel', 'payload')->then($this->expectCallableOnce());
$redis->on('message', $this->expectCallableNever());
await($promise);
}
public function testPipeline(): void
{
$redis = new RedisClient($this->uri);
$redis->set('a', 1)->then($this->expectCallableOnceWith('OK'));
$redis->incr('a')->then($this->expectCallableOnceWith(2));
$redis->incr('a')->then($this->expectCallableOnceWith(3));
/** @var PromiseInterface<void> */
$promise = $redis->get('a')->then($this->expectCallableOnceWith('3'));
await($promise);
}
public function testInvalidCommand(): void
{
$redis = new RedisClient($this->uri);
/** @var PromiseInterface<never> */
$promise = $redis->doesnotexist(1, 2, 3);
$this->expectException(\Exception::class);
await($promise);
}
public function testMultiExecEmpty(): void
{
$redis = new RedisClient($this->uri);
$redis->multi()->then($this->expectCallableOnceWith('OK'));
/** @var PromiseInterface<void> */
$promise = $redis->exec()->then($this->expectCallableOnceWith([]));
await($promise);
}
public function testMultiExecQueuedExecHasValues(): void
{
$redis = new RedisClient($this->uri);
$redis->multi()->then($this->expectCallableOnceWith('OK'));
$redis->set('b', 10)->then($this->expectCallableOnceWith('QUEUED'));
$redis->expire('b', 20)->then($this->expectCallableOnceWith('QUEUED'));
$redis->incrBy('b', 2)->then($this->expectCallableOnceWith('QUEUED'));
$redis->ttl('b')->then($this->expectCallableOnceWith('QUEUED'));
/** @var PromiseInterface<void> */
$promise = $redis->exec()->then($this->expectCallableOnceWith(['OK', 1, 12, 20]));
await($promise);
}
public function testPubSub(): void
{
$consumer = new RedisClient($this->uri);
$producer = new RedisClient($this->uri);
$channel = 'channel:test:' . mt_rand();
// consumer receives a single message
$consumer->on('message', $this->expectCallableOnce());
$once = $this->expectCallableOnceWith(1);
$consumer->subscribe($channel)->then(function() use ($producer, $channel, $once){
// producer sends a single message
$producer->publish($channel, 'hello world')->then($once);
})->then($this->expectCallableOnce());
// expect "message" event to take no longer than 0.1s
await(new Promise(function (callable $resolve, callable $reject) use ($consumer): void {
$timeout = Loop::addTimer(0.1, function () use ($consumer, $reject): void {
$consumer->close();
$reject(new \RuntimeException('Timed out'));
});
$consumer->on('message', function () use ($timeout, $resolve): void {
Loop::cancelTimer($timeout);
$resolve(null);
});
}));
/** @var PromiseInterface<array{0:"unsubscribe",1:string,2:0}> */
$promise = $consumer->unsubscribe($channel);
await($promise);
}
public function testClose(): void
{
$redis = new RedisClient($this->uri);
$redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
$redis->close();
$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
}
}