Skip to content

Set read buffer to 0 #20

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

Merged
merged 3 commits into from
Sep 9, 2015
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: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"license": "MIT",
"require": {
"php": ">=5.4.0",
"evenement/evenement": "~2.0"
"evenement/evenement": "^2.0"
},
"require-dev": {
"react/event-loop": "0.4.*",
"react/promise": "~2.0"
"react/event-loop": "^0.4",
"react/promise": "^2.0"
},
"suggest": {
"react/event-loop": "0.4.*",
"react/promise": "~2.0"
"react/event-loop": "^0.4",
"react/promise": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function __construct($stream, LoopInterface $loop)
}

stream_set_blocking($this->stream, 0);
stream_set_read_buffer($this->stream, 0);
Copy link
Member

Choose a reason for hiding this comment

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

So far this only affects reading from the stream – how about also disabling the write buffer?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm leaning towards "if it ain't broke don't fix it". The read buffer is fixing a specific problem. I'm all for trying to set the write buffer to 0 in another PR if we can show it increasing performance or fixing a bug.

Copy link
Member

Choose a reason for hiding this comment

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

if it ain't broke don't fix it

Sounds fair :)


$this->loop = $loop;
$this->buffer = new Buffer($this->stream, $this->loop);
Expand Down
58 changes: 58 additions & 0 deletions tests/StreamIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace React\Tests\Stream;

use React\Stream\Stream;
use React\EventLoop as rel;

class StreamIntegrationTest extends TestCase
{
public function loopProvider()
{
return [
[function() { return true; }, function() { return new rel\StreamSelectLoop; }],
[function() { return function_exists('event_base_new'); }, function() { return new rel\LibEventLoop; }],
[function() { return class_exists('libev\EventLoop'); }, function() { return new rel\LibEvLoop; }],
[function() { return class_exists('EventBase'); }, function() { return new rel\ExtEventLoop; }]
];
}

/**
* @dataProvider loopProvider
*/
public function testBufferReadsLargeChunks($condition, $loopFactory)
{
if (true !== $condition()) {
return $this->markTestSkipped('Loop implementation not available');
}

$loop = $loopFactory();

list($sockA, $sockB) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);

$streamA = new Stream($sockA, $loop);
$streamB = new Stream($sockB, $loop);

$bufferSize = 4096;
$streamA->bufferSize = $bufferSize;
$streamB->bufferSize = $bufferSize;

$testString = str_repeat("*", $streamA->bufferSize + 1);

$buffer = "";
$streamB->on('data', function ($data, $streamB) use (&$buffer, &$testString) {
$buffer .= $data;
});

$streamA->write($testString);

$loop->tick();
$loop->tick();
$loop->tick();

$streamA->close();
$streamB->close();

$this->assertEquals($testString, $buffer);
}
}