Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close the underlying stream when closing the stream #107

Merged
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
10 changes: 10 additions & 0 deletions src/WritableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function close()

$this->emit('close', array($this));
$this->removeAllListeners();

$this->handleClose();
}

/** @internal */
public function handleClose()
{
if (is_resource($this->stream)) {
fclose($this->stream);
}
}

/** @internal */
Expand Down
20 changes: 16 additions & 4 deletions tests/WritableStreamResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace React\Tests\Stream;

use Clue\StreamFilter as Filter;
use React\Stream\WritableResourceStream;

class WritableResourceStreamTest extends TestCase
Expand Down Expand Up @@ -294,19 +295,24 @@ public function testEndWithoutDataDoesNotCloseIfWritableResourceStreamIsFull()
public function testEndWithDataClosesImmediatelyIfWritableResourceStreamFlushes()
{
$stream = fopen('php://temp', 'r+');
$filterBuffer = '';
$loop = $this->createLoopMock();

$buffer = new WritableResourceStream($stream, $loop);
$buffer->on('error', $this->expectCallableNever());
$buffer->on('close', $this->expectCallableOnce());

Filter\append($stream, function ($chunk) use (&$filterBuffer) {
$filterBuffer .= $chunk;
return $chunk;
});

$this->assertTrue($buffer->isWritable());
$buffer->end('final words');
$this->assertFalse($buffer->isWritable());

$buffer->handleWrite();
rewind($stream);
$this->assertSame('final words', stream_get_contents($stream));
$this->assertSame('final words', $filterBuffer);
}

/**
Expand Down Expand Up @@ -402,16 +408,22 @@ public function testDoubleCloseWillEmitOnlyOnce()
public function testWritingToClosedWritableResourceStreamShouldNotWriteToStream()
{
$stream = fopen('php://temp', 'r+');
$filterBuffer = '';
$loop = $this->createLoopMock();

$buffer = new WritableResourceStream($stream, $loop);

Filter\append($stream, function ($chunk) use (&$filterBuffer) {
$filterBuffer .= $chunk;
return $chunk;
});

$buffer->close();

$buffer->write('foo');

$buffer->handleWrite();
rewind($stream);
$this->assertSame('', stream_get_contents($stream));
$this->assertSame('', $filterBuffer);
}

/**
Expand Down