Skip to content

Commit

Permalink
Merge pull request #301 from clue-labs/sock
Browse files Browse the repository at this point in the history
Improve test suite, clean up leftover `.sock` files
  • Loading branch information
WyriHaximus committed Oct 25, 2022
2 parents 8397f22 + 4daf962 commit 1bc5337
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
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

0 comments on commit 1bc5337

Please sign in to comment.