From 7dc300646b764d4e533a5adb0c49c2722805d3a4 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 25 Mar 2017 13:41:33 +0100 Subject: [PATCH 1/2] Add tests for double watchers to be ignored --- tests/AbstractLoopTest.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) 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(); From b2acd81a2bd19eb137466595e94abd821796e440 Mon Sep 17 00:00:00 2001 From: Niklas Keller Date: Sat, 25 Mar 2017 13:59:58 +0100 Subject: [PATCH 2/2] Fix LibEvLoop to ignore double IO streams --- src/LibEvLoop.php | 8 ++++++++ 1 file changed, 8 insertions(+) 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); };