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