Skip to content

Commit cfd3e05

Browse files
authored
Merge pull request #149 from clue-labs/async
Update test suite to use new reactphp/async package instead of clue/reactphp-block
2 parents d4e16d3 + d1b2e0f commit cfd3e05

File tree

2 files changed

+45
-33
lines changed

2 files changed

+45
-33
lines changed

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
"react/event-loop": "^1.2",
1818
"react/promise": "^3 || ^2.0 || ^1.1",
1919
"react/promise-timer": "^1.10",
20-
"react/socket": "^1.12"
20+
"react/socket": "^1.15"
2121
},
2222
"require-dev": {
23-
"clue/block-react": "^1.5",
2423
"phpstan/phpstan": "1.10.15 || 1.4.10",
25-
"phpunit/phpunit": "^9.6 || ^7.5"
24+
"phpunit/phpunit": "^9.6 || ^7.5",
25+
"react/async": "^4.2 || ^3.2"
2626
},
2727
"autoload": {
2828
"psr-4": {

tests/FunctionalTest.php

+42-30
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
namespace Clue\Tests\React\Redis;
44

55
use Clue\React\Redis\RedisClient;
6-
use React\EventLoop\StreamSelectLoop;
6+
use React\EventLoop\Loop;
77
use React\Promise\Deferred;
88
use React\Promise\PromiseInterface;
9-
use function Clue\React\Block\await;
9+
use function React\Async\await;
10+
use function React\Promise\Timer\timeout;
1011

1112
class FunctionalTest extends TestCase
1213
{
13-
/** @var StreamSelectLoop */
14-
private $loop;
1514

1615
/** @var string */
1716
private $uri;
@@ -22,30 +21,28 @@ public function setUp(): void
2221
if ($this->uri === '') {
2322
$this->markTestSkipped('No REDIS_URI environment variable given');
2423
}
25-
26-
$this->loop = new StreamSelectLoop();
2724
}
2825

2926
public function testPing(): void
3027
{
31-
$redis = new RedisClient($this->uri, null, $this->loop);
28+
$redis = new RedisClient($this->uri);
3229

30+
/** @var PromiseInterface<string> */
3331
$promise = $redis->ping();
34-
$this->assertInstanceOf(PromiseInterface::class, $promise);
3532

36-
$ret = await($promise, $this->loop);
33+
$ret = await($promise);
3734

3835
$this->assertEquals('PONG', $ret);
3936
}
4037

4138
public function testPingLazy(): void
4239
{
43-
$redis = new RedisClient($this->uri, null, $this->loop);
40+
$redis = new RedisClient($this->uri);
4441

42+
/** @var PromiseInterface<string> */
4543
$promise = $redis->ping();
46-
$this->assertInstanceOf(PromiseInterface::class, $promise);
4744

48-
$ret = await($promise, $this->loop);
45+
$ret = await($promise);
4946

5047
$this->assertEquals('PONG', $ret);
5148
}
@@ -55,89 +52,99 @@ public function testPingLazy(): void
5552
*/
5653
public function testPingLazyWillNotBlockLoop(): void
5754
{
58-
$redis = new RedisClient($this->uri, null, $this->loop);
55+
$redis = new RedisClient($this->uri);
5956

6057
$redis->ping();
6158

62-
$this->loop->run();
59+
Loop::run();
6360
}
6461

6562
/**
6663
* @doesNotPerformAssertions
6764
*/
6865
public function testLazyClientWithoutCommandsWillNotBlockLoop(): void
6966
{
70-
$redis = new RedisClient($this->uri, null, $this->loop);
67+
$redis = new RedisClient($this->uri);
7168

72-
$this->loop->run();
69+
Loop::run();
7370

7471
unset($redis);
7572
}
7673

7774
public function testMgetIsNotInterpretedAsSubMessage(): void
7875
{
79-
$redis = new RedisClient($this->uri, null, $this->loop);
76+
$redis = new RedisClient($this->uri);
8077

8178
$redis->mset('message', 'message', 'channel', 'channel', 'payload', 'payload');
8279

80+
/** @var PromiseInterface<never> */
8381
$promise = $redis->mget('message', 'channel', 'payload')->then($this->expectCallableOnce());
8482
$redis->on('message', $this->expectCallableNever());
8583

86-
await($promise, $this->loop);
84+
await($promise);
8785
}
8886

8987
public function testPipeline(): void
9088
{
91-
$redis = new RedisClient($this->uri, null, $this->loop);
89+
$redis = new RedisClient($this->uri);
9290

9391
$redis->set('a', 1)->then($this->expectCallableOnceWith('OK'));
9492
$redis->incr('a')->then($this->expectCallableOnceWith(2));
9593
$redis->incr('a')->then($this->expectCallableOnceWith(3));
94+
95+
/** @var PromiseInterface<void> */
9696
$promise = $redis->get('a')->then($this->expectCallableOnceWith('3'));
9797

98-
await($promise, $this->loop);
98+
await($promise);
9999
}
100100

101101
public function testInvalidCommand(): void
102102
{
103-
$redis = new RedisClient($this->uri, null, $this->loop);
103+
$redis = new RedisClient($this->uri);
104+
105+
/** @var PromiseInterface<never> */
104106
$promise = $redis->doesnotexist(1, 2, 3);
105107

106108
$this->expectException(\Exception::class);
107-
await($promise, $this->loop);
109+
await($promise);
108110
}
109111

110112
public function testMultiExecEmpty(): void
111113
{
112-
$redis = new RedisClient($this->uri, null, $this->loop);
114+
$redis = new RedisClient($this->uri);
113115
$redis->multi()->then($this->expectCallableOnceWith('OK'));
116+
117+
/** @var PromiseInterface<void> */
114118
$promise = $redis->exec()->then($this->expectCallableOnceWith([]));
115119

116-
await($promise, $this->loop);
120+
await($promise);
117121
}
118122

119123
public function testMultiExecQueuedExecHasValues(): void
120124
{
121-
$redis = new RedisClient($this->uri, null, $this->loop);
125+
$redis = new RedisClient($this->uri);
122126

123127
$redis->multi()->then($this->expectCallableOnceWith('OK'));
124128
$redis->set('b', 10)->then($this->expectCallableOnceWith('QUEUED'));
125129
$redis->expire('b', 20)->then($this->expectCallableOnceWith('QUEUED'));
126130
$redis->incrBy('b', 2)->then($this->expectCallableOnceWith('QUEUED'));
127131
$redis->ttl('b')->then($this->expectCallableOnceWith('QUEUED'));
132+
133+
/** @var PromiseInterface<void> */
128134
$promise = $redis->exec()->then($this->expectCallableOnceWith(['OK', 1, 12, 20]));
129135

130-
await($promise, $this->loop);
136+
await($promise);
131137
}
132138

133139
public function testPubSub(): void
134140
{
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);
137143

138144
$channel = 'channel:test:' . mt_rand();
139145

140146
// consumer receives a single message
147+
/** @var Deferred<void> */
141148
$deferred = new Deferred();
142149
$consumer->on('message', $this->expectCallableOnce());
143150
$consumer->on('message', [$deferred, 'resolve']);
@@ -148,12 +155,17 @@ public function testPubSub(): void
148155
})->then($this->expectCallableOnce());
149156

150157
// 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);
152164
}
153165

154166
public function testClose(): void
155167
{
156-
$redis = new RedisClient($this->uri, null, $this->loop);
168+
$redis = new RedisClient($this->uri);
157169

158170
$redis->get('willBeCanceledAnyway')->then(null, $this->expectCallableOnce());
159171

0 commit comments

Comments
 (0)