@@ -20,6 +20,7 @@ class FunctionalTest extends TestCase
2020 private $ connector ;
2121 private $ client ;
2222
23+ private $ socket ;
2324 private $ port ;
2425 private $ server ;
2526
@@ -28,20 +29,28 @@ class FunctionalTest extends TestCase
2829 */
2930 public function setUpClientServer ()
3031 {
31- $ socket = new SocketServer ('127.0.0.1:0 ' );
32- $ address = $ socket ->getAddress ();
32+ $ this -> socket = new SocketServer ('127.0.0.1:0 ' );
33+ $ address = $ this -> socket ->getAddress ();
3334 if (strpos ($ address , ':// ' ) === false ) {
3435 $ address = 'tcp:// ' . $ address ;
3536 }
3637 $ this ->port = parse_url ($ address , PHP_URL_PORT );
3738 $ this ->assertNotEquals (0 , $ this ->port );
3839
3940 $ this ->server = new Server ();
40- $ this ->server ->listen ($ socket );
41+ $ this ->server ->listen ($ this -> socket );
4142 $ this ->connector = new TcpConnector ();
4243 $ this ->client = new Client ('127.0.0.1: ' . $ this ->port , $ this ->connector );
4344 }
4445
46+ /**
47+ * @after
48+ */
49+ public function closeSocket ()
50+ {
51+ $ this ->socket ->close ();
52+ }
53+
4554 /** @group internet */
4655 public function testConnection ()
4756 {
@@ -117,6 +126,8 @@ public function testConnectionSocksOverTls()
117126 $ this ->client = new Client (str_replace ('tls: ' , 'sockss: ' , $ socket ->getAddress ()), $ this ->connector );
118127
119128 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
129+
130+ $ socket ->close ();
120131 }
121132
122133 /**
@@ -146,6 +157,8 @@ public function testConnectionSocksOverTlsUsesPeerNameFromSocksUri()
146157 $ this ->client = new Client (str_replace ('tls: ' , 'sockss: ' , $ socket ->getAddress ()), $ this ->connector );
147158
148159 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
160+
161+ $ socket ->close ();
149162 }
150163
151164 /** @group internet */
@@ -166,6 +179,8 @@ public function testConnectionSocksOverUnix()
166179 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
167180
168181 unlink ($ path );
182+
183+ $ socket ->close ();
169184 }
170185
171186 /** @group internet */
@@ -186,6 +201,8 @@ public function testConnectionSocks5OverUnix()
186201 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
187202
188203 unlink ($ path );
204+
205+ $ socket ->close ();
189206 }
190207
191208 /** @group internet */
@@ -206,6 +223,8 @@ public function testConnectionSocksWithAuthenticationOverUnix()
206223 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
207224
208225 unlink ($ path );
226+
227+ $ socket ->close ();
209228 }
210229
211230 /** @group internet */
@@ -220,6 +239,8 @@ public function testConnectionAuthenticationFromUri()
220239 $ this ->client = new Client ('name:pass@127.0.0.1: ' . $ this ->port , $ this ->connector );
221240
222241 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
242+
243+ $ socket ->close ();
223244 }
224245
225246 /** @group internet */
@@ -244,6 +265,8 @@ public function testConnectionAuthenticationCallback()
244265
245266 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
246267 $ this ->assertEquals (1 , $ called );
268+
269+ $ socket ->close ();
247270 }
248271
249272 /** @group internet */
@@ -257,6 +280,9 @@ public function testConnectionAuthenticationCallbackWillNotBeInvokedIfClientsSen
257280 });
258281
259282 $ socket = new SocketServer ('127.0.0.1:0 ' );
283+ $ socket ->on ('connection ' , function () use ($ socket ) {
284+ $ socket ->close ();
285+ });
260286 $ this ->server ->listen ($ socket );
261287 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
262288
@@ -278,6 +304,8 @@ public function testConnectionAuthenticationFromUriEncoded()
278304 $ this ->client = new Client (rawurlencode ('name ' ) . ': ' . rawurlencode ('p@ss:w0rd ' ) . '@127.0.0.1: ' . $ this ->port , $ this ->connector );
279305
280306 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
307+
308+ $ socket ->close ();
281309 }
282310
283311 /** @group internet */
@@ -292,6 +320,8 @@ public function testConnectionAuthenticationFromUriWithOnlyUserAndNoPassword()
292320 $ this ->client = new Client ('empty@127.0.0.1: ' . $ this ->port , $ this ->connector );
293321
294322 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
323+
324+ $ socket ->close ();
295325 }
296326
297327 /** @group internet */
@@ -306,6 +336,8 @@ public function testConnectionAuthenticationEmptyPassword()
306336 $ this ->client = new Client ('user@127.0.0.1: ' . $ this ->port , $ this ->connector );
307337
308338 $ this ->assertResolveStream ($ this ->client ->connect ('www.google.com:80 ' ));
339+
340+ $ socket ->close ();
309341 }
310342
311343 /** @group internet */
@@ -321,6 +353,9 @@ public function testConnectionInvalidNoAuthenticationOverLegacySocks4()
321353 $ this ->server = new Server (null , null , array ('name ' => 'pass ' ));
322354
323355 $ socket = new SocketServer ('127.0.0.1:0 ' );
356+ $ socket ->on ('connection ' , function () use ($ socket ) {
357+ $ socket ->close ();
358+ });
324359 $ this ->server ->listen ($ socket );
325360 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
326361
@@ -334,6 +369,9 @@ public function testConnectionInvalidNoAuthentication()
334369 $ this ->server = new Server (null , null , array ('name ' => 'pass ' ));
335370
336371 $ socket = new SocketServer ('127.0.0.1:0 ' );
372+ $ socket ->on ('connection ' , function () use ($ socket ) {
373+ $ socket ->close ();
374+ });
337375 $ this ->server ->listen ($ socket );
338376 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
339377
@@ -347,6 +385,9 @@ public function testConnectionInvalidAuthenticationMismatch()
347385 $ this ->server = new Server (null , null , array ('name ' => 'pass ' ));
348386
349387 $ socket = new SocketServer ('127.0.0.1:0 ' );
388+ $ socket ->on ('connection ' , function () use ($ socket ) {
389+ $ socket ->close ();
390+ });
350391 $ this ->server ->listen ($ socket );
351392 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
352393
@@ -362,6 +403,9 @@ public function testConnectionInvalidAuthenticatorReturnsFalse()
362403 });
363404
364405 $ socket = new SocketServer ('127.0.0.1:0 ' );
406+ $ socket ->on ('connection ' , function () use ($ socket ) {
407+ $ socket ->close ();
408+ });
365409 $ this ->server ->listen ($ socket );
366410 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
367411
@@ -377,6 +421,9 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
377421 });
378422
379423 $ socket = new SocketServer ('127.0.0.1:0 ' );
424+ $ socket ->on ('connection ' , function () use ($ socket ) {
425+ $ socket ->close ();
426+ });
380427 $ this ->server ->listen ($ socket );
381428 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
382429
@@ -388,10 +435,13 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal
388435 public function testConnectionInvalidAuthenticatorReturnsPromiseRejected ()
389436 {
390437 $ this ->server = new Server (null , null , function () {
391- return \React \Promise \reject ();
438+ return \React \Promise \reject (new \ RuntimeException () );
392439 });
393440
394441 $ socket = new SocketServer ('127.0.0.1:0 ' );
442+ $ socket ->on ('connection ' , function () use ($ socket ) {
443+ $ socket ->close ();
444+ });
395445 $ this ->server ->listen ($ socket );
396446 $ this ->port = parse_url ($ socket ->getAddress (), PHP_URL_PORT );
397447
@@ -457,7 +507,12 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateFailsWit
457507
458508 $ ssl = new SecureConnector ($ this ->client , null , array ('verify_peer ' => true ));
459509
460- $ this ->assertRejectPromise ($ ssl ->connect ($ socket ->getAddress ()));
510+ $ promise = $ ssl ->connect ($ socket ->getAddress ());
511+ $ promise ->then (null , function () use ($ socket ) {
512+ $ socket ->close ();
513+ });
514+
515+ $ this ->assertRejectPromise ($ promise );
461516 }
462517
463518 public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWithoutVerifyPeer ()
@@ -480,6 +535,8 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWit
480535
481536 $ this ->assertResolveStream ($ ssl ->connect ($ socket ->getAddress ()));
482537 $ this ->assertResolveStream ($ promise );
538+
539+ $ socket ->close ();
483540 }
484541
485542 /** @group internet */
0 commit comments