Skip to content

Commit

Permalink
Merge 5006692 into 2120bdd
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Mar 26, 2019
2 parents 2120bdd + 5006692 commit f8e60ec
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/ResourceInputStream.php
Original file line number Diff line number Diff line change
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 f8e60ec

Please sign in to comment.