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 and switch to normal stream handler #51

Merged
merged 3 commits into from
Dec 19, 2016
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
14 changes: 0 additions & 14 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,6 @@

class Connection extends Stream implements ConnectionInterface
{
public function handleData($stream)
{
// Socket is raw, not using fread as it's interceptable by filters
// See issues #192, #209, and #240
$data = stream_socket_recvfrom($stream, $this->bufferSize);
if ('' !== $data && false !== $data) {
$this->emit('data', array($data, $this));
}

if ('' === $data || false === $data || !is_resource($stream) || feof($stream)) {
$this->end();
}
}

public function handleClose()
{
if (is_resource($this->stream)) {
Expand Down
9 changes: 3 additions & 6 deletions tests/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ public function testGetRemoteAddress()

$servConn = new Connection($server->master, $loop);

$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($method->invokeArgs($servConn, array(stream_socket_get_name($master->getValue($server), false))))
;
$mock = $this->expectCallableOnceWith(
$method->invokeArgs($servConn, array(stream_socket_get_name($master->getValue($server), false)))
);

$server->on('connection', function ($conn) use ($mock) {
$mock($conn->getRemoteAddress());
Expand Down
102 changes: 78 additions & 24 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use React\Socket\Server;
use React\EventLoop\StreamSelectLoop;
use React\Stream\Stream;

class ServerTest extends TestCase
{
Expand Down Expand Up @@ -64,7 +65,7 @@ public function testConnectionWithManyClients()
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::handleData
*/
public function testDataWithNoData()
public function testDataEventWillNotBeEmittedWhenClientSendsNoData()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);

Expand All @@ -81,17 +82,13 @@ public function testDataWithNoData()
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::handleData
*/
public function testData()
public function testDataWillBeEmittedWithDataClientSends()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);

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

$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with("foo\n");
$mock = $this->expectCallableOnceWith("foo\n");

$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('data', $mock);
Expand All @@ -101,23 +98,16 @@ public function testData()
}

/**
* Test data sent from python language
*
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::handleData
*/
public function testDataSentFromPy()
public function testDataWillBeEmittedEvenWhenClientShutsDownAfterSending()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);
fwrite($client, "foo\n");
stream_socket_shutdown($client, STREAM_SHUT_WR);

$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with("foo\n");

$mock = $this->expectCallableOnceWith("foo\n");

$this->server->on('connection', function ($conn) use ($mock) {
$conn->on('data', $mock);
Expand All @@ -126,17 +116,13 @@ public function testDataSentFromPy()
$this->loop->tick();
}

public function testFragmentedMessage()
public function testDataWillBeFragmentedToBufferSize()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);

fwrite($client, "Hello World!\n");

$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with("He");
$mock = $this->expectCallableOnceWith("He");

$this->server->on('connection', function ($conn) use ($mock) {
$conn->bufferSize = 2;
Expand All @@ -146,10 +132,69 @@ public function testFragmentedMessage()
$this->loop->tick();
}

public function testLoopWillEndWhenServerIsShutDown()
{
// explicitly unset server because we already call shutdown()
$this->server->shutdown();
$this->server = null;

$this->loop->run();
}

public function testLoopWillEndWhenServerIsShutDownAfterSingleConnection()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);

// explicitly unset server because we only accept a single connection
// and then already call shutdown()
$server = $this->server;
$this->server = null;

$server->on('connection', function ($conn) use ($server) {
$conn->close();
$server->shutdown();
});

$this->loop->run();
}

public function testDataWillBeEmittedInMultipleChunksWhenClientSendsExcessiveAmounts()
{
$client = stream_socket_client('tcp://localhost:' . $this->port);
$stream = new Stream($client, $this->loop);

$bytes = 1024 * 1024;
$stream->end(str_repeat('*', $bytes));

$mock = $this->expectCallableOnce();

// explicitly unset server because we only accept a single connection
// and then already call shutdown()
$server = $this->server;
$this->server = null;

$received = 0;
$server->on('connection', function ($conn) use ($mock, &$received, $server) {
// count number of bytes received
$conn->on('data', function ($data) use (&$received) {
$received += strlen($data);
});

$conn->on('end', $mock);

// do not await any further connections in order to let the loop terminate
$server->shutdown();
});

$this->loop->run();

$this->assertEquals($bytes, $received);
}

/**
* @covers React\EventLoop\StreamSelectLoop::tick
*/
public function testDisconnectWithoutDisconnect()
public function testConnectionDoesNotEndWhenClientDoesNotClose()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);

Expand All @@ -166,7 +211,7 @@ public function testDisconnectWithoutDisconnect()
* @covers React\EventLoop\StreamSelectLoop::tick
* @covers React\Socket\Connection::end
*/
public function testDisconnect()
public function testConnectionDoesEndWhenClientCloses()
{
$client = stream_socket_client('tcp://localhost:'.$this->port);

Expand All @@ -181,6 +226,15 @@ public function testDisconnect()
$this->loop->tick();
}

/**
* @expectedException React\Socket\ConnectionException
*/
public function testListenOnBusyPortThrows()
{
$another = new Server($this->loop);
$another->listen($this->port);
}

/**
* @covers React\Socket\Server::shutdown
*/
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ protected function expectCallableOnce()
return $mock;
}

protected function expectCallableOnceWith($value)
{
$mock = $this->createCallableMock();
$mock
->expects($this->once())
->method('__invoke')
->with($value);

return $mock;
}

protected function expectCallableNever()
{
$mock = $this->createCallableMock();
Expand Down