Skip to content

Commit

Permalink
Merge d12d6ef into 2120bdd
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Mar 31, 2019
2 parents 2120bdd + d12d6ef commit 6910332
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/ResourceInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ public function __construct($stream, int $chunkSize = self::DEFAULT_CHUNK_SIZE)
\stream_set_read_buffer($stream, 0);

$this->resource = $stream;
$this->chunkSize = $chunkSize;
$this->chunkSize = &$chunkSize;

$deferred = &$this->deferred;
$readable = &$this->readable;

$this->watcher = Loop::onReadable($this->resource, static function ($watcher, $stream) use (
&$deferred,
&$readable,
$chunkSize,
&$chunkSize,
$useSingleRead
) {
if ($useSingleRead) {
Expand Down Expand Up @@ -204,6 +204,11 @@ public function getResource()
return $this->resource;
}

public function setChunkSize(int $chunkSize)
{
$this->chunkSize = $chunkSize;
}

/**
* References the read watcher, so the loop keeps running in case there's an active read.
*
Expand Down
9 changes: 7 additions & 2 deletions lib/ResourceOutputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public function __construct($stream, int $chunkSize = null)
\stream_set_write_buffer($stream, 0);

$this->resource = $stream;
$this->chunkSize = $chunkSize;
$this->chunkSize = &$chunkSize;

$writes = $this->writes = new \SplQueue;
$writable = &$this->writable;
$resource = &$this->resource;

$this->watcher = Loop::onWritable($stream, static function ($watcher, $stream) use ($writes, $chunkSize, &$writable, &$resource) {
$this->watcher = Loop::onWritable($stream, static function ($watcher, $stream) use ($writes, &$chunkSize, &$writable, &$resource) {
static $emptyWrites = 0;

try {
Expand Down Expand Up @@ -274,6 +274,11 @@ public function getResource()
return $this->resource;
}

public function setChunkSize(int $chunkSize)
{
$this->chunkSize = $chunkSize;
}

public function __destruct()
{
if ($this->resource !== null) {
Expand Down
15 changes: 15 additions & 0 deletions test/ResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,19 @@ public function testIssue47()
$this->assertStringEqualsFile(__FILE__, $buffer);
});
}

public function testSetChunkSize()
{
Loop::run(function () {
list($a, $b) = $this->getStreamPair();
$a->setChunkSize(1);
$b->setChunkSize(1);

$this->assertSame(3, yield $a->write('foo'));
$this->assertSame('f', yield $b->read());

$b->setChunkSize(3);
$this->assertSame('oo', yield $b->read());
});
}
}

0 comments on commit 6910332

Please sign in to comment.