Skip to content

Commit c7af3d8

Browse files
authored
Merge pull request #164 from clue-labs/unneeded-nonblock
Avoid unneeded syscall when creating non-blocking `DuplexResourceStream`
2 parents d1ae7a3 + 2c436bb commit c7af3d8

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/DuplexResourceStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function __construct($stream, LoopInterface $loop = null, $readChunkSize
5252

5353
// this class relies on non-blocking I/O in order to not interrupt the event loop
5454
// e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918
55-
if (\stream_set_blocking($stream, false) !== true) {
55+
if ($buffer !== null && !$buffer instanceof WritableResourceStream && \stream_set_blocking($stream, false) !== true) {
5656
throw new \RuntimeException('Unable to set stream resource to non-blocking mode');
5757
}
5858

tests/DuplexResourceStreamTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,25 @@ public function testConstructorAcceptsBuffer()
117117

118118
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
119119

120-
$conn = new DuplexResourceStream($stream, $loop, null, $buffer);
120+
new DuplexResourceStream($stream, $loop, null, $buffer);
121+
}
122+
123+
/**
124+
* @covers React\Stream\DuplexResourceStream::__construct
125+
*/
126+
public function testConstructorThrowsExceptionIfStreamDoesNotSupportNonBlockingWithBufferGiven()
127+
{
128+
if (!in_array('blocking', stream_get_wrappers())) {
129+
stream_wrapper_register('blocking', 'React\Tests\Stream\EnforceBlockingWrapper');
130+
}
131+
132+
$stream = fopen('blocking://test', 'r+');
133+
$loop = $this->createLoopMock();
134+
135+
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
136+
137+
$this->setExpectedException('RunTimeException');
138+
new DuplexResourceStream($stream, $loop, null, $buffer);
121139
}
122140

123141
public function testCloseShouldEmitCloseEvent()

0 commit comments

Comments
 (0)