Skip to content

Commit e9464d8

Browse files
committed
More reliable tests by blocking
1 parent 7f80b09 commit e9464d8

File tree

3 files changed

+18
-27
lines changed

3 files changed

+18
-27
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
},
2121
"autoload": {
2222
"psr-4": { "Clue\\React\\Redis\\": "src/" }
23+
},
24+
"require-dev": {
25+
"clue/block-react": "^1.1"
2326
}
2427
}

tests/FunctionalTest.php

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<?php
22

33
use React\Stream\Stream;
4-
54
use React\Stream\ReadableStream;
6-
75
use Clue\React\Redis\Factory;
8-
96
use Clue\React\Redis\StreamingClient;
7+
use React\Promise\Deferred;
8+
use Clue\React\Block;
109

1110
class FunctionalTest extends TestCase
1211
{
@@ -135,15 +134,21 @@ public function testPubSub()
135134
$consumer = $this->createClient();
136135
$producer = $this->createClient();
137136

138-
$that = $this;
137+
$channel = 'channel:test:' . mt_rand();
139138

140-
$producer->publish('channel:test', 'nobody sees this')->then($this->expectCallableOnce(0));
139+
// consumer receives a single message
140+
$deferred = new Deferred();
141+
$consumer->on('message', $this->expectCallableOnce());
142+
$consumer->on('message', array($deferred, 'resolve'));
143+
$consumer->subscribe($channel)->then($this->expectCallableOnce());
144+
$this->waitFor($consumer);
141145

146+
// producer sends a single message
147+
$producer->publish($channel, 'hello world')->then($this->expectCallableOnce());
142148
$this->waitFor($producer);
143149

144-
$consumer->subscribe('channel:test')->then(function () {
145-
// ?
146-
});
150+
// expect "message" event to take no longer than 0.1s
151+
Block\await($deferred->promise(), self::$loop, 0.1);
147152
}
148153

149154
public function testClose()
@@ -186,24 +191,7 @@ public function testInvalidServerRepliesWithDuplicateMessages()
186191
*/
187192
protected function createClient()
188193
{
189-
$client = null;
190-
$exception = null;
191-
192-
self::$factory->createClient()->then(function ($c) use (&$client) {
193-
$client = $c;
194-
}, function($error) use (&$exception) {
195-
$exception = $error;
196-
});
197-
198-
while ($client === null && $exception === null) {
199-
self::$loop->tick();
200-
}
201-
202-
if ($exception !== null) {
203-
throw $exception;
204-
}
205-
206-
return $client;
194+
return Block\await(self::$factory->createClient(), self::$loop);
207195
}
208196

209197
protected function createClientResponse($response)

tests/StreamingClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use Clue\React\Redis\Client;
1010
use Clue\Redis\Protocol\Model\StatusReply;
1111

12-
class ClientTest extends TestCase
12+
class StreamingClientTest extends TestCase
1313
{
1414
private $stream;
1515
private $parser;

0 commit comments

Comments
 (0)