88
99class UnixServerTest extends TestCase
1010{
11+ /** @var ?UnixServer */
1112 private $ server ;
13+
14+ /** @var ?string */
1215 private $ uds ;
1316
1417 /**
@@ -28,7 +31,10 @@ public function setUpServer()
2831
2932 public function testConstructWithoutLoopAssignsLoopAutomatically ()
3033 {
31- $ server = new UnixServer ($ this ->getRandomSocketUri ());
34+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
35+ $ this ->uds = $ this ->getRandomSocketUri ();
36+
37+ $ server = new UnixServer ($ this ->uds );
3238
3339 $ ref = new \ReflectionProperty ($ server , 'loop ' );
3440 $ ref ->setAccessible (true );
@@ -45,9 +51,11 @@ public function testConstructWithoutLoopAssignsLoopAutomatically()
4551 public function testConnection ()
4652 {
4753 $ client = stream_socket_client ($ this ->uds );
54+ assert (is_resource ($ client ));
4855
4956 $ this ->server ->on ('connection ' , $ this ->expectCallableOnce ());
5057 $ this ->tick ();
58+ $ this ->tick ();
5159 }
5260
5361 /**
@@ -56,8 +64,11 @@ public function testConnection()
5664 public function testConnectionWithManyClients ()
5765 {
5866 $ client1 = stream_socket_client ($ this ->uds );
67+ assert (is_resource ($ client1 ));
5968 $ client2 = stream_socket_client ($ this ->uds );
69+ assert (is_resource ($ client2 ));
6070 $ client3 = stream_socket_client ($ this ->uds );
71+ assert (is_resource ($ client3 ));
6172
6273 $ this ->server ->on ('connection ' , $ this ->expectCallableExactly (3 ));
6374 $ this ->tick ();
@@ -68,6 +79,7 @@ public function testConnectionWithManyClients()
6879 public function testDataEventWillNotBeEmittedWhenClientSendsNoData ()
6980 {
7081 $ client = stream_socket_client ($ this ->uds );
82+ assert (is_resource ($ client ));
7183
7284 $ mock = $ this ->expectCallableNever ();
7385
@@ -81,6 +93,7 @@ public function testDataEventWillNotBeEmittedWhenClientSendsNoData()
8193 public function testDataWillBeEmittedWithDataClientSends ()
8294 {
8395 $ client = stream_socket_client ($ this ->uds );
96+ assert (is_resource ($ client ));
8497
8598 fwrite ($ client , "foo \n" );
8699
@@ -138,6 +151,7 @@ public function testGetAddressAfterCloseReturnsNull()
138151 public function testLoopWillEndWhenServerIsClosedAfterSingleConnection ()
139152 {
140153 $ client = stream_socket_client ($ this ->uds );
154+ assert (is_resource ($ client ));
141155
142156 // explicitly unset server because we only accept a single connection
143157 // and then already call close()
@@ -191,6 +205,7 @@ public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmo
191205 public function testConnectionDoesNotEndWhenClientDoesNotClose ()
192206 {
193207 $ client = stream_socket_client ($ this ->uds );
208+ assert (is_resource ($ client ));
194209
195210 $ mock = $ this ->expectCallableNever ();
196211
@@ -221,10 +236,13 @@ public function testConnectionDoesEndWhenClientCloses()
221236
222237 public function testCtorAddsResourceToLoop ()
223238 {
239+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
240+ $ this ->uds = $ this ->getRandomSocketUri ();
241+
224242 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
225243 $ loop ->expects ($ this ->once ())->method ('addReadStream ' );
226244
227- $ server = new UnixServer ($ this ->getRandomSocketUri () , $ loop );
245+ new UnixServer ($ this ->uds , $ loop );
228246 }
229247
230248 public function testCtorThrowsForInvalidAddressScheme ()
@@ -264,51 +282,66 @@ public function testCtorThrowsWhenPathIsNotWritableWithoutCallingCustomErrorHand
264282
265283 public function testResumeWithoutPauseIsNoOp ()
266284 {
285+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
286+ $ this ->uds = $ this ->getRandomSocketUri ();
287+
267288 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
268289 $ loop ->expects ($ this ->once ())->method ('addReadStream ' );
269290
270- $ server = new UnixServer ($ this ->getRandomSocketUri () , $ loop );
291+ $ server = new UnixServer ($ this ->uds , $ loop );
271292 $ server ->resume ();
272293 }
273294
274295 public function testPauseRemovesResourceFromLoop ()
275296 {
297+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
298+ $ this ->uds = $ this ->getRandomSocketUri ();
299+
276300 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
277301 $ loop ->expects ($ this ->once ())->method ('removeReadStream ' );
278302
279- $ server = new UnixServer ($ this ->getRandomSocketUri () , $ loop );
303+ $ server = new UnixServer ($ this ->uds , $ loop );
280304 $ server ->pause ();
281305 }
282306
283307 public function testPauseAfterPauseIsNoOp ()
284308 {
309+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
310+ $ this ->uds = $ this ->getRandomSocketUri ();
311+
285312 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
286313 $ loop ->expects ($ this ->once ())->method ('removeReadStream ' );
287314
288- $ server = new UnixServer ($ this ->getRandomSocketUri () , $ loop );
315+ $ server = new UnixServer ($ this ->uds , $ loop );
289316 $ server ->pause ();
290317 $ server ->pause ();
291318 }
292319
293320 public function testCloseRemovesResourceFromLoop ()
294321 {
322+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
323+ $ this ->uds = $ this ->getRandomSocketUri ();
324+
295325 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
296326 $ loop ->expects ($ this ->once ())->method ('removeReadStream ' );
297327
298- $ server = new UnixServer ($ this ->getRandomSocketUri () , $ loop );
328+ $ server = new UnixServer ($ this ->uds , $ loop );
299329 $ server ->close ();
300330 }
301331
302332 public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHandler ()
303333 {
334+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
335+ $ this ->uds = $ this ->getRandomSocketUri ();
336+
304337 $ listener = null ;
305338 $ loop = $ this ->getMockBuilder ('React\EventLoop\LoopInterface ' )->getMock ();
306339 $ loop ->expects ($ this ->once ())->method ('addReadStream ' )->with ($ this ->anything (), $ this ->callback (function ($ cb ) use (&$ listener ) {
307340 $ listener = $ cb ;
308341 return true ;
309342 }));
310343
311- $ server = new UnixServer ($ this ->getRandomSocketUri () , $ loop );
344+ $ server = new UnixServer ($ this ->uds , $ loop );
312345
313346 $ exception = null ;
314347 $ server ->on ('error ' , function ($ e ) use (&$ exception ) {
@@ -361,7 +394,7 @@ public function testListenOnBusyPortThrows()
361394 }
362395
363396 $ this ->setExpectedException ('RuntimeException ' );
364- $ another = new UnixServer ($ this ->uds );
397+ new UnixServer ($ this ->uds );
365398 }
366399
367400 /**
@@ -372,7 +405,11 @@ public function tearDownServer()
372405 {
373406 if ($ this ->server ) {
374407 $ this ->server ->close ();
408+ $ this ->server = null ;
375409 }
410+
411+ assert (is_string ($ this ->uds ));
412+ unlink (str_replace ('unix:// ' , '' , $ this ->uds ));
376413 }
377414
378415 private function getRandomSocketUri ()
0 commit comments