33namespace React \Tests \Stream ;
44
55use React \EventLoop \Factory ;
6+ use React \EventLoop \LoopInterface ;
67use React \Stream \DuplexResourceStream ;
78use React \Stream \WritableResourceStream ;
89
@@ -28,7 +29,7 @@ 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 }
@@ -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,7 +73,7 @@ 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 }
@@ -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