Skip to content
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: 13 additions & 1 deletion src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Buffer extends EventEmitter implements WritableStreamInterface
public $listening = false;
public $softLimit = 65536;
private $writable = true;
private $closed = false;
private $loop;
private $data = '';

Expand Down Expand Up @@ -64,11 +65,21 @@ public function end($data = null)

public function close()
{
if ($this->closed) {
return;
}

if ($this->listening) {
$this->listening = false;
$this->loop->removeWriteStream($this->stream);
}

$this->closed = true;
$this->writable = false;
$this->listening = false;
$this->data = '';

$this->emit('close', array($this));
$this->removeAllListeners();
}

public function handleWrite()
Expand Down Expand Up @@ -108,6 +119,7 @@ public function handleWrite()
}

$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error->getMessage(), 0, $error), $this));
$this->close();

return;
}
Expand Down
60 changes: 60 additions & 0 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,20 @@ public function testEmptyWriteDoesNotAddToLoop()
$buffer->write(null);
}

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

$loop->expects($this->once())->method('addWriteStream')->with($stream);

$buffer->write('foo');
}

/**
* @covers React\Stream\Buffer::write
* @covers React\Stream\Buffer::handleWrite
Expand Down Expand Up @@ -292,6 +306,52 @@ public function testClose()
$this->assertTrue($buffer->isWritable());
$buffer->close();
$this->assertFalse($buffer->isWritable());

$this->assertEquals(array(), $buffer->listeners('close'));
}

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

$loop->expects($this->once())->method('removeWriteStream')->with($stream);

$buffer->write('foo');
$buffer->close();
}

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

$loop->expects($this->never())->method('removeWriteStream');

$buffer->close();
}

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

$buffer = new Buffer($stream, $loop);
$buffer->on('close', $this->expectCallableOnce());

$buffer->close();
$buffer->close();
}

/**
Expand Down