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
4 changes: 2 additions & 2 deletions src/Buffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,12 @@ public function handleWrite()
}

$len = strlen($this->data);
$this->data = (string) substr($this->data, $sent);

if ($len >= $this->softLimit && $len - $sent < $this->softLimit) {
$this->emit('drain', [$this]);
}

$this->data = (string) substr($this->data, $sent);

if (0 === strlen($this->data)) {
$this->loop->removeWriteStream($this->stream);
$this->listening = false;
Expand Down
30 changes: 30 additions & 0 deletions tests/BufferTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ public function testDrain()
$buffer->write("bar\n");
}

/**
* @covers React\Stream\Buffer::write
* @covers React\Stream\Buffer::handleWrite
*/
public function testWriteInDrain()
{
$writeStreams = array();

$stream = fopen('php://temp', 'r+');
$loop = $this->createWriteableLoopMock();
$loop->preventWrites = true;

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

$buffer->once('drain', function ($buffer) {
$buffer->listening = false;
$buffer->write("bar\n");
});

$this->assertFalse($buffer->write("foo"));
$loop->preventWrites = false;
$buffer->listening = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I follow, are these assignments required only in the testing environment to prevent actual writes? Some background info and/or documentation would be much appreciated! 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I duplicated what testDrain() was doing; it does this to simulate a stream that's not immediately available for writes.

$buffer->write("\n");

fseek($stream, 0);
$this->assertSame("foo\nbar\n", stream_get_contents($stream));
}

/**
* @covers React\Stream\Buffer::end
*/
Expand Down