11<?php
22
33use React \Stream \Stream ;
4-
54use React \Stream \ReadableStream ;
6-
75use Clue \React \Redis \Factory ;
8-
96use Clue \React \Redis \StreamingClient ;
7+ use React \Promise \Deferred ;
8+ use Clue \React \Block ;
109
1110class FunctionalTest extends TestCase
1211{
13- protected static $ loop ;
14- protected static $ factory ;
12+ private $ loop ;
13+ private $ factory ;
14+ private $ client ;
1515
16- public static function setUpBeforeClass ()
16+ public function setUp ()
1717 {
18- self ::$ loop = new React \EventLoop \StreamSelectLoop ();
19- self ::$ factory = new Factory (self ::$ loop );
18+ $ this ->loop = new React \EventLoop \StreamSelectLoop ();
19+ $ this ->factory = new Factory ($ this ->loop );
20+ $ this ->client = $ this ->createClient ();
2021 }
2122
2223 public function testPing ()
2324 {
24- $ client = $ this ->createClient () ;
25+ $ client = $ this ->client ;
2526
2627 $ promise = $ client ->ping ();
2728 $ this ->assertInstanceOf ('React\Promise\PromiseInterface ' , $ promise );
@@ -36,7 +37,7 @@ public function testPing()
3637
3738 public function testMgetIsNotInterpretedAsSubMessage ()
3839 {
39- $ client = $ this ->createClient () ;
40+ $ client = $ this ->client ;
4041
4142 $ client ->mset ('message ' , 'message ' , 'channel ' , 'channel ' , 'payload ' , 'payload ' );
4243
@@ -46,14 +47,9 @@ public function testMgetIsNotInterpretedAsSubMessage()
4647 $ this ->waitFor ($ client );
4748 }
4849
49- /**
50- *
51- * @param StreamingClient $client
52- * @depends testPing
53- */
54- public function testPipeline (StreamingClient $ client )
50+ public function testPipeline ()
5551 {
56- $ this ->assertFalse ( $ client-> isBusy ()) ;
52+ $ client = $ this ->client ;
5753
5854 $ client ->set ('a ' , 1 )->then ($ this ->expectCallableOnce ('OK ' ));
5955 $ client ->incr ('a ' )->then ($ this ->expectCallableOnce (2 ));
@@ -63,46 +59,27 @@ public function testPipeline(StreamingClient $client)
6359 $ this ->assertTrue ($ client ->isBusy ());
6460
6561 $ this ->waitFor ($ client );
66-
67- return $ client ;
6862 }
6963
70- /**
71- *
72- * @param StreamingClient $client
73- * @depends testPipeline
74- */
75- public function testInvalidCommand (StreamingClient $ client )
64+ public function testInvalidCommand ()
7665 {
77- $ client ->doesnotexist (1 , 2 , 3 )->then ($ this ->expectCallableNever ());
78-
79- $ this ->waitFor ($ client );
66+ $ this ->client ->doesnotexist (1 , 2 , 3 )->then ($ this ->expectCallableNever ());
8067
81- return $ client ;
68+ $ this -> waitFor ( $ this -> client ) ;
8269 }
8370
84- /**
85- *
86- * @param StreamingClient $client
87- * @depends testInvalidCommand
88- */
89- public function testMultiExecEmpty (StreamingClient $ client )
71+ public function testMultiExecEmpty ()
9072 {
91- $ client ->multi ()->then ($ this ->expectCallableOnce ('OK ' ));
92- $ client ->exec ()->then ($ this ->expectCallableOnce (array ()));
93-
94- $ this ->waitFor ($ client );
73+ $ this ->client ->multi ()->then ($ this ->expectCallableOnce ('OK ' ));
74+ $ this ->client ->exec ()->then ($ this ->expectCallableOnce (array ()));
9575
96- return $ client ;
76+ $ this -> waitFor ( $ this -> client ) ;
9777 }
9878
99- /**
100- *
101- * @param StreamingClient $client
102- * @depends testMultiExecEmpty
103- */
104- public function testMultiExecQueuedExecHasValues (StreamingClient $ client )
79+ public function testMultiExecQueuedExecHasValues ()
10580 {
81+ $ client = $ this ->client ;
82+
10683 $ client ->multi ()->then ($ this ->expectCallableOnce ('OK ' ));
10784 $ client ->set ('b ' , 10 )->then ($ this ->expectCallableOnce ('QUEUED ' ));
10885 $ client ->expire ('b ' , 20 )->then ($ this ->expectCallableOnce ('QUEUED ' ));
@@ -111,17 +88,12 @@ public function testMultiExecQueuedExecHasValues(StreamingClient $client)
11188 $ client ->exec ()->then ($ this ->expectCallableOnce (array ('OK ' , 1 , 12 , 20 )));
11289
11390 $ this ->waitFor ($ client );
114-
115- return $ client ;
11691 }
11792
118- /**
119- *
120- * @param StreamingClient $client
121- * @depends testPipeline
122- */
123- public function testMonitorPing (StreamingClient $ client )
93+ public function testMonitorPing ()
12494 {
95+ $ client = $ this ->client ;
96+
12597 $ client ->on ('monitor ' , $ this ->expectCallableOnce ());
12698
12799 $ client ->monitor ()->then ($ this ->expectCallableOnce ('OK ' ));
@@ -132,29 +104,33 @@ public function testMonitorPing(StreamingClient $client)
132104
133105 public function testPubSub ()
134106 {
135- $ consumer = $ this ->createClient () ;
107+ $ consumer = $ this ->client ;
136108 $ producer = $ this ->createClient ();
137109
138- $ that = $ this ;
110+ $ channel = ' channel:test: ' . mt_rand () ;
139111
140- $ producer ->publish ('channel:test ' , 'nobody sees this ' )->then ($ this ->expectCallableOnce (0 ));
112+ // consumer receives a single message
113+ $ deferred = new Deferred ();
114+ $ consumer ->on ('message ' , $ this ->expectCallableOnce ());
115+ $ consumer ->on ('message ' , array ($ deferred , 'resolve ' ));
116+ $ consumer ->subscribe ($ channel )->then ($ this ->expectCallableOnce ());
117+ $ this ->waitFor ($ consumer );
141118
119+ // producer sends a single message
120+ $ producer ->publish ($ channel , 'hello world ' )->then ($ this ->expectCallableOnce ());
142121 $ this ->waitFor ($ producer );
143122
144- $ consumer ->subscribe ('channel:test ' )->then (function () {
145- // ?
146- });
123+ // expect "message" event to take no longer than 0.1s
124+ Block \await ($ deferred ->promise (), $ this ->loop , 0.1 );
147125 }
148126
149127 public function testClose ()
150128 {
151- $ client = $ this ->createClient ( );
129+ $ this -> client -> get ( ' willBeCanceledAnyway ' )-> then ( null , $ this ->expectCallableOnce () );
152130
153- $ client -> get ( ' willBeCanceledAnyway ' )-> then ( null , $ this -> expectCallableOnce () );
131+ $ this -> client -> close ( );
154132
155- $ client ->close ();
156-
157- $ client ->get ('willBeRejectedRightAway ' )->then (null , $ this ->expectCallableOnce ());
133+ $ this ->client ->get ('willBeRejectedRightAway ' )->then (null , $ this ->expectCallableOnce ());
158134 }
159135
160136 public function testInvalidProtocol ()
@@ -186,24 +162,7 @@ public function testInvalidServerRepliesWithDuplicateMessages()
186162 */
187163 protected function createClient ()
188164 {
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 ;
165+ return Block \await ($ this ->factory ->createClient (), $ this ->loop );
207166 }
208167
209168 protected function createClientResponse ($ response )
@@ -212,7 +171,7 @@ protected function createClientResponse($response)
212171 fwrite ($ fp , $ response );
213172 fseek ($ fp , 0 );
214173
215- $ stream = new Stream ($ fp , self :: $ loop );
174+ $ stream = new Stream ($ fp , $ this -> loop );
216175
217176 return new StreamingClient ($ stream );
218177 }
@@ -229,7 +188,7 @@ protected function waitFor(StreamingClient $client)
229188 $ this ->assertTrue ($ client ->isBusy ());
230189
231190 while ($ client ->isBusy ()) {
232- self :: $ loop ->tick ();
191+ $ this -> loop ->tick ();
233192 }
234193 }
235194}
0 commit comments