Skip to content

Commit 3edf337

Browse files
committed
Merge pull request #9 from steverhoades/stream-nonblock
add stream_set_blocking for objects of type stream to limit boilerplate
2 parents b5d221c + 2e96b08 commit 3edf337

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

src/Stream.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Evenement\EventEmitter;
66
use React\EventLoop\LoopInterface;
7+
use InvalidArgumentException;
78

89
class Stream extends EventEmitter implements DuplexStreamInterface
910
{
@@ -18,6 +19,12 @@ class Stream extends EventEmitter implements DuplexStreamInterface
1819
public function __construct($stream, LoopInterface $loop)
1920
{
2021
$this->stream = $stream;
22+
if (!is_resource($this->stream) || get_resource_type($this->stream) !== "stream") {
23+
throw new InvalidArgumentException('First parameter must be a valid stream resource');
24+
}
25+
26+
stream_set_blocking($this->stream, 0);
27+
2128
$this->loop = $loop;
2229
$this->buffer = new Buffer($this->stream, $this->loop);
2330

tests/StreamTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ public function testConstructor()
1717
$conn = new Stream($stream, $loop);
1818
}
1919

20+
/**
21+
* @covers React\Stream\Stream::__construct
22+
*/
23+
public function testConstructorThrowsExceptionOnInvalidStream()
24+
{
25+
$this->setExpectedException('InvalidArgumentException');
26+
$loop = $this->createLoopMock();
27+
$conn = new Stream('breakme', $loop);
28+
}
29+
2030
/**
2131
* @covers React\Stream\Stream::__construct
2232
* @covers React\Stream\Stream::handleData
@@ -55,19 +65,6 @@ public function testWrite()
5565
$this->assertSame("foo\n", fgets($stream));
5666
}
5767

58-
/**
59-
* @covers React\Stream\Stream::write
60-
*/
61-
public function testWriteError()
62-
{
63-
$stream = "Silly developer, you can't write to to a string!";
64-
$loop = $this->createWriteableLoopMock();
65-
66-
$conn = new Stream($stream, $loop);
67-
$conn->on('error', $this->expectCallableOnce());
68-
$conn->write('Attempting to write to a string');
69-
}
70-
7168
/**
7269
* @covers React\Stream\Stream::end
7370
*/

0 commit comments

Comments
 (0)