88use React \Promise \Promise ;
99use React \Socket \ConnectionInterface ;
1010use React \Socket \SecureConnector ;
11+ use React \Socket \ServerInterface ;
1112use React \Socket \SecureServer ;
1213use React \Socket \TcpServer ;
1314use React \Socket \TcpConnector ;
14- use React \Socket \ServerInterface ;
1515
1616class FunctionalSecureServerTest extends TestCase
1717{
@@ -184,15 +184,14 @@ public function testServerEmitsConnectionForClientConnection()
184184 $ server ->close ();
185185 }
186186
187- public function testWritesDataToConnection ()
187+ public function testClientEmitsDataEventOnceForDataWrittenFromServer ()
188188 {
189189 $ loop = Factory::create ();
190190
191191 $ server = new TcpServer (0 , $ loop );
192192 $ server = new SecureServer ($ server , $ loop , array (
193193 'local_cert ' => __DIR__ . '/../examples/localhost.pem '
194194 ));
195- $ server ->on ('connection ' , $ this ->expectCallableOnce ());
196195
197196 $ server ->on ('connection ' , function (ConnectionInterface $ conn ) {
198197 $ conn ->write ('foo ' );
@@ -203,12 +202,15 @@ public function testWritesDataToConnection()
203202 ));
204203 $ promise = $ connector ->connect ($ server ->getAddress ());
205204
206- $ local = Block \await ($ promise , $ loop , self ::TIMEOUT );
207- /* @var $local ConnectionInterface */
205+ $ promise = new Promise (function ($ resolve , $ reject ) use ($ promise ) {
206+ $ promise ->then (function (ConnectionInterface $ connection ) use ($ resolve ) {
207+ $ connection ->on ('data ' , $ resolve );
208+ }, $ reject );
209+ });
208210
209- $ local -> on ( ' data ' , $ this -> expectCallableOnceWith ( ' foo ' ) );
211+ $ data = Block \await ( $ promise , $ loop , self :: TIMEOUT );
210212
211- Block \sleep ( self :: TIMEOUT , $ loop );
213+ $ this -> assertEquals ( ' foo ' , $ data );
212214 }
213215
214216 public function testWritesDataInMultipleChunksToConnection ()
@@ -230,15 +232,20 @@ public function testWritesDataInMultipleChunksToConnection()
230232 ));
231233 $ promise = $ connector ->connect ($ server ->getAddress ());
232234
233- $ local = Block \await ($ promise , $ loop , self ::TIMEOUT );
234- /* @var $local ConnectionInterface */
235-
236- $ received = 0 ;
237- $ local ->on ('data ' , function ($ chunk ) use (&$ received ) {
238- $ received += strlen ($ chunk );
235+ $ promise = new Promise (function ($ resolve , $ reject ) use ($ promise ) {
236+ $ promise ->then (function (ConnectionInterface $ connection ) use ($ resolve ) {
237+ $ received = 0 ;
238+ $ connection ->on ('data ' , function ($ chunk ) use (&$ received , $ resolve ) {
239+ $ received += strlen ($ chunk );
240+
241+ if ($ received >= 400000 ) {
242+ $ resolve ($ received );
243+ }
244+ });
245+ }, $ reject );
239246 });
240247
241- Block \sleep ( self :: TIMEOUT , $ loop );
248+ $ received = Block \await ( $ promise , $ loop, self :: TIMEOUT );
242249
243250 $ this ->assertEquals (400000 , $ received );
244251 }
@@ -262,15 +269,20 @@ public function testWritesMoreDataInMultipleChunksToConnection()
262269 ));
263270 $ promise = $ connector ->connect ($ server ->getAddress ());
264271
265- $ local = Block \await ($ promise , $ loop , self ::TIMEOUT );
266- /* @var $local ConnectionInterface */
267-
268- $ received = 0 ;
269- $ local ->on ('data ' , function ($ chunk ) use (&$ received ) {
270- $ received += strlen ($ chunk );
272+ $ promise = new Promise (function ($ resolve , $ reject ) use ($ promise ) {
273+ $ promise ->then (function (ConnectionInterface $ connection ) use ($ resolve ) {
274+ $ received = 0 ;
275+ $ connection ->on ('data ' , function ($ chunk ) use (&$ received , $ resolve ) {
276+ $ received += strlen ($ chunk );
277+
278+ if ($ received >= 2000000 ) {
279+ $ resolve ($ received );
280+ }
281+ });
282+ }, $ reject );
271283 });
272284
273- Block \sleep ( self :: TIMEOUT , $ loop );
285+ $ received = Block \await ( $ promise , $ loop, self :: TIMEOUT );
274286
275287 $ this ->assertEquals (2000000 , $ received );
276288 }
@@ -285,22 +297,22 @@ public function testEmitsDataFromConnection()
285297 ));
286298 $ server ->on ('connection ' , $ this ->expectCallableOnce ());
287299
288- $ once = $ this ->expectCallableOnceWith ('foo ' );
289- $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use ($ once ) {
290- $ conn ->on ('data ' , $ once );
300+ $ promise = new Promise (function ($ resolve , $ reject ) use ($ server ) {
301+ $ server ->on ('connection ' , function (ConnectionInterface $ connection ) use ($ resolve ) {
302+ $ connection ->on ('data ' , $ resolve );
303+ });
291304 });
292305
293306 $ connector = new SecureConnector (new TcpConnector ($ loop ), $ loop , array (
294307 'verify_peer ' => false
295308 ));
296- $ promise = $ connector ->connect ($ server ->getAddress ());
297-
298- $ local = Block \await ($ promise , $ loop , self ::TIMEOUT );
299- /* @var $local ConnectionInterface */
309+ $ connector ->connect ($ server ->getAddress ())->then (function (ConnectionInterface $ connection ) {
310+ $ connection ->write ('foo ' );
311+ });
300312
301- $ local -> write ( " foo " );
313+ $ data = Block \await ( $ promise , $ loop , self :: TIMEOUT );
302314
303- Block \sleep ( self :: TIMEOUT , $ loop );
315+ $ this -> assertEquals ( ' foo ' , $ data );
304316 }
305317
306318 public function testEmitsDataInMultipleChunksFromConnection ()
@@ -313,24 +325,27 @@ public function testEmitsDataInMultipleChunksFromConnection()
313325 ));
314326 $ server ->on ('connection ' , $ this ->expectCallableOnce ());
315327
316- $ received = 0 ;
317- $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (&$ received ) {
318- $ conn ->on ('data ' , function ($ chunk ) use (&$ received ) {
319- $ received += strlen ($ chunk );
328+ $ promise = new Promise (function ($ resolve , $ reject ) use ($ server ) {
329+ $ server ->on ('connection ' , function (ConnectionInterface $ connection ) use ($ resolve ) {
330+ $ received = 0 ;
331+ $ connection ->on ('data ' , function ($ chunk ) use (&$ received , $ resolve ) {
332+ $ received += strlen ($ chunk );
333+
334+ if ($ received >= 400000 ) {
335+ $ resolve ($ received );
336+ }
337+ });
320338 });
321339 });
322340
323341 $ connector = new SecureConnector (new TcpConnector ($ loop ), $ loop , array (
324342 'verify_peer ' => false
325343 ));
326- $ promise = $ connector ->connect ($ server ->getAddress ());
327-
328- $ local = Block \await ($ promise , $ loop , self ::TIMEOUT );
329- /* @var $local ConnectionInterface */
330-
331- $ local ->write (str_repeat ('* ' , 400000 ));
344+ $ connector ->connect ($ server ->getAddress ())->then (function (ConnectionInterface $ connection ) {
345+ $ connection ->write (str_repeat ('* ' , 400000 ));
346+ });
332347
333- Block \sleep ( self :: TIMEOUT , $ loop );
348+ $ received = Block \await ( $ promise , $ loop, self :: TIMEOUT );
334349
335350 $ this ->assertEquals (400000 , $ received );
336351 }
@@ -345,7 +360,7 @@ public function testPipesDataBackInMultipleChunksFromConnection()
345360 ));
346361 $ server ->on ('connection ' , $ this ->expectCallableOnce ());
347362
348- $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (& $ received ) {
363+ $ server ->on ('connection ' , function (ConnectionInterface $ conn ) {
349364 $ conn ->pipe ($ conn );
350365 });
351366
@@ -354,17 +369,21 @@ public function testPipesDataBackInMultipleChunksFromConnection()
354369 ));
355370 $ promise = $ connector ->connect ($ server ->getAddress ());
356371
357- $ local = Block \await ($ promise , $ loop , self ::TIMEOUT );
358- /* @var $local ConnectionInterface */
359-
360- $ received = 0 ;
361- $ local ->on ('data ' , function ($ chunk ) use (&$ received ) {
362- $ received += strlen ($ chunk );
372+ $ promise = new Promise (function ($ resolve , $ reject ) use ($ promise ) {
373+ $ promise ->then (function (ConnectionInterface $ connection ) use ($ resolve ) {
374+ $ received = 0 ;
375+ $ connection ->on ('data ' , function ($ chunk ) use (&$ received , $ resolve ) {
376+ $ received += strlen ($ chunk );
377+
378+ if ($ received >= 400000 ) {
379+ $ resolve ($ received );
380+ }
381+ });
382+ $ connection ->write (str_repeat ('* ' , 400000 ));
383+ }, $ reject );
363384 });
364385
365- $ local ->write (str_repeat ('* ' , 400000 ));
366-
367- Block \sleep (self ::TIMEOUT , $ loop );
386+ $ received = Block \await ($ promise , $ loop , self ::TIMEOUT );
368387
369388 $ this ->assertEquals (400000 , $ received );
370389 }
@@ -627,7 +646,7 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake()
627646 $ this ->assertNull ($ error ->getPrevious ());
628647 }
629648
630- public function testEmitsNothingIfConnectionIsIdle ()
649+ public function testEmitsNothingIfPlaintextConnectionIsIdle ()
631650 {
632651 $ loop = Factory::create ();
633652
@@ -641,8 +660,8 @@ public function testEmitsNothingIfConnectionIsIdle()
641660 $ connector = new TcpConnector ($ loop );
642661 $ promise = $ connector ->connect (str_replace ('tls:// ' , '' , $ server ->getAddress ()));
643662
644- $ promise -> then ( $ this -> expectCallableOnce () );
645- Block \sleep ( self :: TIMEOUT , $ loop );
663+ $ connection = Block \await ( $ promise , $ loop , self :: TIMEOUT );
664+ $ this -> assertInstanceOf ( ' React\Socket\ConnectionInterface ' , $ connection );
646665 }
647666
648667 public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake ()
@@ -705,16 +724,9 @@ public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandsh
705724
706725 private function createPromiseForServerError (ServerInterface $ server )
707726 {
708- return $ this ->createPromiseForEvent ($ server , 'error ' , function ($ error ) {
709- return $ error ;
710- });
711- }
712-
713- private function createPromiseForEvent (EventEmitterInterface $ emitter , $ event , $ fn )
714- {
715- return new Promise (function ($ resolve ) use ($ emitter , $ event , $ fn ) {
716- $ emitter ->on ($ event , function () use ($ resolve , $ fn ) {
717- $ resolve (call_user_func_array ($ fn , func_get_args ()));
727+ return new Promise (function ($ resolve ) use ($ server ) {
728+ $ server ->on ('error ' , function ($ arg ) use ($ resolve ) {
729+ $ resolve ($ arg );
718730 });
719731 });
720732 }
0 commit comments