diff --git a/composer.json b/composer.json index d462381..3f824db 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,9 @@ "react/socket": "^1.12" }, "require-dev": { - "clue/block-react": "^1.5", "clue/connection-manager-extra": "^1.3", "phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36", + "react/async": "^4 || ^3 || ^2", "react/event-loop": "^1.2", "react/http": "^1.6" }, diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index e5323aa..0c52325 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -2,10 +2,8 @@ namespace Clue\Tests\React\Socks; -use Clue\React\Block; use Clue\React\Socks\Client; use Clue\React\Socks\Server; -use React\EventLoop\Loop; use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Socket\Connector; @@ -20,6 +18,7 @@ class FunctionalTest extends TestCase private $connector; private $client; + private $socket; private $port; private $server; @@ -28,8 +27,8 @@ class FunctionalTest extends TestCase */ public function setUpClientServer() { - $socket = new SocketServer('127.0.0.1:0'); - $address = $socket->getAddress(); + $this->socket = new SocketServer('127.0.0.1:0'); + $address = $this->socket->getAddress(); if (strpos($address, '://') === false) { $address = 'tcp://' . $address; } @@ -37,11 +36,19 @@ public function setUpClientServer() $this->assertNotEquals(0, $this->port); $this->server = new Server(); - $this->server->listen($socket); + $this->server->listen($this->socket); $this->connector = new TcpConnector(); $this->client = new Client('127.0.0.1:' . $this->port, $this->connector); } + /** + * @after + */ + public function closeSocket() + { + $this->socket->close(); + } + /** @group internet */ public function testConnection() { @@ -117,6 +124,8 @@ public function testConnectionSocksOverTls() $this->client = new Client(str_replace('tls:', 'sockss:', $socket->getAddress()), $this->connector); $this->assertResolveStream($this->client->connect('www.google.com:80')); + + $socket->close(); } /** @@ -146,6 +155,8 @@ public function testConnectionSocksOverTlsUsesPeerNameFromSocksUri() $this->client = new Client(str_replace('tls:', 'sockss:', $socket->getAddress()), $this->connector); $this->assertResolveStream($this->client->connect('www.google.com:80')); + + $socket->close(); } /** @group internet */ @@ -166,6 +177,8 @@ public function testConnectionSocksOverUnix() $this->assertResolveStream($this->client->connect('www.google.com:80')); unlink($path); + + $socket->close(); } /** @group internet */ @@ -186,6 +199,8 @@ public function testConnectionSocks5OverUnix() $this->assertResolveStream($this->client->connect('www.google.com:80')); unlink($path); + + $socket->close(); } /** @group internet */ @@ -206,6 +221,8 @@ public function testConnectionSocksWithAuthenticationOverUnix() $this->assertResolveStream($this->client->connect('www.google.com:80')); unlink($path); + + $socket->close(); } /** @group internet */ @@ -220,6 +237,8 @@ public function testConnectionAuthenticationFromUri() $this->client = new Client('name:pass@127.0.0.1:' . $this->port, $this->connector); $this->assertResolveStream($this->client->connect('www.google.com:80')); + + $socket->close(); } /** @group internet */ @@ -244,6 +263,8 @@ public function testConnectionAuthenticationCallback() $this->assertResolveStream($this->client->connect('www.google.com:80')); $this->assertEquals(1, $called); + + $socket->close(); } /** @group internet */ @@ -257,6 +278,9 @@ public function testConnectionAuthenticationCallbackWillNotBeInvokedIfClientsSen }); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -278,6 +302,8 @@ public function testConnectionAuthenticationFromUriEncoded() $this->client = new Client(rawurlencode('name') . ':' . rawurlencode('p@ss:w0rd') . '@127.0.0.1:' . $this->port, $this->connector); $this->assertResolveStream($this->client->connect('www.google.com:80')); + + $socket->close(); } /** @group internet */ @@ -292,6 +318,8 @@ public function testConnectionAuthenticationFromUriWithOnlyUserAndNoPassword() $this->client = new Client('empty@127.0.0.1:' . $this->port, $this->connector); $this->assertResolveStream($this->client->connect('www.google.com:80')); + + $socket->close(); } /** @group internet */ @@ -306,6 +334,8 @@ public function testConnectionAuthenticationEmptyPassword() $this->client = new Client('user@127.0.0.1:' . $this->port, $this->connector); $this->assertResolveStream($this->client->connect('www.google.com:80')); + + $socket->close(); } /** @group internet */ @@ -321,6 +351,9 @@ public function testConnectionInvalidNoAuthenticationOverLegacySocks4() $this->server = new Server(null, null, array('name' => 'pass')); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -334,6 +367,9 @@ public function testConnectionInvalidNoAuthentication() $this->server = new Server(null, null, array('name' => 'pass')); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -347,6 +383,9 @@ public function testConnectionInvalidAuthenticationMismatch() $this->server = new Server(null, null, array('name' => 'pass')); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -362,6 +401,9 @@ public function testConnectionInvalidAuthenticatorReturnsFalse() }); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -377,6 +419,9 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal }); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -388,10 +433,13 @@ public function testConnectionInvalidAuthenticatorReturnsPromiseFulfilledWithFal public function testConnectionInvalidAuthenticatorReturnsPromiseRejected() { $this->server = new Server(null, null, function () { - return \React\Promise\reject(); + return \React\Promise\reject(new \RuntimeException()); }); $socket = new SocketServer('127.0.0.1:0'); + $socket->on('connection', function () use ($socket) { + $socket->close(); + }); $this->server->listen($socket); $this->port = parse_url($socket->getAddress(), PHP_URL_PORT); @@ -457,7 +505,12 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateFailsWit $ssl = new SecureConnector($this->client, null, array('verify_peer' => true)); - $this->assertRejectPromise($ssl->connect($socket->getAddress())); + $promise = $ssl->connect($socket->getAddress()); + $promise->then(null, function () use ($socket) { + $socket->close(); + }); + + $this->assertRejectPromise($promise); } public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWithoutVerifyPeer() @@ -480,6 +533,8 @@ public function testSecureConnectionToTlsServerWithSelfSignedCertificateWorksWit $this->assertResolveStream($ssl->connect($socket->getAddress())); $this->assertResolveStream($promise); + + $socket->close(); } /** @group internet */ @@ -511,7 +566,7 @@ private function assertResolveStream($promise) $stream->close(); }); - Block\await($promise, Loop::get(), 2.0); + \React\Async\await($promise); } private function assertRejectPromise($promise, $message = null, $code = null) @@ -528,6 +583,6 @@ private function assertRejectPromise($promise, $message = null, $code = null) $this->setExpectedException('Exception', $message, $code); } - Block\await($promise, Loop::get(), 2.0); + \React\Async\await($promise); } }