Skip to content

Commit

Permalink
Simplify testing interaction with loop and fix now incorrect assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Aug 22, 2016
1 parent 03e61d4 commit 96cc409
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 51 deletions.
64 changes: 27 additions & 37 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testConstructor()
public function testWrite()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->on('error', $this->expectCallableNever());
Expand Down Expand Up @@ -84,22 +84,18 @@ public function testWriteCanFlushImmediatelyWithoutBufferGettingFull()

/**
* @covers React\Stream\Buffer::write
* @covers React\Stream\Buffer::handleWrite
*/
public function testWriteReturnsFalseWhenBufferIsFull()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop->preventWrites = true;
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->softLimit = 4;
$buffer->listening = true;
$buffer->on('error', $this->expectCallableNever());

$this->assertTrue($buffer->write("foo"));
$loop->preventWrites = false;
$this->assertFalse($buffer->write("bar\n"));
$this->assertFalse($buffer->write("foobar"));
}

/**
Expand All @@ -121,7 +117,6 @@ public function testWriteEmitsErrorWhenResourceIsNotWritable()
//$buffer->on('close', $this->expectCallableOnce());

$buffer->write('hello');
$buffer->handleWrite();
}

/**
Expand All @@ -146,21 +141,34 @@ public function testWriteDetectsWhenOtherSideIsClosed()
* @covers React\Stream\Buffer::write
* @covers React\Stream\Buffer::handleWrite
*/
public function testDrain()
public function testEmitsDrainIfBufferIsFullAndThenEmpties()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop->preventWrites = true;
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->softLimit = 4;
$buffer->on('error', $this->expectCallableNever());
$buffer->on('drain', $this->expectCallableOnce());

$buffer->write("foobar");
}

/**
* @covers React\Stream\Buffer::write
* @covers React\Stream\Buffer::handleWrite
*/
public function testDoesNotEmitDrainIfBufferIsNotFull()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->softLimit = 4;
$buffer->on('error', $this->expectCallableNever());
$buffer->on('drain', $this->expectCallableNever());

$buffer->write("foo");
$loop->preventWrites = false;
$buffer->listening = false;
$buffer->write("bar\n");
}

/**
Expand All @@ -170,8 +178,7 @@ public function testDrain()
public function testWriteInDrain()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop->preventWrites = true;
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->softLimit = 2;
Expand All @@ -184,7 +191,6 @@ public function testWriteInDrain()

$buffer->listening = true;
$this->assertFalse($buffer->write("foo"));
$loop->preventWrites = false;
$buffer->listening = false;
$buffer->write("\n");

Expand Down Expand Up @@ -215,7 +221,7 @@ public function testEnd()
public function testEndWithData()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->on('error', $this->expectCallableNever());
Expand Down Expand Up @@ -252,7 +258,7 @@ public function testClose()
public function testWritingToClosedBufferShouldNotWriteToStream()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop = $this->createLoopMock();

$buffer = new Buffer($stream, $loop);
$buffer->close();
Expand All @@ -270,7 +276,7 @@ public function testWritingToClosedBufferShouldNotWriteToStream()
public function testError()
{
$stream = null;
$loop = $this->createWriteableLoopMock();
$loop = $this->createLoopMock();

$error = null;

Expand All @@ -291,7 +297,7 @@ public function testWritingToClosedStream()
}

list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
$loop = $this->createWriteableLoopMock();
$loop = $this->createLoopMock();

$error = null;

Expand All @@ -309,22 +315,6 @@ public function testWritingToClosedStream()
$this->assertSame('fwrite(): send of 3 bytes failed with errno=32 Broken pipe', $error->getMessage());
}

private function createWriteableLoopMock()
{
$loop = $this->createLoopMock();
$loop->preventWrites = false;
$loop
->expects($this->any())
->method('addWriteStream')
->will($this->returnCallback(function ($stream, $listener) use ($loop) {
if (!$loop->preventWrites) {
call_user_func($listener, $stream);
}
}));

return $loop;
}

private function createLoopMock()
{
return $this->getMock('React\EventLoop\LoopInterface');
Expand Down
15 changes: 1 addition & 14 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testEmptyStreamShouldNotEmitData()
public function testWrite()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop = $this->createLoopMock();

$conn = new Stream($stream, $loop);
$conn->write("foo\n");
Expand Down Expand Up @@ -184,19 +184,6 @@ public function testDataErrorShouldEmitErrorAndClose()
$conn->handleData($stream);
}

private function createWriteableLoopMock()
{
$loop = $this->createLoopMock();
$loop
->expects($this->once())
->method('addWriteStream')
->will($this->returnCallback(function ($stream, $listener) {
call_user_func($listener, $stream);
}));

return $loop;
}

private function createLoopMock()
{
return $this->getMock('React\EventLoop\LoopInterface');
Expand Down

0 comments on commit 96cc409

Please sign in to comment.