8
8
use React \Promise \Promise ;
9
9
use React \Socket \ConnectionInterface ;
10
10
use React \Socket \SecureConnector ;
11
+ use React \Socket \ServerInterface ;
11
12
use React \Socket \SecureServer ;
12
13
use React \Socket \TcpServer ;
13
14
use React \Socket \TcpConnector ;
14
- use React \Socket \ServerInterface ;
15
15
16
16
class FunctionalSecureServerTest extends TestCase
17
17
{
@@ -184,15 +184,14 @@ public function testServerEmitsConnectionForClientConnection()
184
184
$ server ->close ();
185
185
}
186
186
187
- public function testWritesDataToConnection ()
187
+ public function testClientEmitsDataEventOnceForDataWrittenFromServer ()
188
188
{
189
189
$ loop = Factory::create ();
190
190
191
191
$ server = new TcpServer (0 , $ loop );
192
192
$ server = new SecureServer ($ server , $ loop , array (
193
193
'local_cert ' => __DIR__ . '/../examples/localhost.pem '
194
194
));
195
- $ server ->on ('connection ' , $ this ->expectCallableOnce ());
196
195
197
196
$ server ->on ('connection ' , function (ConnectionInterface $ conn ) {
198
197
$ conn ->write ('foo ' );
@@ -203,12 +202,15 @@ public function testWritesDataToConnection()
203
202
));
204
203
$ promise = $ connector ->connect ($ server ->getAddress ());
205
204
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
+ });
208
210
209
- $ local -> on ( ' data ' , $ this -> expectCallableOnceWith ( ' foo ' ) );
211
+ $ data = Block \await ( $ promise , $ loop , self :: TIMEOUT );
210
212
211
- Block \sleep ( self :: TIMEOUT , $ loop );
213
+ $ this -> assertEquals ( ' foo ' , $ data );
212
214
}
213
215
214
216
public function testWritesDataInMultipleChunksToConnection ()
@@ -230,15 +232,20 @@ public function testWritesDataInMultipleChunksToConnection()
230
232
));
231
233
$ promise = $ connector ->connect ($ server ->getAddress ());
232
234
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 );
239
246
});
240
247
241
- Block \sleep ( self :: TIMEOUT , $ loop );
248
+ $ received = Block \await ( $ promise , $ loop, self :: TIMEOUT );
242
249
243
250
$ this ->assertEquals (400000 , $ received );
244
251
}
@@ -262,15 +269,20 @@ public function testWritesMoreDataInMultipleChunksToConnection()
262
269
));
263
270
$ promise = $ connector ->connect ($ server ->getAddress ());
264
271
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 );
271
283
});
272
284
273
- Block \sleep ( self :: TIMEOUT , $ loop );
285
+ $ received = Block \await ( $ promise , $ loop, self :: TIMEOUT );
274
286
275
287
$ this ->assertEquals (2000000 , $ received );
276
288
}
@@ -285,22 +297,22 @@ public function testEmitsDataFromConnection()
285
297
));
286
298
$ server ->on ('connection ' , $ this ->expectCallableOnce ());
287
299
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
+ });
291
304
});
292
305
293
306
$ connector = new SecureConnector (new TcpConnector ($ loop ), $ loop , array (
294
307
'verify_peer ' => false
295
308
));
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
+ });
300
312
301
- $ local -> write ( " foo " );
313
+ $ data = Block \await ( $ promise , $ loop , self :: TIMEOUT );
302
314
303
- Block \sleep ( self :: TIMEOUT , $ loop );
315
+ $ this -> assertEquals ( ' foo ' , $ data );
304
316
}
305
317
306
318
public function testEmitsDataInMultipleChunksFromConnection ()
@@ -313,24 +325,27 @@ public function testEmitsDataInMultipleChunksFromConnection()
313
325
));
314
326
$ server ->on ('connection ' , $ this ->expectCallableOnce ());
315
327
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
+ });
320
338
});
321
339
});
322
340
323
341
$ connector = new SecureConnector (new TcpConnector ($ loop ), $ loop , array (
324
342
'verify_peer ' => false
325
343
));
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
+ });
332
347
333
- Block \sleep ( self :: TIMEOUT , $ loop );
348
+ $ received = Block \await ( $ promise , $ loop, self :: TIMEOUT );
334
349
335
350
$ this ->assertEquals (400000 , $ received );
336
351
}
@@ -345,7 +360,7 @@ public function testPipesDataBackInMultipleChunksFromConnection()
345
360
));
346
361
$ server ->on ('connection ' , $ this ->expectCallableOnce ());
347
362
348
- $ server ->on ('connection ' , function (ConnectionInterface $ conn ) use (& $ received ) {
363
+ $ server ->on ('connection ' , function (ConnectionInterface $ conn ) {
349
364
$ conn ->pipe ($ conn );
350
365
});
351
366
@@ -354,17 +369,21 @@ public function testPipesDataBackInMultipleChunksFromConnection()
354
369
));
355
370
$ promise = $ connector ->connect ($ server ->getAddress ());
356
371
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 );
363
384
});
364
385
365
- $ local ->write (str_repeat ('* ' , 400000 ));
366
-
367
- Block \sleep (self ::TIMEOUT , $ loop );
386
+ $ received = Block \await ($ promise , $ loop , self ::TIMEOUT );
368
387
369
388
$ this ->assertEquals (400000 , $ received );
370
389
}
@@ -627,7 +646,7 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake()
627
646
$ this ->assertNull ($ error ->getPrevious ());
628
647
}
629
648
630
- public function testEmitsNothingIfConnectionIsIdle ()
649
+ public function testEmitsNothingIfPlaintextConnectionIsIdle ()
631
650
{
632
651
$ loop = Factory::create ();
633
652
@@ -641,8 +660,8 @@ public function testEmitsNothingIfConnectionIsIdle()
641
660
$ connector = new TcpConnector ($ loop );
642
661
$ promise = $ connector ->connect (str_replace ('tls:// ' , '' , $ server ->getAddress ()));
643
662
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 );
646
665
}
647
666
648
667
public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake ()
@@ -705,16 +724,9 @@ public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandsh
705
724
706
725
private function createPromiseForServerError (ServerInterface $ server )
707
726
{
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 );
718
730
});
719
731
});
720
732
}
0 commit comments