Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test suite, clean up leftover .sock files #301

Merged
merged 1 commit into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tests/FdServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions tests/SocketServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
53 changes: 45 additions & 8 deletions tests/UnixServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

class UnixServerTest extends TestCase
{
/** @var ?UnixServer */
private $server;

/** @var ?string */
private $uds;

/**
Expand All @@ -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);
Expand All @@ -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();
}

/**
Expand All @@ -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();
Expand All @@ -68,6 +79,7 @@ public function testConnectionWithManyClients()
public function testDataEventWillNotBeEmittedWhenClientSendsNoData()
{
$client = stream_socket_client($this->uds);
assert(is_resource($client));

$mock = $this->expectCallableNever();

Expand All @@ -81,6 +93,7 @@ public function testDataEventWillNotBeEmittedWhenClientSendsNoData()
public function testDataWillBeEmittedWithDataClientSends()
{
$client = stream_socket_client($this->uds);
assert(is_resource($client));

fwrite($client, "foo\n");

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -191,6 +205,7 @@ public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmo
public function testConnectionDoesNotEndWhenClientDoesNotClose()
{
$client = stream_socket_client($this->uds);
assert(is_resource($client));

$mock = $this->expectCallableNever();

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -264,51 +282,66 @@ 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) {
$listener = $cb;
return true;
}));

$server = new UnixServer($this->getRandomSocketUri(), $loop);
$server = new UnixServer($this->uds, $loop);

$exception = null;
$server->on('error', function ($e) use (&$exception) {
Expand Down Expand Up @@ -361,7 +394,7 @@ public function testListenOnBusyPortThrows()
}

$this->setExpectedException('RuntimeException');
$another = new UnixServer($this->uds);
new UnixServer($this->uds);
}

/**
Expand All @@ -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()
Expand Down