Skip to content

Commit

Permalink
Support setting buffer size to null (infinite)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Mar 15, 2016
1 parent 0e05d9f commit 174e718
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

class Stream extends EventEmitter implements DuplexStreamInterface
{
/**
* @var int|null maximum buffer size in bytes to read at once or null=infinite, until reaching EOF
*/
public $bufferSize = 4096;

public $stream;
protected $readable = true;
protected $writable = true;
Expand Down Expand Up @@ -128,7 +132,7 @@ public function pipe(WritableStreamInterface $dest, array $options = array())

public function handleData($stream)
{
$data = fread($stream, $this->bufferSize);
$data = stream_get_contents($stream, $this->bufferSize === null ? -1 : $this->bufferSize);

if ($data !== '') {
$this->emit('data', array($data, $this));
Expand Down
27 changes: 27 additions & 0 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,33 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
$this->assertEquals($conn->bufferSize, strlen($capturedData));
}

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

$capturedData = null;

$conn = new Stream($stream, $loop);
$conn->bufferSize = null;

$conn->on('data', function ($data) use (&$capturedData) {
$capturedData = $data;
});

fwrite($stream, str_repeat("a", 100000));
rewind($stream);

$conn->handleData($stream);

$this->assertFalse($conn->isReadable());
$this->assertEquals(100000, strlen($capturedData));
}

/**
* @covers React\Stream\Stream::handleData
*/
Expand Down

0 comments on commit 174e718

Please sign in to comment.