Skip to content

Commit

Permalink
Test creation of appropriate server types
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 9, 2017
1 parent b672499 commit ea2b0c7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
$context += array(
'tcp' => array(),
'tls' => array(),
'unix' => array(),
'unix' => array()
);

$scheme = 'tcp';
Expand Down
41 changes: 41 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use React\EventLoop\Factory;
use React\Socket\Server;
use React\Socket\TcpConnector;
use React\Socket\UnixConnector;
use Clue\React\Block;
use React\Socket\ConnectionInterface;

class ServerTest extends TestCase
{
const TIMEOUT = 0.1;

public function testCreateServer()
{
$loop = Factory::create();
Expand All @@ -26,6 +30,38 @@ public function testConstructorThrowsForInvalidUri()
$server = new Server('invalid URI', $loop);
}

public function testConstructorCreatesExpectedTcpServer()
{
$loop = Factory::create();

$server = new Server(0, $loop);

$connector = new TcpConnector($loop);
$connector->connect($server->getAddress())
->then($this->expectCallableOnce(), $this->expectCallableNever());

$connection = Block\await($connector->connect($server->getAddress()), $loop, self::TIMEOUT);

$connection->close();
$server->close();
}

public function testConstructorCreatesExpectedUnixServer()
{
$loop = Factory::create();

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

$connector = new UnixConnector($loop);
$connector->connect($server->getAddress())
->then($this->expectCallableOnce(), $this->expectCallableNever());

$connection = Block\await($connector->connect($server->getAddress()), $loop, self::TIMEOUT);

$connection->close();
$server->close();
}

public function testEmitsConnectionForNewConnection()
{
$loop = Factory::create();
Expand Down Expand Up @@ -127,4 +163,9 @@ public function testDoesNotEmitSecureConnectionForNewPlainConnection()

Block\sleep(0.1, $loop);
}

private function getRandomSocketUri()
{
return "unix://" . sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid(rand(), true) . '.sock';
}
}

0 comments on commit ea2b0c7

Please sign in to comment.