22
33namespace React \Tests \Stream ;
44
5- use React \Stream \DuplexResourceStream ;
65use React \EventLoop \Factory ;
6+ use React \EventLoop \LoopInterface ;
7+ use React \Stream \DuplexResourceStream ;
78use React \Stream \WritableResourceStream ;
89
910/**
@@ -28,14 +29,14 @@ public function testUploadKilobytePlain()
2829
2930 $ stream ->write ("POST /post HTTP/1.0 \r\nHost: httpbin.org \r\nContent-Length: $ size \r\n\r\n" . str_repeat ('. ' , $ size ));
3031
31- $ loop -> run ( );
32+ $ this -> awaitStreamClose ( $ stream , $ loop );
3233
3334 $ this ->assertNotEquals ('' , $ buffer );
3435 }
3536
3637 public function testUploadBiggerBlockPlain ()
3738 {
38- $ size = 1000 * 30 ;
39+ $ size = 50 * 1000 ;
3940 $ stream = stream_socket_client ('tcp://httpbin.org:80 ' );
4041
4142 $ loop = Factory::create ();
@@ -50,7 +51,7 @@ public function testUploadBiggerBlockPlain()
5051
5152 $ stream ->write ("POST /post HTTP/1.0 \r\nHost: httpbin.org \r\nContent-Length: $ size \r\n\r\n" . str_repeat ('. ' , $ size ));
5253
53- $ loop -> run ( );
54+ $ this -> awaitStreamClose ( $ stream , $ loop );
5455
5556 $ this ->assertNotEquals ('' , $ buffer );
5657 }
@@ -72,14 +73,14 @@ public function testUploadKilobyteSecure()
7273
7374 $ stream ->write ("POST /post HTTP/1.0 \r\nHost: httpbin.org \r\nContent-Length: $ size \r\n\r\n" . str_repeat ('. ' , $ size ));
7475
75- $ loop -> run ( );
76+ $ this -> awaitStreamClose ( $ stream , $ loop );
7677
7778 $ this ->assertNotEquals ('' , $ buffer );
7879 }
7980
8081 public function testUploadBiggerBlockSecureRequiresSmallerChunkSize ()
8182 {
82- $ size = 1000 * 30000 ;
83+ $ size = 50 * 1000 ;
8384 $ stream = stream_socket_client ('tls://httpbin.org:443 ' );
8485
8586 $ loop = Factory::create ();
@@ -99,8 +100,23 @@ public function testUploadBiggerBlockSecureRequiresSmallerChunkSize()
99100
100101 $ stream ->write ("POST /post HTTP/1.0 \r\nHost: httpbin.org \r\nContent-Length: $ size \r\n\r\n" . str_repeat ('. ' , $ size ));
101102
102- $ loop -> run ( );
103+ $ this -> awaitStreamClose ( $ stream , $ loop );
103104
104105 $ this ->assertNotEquals ('' , $ buffer );
105106 }
107+
108+ private function awaitStreamClose (DuplexResourceStream $ stream , LoopInterface $ loop , $ timeout = 10.0 )
109+ {
110+ $ stream ->on ('close ' , function () use ($ loop ) {
111+ $ loop ->stop ();
112+ });
113+
114+ $ that = $ this ;
115+ $ loop ->addTimer ($ timeout , function () use ($ loop , $ that ) {
116+ $ loop ->stop ();
117+ $ that ->fail ('Timed out while waiting for stream to close ' );
118+ });
119+
120+ $ loop ->run ();
121+ }
106122}
0 commit comments