From d846ea9380f08c640a719c50af223ef2569eeb9d Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Wed, 15 Sep 2021 11:59:19 +0200 Subject: [PATCH 1/3] Clean up event-loop leftovers in test classes --- tests/FunctionalConnectorTest.php | 14 ++++---- tests/FunctionalSecureServerTest.php | 54 ++++++++++++++-------------- tests/FunctionalTcpServerTest.php | 40 ++++++++++----------- tests/IntegrationTest.php | 34 +++++++++--------- tests/LimitingServerTest.php | 8 ++--- tests/SecureIntegrationTest.php | 4 +-- tests/ServerTest.php | 24 ++++++------- tests/SocketServerTest.php | 24 ++++++------- tests/TcpConnectorTest.php | 24 ++++++------- tests/TcpServerTest.php | 4 +-- tests/TimeoutConnectorTest.php | 16 ++++----- tests/TimerSpeedUpEventLoop.php | 5 ++- tests/UnixServerTest.php | 4 +-- 13 files changed, 127 insertions(+), 128 deletions(-) diff --git a/tests/FunctionalConnectorTest.php b/tests/FunctionalConnectorTest.php index 0ece63ae..d277e4d8 100644 --- a/tests/FunctionalConnectorTest.php +++ b/tests/FunctionalConnectorTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Deferred; use React\Socket\ConnectionInterface; use React\Socket\Connector; @@ -20,7 +20,7 @@ class FunctionalConnectorTest extends TestCase /** @test */ public function connectionToTcpServerShouldSucceedWithLocalhost() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(9998, $loop); @@ -44,7 +44,7 @@ public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueTo $this->markTestSkipped('Not supported on Windows for PHP versions < 7.0 and legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $socket = stream_socket_server('udp://127.0.0.1:0', $errno, $errstr, STREAM_SERVER_BIND); @@ -84,7 +84,7 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP() // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP ini_set('xdebug.max_nesting_level', 256); - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('happy_eyeballs' => true), $loop); @@ -99,7 +99,7 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP() */ public function connectionToRemoteTCP4ServerShouldResultInOurIP() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('happy_eyeballs' => true), $loop); @@ -120,7 +120,7 @@ public function connectionToRemoteTCP4ServerShouldResultInOurIP() */ public function connectionToRemoteTCP6ServerShouldResultInOurIP() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('happy_eyeballs' => true), $loop); @@ -141,7 +141,7 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $uri = str_replace('tcp://', 'tls://', $server->getAddress()); diff --git a/tests/FunctionalSecureServerTest.php b/tests/FunctionalSecureServerTest.php index 2dfe955b..769216e2 100644 --- a/tests/FunctionalSecureServerTest.php +++ b/tests/FunctionalSecureServerTest.php @@ -4,7 +4,7 @@ use Clue\React\Block; use Evenement\EventEmitterInterface; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\SecureConnector; @@ -29,7 +29,7 @@ public function setUpSkipTest() public function testClientCanConnectToServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -60,7 +60,7 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL() $this->markTestSkipped('Test requires PHP 7+ for crypto meta data (but excludes PHP 7.3 because it implicitly limits to TLS 1.2) and OpenSSL 1.1.1+ for TLS 1.3'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -97,7 +97,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien $this->markTestSkipped('Test requires PHP 7+ for crypto meta data'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -127,7 +127,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe $this->markTestSkipped('Test requires PHP 7+ for crypto meta data'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -157,7 +157,7 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien $this->markTestSkipped('Test requires PHP 7+ for crypto meta data'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -193,7 +193,7 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien public function testServerEmitsConnectionForClientConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -232,7 +232,7 @@ public function testServerEmitsConnectionForClientConnection() public function testClientEmitsDataEventOnceForDataWrittenFromServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -261,7 +261,7 @@ public function testClientEmitsDataEventOnceForDataWrittenFromServer() public function testWritesDataInMultipleChunksToConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -298,7 +298,7 @@ public function testWritesDataInMultipleChunksToConnection() public function testWritesMoreDataInMultipleChunksToConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -335,7 +335,7 @@ public function testWritesMoreDataInMultipleChunksToConnection() public function testEmitsDataFromConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -363,7 +363,7 @@ public function testEmitsDataFromConnection() public function testEmitsDataInMultipleChunksFromConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -398,7 +398,7 @@ public function testEmitsDataInMultipleChunksFromConnection() public function testPipesDataBackInMultipleChunksFromConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -440,7 +440,7 @@ public function testPipesDataBackInMultipleChunksFromConnection() */ public function testEmitsConnectionForNewTlsv11Connection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -464,7 +464,7 @@ public function testEmitsConnectionForNewTlsv11Connection() */ public function testEmitsErrorForClientWithTlsVersionMismatch() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -486,7 +486,7 @@ public function testEmitsErrorForClientWithTlsVersionMismatch() public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificate() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -511,7 +511,7 @@ public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificat public function testClientRejectsWithErrorForServerWithInvalidCertificate() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -529,7 +529,7 @@ public function testClientRejectsWithErrorForServerWithInvalidCertificate() public function testServerEmitsErrorForClientWithInvalidCertificate() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -558,7 +558,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase $this->markTestSkipped('Not supported on Windows'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -582,7 +582,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph $this->markTestSkipped('Not supported on Windows'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -603,7 +603,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph public function testEmitsErrorForConnectionWithPeerVerification() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -627,7 +627,7 @@ public function testEmitsErrorIfConnectionIsCancelled() $this->markTestSkipped('Linux only (OS is ' . PHP_OS . ')'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -648,7 +648,7 @@ public function testEmitsErrorIfConnectionIsCancelled() public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -676,7 +676,7 @@ public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -704,7 +704,7 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() public function testEmitsNothingIfPlaintextConnectionIsIdle() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -722,7 +722,7 @@ public function testEmitsNothingIfPlaintextConnectionIsIdle() public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( @@ -751,7 +751,7 @@ public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandshake() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new SecureServer($server, $loop, array( diff --git a/tests/FunctionalTcpServerTest.php b/tests/FunctionalTcpServerTest.php index eae1ceaa..4cd47f7e 100644 --- a/tests/FunctionalTcpServerTest.php +++ b/tests/FunctionalTcpServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\TcpConnector; @@ -15,7 +15,7 @@ class FunctionalTcpServerTest extends TestCase public function testEmitsConnectionForNewConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server->on('connection', $this->expectCallableOnce()); @@ -34,7 +34,7 @@ public function testEmitsConnectionForNewConnection() public function testEmitsNoConnectionForNewConnectionWhenPaused() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server->on('connection', $this->expectCallableNever()); @@ -50,7 +50,7 @@ public function testEmitsNoConnectionForNewConnectionWhenPaused() public function testConnectionForNewConnectionWhenResumedAfterPause() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server->on('connection', $this->expectCallableOnce()); @@ -71,7 +71,7 @@ public function testConnectionForNewConnectionWhenResumedAfterPause() public function testEmitsConnectionWithRemoteIp() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $peer = new Promise(function ($resolve, $reject) use ($server) { @@ -92,7 +92,7 @@ public function testEmitsConnectionWithRemoteIp() public function testEmitsConnectionWithLocalIp() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $peer = new Promise(function ($resolve, $reject) use ($server) { @@ -120,7 +120,7 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() $this->markTestSkipped('Skipping on Windows due to default firewall rules'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer('0.0.0.0:0', $loop); $peer = new Promise(function ($resolve, $reject) use ($server) { @@ -141,7 +141,7 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $peer = new Promise(function ($resolve, $reject) use ($server) { @@ -164,7 +164,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedByServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $peer = new Promise(function ($resolve, $reject) use ($server) { @@ -190,7 +190,7 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled() $this->markTestSkipped('Linux only (OS is ' . PHP_OS . ')'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server->on('connection', $this->expectCallableOnce()); @@ -210,7 +210,7 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled() public function testEmitsConnectionForNewIpv6Connection() { - $loop = Factory::create(); + $loop = Loop::get(); try { $server = new TcpServer('[::1]:0', $loop); @@ -234,7 +234,7 @@ public function testEmitsConnectionForNewIpv6Connection() public function testEmitsConnectionWithRemoteIpv6() { - $loop = Factory::create(); + $loop = Loop::get(); try { $server = new TcpServer('[::1]:0', $loop); @@ -260,7 +260,7 @@ public function testEmitsConnectionWithRemoteIpv6() public function testEmitsConnectionWithLocalIpv6() { - $loop = Factory::create(); + $loop = Loop::get(); try { $server = new TcpServer('[::1]:0', $loop); @@ -287,7 +287,7 @@ public function testEmitsConnectionWithLocalIpv6() public function testServerPassesContextOptionsToSocket() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop, array( 'backlog' => 4 @@ -304,7 +304,7 @@ public function testServerPassesContextOptionsToSocket() public function testServerPassesDefaultBacklogSizeViaContextOptionsToSocket() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); @@ -324,7 +324,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop, array( 'backlog' => 4 @@ -348,7 +348,7 @@ public function testEmitsConnectionWithInheritedContextOptions() public function testFailsToListenOnInvalidUri() { - $loop = Factory::create(); + $loop = Loop::get(); $this->setExpectedException( 'InvalidArgumentException', @@ -360,7 +360,7 @@ public function testFailsToListenOnInvalidUri() public function testFailsToListenOnUriWithoutPort() { - $loop = Factory::create(); + $loop = Loop::get(); $this->setExpectedException( 'InvalidArgumentException', @@ -372,7 +372,7 @@ public function testFailsToListenOnUriWithoutPort() public function testFailsToListenOnUriWithWrongScheme() { - $loop = Factory::create(); + $loop = Loop::get(); $this->setExpectedException( 'InvalidArgumentException', @@ -384,7 +384,7 @@ public function testFailsToListenOnUriWithWrongScheme() public function testFailsToListenOnUriWIthHostname() { - $loop = Factory::create(); + $loop = Loop::get(); $this->setExpectedException( 'InvalidArgumentException', diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index f23e980f..93c6033d 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -4,7 +4,7 @@ use Clue\React\Block; use React\Dns\Resolver\Factory as ResolverFactory; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Socket\Connector; use React\Socket\DnsConnector; use React\Socket\SecureConnector; @@ -18,7 +18,7 @@ class IntegrationTest extends TestCase /** @test */ public function gettingStuffFromGoogleShouldWork() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array(), $loop); $conn = Block\await($connector->connect('google.com:80'), $loop); @@ -40,7 +40,7 @@ public function gettingEncryptedStuffFromGoogleShouldWork() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $secureConnector = new Connector(array(), $loop); $conn = Block\await($secureConnector->connect('tls://google.com:443'), $loop); @@ -59,7 +59,7 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $factory = new ResolverFactory(); $dns = $factory->create('8.8.8.8', $loop); @@ -84,7 +84,7 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() /** @test */ public function gettingPlaintextStuffFromEncryptedGoogleShouldNotWork() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array(), $loop); $conn = Block\await($connector->connect('google.com:443'), $loop); @@ -105,7 +105,7 @@ public function testConnectingFailsIfConnectorUsesInvalidDnsResolverAddress() $this->markTestSkipped('Skipped on macOS due to a bug in reactphp/dns (solved in reactphp/dns#171)'); } - $loop = Factory::create(); + $loop = Loop::get(); $factory = new ResolverFactory(); $dns = $factory->create('255.255.255.255', $loop); @@ -124,7 +124,7 @@ public function testCancellingPendingConnectionWithoutTimeoutShouldNotCreateAnyG $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('timeout' => false), $loop); gc_collect_cycles(); @@ -143,7 +143,7 @@ public function testCancellingPendingConnectionShouldNotCreateAnyGarbageReferenc $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array(), $loop); gc_collect_cycles(); @@ -160,7 +160,7 @@ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferen $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('timeout' => false), $loop); gc_collect_cycles(); @@ -196,7 +196,7 @@ public function testWaitingForConnectionTimeoutDuringDnsLookupShouldNotCreateAny $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('timeout' => 0.001), $loop); gc_collect_cycles(); @@ -229,7 +229,7 @@ public function testWaitingForConnectionTimeoutDuringTcpConnectionShouldNotCreat $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('timeout' => 0.000001), $loop); gc_collect_cycles(); @@ -262,7 +262,7 @@ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageRefer $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('timeout' => false), $loop); gc_collect_cycles(); @@ -301,7 +301,7 @@ public function testWaitingForInvalidTlsConnectionShouldNotCreateAnyGarbageRefer $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array( 'tls' => array( 'verify_peer' => true @@ -341,7 +341,7 @@ public function testWaitingForSuccessfullyClosedConnectionShouldNotCreateAnyGarb $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array('timeout' => false), $loop); gc_collect_cycles(); @@ -358,7 +358,7 @@ function ($conn) { public function testConnectingFailsIfTimeoutIsTooSmall() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array( 'timeout' => 0.001 @@ -374,7 +374,7 @@ public function testSelfSignedRejectsIfVerificationIsEnabled() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array( 'tls' => array( @@ -392,7 +392,7 @@ public function testSelfSignedResolvesIfVerificationIsDisabled() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new Connector(array( 'tls' => array( diff --git a/tests/LimitingServerTest.php b/tests/LimitingServerTest.php index 0769836b..6670e70e 100644 --- a/tests/LimitingServerTest.php +++ b/tests/LimitingServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\LimitingServer; @@ -143,7 +143,7 @@ public function testPausingServerWillBePausedOnceLimitIsReached() public function testSocketDisconnectionWillRemoveFromList() { - $loop = Factory::create(); + $loop = Loop::get(); $tcp = new TcpServer(0, $loop); @@ -167,7 +167,7 @@ public function testSocketDisconnectionWillRemoveFromList() public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOperatingSystem() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new LimitingServer($server, 1, true); @@ -189,7 +189,7 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper public function testPausingServerWillEmitTwoConnectionsFromBacklog() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(0, $loop); $server = new LimitingServer($server, 1, true); diff --git a/tests/SecureIntegrationTest.php b/tests/SecureIntegrationTest.php index 16d6dc35..eeba587b 100644 --- a/tests/SecureIntegrationTest.php +++ b/tests/SecureIntegrationTest.php @@ -2,7 +2,7 @@ namespace React\Tests\Socket; -use React\EventLoop\Factory as LoopFactory; +use React\EventLoop\Loop; use React\Socket\TcpServer; use React\Socket\SecureServer; use React\Socket\TcpConnector; @@ -31,7 +31,7 @@ public function setUpConnector() $this->markTestSkipped('Not supported on legacy HHVM'); } - $this->loop = LoopFactory::create(); + $this->loop = Loop::get(); $this->server = new TcpServer(0, $this->loop); $this->server = new SecureServer($this->server, $this->loop, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' diff --git a/tests/ServerTest.php b/tests/ServerTest.php index b46949ba..3635e394 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\Server; @@ -31,7 +31,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() public function testCreateServerWithZeroPortAssignsRandomPort() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); $this->assertNotEquals(0, $server->getAddress()); @@ -48,7 +48,7 @@ public function testConstructorThrowsForInvalidUri() public function testConstructorCreatesExpectedTcpServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); @@ -71,7 +71,7 @@ public function testConstructorCreatesExpectedUnixServer() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server($this->getRandomSocketUri(), $loop); @@ -91,7 +91,7 @@ public function testConstructorThrowsForExistingUnixPath() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Factory::create(); + $loop = Loop::get(); try { $server = new Server('unix://' . __FILE__, $loop); @@ -109,7 +109,7 @@ public function testConstructorThrowsForExistingUnixPath() public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); @@ -126,7 +126,7 @@ public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() public function testEmitsConnectionForNewConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); $server->on('connection', $this->expectCallableOnce()); @@ -142,7 +142,7 @@ public function testEmitsConnectionForNewConnection() public function testDoesNotEmitConnectionForNewConnectionToPausedServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); $server->pause(); @@ -155,7 +155,7 @@ public function testDoesNotEmitConnectionForNewConnectionToPausedServer() public function testDoesEmitConnectionForNewConnectionToResumedServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); $server->pause(); @@ -174,7 +174,7 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() public function testDoesNotAllowConnectionToClosedServer() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop); $server->on('connection', $this->expectCallableNever()); @@ -193,7 +193,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server(0, $loop, array( 'backlog' => 4 @@ -219,7 +219,7 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $server = new Server('tls://127.0.0.1:0', $loop, array( 'tls' => array( diff --git a/tests/SocketServerTest.php b/tests/SocketServerTest.php index 8f453dd1..d2c8e688 100644 --- a/tests/SocketServerTest.php +++ b/tests/SocketServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\SocketServer; @@ -32,7 +32,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() public function testCreateServerWithZeroPortAssignsRandomPort() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); $this->assertNotEquals(0, $socket->getAddress()); @@ -71,7 +71,7 @@ public function testConstructorWithInvalidUriWithSchemaAndPortOnlyThrows() public function testConstructorCreatesExpectedTcpServer() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); @@ -94,7 +94,7 @@ public function testConstructorCreatesExpectedUnixServer() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer($this->getRandomSocketUri(), array(), $loop); @@ -114,7 +114,7 @@ public function testConstructorThrowsForExistingUnixPath() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Factory::create(); + $loop = Loop::get(); try { new SocketServer('unix://' . __FILE__, array(), $loop); @@ -147,7 +147,7 @@ public function testConstructWithExistingFileDescriptorReturnsSameAddressAsOrigi public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); @@ -164,7 +164,7 @@ public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() public function testEmitsConnectionForNewConnection() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); $socket->on('connection', $this->expectCallableOnce()); @@ -180,7 +180,7 @@ public function testEmitsConnectionForNewConnection() public function testDoesNotEmitConnectionForNewConnectionToPausedServer() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); $socket->pause(); @@ -193,7 +193,7 @@ public function testDoesNotEmitConnectionForNewConnectionToPausedServer() public function testDoesEmitConnectionForNewConnectionToResumedServer() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); $socket->pause(); @@ -212,7 +212,7 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() public function testDoesNotAllowConnectionToClosedServer() { - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array(), $loop); $socket->on('connection', $this->expectCallableNever()); @@ -231,7 +231,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); } - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('127.0.0.1:0', array( 'tcp' => array( @@ -259,7 +259,7 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Factory::create(); + $loop = Loop::get(); $socket = new SocketServer('tls://127.0.0.1:0', array( 'tls' => array( diff --git a/tests/TcpConnectorTest.php b/tests/TcpConnectorTest.php index 7ce6621b..2fee0b53 100644 --- a/tests/TcpConnectorTest.php +++ b/tests/TcpConnectorTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Socket\ConnectionInterface; use React\Socket\TcpConnector; use React\Socket\TcpServer; @@ -27,7 +27,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() /** @test */ public function connectionToEmptyPortShouldFail() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new TcpConnector($loop); $promise = $connector->connect('127.0.0.1:9999'); @@ -61,7 +61,7 @@ public function connectionToTcpServerShouldAddResourceToLoop() /** @test */ public function connectionToTcpServerShouldSucceed() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(9999, $loop); @@ -78,7 +78,7 @@ public function connectionToTcpServerShouldSucceed() /** @test */ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new TcpConnector($loop); @@ -144,7 +144,7 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() $this->markTestSkipped('Expected error ' . $enetunreach . ' but got ' . $errno . ' (' . $errstr . ') for ' . $address); } - $loop = Factory::create(); + $loop = Loop::get(); $connector = new TcpConnector($loop); $promise = $connector->connect($address); @@ -160,7 +160,7 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() /** @test */ public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(9999, $loop); @@ -178,7 +178,7 @@ public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget() /** @test */ public function connectionToTcpServerShouldSucceedWithLocalAdressOnLocalhost() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(9999, $loop); @@ -197,7 +197,7 @@ public function connectionToTcpServerShouldSucceedWithLocalAdressOnLocalhost() /** @test */ public function connectionToTcpServerShouldSucceedWithNullAddressesAfterConnectionClosed() { - $loop = Factory::create(); + $loop = Loop::get(); $server = new TcpServer(9999, $loop); @@ -216,7 +216,7 @@ public function connectionToTcpServerShouldSucceedWithNullAddressesAfterConnecti /** @test */ public function connectionToTcpServerWillCloseWhenOtherSideCloses() { - $loop = Factory::create(); + $loop = Loop::get(); // immediately close connection and server once connection is in $server = new TcpServer(0, $loop); @@ -238,7 +238,7 @@ public function connectionToTcpServerWillCloseWhenOtherSideCloses() /** @test */ public function connectionToEmptyIp6PortShouldFail() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new TcpConnector($loop); $connector @@ -251,7 +251,7 @@ public function connectionToEmptyIp6PortShouldFail() /** @test */ public function connectionToIp6TcpServerShouldSucceed() { - $loop = Factory::create(); + $loop = Loop::get(); try { $server = new TcpServer('[::1]:9999', $loop); @@ -346,7 +346,7 @@ public function cancellingConnectionShouldRemoveResourceFromLoopAndCloseResource /** @test */ public function cancellingConnectionShouldRejectPromise() { - $loop = Factory::create(); + $loop = Loop::get(); $connector = new TcpConnector($loop); $server = new TcpServer(0, $loop); diff --git a/tests/TcpServerTest.php b/tests/TcpServerTest.php index b4749cf6..1e909338 100644 --- a/tests/TcpServerTest.php +++ b/tests/TcpServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Socket\TcpServer; use React\Stream\DuplexResourceStream; use React\Promise\Promise; @@ -18,7 +18,7 @@ class TcpServerTest extends TestCase private function createLoop() { - return Factory::create(); + return Loop::get(); } /** diff --git a/tests/TimeoutConnectorTest.php b/tests/TimeoutConnectorTest.php index 81398279..183c40f0 100644 --- a/tests/TimeoutConnectorTest.php +++ b/tests/TimeoutConnectorTest.php @@ -5,7 +5,7 @@ use Clue\React\Block; use React\Socket\TimeoutConnector; use React\Promise; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Promise\Deferred; class TimeoutConnectorTest extends TestCase @@ -30,7 +30,7 @@ public function testRejectsWithTimeoutReasonOnTimeout() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 0.01, $loop); @@ -49,7 +49,7 @@ public function testRejectsWithOriginalReasonWhenConnectorRejects() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 5.0, $loop); @@ -68,7 +68,7 @@ public function testResolvesWhenConnectorResolves() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 5.0, $loop); @@ -87,7 +87,7 @@ public function testRejectsAndCancelsPendingPromiseOnTimeout() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 0.01, $loop); @@ -106,7 +106,7 @@ public function testCancelsPendingPromiseOnCancel() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 0.01, $loop); @@ -128,7 +128,7 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($connection->promise()); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 0.01, $loop); $promise = $timeout->connect('example.com:80'); @@ -152,7 +152,7 @@ public function testRejectionDueToTimeoutShouldNotCreateAnyGarbageReferences() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($connection->promise()); - $loop = Factory::create(); + $loop = Loop::get(); $timeout = new TimeoutConnector($connector, 0, $loop); $promise = $timeout->connect('example.com:80'); diff --git a/tests/TimerSpeedUpEventLoop.php b/tests/TimerSpeedUpEventLoop.php index f6287276..97308023 100644 --- a/tests/TimerSpeedUpEventLoop.php +++ b/tests/TimerSpeedUpEventLoop.php @@ -4,7 +4,6 @@ use React\Dns\Model\Message; use React\Dns\Resolver\ResolverInterface; -use React\EventLoop\Factory; use React\EventLoop\LoopInterface; use React\EventLoop\TimerInterface; use React\Promise; @@ -17,12 +16,12 @@ final class TimerSpeedUpEventLoop implements LoopInterface { /** @var LoopInterface */ private $loop; - + public function __construct(LoopInterface $loop) { $this->loop = $loop; } - + public function addReadStream($stream, $listener) { return $this->loop->addReadStream($stream, $listener); diff --git a/tests/UnixServerTest.php b/tests/UnixServerTest.php index b2d4b59f..fdfdec6a 100644 --- a/tests/UnixServerTest.php +++ b/tests/UnixServerTest.php @@ -3,7 +3,7 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Factory; +use React\EventLoop\Loop; use React\Socket\UnixServer; use React\Stream\DuplexResourceStream; @@ -24,7 +24,7 @@ public function setUpServer() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $this->loop = Factory::create(); + $this->loop = Loop::get(); $this->uds = $this->getRandomSocketUri(); $this->server = new UnixServer($this->uds, $this->loop); } From 82acd6cf2ab996f68f808858cc0530fca2683a5a Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Mon, 15 Nov 2021 15:50:18 +0100 Subject: [PATCH 2/3] Close open servers and connections at end of tests --- tests/FdServerTest.php | 6 + tests/FunctionalConnectorTest.php | 27 +-- tests/FunctionalSecureServerTest.php | 307 ++++++++++++++++++--------- tests/FunctionalTcpServerTest.php | 132 ++++++++---- tests/IntegrationTest.php | 37 ++-- tests/LimitingServerTest.php | 12 +- tests/SecureConnectorTest.php | 1 + tests/SecureIntegrationTest.php | 25 ++- tests/ServerTest.php | 52 ++--- tests/SocketServerTest.php | 51 ++--- tests/TcpConnectorTest.php | 65 +++--- tests/TcpServerTest.php | 14 +- tests/TimeoutConnectorTest.php | 28 +-- tests/UnixServerTest.php | 14 +- 14 files changed, 479 insertions(+), 292 deletions(-) diff --git a/tests/FdServerTest.php b/tests/FdServerTest.php index 3df1b296..34f92d0c 100644 --- a/tests/FdServerTest.php +++ b/tests/FdServerTest.php @@ -5,6 +5,7 @@ use Clue\React\Block; use React\EventLoop\Loop; use React\Promise\Promise; +use React\Socket\ConnectionInterface; use React\Socket\FdServer; class FdServerTest extends TestCase @@ -309,9 +310,14 @@ public function testServerEmitsConnectionEventForNewConnection() $connection = Block\await($promise, Loop::get(), 1.0); + /** + * @var ConnectionInterface $connection + */ $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); fclose($client); + $connection->close(); + $server->close(); } public function testEmitsErrorWhenAcceptListenerFails() diff --git a/tests/FunctionalConnectorTest.php b/tests/FunctionalConnectorTest.php index d277e4d8..4b60e502 100644 --- a/tests/FunctionalConnectorTest.php +++ b/tests/FunctionalConnectorTest.php @@ -22,9 +22,9 @@ public function connectionToTcpServerShouldSucceedWithLocalhost() { $loop = Loop::get(); - $server = new TcpServer(9998, $loop); + $server = new TcpServer(9998); - $connector = new Connector(array(), $loop); + $connector = new Connector(array()); $connection = Block\await($connector->connect('localhost:9998'), $loop, self::TIMEOUT); @@ -44,18 +44,16 @@ public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueTo $this->markTestSkipped('Not supported on Windows for PHP versions < 7.0 and legacy HHVM'); } - $loop = Loop::get(); - $socket = stream_socket_server('udp://127.0.0.1:0', $errno, $errstr, STREAM_SERVER_BIND); $connector = new Connector(array( 'dns' => 'udp://' . stream_socket_get_name($socket, false), 'happy_eyeballs' => false - ), $loop); + )); // minimal DNS proxy stub which forwards DNS messages to actual DNS server $received = 0; - $loop->addReadStream($socket, function ($socket) use (&$received) { + Loop::addReadStream($socket, function ($socket) use (&$received) { $request = stream_socket_recvfrom($socket, 65536, 0, $peer); $client = stream_socket_client('udp://8.8.8.8:53'); @@ -64,15 +62,18 @@ public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueTo stream_socket_sendto($socket, $response, 0, $peer); ++$received; + fclose($client); }); - $connection = Block\await($connector->connect('example.com:80'), $loop); + $connection = Block\await($connector->connect('example.com:80'), Loop::get()); $connection->close(); $this->assertEquals(1, $received); - $connection = Block\await($connector->connect('example.com:80'), $loop); + $connection = Block\await($connector->connect('example.com:80'), Loop::get()); $connection->close(); $this->assertEquals(1, $received); + + Loop::removeReadStream($socket); } /** @@ -86,7 +87,7 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP() $loop = Loop::get(); - $connector = new Connector(array('happy_eyeballs' => true), $loop); + $connector = new Connector(array('happy_eyeballs' => true)); $ip = Block\await($this->request('dual.tlund.se', $connector), $loop, self::TIMEOUT); @@ -101,7 +102,7 @@ public function connectionToRemoteTCP4ServerShouldResultInOurIP() { $loop = Loop::get(); - $connector = new Connector(array('happy_eyeballs' => true), $loop); + $connector = new Connector(array('happy_eyeballs' => true)); try { $ip = Block\await($this->request('ipv4.tlund.se', $connector), $loop, self::TIMEOUT); @@ -122,7 +123,7 @@ public function connectionToRemoteTCP6ServerShouldResultInOurIP() { $loop = Loop::get(); - $connector = new Connector(array('happy_eyeballs' => true), $loop); + $connector = new Connector(array('happy_eyeballs' => true)); try { $ip = Block\await($this->request('ipv6.tlund.se', $connector), $loop, self::TIMEOUT); @@ -143,10 +144,10 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $uri = str_replace('tcp://', 'tls://', $server->getAddress()); - $connector = new Connector(array(), $loop); + $connector = new Connector(array()); $promise = $connector->connect($uri); $deferred = new Deferred(); diff --git a/tests/FunctionalSecureServerTest.php b/tests/FunctionalSecureServerTest.php index 769216e2..254d9c55 100644 --- a/tests/FunctionalSecureServerTest.php +++ b/tests/FunctionalSecureServerTest.php @@ -31,12 +31,12 @@ public function testClientCanConnectToServer() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); @@ -62,12 +62,12 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL() $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); @@ -89,6 +89,9 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL() } else { $this->assertEquals('TLSv1.3', $meta['crypto']['protocol']); } + + $client->close(); + $server->close(); } public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClient() @@ -99,12 +102,12 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT )); @@ -119,6 +122,9 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien $meta = stream_get_meta_data($client->stream); $this->assertTrue(isset($meta['crypto']['protocol'])); $this->assertEquals('TLSv1.2', $meta['crypto']['protocol']); + + $client->close(); + $server->close(); } public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServer() @@ -129,13 +135,13 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem', 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER )); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); @@ -149,6 +155,9 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe $meta = stream_get_meta_data($client->stream); $this->assertTrue(isset($meta['crypto']['protocol'])); $this->assertEquals('TLSv1.2', $meta['crypto']['protocol']); + + $client->close(); + $server->close(); } public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClient() @@ -159,12 +168,12 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT )); @@ -180,6 +189,7 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien // […] routines:state_machine:internal error // SSL operation failed with code 1. OpenSSL Error messages: error:0A000438:SSL routines::tlsv1 alert internal error // Connection lost during TLS handshake (ECONNRESET) + $server->close(); $this->markTestSkipped('TLS 1.0 not available on this system (' . $e->getMessage() . ')'); } @@ -189,14 +199,17 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien $meta = stream_get_meta_data($client->stream); $this->assertTrue(isset($meta['crypto']['protocol'])); $this->assertEquals('TLSv1', $meta['crypto']['protocol']); + + $client->close(); + $server->close(); } public function testServerEmitsConnectionForClientConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); @@ -205,7 +218,7 @@ public function testServerEmitsConnectionForClientConnection() $server->on('error', $reject); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $client = $connector->connect($server->getAddress()); @@ -234,8 +247,8 @@ public function testClientEmitsDataEventOnceForDataWrittenFromServer() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); @@ -243,13 +256,13 @@ public function testClientEmitsDataEventOnceForDataWrittenFromServer() $conn->write('foo'); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); - $promise = $connector->connect($server->getAddress()); + $connecting = $connector->connect($server->getAddress()); - $promise = new Promise(function ($resolve, $reject) use ($promise) { - $promise->then(function (ConnectionInterface $connection) use ($resolve) { + $promise = new Promise(function ($resolve, $reject) use ($connecting) { + $connecting->then(function (ConnectionInterface $connection) use ($resolve) { $connection->on('data', $resolve); }, $reject); }); @@ -257,14 +270,20 @@ public function testClientEmitsDataEventOnceForDataWrittenFromServer() $data = Block\await($promise, $loop, self::TIMEOUT); $this->assertEquals('foo', $data); + + $server->close(); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testWritesDataInMultipleChunksToConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableOnce()); @@ -273,15 +292,15 @@ public function testWritesDataInMultipleChunksToConnection() $conn->write(str_repeat('*', 400000)); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); - $promise = $connector->connect($server->getAddress()); + $connecting = $connector->connect($server->getAddress()); - $promise = new Promise(function ($resolve, $reject) use ($promise) { - $promise->then(function (ConnectionInterface $connection) use ($resolve) { + $promise = new Promise(function ($resolve, $reject) use ($connecting) { + $connecting->then(function (ConnectionInterface $connection) use ($resolve) { $received = 0; - $connection->on('data', function ($chunk) use (&$received, $resolve) { + $connection->on('data', function ($chunk) use (&$received, $resolve, $connection) { $received += strlen($chunk); if ($received >= 400000) { @@ -294,14 +313,20 @@ public function testWritesDataInMultipleChunksToConnection() $received = Block\await($promise, $loop, self::TIMEOUT); $this->assertEquals(400000, $received); + + $server->close(); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testWritesMoreDataInMultipleChunksToConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableOnce()); @@ -310,13 +335,13 @@ public function testWritesMoreDataInMultipleChunksToConnection() $conn->write(str_repeat('*', 2000000)); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); - $promise = $connector->connect($server->getAddress()); + $connecting = $connector->connect($server->getAddress()); - $promise = new Promise(function ($resolve, $reject) use ($promise) { - $promise->then(function (ConnectionInterface $connection) use ($resolve) { + $promise = new Promise(function ($resolve, $reject) use ($connecting) { + $connecting->then(function (ConnectionInterface $connection) use ($resolve) { $received = 0; $connection->on('data', function ($chunk) use (&$received, $resolve) { $received += strlen($chunk); @@ -331,14 +356,20 @@ public function testWritesMoreDataInMultipleChunksToConnection() $received = Block\await($promise, $loop, self::TIMEOUT); $this->assertEquals(2000000, $received); + + $server->close(); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsDataFromConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableOnce()); @@ -349,24 +380,31 @@ public function testEmitsDataFromConnection() }); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); - $connector->connect($server->getAddress())->then(function (ConnectionInterface $connection) { + $connecting = $connector->connect($server->getAddress()); + $connecting->then(function (ConnectionInterface $connection) { $connection->write('foo'); }); $data = Block\await($promise, $loop, self::TIMEOUT); $this->assertEquals('foo', $data); + + $server->close(); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsDataInMultipleChunksFromConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableOnce()); @@ -384,24 +422,31 @@ public function testEmitsDataInMultipleChunksFromConnection() }); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); - $connector->connect($server->getAddress())->then(function (ConnectionInterface $connection) { + $connecting = $connector->connect($server->getAddress()); + $connecting->then(function (ConnectionInterface $connection) { $connection->write(str_repeat('*', 400000)); }); $received = Block\await($promise, $loop, self::TIMEOUT); $this->assertEquals(400000, $received); + + $server->close(); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testPipesDataBackInMultipleChunksFromConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableOnce()); @@ -410,13 +455,13 @@ public function testPipesDataBackInMultipleChunksFromConnection() $conn->pipe($conn); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); - $promise = $connector->connect($server->getAddress()); + $connecting = $connector->connect($server->getAddress()); - $promise = new Promise(function ($resolve, $reject) use ($promise) { - $promise->then(function (ConnectionInterface $connection) use ($resolve) { + $promise = new Promise(function ($resolve, $reject) use ($connecting) { + $connecting->then(function (ConnectionInterface $connection) use ($resolve) { $received = 0; $connection->on('data', function ($chunk) use (&$received, $resolve) { $received += strlen($chunk); @@ -432,6 +477,12 @@ public function testPipesDataBackInMultipleChunksFromConnection() $received = Block\await($promise, $loop, self::TIMEOUT); $this->assertEquals(400000, $received); + + $server->close(); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } /** @@ -442,20 +493,25 @@ public function testEmitsConnectionForNewTlsv11Connection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem', 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_SERVER )); $server->on('connection', $this->expectCallableOnce()); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT )); $promise = $connector->connect($server->getAddress()); Block\await($promise, $loop, self::TIMEOUT); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } /** @@ -466,30 +522,37 @@ public function testEmitsErrorForClientWithTlsVersionMismatch() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem', 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_1_SERVER|STREAM_CRYPTO_METHOD_TLSv1_2_SERVER )); $server->on('connection', $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT )); $promise = $connector->connect($server->getAddress()); $this->setExpectedException('RuntimeException', 'handshake'); - Block\await($promise, $loop, self::TIMEOUT); + + try { + Block\await($promise, $loop, self::TIMEOUT); + } catch (\Exception $e) { + $server->close(); + + throw $e; + } } public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificate() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem', 'passphrase' => 'swordfish' )); @@ -499,7 +562,7 @@ public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificat $server->on('error', $reject); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $connector->connect($server->getAddress()); @@ -507,32 +570,42 @@ public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificat $connection = Block\await($peer, $loop, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + + $server->close(); + $connection->close(); } public function testClientRejectsWithErrorForServerWithInvalidCertificate() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => 'invalid.pem' )); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); $this->setExpectedException('RuntimeException', 'handshake'); - Block\await($promise, $loop, self::TIMEOUT); + + try { + Block\await($promise, $loop, self::TIMEOUT); + } catch (\Exception $e) { + $server->close(); + + throw $e; + } } public function testServerEmitsErrorForClientWithInvalidCertificate() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => 'invalid.pem' )); @@ -543,13 +616,20 @@ public function testServerEmitsErrorForClientWithInvalidCertificate() $server->on('error', $reject); }); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $connector->connect($server->getAddress()); $this->setExpectedException('RuntimeException', 'handshake'); - Block\await($peer, $loop, self::TIMEOUT); + + try { + Block\await($peer, $loop, self::TIMEOUT); + } catch (\Exception $e) { + $server->close(); + + throw $e; + } } public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase() @@ -560,20 +640,27 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem' )); $server->on('connection', $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); $this->setExpectedException('RuntimeException', 'handshake'); - Block\await($promise, $loop, self::TIMEOUT); + + try { + Block\await($promise, $loop, self::TIMEOUT); + } catch (\Exception $e) { + $server->close(); + + throw $e; + } } public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassphrase() @@ -584,41 +671,50 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem', 'passphrase' => 'nope' )); $server->on('connection', $this->expectCallableNever()); $server->on('error', $this->expectCallableOnce()); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); $this->setExpectedException('RuntimeException', 'handshake'); - Block\await($promise, $loop, self::TIMEOUT); + + try { + Block\await($promise, $loop, self::TIMEOUT); + } catch (\Exception $e) { + $server->close(); + + throw $e; + } } public function testEmitsErrorForConnectionWithPeerVerification() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $errorEvent = $this->createPromiseForServerError($server); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => true )); $promise = $connector->connect($server->getAddress()); $promise->then(null, $this->expectCallableOnce()); Block\await($errorEvent, $loop, self::TIMEOUT); + + $server->close(); } public function testEmitsErrorIfConnectionIsCancelled() @@ -629,14 +725,14 @@ public function testEmitsErrorIfConnectionIsCancelled() $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $errorEvent = $this->createPromiseForServerError($server); - $connector = new SecureConnector(new TcpConnector($loop), $loop, array( + $connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false )); $promise = $connector->connect($server->getAddress()); @@ -644,20 +740,22 @@ public function testEmitsErrorIfConnectionIsCancelled() $promise->then(null, $this->expectCallableOnce()); Block\await($errorEvent, $loop, self::TIMEOUT); + + $server->close(); } public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $errorEvent = $this->createPromiseForServerError($server); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); $promise->then(function (ConnectionInterface $stream) { @@ -672,20 +770,22 @@ public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() $this->assertStringEndsWith('failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET)', $error->getMessage()); $this->assertEquals(defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104, $error->getCode()); $this->assertNull($error->getPrevious()); + + $server->close(); } public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $errorEvent = $this->createPromiseForServerError($server); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); $promise->then(function (ConnectionInterface $stream) { @@ -700,38 +800,45 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() $this->assertStringEndsWith('failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET)', $error->getMessage()); $this->assertEquals(defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104, $error->getCode()); $this->assertNull($error->getPrevious()); + + $server->close(); } public function testEmitsNothingIfPlaintextConnectionIsIdle() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $server->on('error', $this->expectCallableNever()); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); $connection = Block\await($promise, $loop, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $errorEvent = $this->createPromiseForServerError($server); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); $promise->then(function (ConnectionInterface $stream) { @@ -747,20 +854,22 @@ public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() // Unable to complete TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number // Unable to complete TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:func(143):reason(267) // Unable to complete TLS handshake: Failed setting RSA key + + $server->close(); } public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandshake() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); - $server = new SecureServer($server, $loop, array( + $server = new TcpServer(0); + $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $server->on('connection', $this->expectCallableNever()); $errorEvent = $this->createPromiseForServerError($server); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); $promise->then(function (ConnectionInterface $stream) { @@ -776,6 +885,8 @@ public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandsh // Unable to complete TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:ssl3_get_record:wrong version number // Unable to complete TLS handshake: SSL operation failed with code 1. OpenSSL Error messages: error:1408F10B:SSL routines:func(143):reason(267) // Unable to complete TLS handshake: Failed setting RSA key + + $server->close(); } private function createPromiseForServerError(ServerInterface $server) diff --git a/tests/FunctionalTcpServerTest.php b/tests/FunctionalTcpServerTest.php index 4cd47f7e..bbf4c59e 100644 --- a/tests/FunctionalTcpServerTest.php +++ b/tests/FunctionalTcpServerTest.php @@ -17,30 +17,36 @@ public function testEmitsConnectionForNewConnection() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', $resolve); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); + + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsNoConnectionForNewConnectionWhenPaused() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server->on('connection', $this->expectCallableNever()); $server->pause(); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -52,7 +58,7 @@ public function testConnectionForNewConnectionWhenResumedAfterPause() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server->on('connection', $this->expectCallableOnce()); $server->pause(); $server->resume(); @@ -61,26 +67,31 @@ public function testConnectionForNewConnectionWhenResumedAfterPause() $server->on('connection', $resolve); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsConnectionWithRemoteIp() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { $resolve($connection->getRemoteAddress()); }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -88,20 +99,25 @@ public function testEmitsConnectionWithRemoteIp() $peer = Block\await($peer, $loop, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $peer); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsConnectionWithLocalIp() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { $resolve($connection->getLocalAddress()); }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -112,6 +128,11 @@ public function testEmitsConnectionWithLocalIp() $this->assertContainsString('127.0.0.1:', $local); $this->assertEquals($server->getAddress(), $local); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() @@ -122,14 +143,14 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() $loop = Loop::get(); - $server = new TcpServer('0.0.0.0:0', $loop); + $server = new TcpServer('0.0.0.0:0'); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { $resolve($connection->getLocalAddress()); }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -137,13 +158,18 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() $local = Block\await($peer, $loop, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $local); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { $connection->on('close', function () use ($connection, $resolve) { @@ -152,7 +178,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connector->connect($server->getAddress())->then(function (ConnectionInterface $connection) { $connection->end(); }); @@ -160,13 +186,15 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() $peer = Block\await($peer, $loop, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $peer); + + $server->close(); } public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedByServer() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { $connection->close(); @@ -174,7 +202,7 @@ public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedB }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -182,6 +210,8 @@ public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedB $peer = Block\await($peer, $loop, self::TIMEOUT); $this->assertNull($peer); + + $server->close(); } public function testEmitsConnectionEvenIfClientConnectionIsCancelled() @@ -192,20 +222,22 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled() $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', $resolve); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->cancel(); $promise->then(null, $this->expectCallableOnce()); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); } public function testEmitsConnectionForNewIpv6Connection() @@ -213,7 +245,7 @@ public function testEmitsConnectionForNewIpv6Connection() $loop = Loop::get(); try { - $server = new TcpServer('[::1]:0', $loop); + $server = new TcpServer('[::1]:0'); } catch (\RuntimeException $e) { $this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)'); } @@ -224,12 +256,17 @@ public function testEmitsConnectionForNewIpv6Connection() $server->on('connection', $resolve); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsConnectionWithRemoteIpv6() @@ -237,7 +274,7 @@ public function testEmitsConnectionWithRemoteIpv6() $loop = Loop::get(); try { - $server = new TcpServer('[::1]:0', $loop); + $server = new TcpServer('[::1]:0'); } catch (\RuntimeException $e) { $this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)'); } @@ -248,7 +285,7 @@ public function testEmitsConnectionWithRemoteIpv6() }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -256,6 +293,11 @@ public function testEmitsConnectionWithRemoteIpv6() $peer = Block\await($peer, $loop, self::TIMEOUT); $this->assertContainsString('[::1]:', $peer); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testEmitsConnectionWithLocalIpv6() @@ -263,7 +305,7 @@ public function testEmitsConnectionWithLocalIpv6() $loop = Loop::get(); try { - $server = new TcpServer('[::1]:0', $loop); + $server = new TcpServer('[::1]:0'); } catch (\RuntimeException $e) { $this->markTestSkipped('Unable to start IPv6 server socket (not available on your platform?)'); } @@ -274,7 +316,7 @@ public function testEmitsConnectionWithLocalIpv6() }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -283,13 +325,16 @@ public function testEmitsConnectionWithLocalIpv6() $this->assertContainsString('[::1]:', $local); $this->assertEquals($server->getAddress(), $local); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testServerPassesContextOptionsToSocket() { - $loop = Loop::get(); - - $server = new TcpServer(0, $loop, array( + $server = new TcpServer(0, null, array( 'backlog' => 4 )); @@ -300,13 +345,13 @@ public function testServerPassesContextOptionsToSocket() $context = stream_context_get_options($socket); $this->assertEquals(array('socket' => array('backlog' => 4)), $context); + + $server->close(); } public function testServerPassesDefaultBacklogSizeViaContextOptionsToSocket() { - $loop = Loop::get(); - - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $ref = new \ReflectionProperty($server, 'master'); $ref->setAccessible(true); @@ -315,6 +360,8 @@ public function testServerPassesDefaultBacklogSizeViaContextOptionsToSocket() $context = stream_context_get_options($socket); $this->assertEquals(array('socket' => array('backlog' => 511)), $context); + + $server->close(); } public function testEmitsConnectionWithInheritedContextOptions() @@ -326,7 +373,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $loop = Loop::get(); - $server = new TcpServer(0, $loop, array( + $server = new TcpServer(0, null, array( 'backlog' => 4 )); @@ -336,7 +383,7 @@ public function testEmitsConnectionWithInheritedContextOptions() }); }); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce()); @@ -344,53 +391,50 @@ public function testEmitsConnectionWithInheritedContextOptions() $all = Block\await($peer, $loop, self::TIMEOUT); $this->assertEquals(array('socket' => array('backlog' => 4)), $all); + + $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testFailsToListenOnInvalidUri() { - $loop = Loop::get(); - $this->setExpectedException( 'InvalidArgumentException', 'Invalid URI "tcp://///" given (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 ); - new TcpServer('///', $loop); + new TcpServer('///'); } public function testFailsToListenOnUriWithoutPort() { - $loop = Loop::get(); - $this->setExpectedException( 'InvalidArgumentException', 'Invalid URI "tcp://127.0.0.1" given (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 ); - new TcpServer('127.0.0.1', $loop); + new TcpServer('127.0.0.1'); } public function testFailsToListenOnUriWithWrongScheme() { - $loop = Loop::get(); - $this->setExpectedException( 'InvalidArgumentException', 'Invalid URI "udp://127.0.0.1:0" given (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 ); - new TcpServer('udp://127.0.0.1:0', $loop); + new TcpServer('udp://127.0.0.1:0'); } public function testFailsToListenOnUriWIthHostname() { - $loop = Loop::get(); - $this->setExpectedException( 'InvalidArgumentException', 'Given URI "tcp://localhost:8080" does not contain a valid host IP (EINVAL)', defined('SOCKET_EINVAL') ? SOCKET_EINVAL : 22 ); - new TcpServer('localhost:8080', $loop); + new TcpServer('localhost:8080'); } } diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 93c6033d..983f5f4d 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -19,7 +19,7 @@ class IntegrationTest extends TestCase public function gettingStuffFromGoogleShouldWork() { $loop = Loop::get(); - $connector = new Connector(array(), $loop); + $connector = new Connector(array()); $conn = Block\await($connector->connect('google.com:80'), $loop); @@ -41,7 +41,7 @@ public function gettingEncryptedStuffFromGoogleShouldWork() } $loop = Loop::get(); - $secureConnector = new Connector(array(), $loop); + $secureConnector = new Connector(array()); $conn = Block\await($secureConnector->connect('tls://google.com:443'), $loop); @@ -62,12 +62,11 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() $loop = Loop::get(); $factory = new ResolverFactory(); - $dns = $factory->create('8.8.8.8', $loop); + $dns = $factory->create('8.8.8.8'); $connector = new DnsConnector( new SecureConnector( - new TcpConnector($loop), - $loop + new TcpConnector() ), $dns ); @@ -85,7 +84,7 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() public function gettingPlaintextStuffFromEncryptedGoogleShouldNotWork() { $loop = Loop::get(); - $connector = new Connector(array(), $loop); + $connector = new Connector(array()); $conn = Block\await($connector->connect('google.com:443'), $loop); @@ -108,7 +107,7 @@ public function testConnectingFailsIfConnectorUsesInvalidDnsResolverAddress() $loop = Loop::get(); $factory = new ResolverFactory(); - $dns = $factory->create('255.255.255.255', $loop); + $dns = $factory->create('255.255.255.255'); $connector = new Connector(array( 'dns' => $dns @@ -124,8 +123,7 @@ public function testCancellingPendingConnectionWithoutTimeoutShouldNotCreateAnyG $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); - $connector = new Connector(array('timeout' => false), $loop); + $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on @@ -143,8 +141,7 @@ public function testCancellingPendingConnectionShouldNotCreateAnyGarbageReferenc $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); - $connector = new Connector(array(), $loop); + $connector = new Connector(array()); gc_collect_cycles(); $promise = $connector->connect('8.8.8.8:80'); @@ -161,7 +158,7 @@ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferen } $loop = Loop::get(); - $connector = new Connector(array('timeout' => false), $loop); + $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); @@ -197,7 +194,7 @@ public function testWaitingForConnectionTimeoutDuringDnsLookupShouldNotCreateAny } $loop = Loop::get(); - $connector = new Connector(array('timeout' => 0.001), $loop); + $connector = new Connector(array('timeout' => 0.001)); gc_collect_cycles(); @@ -230,7 +227,7 @@ public function testWaitingForConnectionTimeoutDuringTcpConnectionShouldNotCreat } $loop = Loop::get(); - $connector = new Connector(array('timeout' => 0.000001), $loop); + $connector = new Connector(array('timeout' => 0.000001)); gc_collect_cycles(); @@ -263,7 +260,7 @@ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageRefer } $loop = Loop::get(); - $connector = new Connector(array('timeout' => false), $loop); + $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); @@ -306,7 +303,7 @@ public function testWaitingForInvalidTlsConnectionShouldNotCreateAnyGarbageRefer 'tls' => array( 'verify_peer' => true ) - ), $loop); + )); gc_collect_cycles(); @@ -342,7 +339,7 @@ public function testWaitingForSuccessfullyClosedConnectionShouldNotCreateAnyGarb } $loop = Loop::get(); - $connector = new Connector(array('timeout' => false), $loop); + $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); $promise = $connector->connect('google.com:80')->then( @@ -362,7 +359,7 @@ public function testConnectingFailsIfTimeoutIsTooSmall() $connector = new Connector(array( 'timeout' => 0.001 - ), $loop); + )); $this->setExpectedException('RuntimeException'); Block\await($connector->connect('google.com:80'), $loop, self::TIMEOUT); @@ -380,7 +377,7 @@ public function testSelfSignedRejectsIfVerificationIsEnabled() 'tls' => array( 'verify_peer' => true ) - ), $loop); + )); $this->setExpectedException('RuntimeException'); Block\await($connector->connect('tls://self-signed.badssl.com:443'), $loop, self::TIMEOUT); @@ -398,7 +395,7 @@ public function testSelfSignedResolvesIfVerificationIsDisabled() 'tls' => array( 'verify_peer' => false ) - ), $loop); + )); $conn = Block\await($connector->connect('tls://self-signed.badssl.com:443'), $loop, self::TIMEOUT); $conn->close(); diff --git a/tests/LimitingServerTest.php b/tests/LimitingServerTest.php index 6670e70e..4a8d50bb 100644 --- a/tests/LimitingServerTest.php +++ b/tests/LimitingServerTest.php @@ -145,7 +145,7 @@ public function testSocketDisconnectionWillRemoveFromList() { $loop = Loop::get(); - $tcp = new TcpServer(0, $loop); + $tcp = new TcpServer(0); $socket = stream_socket_client($tcp->getAddress()); fclose($socket); @@ -163,13 +163,15 @@ public function testSocketDisconnectionWillRemoveFromList() Block\await($peer, $loop, self::TIMEOUT); $this->assertEquals(array(), $server->getConnections()); + + $server->close(); } public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOperatingSystem() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server = new LimitingServer($server, 1, true); $server->on('connection', $this->expectCallableOnce()); $server->on('error', $this->expectCallableNever()); @@ -185,13 +187,15 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper fclose($first); fclose($second); + + $server->close(); } public function testPausingServerWillEmitTwoConnectionsFromBacklog() { $loop = Loop::get(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server = new LimitingServer($server, 1, true); $server->on('error', $this->expectCallableNever()); @@ -212,5 +216,7 @@ public function testPausingServerWillEmitTwoConnectionsFromBacklog() fclose($second); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); } } diff --git a/tests/SecureConnectorTest.php b/tests/SecureConnectorTest.php index af3a6f58..2a8773be 100644 --- a/tests/SecureConnectorTest.php +++ b/tests/SecureConnectorTest.php @@ -5,6 +5,7 @@ use React\Promise; use React\Promise\Deferred; use React\Socket\SecureConnector; +use React\EventLoop\Loop; class SecureConnectorTest extends TestCase { diff --git a/tests/SecureIntegrationTest.php b/tests/SecureIntegrationTest.php index eeba587b..93f94ccd 100644 --- a/tests/SecureIntegrationTest.php +++ b/tests/SecureIntegrationTest.php @@ -32,12 +32,12 @@ public function setUpConnector() } $this->loop = Loop::get(); - $this->server = new TcpServer(0, $this->loop); - $this->server = new SecureServer($this->server, $this->loop, array( + $this->server = new TcpServer(0); + $this->server = new SecureServer($this->server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); $this->address = $this->server->getAddress(); - $this->connector = new SecureConnector(new TcpConnector($this->loop), $this->loop, array('verify_peer' => false)); + $this->connector = new SecureConnector(new TcpConnector(), null, array('verify_peer' => false)); } /** @@ -108,7 +108,7 @@ public function testSendDataWithEndToServerReceivesAllData() $this->markTestSkipped('TLS 1.3 supported, but this legacy PHP version does not support explicit choice'); } - $this->connector = new SecureConnector(new TcpConnector($this->loop), $this->loop, array( + $this->connector = new SecureConnector(new TcpConnector(), null, array( 'verify_peer' => false, 'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT )); @@ -155,7 +155,8 @@ public function testSendDataWithoutEndingToServerReceivesAllData() }); $data = str_repeat('d', 200000); - $this->connector->connect($this->address)->then(function (ConnectionInterface $connection) use ($data) { + $connecting = $this->connector->connect($this->address); + $connecting->then(function (ConnectionInterface $connection) use ($data) { $connection->write($data); }); @@ -163,6 +164,10 @@ public function testSendDataWithoutEndingToServerReceivesAllData() $this->assertEquals(strlen($data), strlen($received)); $this->assertEquals($data, $received); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testConnectToServerWhichSendsSmallDataReceivesOneChunk() @@ -204,10 +209,10 @@ public function testConnectToServerWhichSendsDataWithoutEndingReceivesAllData() $peer->write($data); }); - $promise = $this->connector->connect($this->address); + $connecting = $this->connector->connect($this->address); - $promise = new Promise(function ($resolve, $reject) use ($promise) { - $promise->then(function (ConnectionInterface $connection) use ($resolve) { + $promise = new Promise(function ($resolve, $reject) use ($connecting) { + $connecting->then(function (ConnectionInterface $connection) use ($resolve) { $received = 0; $connection->on('data', function ($chunk) use (&$received, $resolve) { $received += strlen($chunk); @@ -222,6 +227,10 @@ public function testConnectToServerWhichSendsDataWithoutEndingReceivesAllData() $received = Block\await($promise, $this->loop, self::TIMEOUT); $this->assertEquals(strlen($data), $received); + + $connecting->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } private function createPromiseForEvent(EventEmitterInterface $emitter, $event, $fn) diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 3635e394..484e09bb 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -27,13 +27,13 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $loop = $ref->getValue($tcp); $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + + $server->close(); } public function testCreateServerWithZeroPortAssignsRandomPort() { - $loop = Loop::get(); - - $server = new Server(0, $loop); + $server = new Server(0); $this->assertNotEquals(0, $server->getAddress()); $server->close(); } @@ -50,16 +50,18 @@ public function testConstructorCreatesExpectedTcpServer() { $loop = Loop::get(); - $server = new Server(0, $loop); + $server = new Server(0); - $connector = new TcpConnector($loop); - $connector->connect($server->getAddress()) - ->then($this->expectCallableOnce(), $this->expectCallableNever()); + $connector = new TcpConnector(); + $promise = $connector->connect($server->getAddress()); + $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); $connection = Block\await($connector->connect($server->getAddress()), $loop, self::TIMEOUT); - $connection->close(); $server->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testConstructorCreatesExpectedUnixServer() @@ -73,9 +75,9 @@ public function testConstructorCreatesExpectedUnixServer() $loop = Loop::get(); - $server = new Server($this->getRandomSocketUri(), $loop); + $server = new Server($this->getRandomSocketUri()); - $connector = new UnixConnector($loop); + $connector = new UnixConnector(); $connector->connect($server->getAddress()) ->then($this->expectCallableOnce(), $this->expectCallableNever()); @@ -91,10 +93,8 @@ public function testConstructorThrowsForExistingUnixPath() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Loop::get(); - try { - $server = new Server('unix://' . __FILE__, $loop); + $server = new Server('unix://' . __FILE__); $this->fail(); } catch (\RuntimeException $e) { if ($e->getCode() === 0) { @@ -109,9 +109,7 @@ public function testConstructorThrowsForExistingUnixPath() public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() { - $loop = Loop::get(); - - $server = new Server(0, $loop); + $server = new Server(0); $ref = new \ReflectionProperty($server, 'server'); $ref->setAccessible(true); @@ -128,7 +126,7 @@ public function testEmitsConnectionForNewConnection() { $loop = Loop::get(); - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($server) { @@ -138,13 +136,15 @@ public function testEmitsConnectionForNewConnection() $client = stream_socket_client($server->getAddress()); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); } public function testDoesNotEmitConnectionForNewConnectionToPausedServer() { $loop = Loop::get(); - $server = new Server(0, $loop); + $server = new Server(0); $server->pause(); $server->on('connection', $this->expectCallableNever()); @@ -157,7 +157,7 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() { $loop = Loop::get(); - $server = new Server(0, $loop); + $server = new Server(0); $server->pause(); $server->on('connection', $this->expectCallableOnce()); @@ -170,13 +170,13 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() $server->resume(); Block\await($peer, $loop, self::TIMEOUT); + + $server->close(); } public function testDoesNotAllowConnectionToClosedServer() { - $loop = Loop::get(); - - $server = new Server(0, $loop); + $server = new Server(0); $server->on('connection', $this->expectCallableNever()); $address = $server->getAddress(); $server->close(); @@ -195,7 +195,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $loop = Loop::get(); - $server = new Server(0, $loop, array( + $server = new Server(0, null, array( 'backlog' => 4 )); @@ -211,6 +211,8 @@ public function testEmitsConnectionWithInheritedContextOptions() $all = Block\await($peer, $loop, self::TIMEOUT); $this->assertEquals(array('socket' => array('backlog' => 4)), $all); + + $server->close(); } public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsIdle() @@ -221,7 +223,7 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $loop = Loop::get(); - $server = new Server('tls://127.0.0.1:0', $loop, array( + $server = new Server('tls://127.0.0.1:0', null, array( 'tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' ) @@ -231,6 +233,8 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $client = stream_socket_client(str_replace('tls://', '', $server->getAddress())); Block\sleep(0.1, $loop); + + $server->close(); } private function getRandomSocketUri() diff --git a/tests/SocketServerTest.php b/tests/SocketServerTest.php index d2c8e688..e523c075 100644 --- a/tests/SocketServerTest.php +++ b/tests/SocketServerTest.php @@ -32,9 +32,7 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() public function testCreateServerWithZeroPortAssignsRandomPort() { - $loop = Loop::get(); - - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); $this->assertNotEquals(0, $socket->getAddress()); $socket->close(); } @@ -73,16 +71,18 @@ public function testConstructorCreatesExpectedTcpServer() { $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); - $connector = new TcpConnector($loop); - $connector->connect($socket->getAddress()) - ->then($this->expectCallableOnce(), $this->expectCallableNever()); + $connector = new TcpConnector(); + $promise = $connector->connect($socket->getAddress()); + $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); $connection = Block\await($connector->connect($socket->getAddress()), $loop, self::TIMEOUT); - $connection->close(); $socket->close(); + $promise->then(function (ConnectionInterface $connection) { + $connection->close(); + }); } public function testConstructorCreatesExpectedUnixServer() @@ -96,15 +96,14 @@ public function testConstructorCreatesExpectedUnixServer() $loop = Loop::get(); - $socket = new SocketServer($this->getRandomSocketUri(), array(), $loop); + $socket = new SocketServer($this->getRandomSocketUri(), array()); - $connector = new UnixConnector($loop); + $connector = new UnixConnector(); $connector->connect($socket->getAddress()) ->then($this->expectCallableOnce(), $this->expectCallableNever()); $connection = Block\await($connector->connect($socket->getAddress()), $loop, self::TIMEOUT); - $connection->close(); $socket->close(); } @@ -114,10 +113,8 @@ public function testConstructorThrowsForExistingUnixPath() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Loop::get(); - try { - new SocketServer('unix://' . __FILE__, array(), $loop); + new SocketServer('unix://' . __FILE__, array()); $this->fail(); } catch (\RuntimeException $e) { if ($e->getCode() === 0) { @@ -147,9 +144,7 @@ public function testConstructWithExistingFileDescriptorReturnsSameAddressAsOrigi public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() { - $loop = Loop::get(); - - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); $ref = new \ReflectionProperty($socket, 'server'); $ref->setAccessible(true); @@ -166,7 +161,7 @@ public function testEmitsConnectionForNewConnection() { $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); $socket->on('connection', $this->expectCallableOnce()); $peer = new Promise(function ($resolve, $reject) use ($socket) { @@ -176,13 +171,15 @@ public function testEmitsConnectionForNewConnection() $client = stream_socket_client($socket->getAddress()); Block\await($peer, $loop, self::TIMEOUT); + + $socket->close(); } public function testDoesNotEmitConnectionForNewConnectionToPausedServer() { $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); $socket->pause(); $socket->on('connection', $this->expectCallableNever()); @@ -195,7 +192,7 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() { $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); $socket->pause(); $socket->on('connection', $this->expectCallableOnce()); @@ -208,13 +205,13 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() $socket->resume(); Block\await($peer, $loop, self::TIMEOUT); + + $socket->close(); } public function testDoesNotAllowConnectionToClosedServer() { - $loop = Loop::get(); - - $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket = new SocketServer('127.0.0.1:0', array()); $socket->on('connection', $this->expectCallableNever()); $address = $socket->getAddress(); $socket->close(); @@ -237,7 +234,7 @@ public function testEmitsConnectionWithInheritedContextOptions() 'tcp' => array( 'backlog' => 4 ) - ), $loop); + )); $peer = new Promise(function ($resolve, $reject) use ($socket) { $socket->on('connection', function (ConnectionInterface $connection) use ($resolve) { @@ -251,6 +248,8 @@ public function testEmitsConnectionWithInheritedContextOptions() $all = Block\await($peer, $loop, self::TIMEOUT); $this->assertEquals(array('socket' => array('backlog' => 4)), $all); + + $socket->close(); } public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsIdle() @@ -265,12 +264,14 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId 'tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' ) - ), $loop); + )); $socket->on('connection', $this->expectCallableNever()); $client = stream_socket_client(str_replace('tls://', '', $socket->getAddress())); Block\sleep(0.1, $loop); + + $socket->close(); } private function getRandomSocketUri() diff --git a/tests/TcpConnectorTest.php b/tests/TcpConnectorTest.php index 2fee0b53..0a82dfae 100644 --- a/tests/TcpConnectorTest.php +++ b/tests/TcpConnectorTest.php @@ -29,7 +29,7 @@ public function connectionToEmptyPortShouldFail() { $loop = Loop::get(); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect('127.0.0.1:9999'); $this->setExpectedException( @@ -63,9 +63,9 @@ public function connectionToTcpServerShouldSucceed() { $loop = Loop::get(); - $server = new TcpServer(9999, $loop); + $server = new TcpServer(9999); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); @@ -80,7 +80,7 @@ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded() { $loop = Loop::get(); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); /** @var string[] $_ */ /** @var int $exit */ @@ -145,7 +145,7 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() } $loop = Loop::get(); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $promise = $connector->connect($address); @@ -154,7 +154,14 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() 'Connection to ' . $address . ' failed: ' . (function_exists('socket_strerror') ? socket_strerror($enetunreach) . ' (ENETUNREACH)' : 'Network is unreachable'), $enetunreach ); - Block\await($promise, $loop, self::TIMEOUT); + + try { + Block\await($promise, $loop, self::TIMEOUT); + } catch (\Exception $e) { + fclose($client); + + throw $e; + } } /** @test */ @@ -162,9 +169,9 @@ public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget() { $loop = Loop::get(); - $server = new TcpServer(9999, $loop); + $server = new TcpServer(9999); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); /* @var $connection ConnectionInterface */ @@ -180,9 +187,9 @@ public function connectionToTcpServerShouldSucceedWithLocalAdressOnLocalhost() { $loop = Loop::get(); - $server = new TcpServer(9999, $loop); + $server = new TcpServer(9999); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); /* @var $connection ConnectionInterface */ @@ -199,9 +206,9 @@ public function connectionToTcpServerShouldSucceedWithNullAddressesAfterConnecti { $loop = Loop::get(); - $server = new TcpServer(9999, $loop); + $server = new TcpServer(9999); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); /* @var $connection ConnectionInterface */ @@ -216,36 +223,34 @@ public function connectionToTcpServerShouldSucceedWithNullAddressesAfterConnecti /** @test */ public function connectionToTcpServerWillCloseWhenOtherSideCloses() { - $loop = Loop::get(); - // immediately close connection and server once connection is in - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $server->on('connection', function (ConnectionInterface $conn) use ($server) { $conn->close(); $server->close(); }); $once = $this->expectCallableOnce(); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connector->connect($server->getAddress())->then(function (ConnectionInterface $conn) use ($once) { $conn->write('hello'); $conn->on('close', $once); }); - $loop->run(); + Loop::run(); } - /** @test */ + /** @test + * @group test + */ public function connectionToEmptyIp6PortShouldFail() { - $loop = Loop::get(); - - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connector ->connect('[::1]:9999') ->then($this->expectCallableNever(), $this->expectCallableOnce()); - $loop->run(); + Loop::run(); } /** @test */ @@ -254,12 +259,12 @@ public function connectionToIp6TcpServerShouldSucceed() $loop = Loop::get(); try { - $server = new TcpServer('[::1]:9999', $loop); + $server = new TcpServer('[::1]:9999'); } catch (\Exception $e) { $this->markTestSkipped('Unable to start IPv6 server socket (IPv6 not supported on this system?)'); } - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); $connection = Block\await($connector->connect('[::1]:9999'), $loop, self::TIMEOUT); /* @var $connection ConnectionInterface */ @@ -347,9 +352,9 @@ public function cancellingConnectionShouldRemoveResourceFromLoopAndCloseResource public function cancellingConnectionShouldRejectPromise() { $loop = Loop::get(); - $connector = new TcpConnector($loop); + $connector = new TcpConnector(); - $server = new TcpServer(0, $loop); + $server = new TcpServer(0); $promise = $connector->connect($server->getAddress()); $promise->cancel(); @@ -359,7 +364,13 @@ public function cancellingConnectionShouldRejectPromise() 'Connection to ' . $server->getAddress() . ' cancelled during TCP/IP handshake (ECONNABORTED)', defined('SOCKET_ECONNABORTED') ? SOCKET_ECONNABORTED : 103 ); - Block\await($promise, $loop); + + try { + Block\await($promise, $loop); + } catch (\Exception $e) { + $server->close(); + throw $e; + } } public function testCancelDuringConnectionShouldNotCreateAnyGarbageReferences() diff --git a/tests/TcpServerTest.php b/tests/TcpServerTest.php index 1e909338..22e6675c 100644 --- a/tests/TcpServerTest.php +++ b/tests/TcpServerTest.php @@ -29,7 +29,7 @@ private function createLoop() public function setUpServer() { $this->loop = $this->createLoop(); - $this->server = new TcpServer(0, $this->loop); + $this->server = new TcpServer(0); $this->port = parse_url($this->server->getAddress(), PHP_URL_PORT); } @@ -43,6 +43,8 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $loop = $ref->getValue($server); $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + + $server->close(); } /** @@ -129,7 +131,7 @@ public function testLoopWillEndWhenServerIsClosed() $this->server->close(); $this->server = null; - $this->loop->run(); + Loop::run(); // if we reach this, then everything is good $this->assertNull(null); @@ -165,7 +167,7 @@ public function testLoopWillEndWhenServerIsClosedAfterSingleConnection() $server->close(); }); - $this->loop->run(); + Loop::run(); // if we reach this, then everything is good $this->assertNull(null); @@ -174,7 +176,7 @@ public function testLoopWillEndWhenServerIsClosedAfterSingleConnection() public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmounts() { $client = stream_socket_client('tcp://localhost:' . $this->port); - $stream = new DuplexResourceStream($client, $this->loop); + $stream = new DuplexResourceStream($client); $bytes = 1024 * 1024; $stream->end(str_repeat('*', $bytes)); @@ -199,7 +201,7 @@ public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmo $server->close(); }); - $this->loop->run(); + Loop::run(); $this->assertEquals($bytes, $received); } @@ -342,7 +344,7 @@ public function testListenOnBusyPortThrows() 'Failed to listen on "tcp://127.0.0.1:' . $this->port . '": ' . (function_exists('socket_strerror') ? socket_strerror(SOCKET_EADDRINUSE) . ' (EADDRINUSE)' : 'Address already in use'), defined('SOCKET_EADDRINUSE') ? SOCKET_EADDRINUSE : 0 ); - new TcpServer($this->port, $this->loop); + new TcpServer($this->port); } /** diff --git a/tests/TimeoutConnectorTest.php b/tests/TimeoutConnectorTest.php index 183c40f0..d918aea0 100644 --- a/tests/TimeoutConnectorTest.php +++ b/tests/TimeoutConnectorTest.php @@ -32,7 +32,7 @@ public function testRejectsWithTimeoutReasonOnTimeout() $loop = Loop::get(); - $timeout = new TimeoutConnector($connector, 0.01, $loop); + $timeout = new TimeoutConnector($connector, 0.01); $this->setExpectedException( 'RuntimeException', @@ -51,7 +51,7 @@ public function testRejectsWithOriginalReasonWhenConnectorRejects() $loop = Loop::get(); - $timeout = new TimeoutConnector($connector, 5.0, $loop); + $timeout = new TimeoutConnector($connector, 5.0); $this->setExpectedException( 'RuntimeException', @@ -68,16 +68,14 @@ public function testResolvesWhenConnectorResolves() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Loop::get(); - - $timeout = new TimeoutConnector($connector, 5.0, $loop); + $timeout = new TimeoutConnector($connector, 5.0); $timeout->connect('google.com:80')->then( $this->expectCallableOnce(), $this->expectCallableNever() ); - $loop->run(); + Loop::run(); } public function testRejectsAndCancelsPendingPromiseOnTimeout() @@ -87,16 +85,14 @@ public function testRejectsAndCancelsPendingPromiseOnTimeout() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Loop::get(); - - $timeout = new TimeoutConnector($connector, 0.01, $loop); + $timeout = new TimeoutConnector($connector, 0.01); $timeout->connect('google.com:80')->then( $this->expectCallableNever(), $this->expectCallableOnce() ); - $loop->run(); + Loop::run(); } public function testCancelsPendingPromiseOnCancel() @@ -106,9 +102,7 @@ public function testCancelsPendingPromiseOnCancel() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Loop::get(); - - $timeout = new TimeoutConnector($connector, 0.01, $loop); + $timeout = new TimeoutConnector($connector, 0.01); $out = $timeout->connect('google.com:80'); $out->cancel(); @@ -128,8 +122,7 @@ public function testRejectionDuringConnectionShouldNotCreateAnyGarbageReferences $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($connection->promise()); - $loop = Loop::get(); - $timeout = new TimeoutConnector($connector, 0.01, $loop); + $timeout = new TimeoutConnector($connector, 0.01); $promise = $timeout->connect('example.com:80'); $connection->reject(new \RuntimeException('Connection failed')); @@ -152,12 +145,11 @@ public function testRejectionDueToTimeoutShouldNotCreateAnyGarbageReferences() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('example.com:80')->willReturn($connection->promise()); - $loop = Loop::get(); - $timeout = new TimeoutConnector($connector, 0, $loop); + $timeout = new TimeoutConnector($connector, 0); $promise = $timeout->connect('example.com:80'); - $loop->run(); + Loop::run(); unset($promise, $connection); $this->assertEquals(0, gc_collect_cycles()); diff --git a/tests/UnixServerTest.php b/tests/UnixServerTest.php index fdfdec6a..0d996641 100644 --- a/tests/UnixServerTest.php +++ b/tests/UnixServerTest.php @@ -26,7 +26,7 @@ public function setUpServer() $this->loop = Loop::get(); $this->uds = $this->getRandomSocketUri(); - $this->server = new UnixServer($this->uds, $this->loop); + $this->server = new UnixServer($this->uds); } public function testConstructWithoutLoopAssignsLoopAutomatically() @@ -38,6 +38,8 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() $loop = $ref->getValue($server); $this->assertInstanceOf('React\EventLoop\LoopInterface', $loop); + + $server->close(); } /** @@ -115,7 +117,7 @@ public function testLoopWillEndWhenServerIsClosed() $this->server->close(); $this->server = null; - $this->loop->run(); + Loop::run(); // if we reach this, then everything is good $this->assertNull(null); @@ -150,7 +152,7 @@ public function testLoopWillEndWhenServerIsClosedAfterSingleConnection() $server->close(); }); - $this->loop->run(); + Loop::run(); // if we reach this, then everything is good $this->assertNull(null); @@ -159,7 +161,7 @@ public function testLoopWillEndWhenServerIsClosedAfterSingleConnection() public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmounts() { $client = stream_socket_client($this->uds); - $stream = new DuplexResourceStream($client, $this->loop); + $stream = new DuplexResourceStream($client); $bytes = 1024 * 1024; $stream->end(str_repeat('*', $bytes)); @@ -184,7 +186,7 @@ public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmo $server->close(); }); - $this->loop->run(); + Loop::run(); $this->assertEquals($bytes, $received); } @@ -339,7 +341,7 @@ public function testListenOnBusyPortThrows() } $this->setExpectedException('RuntimeException'); - $another = new UnixServer($this->uds, $this->loop); + $another = new UnixServer($this->uds); } /** From 4f2497bbf69ba576533aaeba414732f8ab27f26f Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 13 Jan 2022 11:23:30 +0100 Subject: [PATCH 3/3] Use reactphp-block v1.5.0 and remove loop where possible --- composer.json | 2 +- tests/FdServerTest.php | 5 +- tests/FunctionalConnectorTest.php | 30 +++----- tests/FunctionalSecureServerTest.php | 105 +++++++-------------------- tests/FunctionalTcpServerTest.php | 53 ++++---------- tests/IntegrationTest.php | 74 +++++++------------ tests/LimitingServerTest.php | 13 +--- tests/SecureConnectorTest.php | 1 - tests/SecureIntegrationTest.php | 27 +++---- tests/ServerTest.php | 29 ++------ tests/SocketServerTest.php | 29 ++------ tests/TcpConnectorTest.php | 34 +++------ tests/TcpServerTest.php | 11 +-- tests/TestCase.php | 5 +- tests/TimeoutConnectorTest.php | 8 +- tests/UnixServerTest.php | 4 +- 16 files changed, 124 insertions(+), 306 deletions(-) diff --git a/composer.json b/composer.json index 44805eac..ec50942e 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "react/stream": "^1.2" }, "require-dev": { - "clue/block-react": "^1.2", + "clue/block-react": "^1.5", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8.35", "react/promise-stream": "^1.2" }, diff --git a/tests/FdServerTest.php b/tests/FdServerTest.php index 34f92d0c..e7ba55d0 100644 --- a/tests/FdServerTest.php +++ b/tests/FdServerTest.php @@ -3,7 +3,6 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\FdServer; @@ -303,12 +302,12 @@ public function testServerEmitsConnectionEventForNewConnection() $client = stream_socket_client('tcp://' . stream_socket_get_name($socket, false)); - $server = new FdServer($fd, Loop::get()); + $server = new FdServer($fd); $promise = new Promise(function ($resolve) use ($server) { $server->on('connection', $resolve); }); - $connection = Block\await($promise, Loop::get(), 1.0); + $connection = Block\await($promise, null, 1.0); /** * @var ConnectionInterface $connection diff --git a/tests/FunctionalConnectorTest.php b/tests/FunctionalConnectorTest.php index 4b60e502..767b92f0 100644 --- a/tests/FunctionalConnectorTest.php +++ b/tests/FunctionalConnectorTest.php @@ -20,13 +20,11 @@ class FunctionalConnectorTest extends TestCase /** @test */ public function connectionToTcpServerShouldSucceedWithLocalhost() { - $loop = Loop::get(); - $server = new TcpServer(9998); $connector = new Connector(array()); - $connection = Block\await($connector->connect('localhost:9998'), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect('localhost:9998'), null, self::TIMEOUT); $server->close(); @@ -65,11 +63,11 @@ public function testConnectTwiceWithoutHappyEyeBallsOnlySendsSingleDnsQueryDueTo fclose($client); }); - $connection = Block\await($connector->connect('example.com:80'), Loop::get()); + $connection = Block\await($connector->connect('example.com:80')); $connection->close(); $this->assertEquals(1, $received); - $connection = Block\await($connector->connect('example.com:80'), Loop::get()); + $connection = Block\await($connector->connect('example.com:80')); $connection->close(); $this->assertEquals(1, $received); @@ -85,11 +83,9 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP() // max_nesting_level was set to 100 for PHP Versions < 5.4 which resulted in failing test for legacy PHP ini_set('xdebug.max_nesting_level', 256); - $loop = Loop::get(); - $connector = new Connector(array('happy_eyeballs' => true)); - $ip = Block\await($this->request('dual.tlund.se', $connector), $loop, self::TIMEOUT); + $ip = Block\await($this->request('dual.tlund.se', $connector), null, self::TIMEOUT); $this->assertNotFalse(inet_pton($ip)); } @@ -100,12 +96,10 @@ public function connectionToRemoteTCP4n6ServerShouldResultInOurIP() */ public function connectionToRemoteTCP4ServerShouldResultInOurIP() { - $loop = Loop::get(); - $connector = new Connector(array('happy_eyeballs' => true)); try { - $ip = Block\await($this->request('ipv4.tlund.se', $connector), $loop, self::TIMEOUT); + $ip = Block\await($this->request('ipv4.tlund.se', $connector), null, self::TIMEOUT); } catch (\Exception $e) { $this->checkIpv4(); throw $e; @@ -121,12 +115,10 @@ public function connectionToRemoteTCP4ServerShouldResultInOurIP() */ public function connectionToRemoteTCP6ServerShouldResultInOurIP() { - $loop = Loop::get(); - $connector = new Connector(array('happy_eyeballs' => true)); try { - $ip = Block\await($this->request('ipv6.tlund.se', $connector), $loop, self::TIMEOUT); + $ip = Block\await($this->request('ipv6.tlund.se', $connector), null, self::TIMEOUT); } catch (\Exception $e) { $this->checkIpv6(); throw $e; @@ -142,8 +134,6 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); - $server = new TcpServer(0); $uri = str_replace('tcp://', 'tls://', $server->getAddress()); @@ -151,21 +141,21 @@ public function testCancelPendingTlsConnectionDuringTlsHandshakeShouldCloseTcpCo $promise = $connector->connect($uri); $deferred = new Deferred(); - $server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred, $loop) { + $server->on('connection', function (ConnectionInterface $connection) use ($promise, $deferred) { $connection->on('close', function () use ($deferred) { $deferred->resolve(); }); - $loop->futureTick(function () use ($promise) { + Loop::futureTick(function () use ($promise) { $promise->cancel(); }); }); - Block\await($deferred->promise(), $loop, self::TIMEOUT); + Block\await($deferred->promise(), null, self::TIMEOUT); $server->close(); try { - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); $this->fail(); } catch (\Exception $e) { $this->assertInstanceOf('RuntimeException', $e); diff --git a/tests/FunctionalSecureServerTest.php b/tests/FunctionalSecureServerTest.php index 254d9c55..d81040e6 100644 --- a/tests/FunctionalSecureServerTest.php +++ b/tests/FunctionalSecureServerTest.php @@ -4,7 +4,6 @@ use Clue\React\Block; use Evenement\EventEmitterInterface; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\SecureConnector; @@ -29,8 +28,6 @@ public function setUpSkipTest() public function testClientCanConnectToServer() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -42,7 +39,7 @@ public function testClientCanConnectToServer() $promise = $connector->connect($server->getAddress()); /* @var ConnectionInterface $client */ - $client = Block\await($promise, $loop, self::TIMEOUT); + $client = Block\await($promise, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $client); $this->assertEquals($server->getAddress(), $client->getRemoteAddress()); @@ -60,8 +57,6 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL() $this->markTestSkipped('Test requires PHP 7+ for crypto meta data (but excludes PHP 7.3 because it implicitly limits to TLS 1.2) and OpenSSL 1.1.1+ for TLS 1.3'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -73,7 +68,7 @@ public function testClientUsesTls13ByDefaultWhenSupportedByOpenSSL() $promise = $connector->connect($server->getAddress()); /* @var ConnectionInterface $client */ - $client = Block\await($promise, $loop, self::TIMEOUT); + $client = Block\await($promise, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\Connection', $client); $this->assertTrue(isset($client->stream)); @@ -100,8 +95,6 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien $this->markTestSkipped('Test requires PHP 7+ for crypto meta data'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -114,7 +107,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByClien $promise = $connector->connect($server->getAddress()); /* @var ConnectionInterface $client */ - $client = Block\await($promise, $loop, self::TIMEOUT); + $client = Block\await($promise, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\Connection', $client); $this->assertTrue(isset($client->stream)); @@ -133,8 +126,6 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe $this->markTestSkipped('Test requires PHP 7+ for crypto meta data'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem', @@ -147,7 +138,7 @@ public function testClientUsesTls12WhenCryptoMethodIsExplicitlyConfiguredByServe $promise = $connector->connect($server->getAddress()); /* @var ConnectionInterface $client */ - $client = Block\await($promise, $loop, self::TIMEOUT); + $client = Block\await($promise, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\Connection', $client); $this->assertTrue(isset($client->stream)); @@ -166,8 +157,6 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien $this->markTestSkipped('Test requires PHP 7+ for crypto meta data'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -181,7 +170,7 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien /* @var ConnectionInterface $client */ try { - $client = Block\await($promise, $loop, self::TIMEOUT); + $client = Block\await($promise, null, self::TIMEOUT); } catch (\RuntimeException $e) { // legacy TLS 1.0 would be considered insecure by today's standards, so skip test if connection fails // OpenSSL error messages are version/platform specific @@ -206,8 +195,6 @@ public function testClientUsesTls10WhenCryptoMethodIsExplicitlyConfiguredByClien public function testServerEmitsConnectionForClientConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -225,7 +212,7 @@ public function testServerEmitsConnectionForClientConnection() // await both client and server side end of connection /* @var ConnectionInterface[] $both */ - $both = Block\awaitAll(array($peer, $client), $loop, self::TIMEOUT); + $both = Block\awaitAll(array($peer, $client), null, self::TIMEOUT); // both ends of the connection are represented by different instances of ConnectionInterface $this->assertCount(2, $both); @@ -245,8 +232,6 @@ public function testServerEmitsConnectionForClientConnection() public function testClientEmitsDataEventOnceForDataWrittenFromServer() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -267,7 +252,7 @@ public function testClientEmitsDataEventOnceForDataWrittenFromServer() }, $reject); }); - $data = Block\await($promise, $loop, self::TIMEOUT); + $data = Block\await($promise, null, self::TIMEOUT); $this->assertEquals('foo', $data); @@ -280,8 +265,6 @@ public function testClientEmitsDataEventOnceForDataWrittenFromServer() public function testWritesDataInMultipleChunksToConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -310,7 +293,7 @@ public function testWritesDataInMultipleChunksToConnection() }, $reject); }); - $received = Block\await($promise, $loop, self::TIMEOUT); + $received = Block\await($promise, null, self::TIMEOUT); $this->assertEquals(400000, $received); @@ -323,8 +306,6 @@ public function testWritesDataInMultipleChunksToConnection() public function testWritesMoreDataInMultipleChunksToConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -353,7 +334,7 @@ public function testWritesMoreDataInMultipleChunksToConnection() }, $reject); }); - $received = Block\await($promise, $loop, self::TIMEOUT); + $received = Block\await($promise, null, self::TIMEOUT); $this->assertEquals(2000000, $received); @@ -366,8 +347,6 @@ public function testWritesMoreDataInMultipleChunksToConnection() public function testEmitsDataFromConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -388,7 +367,7 @@ public function testEmitsDataFromConnection() $connection->write('foo'); }); - $data = Block\await($promise, $loop, self::TIMEOUT); + $data = Block\await($promise, null, self::TIMEOUT); $this->assertEquals('foo', $data); @@ -401,8 +380,6 @@ public function testEmitsDataFromConnection() public function testEmitsDataInMultipleChunksFromConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -430,7 +407,7 @@ public function testEmitsDataInMultipleChunksFromConnection() $connection->write(str_repeat('*', 400000)); }); - $received = Block\await($promise, $loop, self::TIMEOUT); + $received = Block\await($promise, null, self::TIMEOUT); $this->assertEquals(400000, $received); @@ -443,8 +420,6 @@ public function testEmitsDataInMultipleChunksFromConnection() public function testPipesDataBackInMultipleChunksFromConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -474,7 +449,7 @@ public function testPipesDataBackInMultipleChunksFromConnection() }, $reject); }); - $received = Block\await($promise, $loop, self::TIMEOUT); + $received = Block\await($promise, null, self::TIMEOUT); $this->assertEquals(400000, $received); @@ -491,8 +466,6 @@ public function testPipesDataBackInMultipleChunksFromConnection() */ public function testEmitsConnectionForNewTlsv11Connection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem', @@ -506,7 +479,7 @@ public function testEmitsConnectionForNewTlsv11Connection() )); $promise = $connector->connect($server->getAddress()); - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -520,8 +493,6 @@ public function testEmitsConnectionForNewTlsv11Connection() */ public function testEmitsErrorForClientWithTlsVersionMismatch() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem', @@ -539,7 +510,7 @@ public function testEmitsErrorForClientWithTlsVersionMismatch() $this->setExpectedException('RuntimeException', 'handshake'); try { - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } catch (\Exception $e) { $server->close(); @@ -549,8 +520,6 @@ public function testEmitsErrorForClientWithTlsVersionMismatch() public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificate() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem', @@ -567,7 +536,7 @@ public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificat )); $connector->connect($server->getAddress()); - $connection = Block\await($peer, $loop, self::TIMEOUT); + $connection = Block\await($peer, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); @@ -577,8 +546,6 @@ public function testServerEmitsConnectionForNewConnectionWithEncryptedCertificat public function testClientRejectsWithErrorForServerWithInvalidCertificate() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => 'invalid.pem' @@ -592,7 +559,7 @@ public function testClientRejectsWithErrorForServerWithInvalidCertificate() $this->setExpectedException('RuntimeException', 'handshake'); try { - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } catch (\Exception $e) { $server->close(); @@ -602,8 +569,6 @@ public function testClientRejectsWithErrorForServerWithInvalidCertificate() public function testServerEmitsErrorForClientWithInvalidCertificate() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => 'invalid.pem' @@ -624,7 +589,7 @@ public function testServerEmitsErrorForClientWithInvalidCertificate() $this->setExpectedException('RuntimeException', 'handshake'); try { - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); } catch (\Exception $e) { $server->close(); @@ -638,8 +603,6 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase $this->markTestSkipped('Not supported on Windows'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem' @@ -655,7 +618,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateMissingPassphrase $this->setExpectedException('RuntimeException', 'handshake'); try { - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } catch (\Exception $e) { $server->close(); @@ -669,8 +632,6 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph $this->markTestSkipped('Not supported on Windows'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost_swordfish.pem', @@ -687,7 +648,7 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph $this->setExpectedException('RuntimeException', 'handshake'); try { - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } catch (\Exception $e) { $server->close(); @@ -697,8 +658,6 @@ public function testEmitsErrorForServerWithEncryptedCertificateWithInvalidPassph public function testEmitsErrorForConnectionWithPeerVerification() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -712,7 +671,7 @@ public function testEmitsErrorForConnectionWithPeerVerification() $promise = $connector->connect($server->getAddress()); $promise->then(null, $this->expectCallableOnce()); - Block\await($errorEvent, $loop, self::TIMEOUT); + Block\await($errorEvent, null, self::TIMEOUT); $server->close(); } @@ -723,8 +682,6 @@ public function testEmitsErrorIfConnectionIsCancelled() $this->markTestSkipped('Linux only (OS is ' . PHP_OS . ')'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -739,15 +696,13 @@ public function testEmitsErrorIfConnectionIsCancelled() $promise->cancel(); $promise->then(null, $this->expectCallableOnce()); - Block\await($errorEvent, $loop, self::TIMEOUT); + Block\await($errorEvent, null, self::TIMEOUT); $server->close(); } public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -762,7 +717,7 @@ public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() $stream->close(); }); - $error = Block\await($errorEvent, $loop, self::TIMEOUT); + $error = Block\await($errorEvent, null, self::TIMEOUT); // Connection from tcp://127.0.0.1:39528 failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET) $this->assertInstanceOf('RuntimeException', $error); @@ -776,8 +731,6 @@ public function testEmitsErrorIfConnectionIsClosedBeforeHandshake() public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -792,7 +745,7 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() $stream->end("\x1e"); }); - $error = Block\await($errorEvent, $loop, self::TIMEOUT); + $error = Block\await($errorEvent, null, self::TIMEOUT); // Connection from tcp://127.0.0.1:39528 failed during TLS handshake: Connection lost during TLS handshake (ECONNRESET) $this->assertInstanceOf('RuntimeException', $error); @@ -806,8 +759,6 @@ public function testEmitsErrorIfConnectionIsClosedWithIncompleteHandshake() public function testEmitsNothingIfPlaintextConnectionIsIdle() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -818,7 +769,7 @@ public function testEmitsNothingIfPlaintextConnectionIsIdle() $connector = new TcpConnector(); $promise = $connector->connect(str_replace('tls://', '', $server->getAddress())); - $connection = Block\await($promise, $loop, self::TIMEOUT); + $connection = Block\await($promise, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); $server->close(); @@ -829,8 +780,6 @@ public function testEmitsNothingIfPlaintextConnectionIsIdle() public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -845,7 +794,7 @@ public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() $stream->write("GET / HTTP/1.0\r\n\r\n"); }); - $error = Block\await($errorEvent, $loop, self::TIMEOUT); + $error = Block\await($errorEvent, null, self::TIMEOUT); $this->assertInstanceOf('RuntimeException', $error); @@ -860,8 +809,6 @@ public function testEmitsErrorIfConnectionIsHttpInsteadOfSecureHandshake() public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandshake() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new SecureServer($server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -876,7 +823,7 @@ public function testEmitsErrorIfConnectionIsUnknownProtocolInsteadOfSecureHandsh $stream->write("Hello world!\n"); }); - $error = Block\await($errorEvent, $loop, self::TIMEOUT); + $error = Block\await($errorEvent, null, self::TIMEOUT); $this->assertInstanceOf('RuntimeException', $error); diff --git a/tests/FunctionalTcpServerTest.php b/tests/FunctionalTcpServerTest.php index bbf4c59e..0965f90d 100644 --- a/tests/FunctionalTcpServerTest.php +++ b/tests/FunctionalTcpServerTest.php @@ -3,7 +3,6 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\TcpConnector; @@ -15,8 +14,6 @@ class FunctionalTcpServerTest extends TestCase public function testEmitsConnectionForNewConnection() { - $loop = Loop::get(); - $server = new TcpServer(0); $server->on('connection', $this->expectCallableOnce()); @@ -29,7 +26,7 @@ public function testEmitsConnectionForNewConnection() $promise->then($this->expectCallableOnce()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); @@ -40,8 +37,6 @@ public function testEmitsConnectionForNewConnection() public function testEmitsNoConnectionForNewConnectionWhenPaused() { - $loop = Loop::get(); - $server = new TcpServer(0); $server->on('connection', $this->expectCallableNever()); $server->pause(); @@ -51,13 +46,11 @@ public function testEmitsNoConnectionForNewConnectionWhenPaused() $promise->then($this->expectCallableOnce()); - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } public function testConnectionForNewConnectionWhenResumedAfterPause() { - $loop = Loop::get(); - $server = new TcpServer(0); $server->on('connection', $this->expectCallableOnce()); $server->pause(); @@ -72,7 +65,7 @@ public function testConnectionForNewConnectionWhenResumedAfterPause() $promise->then($this->expectCallableOnce()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -82,8 +75,6 @@ public function testConnectionForNewConnectionWhenResumedAfterPause() public function testEmitsConnectionWithRemoteIp() { - $loop = Loop::get(); - $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { @@ -96,7 +87,7 @@ public function testEmitsConnectionWithRemoteIp() $promise->then($this->expectCallableOnce()); - $peer = Block\await($peer, $loop, self::TIMEOUT); + $peer = Block\await($peer, null, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $peer); @@ -108,8 +99,6 @@ public function testEmitsConnectionWithRemoteIp() public function testEmitsConnectionWithLocalIp() { - $loop = Loop::get(); - $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { @@ -124,7 +113,7 @@ public function testEmitsConnectionWithLocalIp() $promise->then($this->expectCallableOnce()); - $local = Block\await($peer, $loop, self::TIMEOUT); + $local = Block\await($peer, null, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $local); $this->assertEquals($server->getAddress(), $local); @@ -141,8 +130,6 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() $this->markTestSkipped('Skipping on Windows due to default firewall rules'); } - $loop = Loop::get(); - $server = new TcpServer('0.0.0.0:0'); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { @@ -155,7 +142,7 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() $promise->then($this->expectCallableOnce()); - $local = Block\await($peer, $loop, self::TIMEOUT); + $local = Block\await($peer, null, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $local); @@ -167,8 +154,6 @@ public function testEmitsConnectionWithLocalIpDespiteListeningOnAll() public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() { - $loop = Loop::get(); - $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { @@ -183,7 +168,7 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() $connection->end(); }); - $peer = Block\await($peer, $loop, self::TIMEOUT); + $peer = Block\await($peer, null, self::TIMEOUT); $this->assertContainsString('127.0.0.1:', $peer); @@ -192,8 +177,6 @@ public function testEmitsConnectionWithRemoteIpAfterConnectionIsClosedByPeer() public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedByServer() { - $loop = Loop::get(); - $server = new TcpServer(0); $peer = new Promise(function ($resolve, $reject) use ($server) { $server->on('connection', function (ConnectionInterface $connection) use ($resolve) { @@ -207,7 +190,7 @@ public function testEmitsConnectionWithRemoteNullAddressAfterConnectionIsClosedB $promise->then($this->expectCallableOnce()); - $peer = Block\await($peer, $loop, self::TIMEOUT); + $peer = Block\await($peer, null, self::TIMEOUT); $this->assertNull($peer); @@ -220,8 +203,6 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled() $this->markTestSkipped('Linux only (OS is ' . PHP_OS . ')'); } - $loop = Loop::get(); - $server = new TcpServer(0); $server->on('connection', $this->expectCallableOnce()); @@ -235,15 +216,13 @@ public function testEmitsConnectionEvenIfClientConnectionIsCancelled() $promise->then(null, $this->expectCallableOnce()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); } public function testEmitsConnectionForNewIpv6Connection() { - $loop = Loop::get(); - try { $server = new TcpServer('[::1]:0'); } catch (\RuntimeException $e) { @@ -261,7 +240,7 @@ public function testEmitsConnectionForNewIpv6Connection() $promise->then($this->expectCallableOnce()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -271,8 +250,6 @@ public function testEmitsConnectionForNewIpv6Connection() public function testEmitsConnectionWithRemoteIpv6() { - $loop = Loop::get(); - try { $server = new TcpServer('[::1]:0'); } catch (\RuntimeException $e) { @@ -290,7 +267,7 @@ public function testEmitsConnectionWithRemoteIpv6() $promise->then($this->expectCallableOnce()); - $peer = Block\await($peer, $loop, self::TIMEOUT); + $peer = Block\await($peer, null, self::TIMEOUT); $this->assertContainsString('[::1]:', $peer); @@ -302,8 +279,6 @@ public function testEmitsConnectionWithRemoteIpv6() public function testEmitsConnectionWithLocalIpv6() { - $loop = Loop::get(); - try { $server = new TcpServer('[::1]:0'); } catch (\RuntimeException $e) { @@ -321,7 +296,7 @@ public function testEmitsConnectionWithLocalIpv6() $promise->then($this->expectCallableOnce()); - $local = Block\await($peer, $loop, self::TIMEOUT); + $local = Block\await($peer, null, self::TIMEOUT); $this->assertContainsString('[::1]:', $local); $this->assertEquals($server->getAddress(), $local); @@ -371,8 +346,6 @@ public function testEmitsConnectionWithInheritedContextOptions() $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); } - $loop = Loop::get(); - $server = new TcpServer(0, null, array( 'backlog' => 4 )); @@ -388,7 +361,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $promise->then($this->expectCallableOnce()); - $all = Block\await($peer, $loop, self::TIMEOUT); + $all = Block\await($peer, null, self::TIMEOUT); $this->assertEquals(array('socket' => array('backlog' => 4)), $all); diff --git a/tests/IntegrationTest.php b/tests/IntegrationTest.php index 983f5f4d..faa91294 100644 --- a/tests/IntegrationTest.php +++ b/tests/IntegrationTest.php @@ -4,7 +4,6 @@ use Clue\React\Block; use React\Dns\Resolver\Factory as ResolverFactory; -use React\EventLoop\Loop; use React\Socket\Connector; use React\Socket\DnsConnector; use React\Socket\SecureConnector; @@ -18,17 +17,16 @@ class IntegrationTest extends TestCase /** @test */ public function gettingStuffFromGoogleShouldWork() { - $loop = Loop::get(); $connector = new Connector(array()); - $conn = Block\await($connector->connect('google.com:80'), $loop); + $conn = Block\await($connector->connect('google.com:80')); $this->assertContainsString(':80', $conn->getRemoteAddress()); $this->assertNotEquals('google.com:80', $conn->getRemoteAddress()); $conn->write("GET / HTTP/1.0\r\n\r\n"); - $response = $this->buffer($conn, $loop, self::TIMEOUT); + $response = $this->buffer($conn, self::TIMEOUT); $this->assertMatchesRegExp('#^HTTP/1\.0#', $response); } @@ -40,14 +38,13 @@ public function gettingEncryptedStuffFromGoogleShouldWork() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); $secureConnector = new Connector(array()); - $conn = Block\await($secureConnector->connect('tls://google.com:443'), $loop); + $conn = Block\await($secureConnector->connect('tls://google.com:443')); $conn->write("GET / HTTP/1.0\r\n\r\n"); - $response = $this->buffer($conn, $loop, self::TIMEOUT); + $response = $this->buffer($conn, self::TIMEOUT); $this->assertMatchesRegExp('#^HTTP/1\.0#', $response); } @@ -59,8 +56,6 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); - $factory = new ResolverFactory(); $dns = $factory->create('8.8.8.8'); @@ -71,11 +66,11 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() $dns ); - $conn = Block\await($connector->connect('google.com:443'), $loop); + $conn = Block\await($connector->connect('google.com:443')); $conn->write("GET / HTTP/1.0\r\n\r\n"); - $response = $this->buffer($conn, $loop, self::TIMEOUT); + $response = $this->buffer($conn, self::TIMEOUT); $this->assertMatchesRegExp('#^HTTP/1\.0#', $response); } @@ -83,17 +78,16 @@ public function gettingEncryptedStuffFromGoogleShouldWorkIfHostIsResolvedFirst() /** @test */ public function gettingPlaintextStuffFromEncryptedGoogleShouldNotWork() { - $loop = Loop::get(); $connector = new Connector(array()); - $conn = Block\await($connector->connect('google.com:443'), $loop); + $conn = Block\await($connector->connect('google.com:443')); $this->assertContainsString(':443', $conn->getRemoteAddress()); $this->assertNotEquals('google.com:443', $conn->getRemoteAddress()); $conn->write("GET / HTTP/1.0\r\n\r\n"); - $response = $this->buffer($conn, $loop, self::TIMEOUT); + $response = $this->buffer($conn, self::TIMEOUT); $this->assertDoesNotMatchRegExp('#^HTTP/1\.0#', $response); } @@ -104,17 +98,15 @@ public function testConnectingFailsIfConnectorUsesInvalidDnsResolverAddress() $this->markTestSkipped('Skipped on macOS due to a bug in reactphp/dns (solved in reactphp/dns#171)'); } - $loop = Loop::get(); - $factory = new ResolverFactory(); $dns = $factory->create('255.255.255.255'); $connector = new Connector(array( 'dns' => $dns - ), $loop); + )); $this->setExpectedException('RuntimeException'); - Block\await($connector->connect('google.com:80'), $loop, self::TIMEOUT); + Block\await($connector->connect('google.com:80'), null, self::TIMEOUT); } public function testCancellingPendingConnectionWithoutTimeoutShouldNotCreateAnyGarbageReferences() @@ -157,7 +149,6 @@ public function testWaitingForRejectedConnectionShouldNotCreateAnyGarbageReferen $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); @@ -172,11 +163,11 @@ function ($e) use (&$wait) { ); // run loop for short period to ensure we detect connection refused error - Block\sleep(0.01, $loop); + Block\sleep(0.01); if ($wait) { - Block\sleep(0.2, $loop); + Block\sleep(0.2); if ($wait) { - Block\sleep(2.0, $loop); + Block\sleep(2.0); if ($wait) { $this->fail('Connection attempt did not fail'); } @@ -193,7 +184,6 @@ public function testWaitingForConnectionTimeoutDuringDnsLookupShouldNotCreateAny $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); $connector = new Connector(array('timeout' => 0.001)); gc_collect_cycles(); @@ -208,9 +198,9 @@ function ($e) use (&$wait) { ); // run loop for short period to ensure we detect a connection timeout error - Block\sleep(0.01, $loop); + Block\sleep(0.01); if ($wait) { - Block\sleep(0.2, $loop); + Block\sleep(0.2); if ($wait) { $this->fail('Connection attempt did not fail'); } @@ -226,7 +216,6 @@ public function testWaitingForConnectionTimeoutDuringTcpConnectionShouldNotCreat $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); $connector = new Connector(array('timeout' => 0.000001)); gc_collect_cycles(); @@ -241,9 +230,9 @@ function ($e) use (&$wait) { ); // run loop for short period to ensure we detect a connection timeout error - Block\sleep(0.01, $loop); + Block\sleep(0.01); if ($wait) { - Block\sleep(0.2, $loop); + Block\sleep(0.2); if ($wait) { $this->fail('Connection attempt did not fail'); } @@ -259,7 +248,6 @@ public function testWaitingForInvalidDnsConnectionShouldNotCreateAnyGarbageRefer $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); @@ -274,11 +262,11 @@ function ($e) use (&$wait) { ); // run loop for short period to ensure we detect a DNS error - Block\sleep(0.01, $loop); + Block\sleep(0.01); if ($wait) { - Block\sleep(0.2, $loop); + Block\sleep(0.2); if ($wait) { - Block\sleep(2.0, $loop); + Block\sleep(2.0); if ($wait) { $this->fail('Connection attempt did not fail'); } @@ -298,7 +286,6 @@ public function testWaitingForInvalidTlsConnectionShouldNotCreateAnyGarbageRefer $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); $connector = new Connector(array( 'tls' => array( 'verify_peer' => true @@ -317,11 +304,11 @@ function ($e) use (&$wait) { ); // run loop for short period to ensure we detect a TLS error - Block\sleep(0.1, $loop); + Block\sleep(0.1); if ($wait) { - Block\sleep(0.4, $loop); + Block\sleep(0.4); if ($wait) { - Block\sleep(self::TIMEOUT - 0.5, $loop); + Block\sleep(self::TIMEOUT - 0.5); if ($wait) { $this->fail('Connection attempt did not fail'); } @@ -338,7 +325,6 @@ public function testWaitingForSuccessfullyClosedConnectionShouldNotCreateAnyGarb $this->markTestSkipped('Not supported on legacy Promise v1 API'); } - $loop = Loop::get(); $connector = new Connector(array('timeout' => false)); gc_collect_cycles(); @@ -347,7 +333,7 @@ function ($conn) { $conn->close(); } ); - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); unset($promise); $this->assertEquals(0, gc_collect_cycles()); @@ -355,14 +341,12 @@ function ($conn) { public function testConnectingFailsIfTimeoutIsTooSmall() { - $loop = Loop::get(); - $connector = new Connector(array( 'timeout' => 0.001 )); $this->setExpectedException('RuntimeException'); - Block\await($connector->connect('google.com:80'), $loop, self::TIMEOUT); + Block\await($connector->connect('google.com:80'), null, self::TIMEOUT); } public function testSelfSignedRejectsIfVerificationIsEnabled() @@ -371,8 +355,6 @@ public function testSelfSignedRejectsIfVerificationIsEnabled() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); - $connector = new Connector(array( 'tls' => array( 'verify_peer' => true @@ -380,7 +362,7 @@ public function testSelfSignedRejectsIfVerificationIsEnabled() )); $this->setExpectedException('RuntimeException'); - Block\await($connector->connect('tls://self-signed.badssl.com:443'), $loop, self::TIMEOUT); + Block\await($connector->connect('tls://self-signed.badssl.com:443'), null, self::TIMEOUT); } public function testSelfSignedResolvesIfVerificationIsDisabled() @@ -389,15 +371,13 @@ public function testSelfSignedResolvesIfVerificationIsDisabled() $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); - $connector = new Connector(array( 'tls' => array( 'verify_peer' => false ) )); - $conn = Block\await($connector->connect('tls://self-signed.badssl.com:443'), $loop, self::TIMEOUT); + $conn = Block\await($connector->connect('tls://self-signed.badssl.com:443'), null, self::TIMEOUT); $conn->close(); // if we reach this, then everything is good diff --git a/tests/LimitingServerTest.php b/tests/LimitingServerTest.php index 4a8d50bb..119fba40 100644 --- a/tests/LimitingServerTest.php +++ b/tests/LimitingServerTest.php @@ -3,7 +3,6 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\LimitingServer; @@ -143,8 +142,6 @@ public function testPausingServerWillBePausedOnceLimitIsReached() public function testSocketDisconnectionWillRemoveFromList() { - $loop = Loop::get(); - $tcp = new TcpServer(0); $socket = stream_socket_client($tcp->getAddress()); @@ -160,7 +157,7 @@ public function testSocketDisconnectionWillRemoveFromList() }); }); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $this->assertEquals(array(), $server->getConnections()); @@ -169,8 +166,6 @@ public function testSocketDisconnectionWillRemoveFromList() public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOperatingSystem() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new LimitingServer($server, 1, true); $server->on('connection', $this->expectCallableOnce()); @@ -183,7 +178,7 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper $first = stream_socket_client($server->getAddress()); $second = stream_socket_client($server->getAddress()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); fclose($first); fclose($second); @@ -193,8 +188,6 @@ public function testPausingServerWillEmitOnlyOneButAcceptTwoConnectionsDueToOper public function testPausingServerWillEmitTwoConnectionsFromBacklog() { - $loop = Loop::get(); - $server = new TcpServer(0); $server = new LimitingServer($server, 1, true); $server->on('error', $this->expectCallableNever()); @@ -215,7 +208,7 @@ public function testPausingServerWillEmitTwoConnectionsFromBacklog() $second = stream_socket_client($server->getAddress()); fclose($second); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); } diff --git a/tests/SecureConnectorTest.php b/tests/SecureConnectorTest.php index 2a8773be..af3a6f58 100644 --- a/tests/SecureConnectorTest.php +++ b/tests/SecureConnectorTest.php @@ -5,7 +5,6 @@ use React\Promise; use React\Promise\Deferred; use React\Socket\SecureConnector; -use React\EventLoop\Loop; class SecureConnectorTest extends TestCase { diff --git a/tests/SecureIntegrationTest.php b/tests/SecureIntegrationTest.php index 93f94ccd..5cf741cb 100644 --- a/tests/SecureIntegrationTest.php +++ b/tests/SecureIntegrationTest.php @@ -2,7 +2,6 @@ namespace React\Tests\Socket; -use React\EventLoop\Loop; use React\Socket\TcpServer; use React\Socket\SecureServer; use React\Socket\TcpConnector; @@ -17,7 +16,6 @@ class SecureIntegrationTest extends TestCase { const TIMEOUT = 2; - private $loop; private $server; private $connector; private $address; @@ -31,7 +29,6 @@ public function setUpConnector() $this->markTestSkipped('Not supported on legacy HHVM'); } - $this->loop = Loop::get(); $this->server = new TcpServer(0); $this->server = new SecureServer($this->server, null, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -53,7 +50,7 @@ public function tearDownServer() public function testConnectToServer() { - $client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT); + $client = Block\await($this->connector->connect($this->address), null, self::TIMEOUT); /* @var $client ConnectionInterface */ $client->close(); @@ -68,7 +65,7 @@ public function testConnectToServerEmitsConnection() $promiseClient = $this->connector->connect($this->address); - list($_, $client) = Block\awaitAll(array($promiseServer, $promiseClient), $this->loop, self::TIMEOUT); + list($_, $client) = Block\awaitAll(array($promiseServer, $promiseClient), null, self::TIMEOUT); /* @var $client ConnectionInterface */ $client->close(); @@ -84,13 +81,13 @@ public function testSendSmallDataToServerReceivesOneChunk() }); }); - $client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT); + $client = Block\await($this->connector->connect($this->address), null, self::TIMEOUT); /* @var $client ConnectionInterface */ $client->write('hello'); // await server to report one "data" event - $data = Block\await($received->promise(), $this->loop, self::TIMEOUT); + $data = Block\await($received->promise(), null, self::TIMEOUT); $client->close(); @@ -125,14 +122,14 @@ public function testSendDataWithEndToServerReceivesAllData() }); }); - $client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT); + $client = Block\await($this->connector->connect($this->address), null, self::TIMEOUT); /* @var $client ConnectionInterface */ $data = str_repeat('a', 200000); $client->end($data); // await server to report connection "close" event - $received = Block\await($disconnected->promise(), $this->loop, self::TIMEOUT); + $received = Block\await($disconnected->promise(), null, self::TIMEOUT); $this->assertEquals(strlen($data), strlen($received)); $this->assertEquals($data, $received); @@ -160,7 +157,7 @@ public function testSendDataWithoutEndingToServerReceivesAllData() $connection->write($data); }); - $received = Block\await($promise, $this->loop, self::TIMEOUT); + $received = Block\await($promise, null, self::TIMEOUT); $this->assertEquals(strlen($data), strlen($received)); $this->assertEquals($data, $received); @@ -176,12 +173,12 @@ public function testConnectToServerWhichSendsSmallDataReceivesOneChunk() $peer->write('hello'); }); - $client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT); + $client = Block\await($this->connector->connect($this->address), null, self::TIMEOUT); /* @var $client ConnectionInterface */ // await client to report one "data" event $receive = $this->createPromiseForEvent($client, 'data', $this->expectCallableOnceWith('hello')); - Block\await($receive, $this->loop, self::TIMEOUT); + Block\await($receive, null, self::TIMEOUT); $client->close(); } @@ -193,11 +190,11 @@ public function testConnectToServerWhichSendsDataWithEndReceivesAllData() $peer->end($data); }); - $client = Block\await($this->connector->connect($this->address), $this->loop, self::TIMEOUT); + $client = Block\await($this->connector->connect($this->address), null, self::TIMEOUT); /* @var $client ConnectionInterface */ // await data from client until it closes - $received = $this->buffer($client, $this->loop, self::TIMEOUT); + $received = $this->buffer($client, self::TIMEOUT); $this->assertEquals($data, $received); } @@ -224,7 +221,7 @@ public function testConnectToServerWhichSendsDataWithoutEndingReceivesAllData() }, $reject); }); - $received = Block\await($promise, $this->loop, self::TIMEOUT); + $received = Block\await($promise, null, self::TIMEOUT); $this->assertEquals(strlen($data), $received); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 484e09bb..7c6af61d 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -3,7 +3,6 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\Server; @@ -48,15 +47,13 @@ public function testConstructorThrowsForInvalidUri() public function testConstructorCreatesExpectedTcpServer() { - $loop = Loop::get(); - $server = new Server(0); $connector = new TcpConnector(); $promise = $connector->connect($server->getAddress()); $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); - $connection = Block\await($connector->connect($server->getAddress()), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect($server->getAddress()), null, self::TIMEOUT); $server->close(); $promise->then(function (ConnectionInterface $connection) { @@ -73,15 +70,13 @@ public function testConstructorCreatesExpectedUnixServer() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Loop::get(); - $server = new Server($this->getRandomSocketUri()); $connector = new UnixConnector(); $connector->connect($server->getAddress()) ->then($this->expectCallableOnce(), $this->expectCallableNever()); - $connection = Block\await($connector->connect($server->getAddress()), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect($server->getAddress()), null, self::TIMEOUT); $connection->close(); $server->close(); @@ -124,8 +119,6 @@ public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() public function testEmitsConnectionForNewConnection() { - $loop = Loop::get(); - $server = new Server(0); $server->on('connection', $this->expectCallableOnce()); @@ -135,28 +128,24 @@ public function testEmitsConnectionForNewConnection() $client = stream_socket_client($server->getAddress()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); } public function testDoesNotEmitConnectionForNewConnectionToPausedServer() { - $loop = Loop::get(); - $server = new Server(0); $server->pause(); $server->on('connection', $this->expectCallableNever()); $client = stream_socket_client($server->getAddress()); - Block\sleep(0.1, $loop); + Block\sleep(0.1, null); } public function testDoesEmitConnectionForNewConnectionToResumedServer() { - $loop = Loop::get(); - $server = new Server(0); $server->pause(); $server->on('connection', $this->expectCallableOnce()); @@ -169,7 +158,7 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() $server->resume(); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $server->close(); } @@ -193,8 +182,6 @@ public function testEmitsConnectionWithInheritedContextOptions() $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); } - $loop = Loop::get(); - $server = new Server(0, null, array( 'backlog' => 4 )); @@ -208,7 +195,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $client = stream_socket_client($server->getAddress()); - $all = Block\await($peer, $loop, self::TIMEOUT); + $all = Block\await($peer, null, self::TIMEOUT); $this->assertEquals(array('socket' => array('backlog' => 4)), $all); @@ -221,8 +208,6 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); - $server = new Server('tls://127.0.0.1:0', null, array( 'tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -232,7 +217,7 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $client = stream_socket_client(str_replace('tls://', '', $server->getAddress())); - Block\sleep(0.1, $loop); + Block\sleep(0.1, null); $server->close(); } diff --git a/tests/SocketServerTest.php b/tests/SocketServerTest.php index e523c075..0011fbaa 100644 --- a/tests/SocketServerTest.php +++ b/tests/SocketServerTest.php @@ -3,7 +3,6 @@ namespace React\Tests\Socket; use Clue\React\Block; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\SocketServer; @@ -69,15 +68,13 @@ public function testConstructorWithInvalidUriWithSchemaAndPortOnlyThrows() public function testConstructorCreatesExpectedTcpServer() { - $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array()); $connector = new TcpConnector(); $promise = $connector->connect($socket->getAddress()); $promise->then($this->expectCallableOnce(), $this->expectCallableNever()); - $connection = Block\await($connector->connect($socket->getAddress()), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect($socket->getAddress()), null, self::TIMEOUT); $socket->close(); $promise->then(function (ConnectionInterface $connection) { @@ -94,15 +91,13 @@ public function testConstructorCreatesExpectedUnixServer() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $loop = Loop::get(); - $socket = new SocketServer($this->getRandomSocketUri(), array()); $connector = new UnixConnector(); $connector->connect($socket->getAddress()) ->then($this->expectCallableOnce(), $this->expectCallableNever()); - $connection = Block\await($connector->connect($socket->getAddress()), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect($socket->getAddress()), null, self::TIMEOUT); $socket->close(); } @@ -159,8 +154,6 @@ public function testEmitsErrorWhenUnderlyingTcpServerEmitsError() public function testEmitsConnectionForNewConnection() { - $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array()); $socket->on('connection', $this->expectCallableOnce()); @@ -170,28 +163,24 @@ public function testEmitsConnectionForNewConnection() $client = stream_socket_client($socket->getAddress()); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $socket->close(); } public function testDoesNotEmitConnectionForNewConnectionToPausedServer() { - $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array()); $socket->pause(); $socket->on('connection', $this->expectCallableNever()); $client = stream_socket_client($socket->getAddress()); - Block\sleep(0.1, $loop); + Block\sleep(0.1); } public function testDoesEmitConnectionForNewConnectionToResumedServer() { - $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array()); $socket->pause(); $socket->on('connection', $this->expectCallableOnce()); @@ -204,7 +193,7 @@ public function testDoesEmitConnectionForNewConnectionToResumedServer() $socket->resume(); - Block\await($peer, $loop, self::TIMEOUT); + Block\await($peer, null, self::TIMEOUT); $socket->close(); } @@ -228,8 +217,6 @@ public function testEmitsConnectionWithInheritedContextOptions() $this->markTestSkipped('Not supported on legacy HHVM < 3.13'); } - $loop = Loop::get(); - $socket = new SocketServer('127.0.0.1:0', array( 'tcp' => array( 'backlog' => 4 @@ -245,7 +232,7 @@ public function testEmitsConnectionWithInheritedContextOptions() $client = stream_socket_client($socket->getAddress()); - $all = Block\await($peer, $loop, self::TIMEOUT); + $all = Block\await($peer, null, self::TIMEOUT); $this->assertEquals(array('socket' => array('backlog' => 4)), $all); @@ -258,8 +245,6 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $this->markTestSkipped('Not supported on legacy HHVM'); } - $loop = Loop::get(); - $socket = new SocketServer('tls://127.0.0.1:0', array( 'tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' @@ -269,7 +254,7 @@ public function testDoesNotEmitSecureConnectionForNewPlaintextConnectionThatIsId $client = stream_socket_client(str_replace('tls://', '', $socket->getAddress())); - Block\sleep(0.1, $loop); + Block\sleep(0.1); $socket->close(); } diff --git a/tests/TcpConnectorTest.php b/tests/TcpConnectorTest.php index 0a82dfae..0a9da7ca 100644 --- a/tests/TcpConnectorTest.php +++ b/tests/TcpConnectorTest.php @@ -27,8 +27,6 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() /** @test */ public function connectionToEmptyPortShouldFail() { - $loop = Loop::get(); - $connector = new TcpConnector(); $promise = $connector->connect('127.0.0.1:9999'); @@ -37,7 +35,7 @@ public function connectionToEmptyPortShouldFail() 'Connection to tcp://127.0.0.1:9999 failed: Connection refused' . (function_exists('socket_import_stream') ? ' (ECONNREFUSED)' : ''), defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } /** @test */ @@ -61,13 +59,11 @@ public function connectionToTcpServerShouldAddResourceToLoop() /** @test */ public function connectionToTcpServerShouldSucceed() { - $loop = Loop::get(); - $server = new TcpServer(9999); $connector = new TcpConnector(); - $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect('127.0.0.1:9999'), null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); @@ -78,8 +74,6 @@ public function connectionToTcpServerShouldSucceed() /** @test */ public function connectionToTcpServerShouldFailIfFileDescriptorsAreExceeded() { - $loop = Loop::get(); - $connector = new TcpConnector(); /** @var string[] $_ */ @@ -121,7 +115,7 @@ class_exists('PHPUnit\Framework\Error\Warning', true); } $this->setExpectedException('RuntimeException'); - Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); + Block\await($connector->connect('127.0.0.1:9999'), null, self::TIMEOUT); } /** @test */ @@ -144,7 +138,6 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() $this->markTestSkipped('Expected error ' . $enetunreach . ' but got ' . $errno . ' (' . $errstr . ') for ' . $address); } - $loop = Loop::get(); $connector = new TcpConnector(); $promise = $connector->connect($address); @@ -156,7 +149,7 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() ); try { - Block\await($promise, $loop, self::TIMEOUT); + Block\await($promise, null, self::TIMEOUT); } catch (\Exception $e) { fclose($client); @@ -167,13 +160,11 @@ public function connectionToInvalidNetworkShouldFailWithUnreachableError() /** @test */ public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget() { - $loop = Loop::get(); - $server = new TcpServer(9999); $connector = new TcpConnector(); - $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect('127.0.0.1:9999'), null, self::TIMEOUT); /* @var $connection ConnectionInterface */ $this->assertEquals('tcp://127.0.0.1:9999', $connection->getRemoteAddress()); @@ -185,13 +176,11 @@ public function connectionToTcpServerShouldSucceedWithRemoteAdressSameAsTarget() /** @test */ public function connectionToTcpServerShouldSucceedWithLocalAdressOnLocalhost() { - $loop = Loop::get(); - $server = new TcpServer(9999); $connector = new TcpConnector(); - $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect('127.0.0.1:9999'), null, self::TIMEOUT); /* @var $connection ConnectionInterface */ $this->assertContainsString('tcp://127.0.0.1:', $connection->getLocalAddress()); @@ -204,13 +193,11 @@ public function connectionToTcpServerShouldSucceedWithLocalAdressOnLocalhost() /** @test */ public function connectionToTcpServerShouldSucceedWithNullAddressesAfterConnectionClosed() { - $loop = Loop::get(); - $server = new TcpServer(9999); $connector = new TcpConnector(); - $connection = Block\await($connector->connect('127.0.0.1:9999'), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect('127.0.0.1:9999'), null, self::TIMEOUT); /* @var $connection ConnectionInterface */ $server->close(); @@ -256,8 +243,6 @@ public function connectionToEmptyIp6PortShouldFail() /** @test */ public function connectionToIp6TcpServerShouldSucceed() { - $loop = Loop::get(); - try { $server = new TcpServer('[::1]:9999'); } catch (\Exception $e) { @@ -266,7 +251,7 @@ public function connectionToIp6TcpServerShouldSucceed() $connector = new TcpConnector(); - $connection = Block\await($connector->connect('[::1]:9999'), $loop, self::TIMEOUT); + $connection = Block\await($connector->connect('[::1]:9999'), null, self::TIMEOUT); /* @var $connection ConnectionInterface */ $this->assertEquals('tcp://[::1]:9999', $connection->getRemoteAddress()); @@ -351,7 +336,6 @@ public function cancellingConnectionShouldRemoveResourceFromLoopAndCloseResource /** @test */ public function cancellingConnectionShouldRejectPromise() { - $loop = Loop::get(); $connector = new TcpConnector(); $server = new TcpServer(0); @@ -366,7 +350,7 @@ public function cancellingConnectionShouldRejectPromise() ); try { - Block\await($promise, $loop); + Block\await($promise); } catch (\Exception $e) { $server->close(); throw $e; diff --git a/tests/TcpServerTest.php b/tests/TcpServerTest.php index 22e6675c..dae7e7d7 100644 --- a/tests/TcpServerTest.php +++ b/tests/TcpServerTest.php @@ -12,15 +12,9 @@ class TcpServerTest extends TestCase { const TIMEOUT = 5.0; - private $loop; private $server; private $port; - private function createLoop() - { - return Loop::get(); - } - /** * @before * @covers React\Socket\TcpServer::__construct @@ -28,7 +22,6 @@ private function createLoop() */ public function setUpServer() { - $this->loop = $this->createLoop(); $this->server = new TcpServer(0); $this->port = parse_url($this->server->getAddress(), PHP_URL_PORT); @@ -60,7 +53,7 @@ public function testServerEmitsConnectionEventForNewConnection() $server->on('connection', $resolve); }); - $connection = Block\await($promise, $this->loop, self::TIMEOUT); + $connection = Block\await($promise, null, self::TIMEOUT); $this->assertInstanceOf('React\Socket\ConnectionInterface', $connection); } @@ -373,6 +366,6 @@ private function tick() $this->markTestSkipped('Not supported on Windows'); } - Block\sleep(0, $this->loop); + Block\sleep(0); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 6010b827..daf4e4aa 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,7 +3,6 @@ namespace React\Tests\Socket; use React\Stream\ReadableStreamInterface; -use React\EventLoop\LoopInterface; use Clue\React\Block; use React\Promise\Promise; use PHPUnit\Framework\TestCase as BaseTestCase; @@ -71,7 +70,7 @@ protected function createCallableMock() return $this->getMockBuilder('React\Tests\Socket\Stub\CallableStub')->getMock(); } - protected function buffer(ReadableStreamInterface $stream, LoopInterface $loop, $timeout) + protected function buffer(ReadableStreamInterface $stream, $timeout) { if (!$stream->isReadable()) { return ''; @@ -94,7 +93,7 @@ function () use ($stream) { $stream->close(); throw new \RuntimeException(); } - ), $loop, $timeout); + ), null, $timeout); } public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) diff --git a/tests/TimeoutConnectorTest.php b/tests/TimeoutConnectorTest.php index d918aea0..806b16c5 100644 --- a/tests/TimeoutConnectorTest.php +++ b/tests/TimeoutConnectorTest.php @@ -30,8 +30,6 @@ public function testRejectsWithTimeoutReasonOnTimeout() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Loop::get(); - $timeout = new TimeoutConnector($connector, 0.01); $this->setExpectedException( @@ -39,7 +37,7 @@ public function testRejectsWithTimeoutReasonOnTimeout() 'Connection to google.com:80 timed out after 0.01 seconds (ETIMEDOUT)', \defined('SOCKET_ETIMEDOUT') ? \SOCKET_ETIMEDOUT : 110 ); - Block\await($timeout->connect('google.com:80'), $loop); + Block\await($timeout->connect('google.com:80')); } public function testRejectsWithOriginalReasonWhenConnectorRejects() @@ -49,8 +47,6 @@ public function testRejectsWithOriginalReasonWhenConnectorRejects() $connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); $connector->expects($this->once())->method('connect')->with('google.com:80')->will($this->returnValue($promise)); - $loop = Loop::get(); - $timeout = new TimeoutConnector($connector, 5.0); $this->setExpectedException( @@ -58,7 +54,7 @@ public function testRejectsWithOriginalReasonWhenConnectorRejects() 'Failed', 42 ); - Block\await($timeout->connect('google.com:80'), $loop); + Block\await($timeout->connect('google.com:80')); } public function testResolvesWhenConnectorResolves() diff --git a/tests/UnixServerTest.php b/tests/UnixServerTest.php index 0d996641..8c0b08f6 100644 --- a/tests/UnixServerTest.php +++ b/tests/UnixServerTest.php @@ -9,7 +9,6 @@ class UnixServerTest extends TestCase { - private $loop; private $server; private $uds; @@ -24,7 +23,6 @@ public function setUpServer() $this->markTestSkipped('Unix domain sockets (UDS) not supported on your platform (Windows?)'); } - $this->loop = Loop::get(); $this->uds = $this->getRandomSocketUri(); $this->server = new UnixServer($this->uds); } @@ -362,6 +360,6 @@ private function getRandomSocketUri() private function tick() { - Block\sleep(0, $this->loop); + Block\sleep(0); } }