Skip to content

Commit

Permalink
Writing to closed unwrapped stream should return false (backpressure)
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Jul 2, 2019
1 parent 2d47cc2 commit 3d3eed1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/UnwrapWritableStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function ($e) use ($out, &$closed) {
public function write($data)
{
if ($this->ending) {
return;
return false;
}

// forward to inner stream if possible
Expand Down
63 changes: 63 additions & 0 deletions tests/UnwrapWritableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,55 @@ public function testForwardsNoDataWhenWritingAfterEndOncePromiseResolves()
$this->loop->run();
}

public function testWriteReturnsFalseWhenPromiseIsPending()
{
$promise = new \React\Promise\Promise(function () { });
$stream = Stream\unwrapWritable($promise);

$ret = $stream->write('nope');

$this->assertFalse($ret);
}

public function testWriteReturnsTrueWhenUnwrappedStreamReturnsTrueForWrite()
{
$input = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$input->expects($this->once())->method('isWritable')->willReturn(true);
$input->expects($this->once())->method('write')->willReturn(true);

$promise = \React\Promise\resolve($input);
$stream = Stream\unwrapWritable($promise);

$ret = $stream->write('hello');

$this->assertTrue($ret);
}

public function testWriteReturnsFalseWhenUnwrappedStreamReturnsFalseForWrite()
{
$input = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$input->expects($this->once())->method('isWritable')->willReturn(true);
$input->expects($this->once())->method('write')->willReturn(false);

$promise = \React\Promise\resolve($input);
$stream = Stream\unwrapWritable($promise);

$ret = $stream->write('nope');

$this->assertFalse($ret);
}

public function testWriteAfterCloseReturnsFalse()
{
$promise = new \React\Promise\Promise(function () { });
$stream = Stream\unwrapWritable($promise);

$stream->close();
$ret = $stream->write('nope');

$this->assertFalse($ret);
}

public function testEmitsErrorAndClosesWhenInputEmitsError()
{
$input = new ThroughStream();
Expand All @@ -267,6 +316,20 @@ public function testEmitsErrorAndClosesWhenInputEmitsError()
$this->assertFalse($stream->isWritable());
}

public function testEmitsDrainWhenPromiseResolvesWithStreamWhenForwardingData()
{
$input = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$input->expects($this->once())->method('isWritable')->willReturn(true);
$input->expects($this->once())->method('write')->with('hello')->willReturn(true);

$deferred = new Deferred();
$stream = Stream\unwrapWritable($deferred->promise());
$stream->write('hello');

$stream->on('drain', $this->expectCallableOnce());
$deferred->resolve($input);
}

public function testDoesNotEmitDrainWhenStreamBufferExceededAfterForwardingData()
{
$input = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
Expand Down

0 comments on commit 3d3eed1

Please sign in to comment.