diff --git a/src/DuplexResourceStream.php b/src/DuplexResourceStream.php index c3163c6..4da2139 100644 --- a/src/DuplexResourceStream.php +++ b/src/DuplexResourceStream.php @@ -52,7 +52,7 @@ public function __construct($stream, LoopInterface $loop = null, $readChunkSize // this class relies on non-blocking I/O in order to not interrupt the event loop // e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918 - if (\stream_set_blocking($stream, false) !== true) { + if ($buffer !== null && !$buffer instanceof WritableResourceStream && \stream_set_blocking($stream, false) !== true) { throw new \RuntimeException('Unable to set stream resource to non-blocking mode'); } diff --git a/tests/DuplexResourceStreamTest.php b/tests/DuplexResourceStreamTest.php index 2edc07e..e61f14b 100644 --- a/tests/DuplexResourceStreamTest.php +++ b/tests/DuplexResourceStreamTest.php @@ -117,7 +117,25 @@ public function testConstructorAcceptsBuffer() $buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock(); - $conn = new DuplexResourceStream($stream, $loop, null, $buffer); + new DuplexResourceStream($stream, $loop, null, $buffer); + } + + /** + * @covers React\Stream\DuplexResourceStream::__construct + */ + public function testConstructorThrowsExceptionIfStreamDoesNotSupportNonBlockingWithBufferGiven() + { + if (!in_array('blocking', stream_get_wrappers())) { + stream_wrapper_register('blocking', 'React\Tests\Stream\EnforceBlockingWrapper'); + } + + $stream = fopen('blocking://test', 'r+'); + $loop = $this->createLoopMock(); + + $buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock(); + + $this->setExpectedException('RunTimeException'); + new DuplexResourceStream($stream, $loop, null, $buffer); } public function testCloseShouldEmitCloseEvent()