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

Only close stream when the we reached end of the stream #139

Merged
merged 1 commit into from
Dec 31, 2018
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
2 changes: 1 addition & 1 deletion src/DuplexResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function handleData($stream)

if ($data !== '') {
$this->emit('data', array($data));
} else{
} elseif (\feof($this->stream)) {
// no data read => we reached the end and close the stream
$this->emit('end');
$this->close();
Expand Down
2 changes: 1 addition & 1 deletion src/ReadableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function handleData()

if ($data !== '') {
$this->emit('data', array($data));
} else{
} elseif (\feof($this->stream)) {
// no data read => we reached the end and close the stream
$this->emit('end');
$this->close();
Expand Down
38 changes: 38 additions & 0 deletions tests/DuplexResourceStreamIntegrationTest.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\DuplexResourceStream;
use React\Stream\ReadableResourceStream;
use React\EventLoop\ExtEventLoop;
Expand Down Expand Up @@ -342,6 +343,43 @@ public function testReadsNothingFromProcessPipeWithNoOutput($condition, $loopFac
$loop->run();
}

/**
* @covers React\Stream\ReadableResourceStream::handleData
* @dataProvider loopProvider
*/
public function testEmptyReadShouldntFcloseStream($condition, $loopFactory)
{
if (true !== $condition()) {
return $this->markTestSkipped('Loop implementation not available');
}

$server = stream_socket_server('tcp://127.0.0.1:0');

$client = stream_socket_client(stream_socket_get_name($server, false));
$stream = stream_socket_accept($server);


// add a filter which returns an error when encountering an 'a' when reading
Filter\append($stream, function ($chunk) {
return '';
}, STREAM_FILTER_READ);

$loop = $loopFactory();

$conn = new DuplexResourceStream($stream, $loop);
$conn->on('error', $this->expectCallableNever());
$conn->on('data', $this->expectCallableNever());
$conn->on('end', $this->expectCallableNever());

fwrite($client, "foobar\n");

$conn->handleData($stream);

fclose($stream);
fclose($client);
fclose($server);
}

private function loopTick(LoopInterface $loop)
{
$loop->addTimer(0, function () use ($loop) {
Expand Down
19 changes: 19 additions & 0 deletions tests/ReadableResourceStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,25 @@ public function testDataErrorShouldEmitErrorAndClose()
$conn->handleData($stream);
}

/**
* @covers React\Stream\ReadableResourceStream::handleData
*/
public function testEmptyReadShouldntFcloseStream()
{
list($stream, $_) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail on Windows.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used throughout the rest suite. If this breaks on windows a follow up PR for all of them seems more appropriate to be honest

$loop = $this->createLoopMock();

$conn = new ReadableResourceStream($stream, $loop);
$conn->on('error', $this->expectCallableNever());
$conn->on('data', $this->expectCallableNever());
$conn->on('end', $this->expectCallableNever());

$conn->handleData();

fclose($stream);
fclose($_);
}

private function createLoopMock()
{
return $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
Expand Down