From 4daf96289b3979077e48fb6b8261d5fdcfecc9e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 25 Oct 2022 13:11:06 +0200 Subject: [PATCH] Improve test suite, clean up leftover `.sock` files --- tests/FdServerTest.php | 3 +++ tests/ServerTest.php | 3 +++ tests/SocketServerTest.php | 4 +++ tests/UnixServerTest.php | 53 ++++++++++++++++++++++++++++++++------ 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/tests/FdServerTest.php b/tests/FdServerTest.php index e464a1b6..efeb1f45 100644 --- a/tests/FdServerTest.php +++ b/tests/FdServerTest.php @@ -192,6 +192,9 @@ public function testGetAddressReturnsSameAddressAsOriginalSocketForUnixDomainSoc $this->markTestSkipped('Listening on Unix domain socket (UDS) not supported'); } + assert(is_resource($socket)); + unlink(str_replace('unix://', '', stream_socket_get_name($socket, false))); + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $server = new FdServer($fd, $loop); diff --git a/tests/ServerTest.php b/tests/ServerTest.php index a87a9b6e..f69e6cb1 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -76,6 +76,9 @@ public function testConstructorCreatesExpectedUnixServer() ->then($this->expectCallableOnce(), $this->expectCallableNever()); $connection = \React\Async\await(\React\Promise\Timer\timeout($connector->connect($server->getAddress()), self::TIMEOUT)); + assert($connection instanceof ConnectionInterface); + + unlink(str_replace('unix://', '', $connection->getRemoteAddress())); $connection->close(); $server->close(); diff --git a/tests/SocketServerTest.php b/tests/SocketServerTest.php index 9bc50340..e4e827ef 100644 --- a/tests/SocketServerTest.php +++ b/tests/SocketServerTest.php @@ -97,7 +97,11 @@ public function testConstructorCreatesExpectedUnixServer() ->then($this->expectCallableOnce(), $this->expectCallableNever()); $connection = \React\Async\await(\React\Promise\Timer\timeout($connector->connect($socket->getAddress()), self::TIMEOUT)); + assert($connection instanceof ConnectionInterface); + unlink(str_replace('unix://', '', $connection->getRemoteAddress())); + + $connection->close(); $socket->close(); } diff --git a/tests/UnixServerTest.php b/tests/UnixServerTest.php index 6a697f97..45cf936c 100644 --- a/tests/UnixServerTest.php +++ b/tests/UnixServerTest.php @@ -8,7 +8,10 @@ class UnixServerTest extends TestCase { + /** @var ?UnixServer */ private $server; + + /** @var ?string */ private $uds; /** @@ -28,7 +31,10 @@ public function setUpServer() public function testConstructWithoutLoopAssignsLoopAutomatically() { - $server = new UnixServer($this->getRandomSocketUri()); + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + + $server = new UnixServer($this->uds); $ref = new \ReflectionProperty($server, 'loop'); $ref->setAccessible(true); @@ -45,9 +51,11 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() public function testConnection() { $client = stream_socket_client($this->uds); + assert(is_resource($client)); $this->server->on('connection', $this->expectCallableOnce()); $this->tick(); + $this->tick(); } /** @@ -56,8 +64,11 @@ public function testConnection() public function testConnectionWithManyClients() { $client1 = stream_socket_client($this->uds); + assert(is_resource($client1)); $client2 = stream_socket_client($this->uds); + assert(is_resource($client2)); $client3 = stream_socket_client($this->uds); + assert(is_resource($client3)); $this->server->on('connection', $this->expectCallableExactly(3)); $this->tick(); @@ -68,6 +79,7 @@ public function testConnectionWithManyClients() public function testDataEventWillNotBeEmittedWhenClientSendsNoData() { $client = stream_socket_client($this->uds); + assert(is_resource($client)); $mock = $this->expectCallableNever(); @@ -81,6 +93,7 @@ public function testDataEventWillNotBeEmittedWhenClientSendsNoData() public function testDataWillBeEmittedWithDataClientSends() { $client = stream_socket_client($this->uds); + assert(is_resource($client)); fwrite($client, "foo\n"); @@ -138,6 +151,7 @@ public function testGetAddressAfterCloseReturnsNull() public function testLoopWillEndWhenServerIsClosedAfterSingleConnection() { $client = stream_socket_client($this->uds); + assert(is_resource($client)); // explicitly unset server because we only accept a single connection // and then already call close() @@ -191,6 +205,7 @@ public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmo public function testConnectionDoesNotEndWhenClientDoesNotClose() { $client = stream_socket_client($this->uds); + assert(is_resource($client)); $mock = $this->expectCallableNever(); @@ -221,10 +236,13 @@ public function testConnectionDoesEndWhenClientCloses() public function testCtorAddsResourceToLoop() { + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $loop->expects($this->once())->method('addReadStream'); - $server = new UnixServer($this->getRandomSocketUri(), $loop); + new UnixServer($this->uds, $loop); } public function testCtorThrowsForInvalidAddressScheme() @@ -264,43 +282,58 @@ public function testCtorThrowsWhenPathIsNotWritableWithoutCallingCustomErrorHand public function testResumeWithoutPauseIsNoOp() { + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $loop->expects($this->once())->method('addReadStream'); - $server = new UnixServer($this->getRandomSocketUri(), $loop); + $server = new UnixServer($this->uds, $loop); $server->resume(); } public function testPauseRemovesResourceFromLoop() { + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $loop->expects($this->once())->method('removeReadStream'); - $server = new UnixServer($this->getRandomSocketUri(), $loop); + $server = new UnixServer($this->uds, $loop); $server->pause(); } public function testPauseAfterPauseIsNoOp() { + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $loop->expects($this->once())->method('removeReadStream'); - $server = new UnixServer($this->getRandomSocketUri(), $loop); + $server = new UnixServer($this->uds, $loop); $server->pause(); $server->pause(); } public function testCloseRemovesResourceFromLoop() { + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $loop->expects($this->once())->method('removeReadStream'); - $server = new UnixServer($this->getRandomSocketUri(), $loop); + $server = new UnixServer($this->uds, $loop); $server->close(); } public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHandler() { + unlink(str_replace('unix://', '', $this->uds)); + $this->uds = $this->getRandomSocketUri(); + $listener = null; $loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock(); $loop->expects($this->once())->method('addReadStream')->with($this->anything(), $this->callback(function ($cb) use (&$listener) { @@ -308,7 +341,7 @@ public function testEmitsErrorWhenAcceptListenerFailsWithoutCallingCustomErrorHa return true; })); - $server = new UnixServer($this->getRandomSocketUri(), $loop); + $server = new UnixServer($this->uds, $loop); $exception = null; $server->on('error', function ($e) use (&$exception) { @@ -361,7 +394,7 @@ public function testListenOnBusyPortThrows() } $this->setExpectedException('RuntimeException'); - $another = new UnixServer($this->uds); + new UnixServer($this->uds); } /** @@ -372,7 +405,11 @@ public function tearDownServer() { if ($this->server) { $this->server->close(); + $this->server = null; } + + assert(is_string($this->uds)); + unlink(str_replace('unix://', '', $this->uds)); } private function getRandomSocketUri()