diff --git a/src/LibEvLoop.php b/src/LibEvLoop.php index 38e4ec2c..bc40ae80 100644 --- a/src/LibEvLoop.php +++ b/src/LibEvLoop.php @@ -38,6 +38,10 @@ public function __construct() */ public function addReadStream($stream, callable $listener) { + if (isset($this->readEvents[(int) $stream])) { + return; + } + $callback = function () use ($stream, $listener) { call_user_func($listener, $stream, $this); }; @@ -53,6 +57,10 @@ public function addReadStream($stream, callable $listener) */ public function addWriteStream($stream, callable $listener) { + if (isset($this->writeEvents[(int) $stream])) { + return; + } + $callback = function () use ($stream, $listener) { call_user_func($listener, $stream, $this); }; diff --git a/tests/AbstractLoopTest.php b/tests/AbstractLoopTest.php index 09b458b8..bd6bb83f 100644 --- a/tests/AbstractLoopTest.php +++ b/tests/AbstractLoopTest.php @@ -47,6 +47,20 @@ public function testAddReadStream() $this->loop->tick(); } + public function testAddReadStreamIgnoresSecondCallable() + { + list ($input, $output) = $this->createSocketPair(); + + $this->loop->addReadStream($input, $this->expectCallableExactly(2)); + $this->loop->addReadStream($input, $this->expectCallableNever()); + + fwrite($output, "foo\n"); + $this->loop->tick(); + + fwrite($output, "bar\n"); + $this->loop->tick(); + } + public function testAddWriteStream() { list ($input) = $this->createSocketPair(); @@ -56,6 +70,16 @@ public function testAddWriteStream() $this->loop->tick(); } + public function testAddWriteStreamIgnoresSecondCallable() + { + list ($input) = $this->createSocketPair(); + + $this->loop->addWriteStream($input, $this->expectCallableExactly(2)); + $this->loop->addWriteStream($input, $this->expectCallableNever()); + $this->loop->tick(); + $this->loop->tick(); + } + public function testRemoveReadStreamInstantly() { list ($input, $output) = $this->createSocketPair();