forked from clue/reactphp-redis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFunctionalTest.php
More file actions
178 lines (132 loc) · 5.23 KB
/
FunctionalTest.php
File metadata and controls
178 lines (132 loc) · 5.23 KB
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
<?php
namespace Clue\Tests\React\Redis;
use Clue\React\Redis\RedisClient;
use React\EventLoop\StreamSelectLoop;
use React\Promise\Deferred;
use React\Promise\PromiseInterface;
class FunctionalTest extends TestCase
{
/** @var StreamSelectLoop */
private $loop;
/** @var string */
private $uri;
public function setUp(): void
{
$this->uri = getenv('REDIS_URI') ?: '';
if ($this->uri === '') {
$this->markTestSkipped('No REDIS_URI environment variable given');
}
$this->loop = new StreamSelectLoop();
}
public function testPing()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$promise = $redis->ping();
$this->assertInstanceOf(PromiseInterface::class, $promise);
$ret = \React\Async\await($promise);
$this->assertEquals('PONG', $ret);
}
public function testPingLazy()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$promise = $redis->ping();
$this->assertInstanceOf(PromiseInterface::class, $promise);
$ret = \React\Async\await($promise);
$this->assertEquals('PONG', $ret);
}
/**
* @doesNotPerformAssertions
*/
public function testPingLazyWillNotBlockLoop()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$redis->ping();
$this->loop->run();
}
/**
* @doesNotPerformAssertions
*/
public function testLazyClientWithoutCommandsWillNotBlockLoop()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$this->loop->run();
unset($redis);
}
public function testMgetIsNotInterpretedAsSubMessage()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$redis->mset('message', 'message', 'channel', 'channel', 'payload', 'payload');
$promise = $redis->mget('message', 'channel', 'payload')->then($this->expectCallableOnce());
$redis->on('message', $this->expectCallableNever());
\React\Async\await($promise);
}
public function testPipeline()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$redis->set('a', 1)->then($this->expectCallableOnceWith('OK'));
$redis->incr('a')->then($this->expectCallableOnceWith(2));
$redis->incr('a')->then($this->expectCallableOnceWith(3));
$promise = $redis->get('a')->then($this->expectCallableOnceWith('3'));
\React\Async\await($promise);
}
public function testInvalidCommand()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$promise = $redis->doesnotexist(1, 2, 3);
if (method_exists($this, 'expectException')) {
$this->expectException('Exception');
} else {
$this->setExpectedException('Exception');
}
\React\Async\await($promise);
}
public function testMultiExecEmpty()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$redis->multi()->then($this->expectCallableOnceWith('OK'));
$promise = $redis->exec()->then($this->expectCallableOnceWith([]));
\React\Async\await($promise);
}
public function testMultiExecQueuedExecHasValues()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$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'));
$promise = $redis->exec()->then($this->expectCallableOnceWith(['OK', 1, 12, 20]));
\React\Async\await($promise);
}
public function testPubSub()
{
$consumer = new RedisClient($this->uri, null, $this->loop);
$producer = new RedisClient($this->uri, null, $this->loop);
$channel = 'channel:test:' . mt_rand();
// consumer receives a single message
$deferred = new Deferred();
$consumer->on('message', $this->expectCallableOnce());
$consumer->on('message', [$deferred, 'resolve']);
$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
\React\Async\await($deferred->promise());
}
public function testClose()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
$redis->close();
$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
}
public function testCloseLazy()
{
$redis = new RedisClient($this->uri, null, $this->loop);
$redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
$redis->close();
$redis->get('willBeRejectedRightAway')->then(null, $this->expectCallableOnce());
}
}