From 1a4c7ea6ff692432491d303b3ce005a51a3dcbfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 8 Jun 2015 00:15:29 +0200 Subject: [PATCH] =?UTF-8?q?Remove=20NextTickQueue=20=E2=80=93=20use=20Futu?= =?UTF-8?q?reTickQueue=20instead?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ExtEventLoop.php | 17 +---- src/LibEvLoop.php | 17 +---- src/LibEventLoop.php | 17 +---- src/LoopInterface.php | 10 --- src/StreamSelectLoop.php | 19 +----- src/Tick/NextTickQueue.php | 57 ---------------- tests/AbstractLoopTest.php | 136 ++----------------------------------- 7 files changed, 10 insertions(+), 263 deletions(-) delete mode 100644 src/Tick/NextTickQueue.php diff --git a/src/ExtEventLoop.php b/src/ExtEventLoop.php index 48657f96..a39bc1b8 100644 --- a/src/ExtEventLoop.php +++ b/src/ExtEventLoop.php @@ -6,7 +6,6 @@ use EventBase; use EventConfig as EventBaseConfig; use React\EventLoop\Tick\FutureTickQueue; -use React\EventLoop\Tick\NextTickQueue; use React\EventLoop\Timer\Timer; use React\EventLoop\Timer\TimerInterface; use SplObjectStorage; @@ -17,7 +16,6 @@ class ExtEventLoop implements LoopInterface { private $eventBase; - private $nextTickQueue; private $futureTickQueue; private $timerCallback; private $timerEvents; @@ -31,7 +29,6 @@ class ExtEventLoop implements LoopInterface public function __construct(EventBaseConfig $config = null) { $this->eventBase = new EventBase($config); - $this->nextTickQueue = new NextTickQueue($this); $this->futureTickQueue = new FutureTickQueue($this); $this->timerEvents = new SplObjectStorage(); @@ -153,14 +150,6 @@ public function isTimerActive(TimerInterface $timer) return $this->timerEvents->contains($timer); } - /** - * {@inheritdoc} - */ - public function nextTick(callable $listener) - { - $this->nextTickQueue->add($listener); - } - /** * {@inheritdoc} */ @@ -174,8 +163,6 @@ public function futureTick(callable $listener) */ public function tick() { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); // @-suppression: https://github.com/reactphp/react/pull/234#discussion-diff-7759616R226 @@ -190,12 +177,10 @@ public function run() $this->running = true; while ($this->running) { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); $flags = EventBase::LOOP_ONCE; - if (!$this->running || !$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) { + if (!$this->running || !$this->futureTickQueue->isEmpty()) { $flags |= EventBase::LOOP_NONBLOCK; } elseif (!$this->streamEvents && !$this->timerEvents->count()) { break; diff --git a/src/LibEvLoop.php b/src/LibEvLoop.php index 38e4ec2c..d1a7093a 100644 --- a/src/LibEvLoop.php +++ b/src/LibEvLoop.php @@ -6,7 +6,6 @@ use libev\IOEvent; use libev\TimerEvent; use React\EventLoop\Tick\FutureTickQueue; -use React\EventLoop\Tick\NextTickQueue; use React\EventLoop\Timer\Timer; use React\EventLoop\Timer\TimerInterface; use SplObjectStorage; @@ -18,7 +17,6 @@ class LibEvLoop implements LoopInterface { private $loop; - private $nextTickQueue; private $futureTickQueue; private $timerEvents; private $readEvents = []; @@ -28,7 +26,6 @@ class LibEvLoop implements LoopInterface public function __construct() { $this->loop = new EventLoop(); - $this->nextTickQueue = new NextTickQueue($this); $this->futureTickQueue = new FutureTickQueue($this); $this->timerEvents = new SplObjectStorage(); } @@ -157,14 +154,6 @@ public function isTimerActive(TimerInterface $timer) return $this->timerEvents->contains($timer); } - /** - * {@inheritdoc} - */ - public function nextTick(callable $listener) - { - $this->nextTickQueue->add($listener); - } - /** * {@inheritdoc} */ @@ -178,8 +167,6 @@ public function futureTick(callable $listener) */ public function tick() { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); $this->loop->run(EventLoop::RUN_ONCE | EventLoop::RUN_NOWAIT); @@ -193,12 +180,10 @@ public function run() $this->running = true; while ($this->running) { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); $flags = EventLoop::RUN_ONCE; - if (!$this->running || !$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) { + if (!$this->running || !$this->futureTickQueue->isEmpty()) { $flags |= EventLoop::RUN_NOWAIT; } elseif (!$this->readEvents && !$this->writeEvents && !$this->timerEvents->count()) { break; diff --git a/src/LibEventLoop.php b/src/LibEventLoop.php index 6fbc8269..0ab2d3e7 100644 --- a/src/LibEventLoop.php +++ b/src/LibEventLoop.php @@ -5,7 +5,6 @@ use Event; use EventBase; use React\EventLoop\Tick\FutureTickQueue; -use React\EventLoop\Tick\NextTickQueue; use React\EventLoop\Timer\Timer; use React\EventLoop\Timer\TimerInterface; use SplObjectStorage; @@ -18,7 +17,6 @@ class LibEventLoop implements LoopInterface const MICROSECONDS_PER_SECOND = 1000000; private $eventBase; - private $nextTickQueue; private $futureTickQueue; private $timerCallback; private $timerEvents; @@ -32,7 +30,6 @@ class LibEventLoop implements LoopInterface public function __construct() { $this->eventBase = event_base_new(); - $this->nextTickQueue = new NextTickQueue($this); $this->futureTickQueue = new FutureTickQueue($this); $this->timerEvents = new SplObjectStorage(); @@ -161,14 +158,6 @@ public function isTimerActive(TimerInterface $timer) return $this->timerEvents->contains($timer); } - /** - * {@inheritdoc} - */ - public function nextTick(callable $listener) - { - $this->nextTickQueue->add($listener); - } - /** * {@inheritdoc} */ @@ -182,8 +171,6 @@ public function futureTick(callable $listener) */ public function tick() { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); event_base_loop($this->eventBase, EVLOOP_ONCE | EVLOOP_NONBLOCK); @@ -197,12 +184,10 @@ public function run() $this->running = true; while ($this->running) { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); $flags = EVLOOP_ONCE; - if (!$this->running || !$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) { + if (!$this->running || !$this->futureTickQueue->isEmpty()) { $flags |= EVLOOP_NONBLOCK; } elseif (!$this->streamEvents && !$this->timerEvents->count()) { break; diff --git a/src/LoopInterface.php b/src/LoopInterface.php index d046526c..d15a376c 100644 --- a/src/LoopInterface.php +++ b/src/LoopInterface.php @@ -85,16 +85,6 @@ public function cancelTimer(TimerInterface $timer); */ public function isTimerActive(TimerInterface $timer); - /** - * Schedule a callback to be invoked on the next tick of the event loop. - * - * Callbacks are guaranteed to be executed in the order they are enqueued, - * before any timer or stream events. - * - * @param callable $listener The callback to invoke. - */ - public function nextTick(callable $listener); - /** * Schedule a callback to be invoked on a future tick of the event loop. * diff --git a/src/StreamSelectLoop.php b/src/StreamSelectLoop.php index 7d455048..0d5af090 100644 --- a/src/StreamSelectLoop.php +++ b/src/StreamSelectLoop.php @@ -3,7 +3,6 @@ namespace React\EventLoop; use React\EventLoop\Tick\FutureTickQueue; -use React\EventLoop\Tick\NextTickQueue; use React\EventLoop\Timer\Timer; use React\EventLoop\Timer\TimerInterface; use React\EventLoop\Timer\Timers; @@ -15,7 +14,6 @@ class StreamSelectLoop implements LoopInterface { const MICROSECONDS_PER_SECOND = 1000000; - private $nextTickQueue; private $futureTickQueue; private $timers; private $readStreams = []; @@ -26,7 +24,6 @@ class StreamSelectLoop implements LoopInterface public function __construct() { - $this->nextTickQueue = new NextTickQueue($this); $this->futureTickQueue = new FutureTickQueue($this); $this->timers = new Timers(); } @@ -132,14 +129,6 @@ public function isTimerActive(TimerInterface $timer) return $this->timers->contains($timer); } - /** - * {@inheritdoc} - */ - public function nextTick(callable $listener) - { - $this->nextTickQueue->add($listener); - } - /** * {@inheritdoc} */ @@ -153,8 +142,6 @@ public function futureTick(callable $listener) */ public function tick() { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); $this->timers->tick(); @@ -170,14 +157,12 @@ public function run() $this->running = true; while ($this->running) { - $this->nextTickQueue->tick(); - $this->futureTickQueue->tick(); $this->timers->tick(); - // Next-tick or future-tick queues have pending callbacks ... - if (!$this->running || !$this->nextTickQueue->isEmpty() || !$this->futureTickQueue->isEmpty()) { + // Future-tick queue has pending callbacks ... + if (!$this->running || !$this->futureTickQueue->isEmpty()) { $timeout = 0; // There is a pending timer, only block until it is due ... diff --git a/src/Tick/NextTickQueue.php b/src/Tick/NextTickQueue.php deleted file mode 100644 index 5b8e1de8..00000000 --- a/src/Tick/NextTickQueue.php +++ /dev/null @@ -1,57 +0,0 @@ -eventLoop = $eventLoop; - $this->queue = new SplQueue(); - } - - /** - * Add a callback to be invoked on the next tick of the event loop. - * - * Callbacks are guaranteed to be executed in the order they are enqueued, - * before any timer or stream events. - * - * @param callable $listener The callback to invoke. - */ - public function add(callable $listener) - { - $this->queue->enqueue($listener); - } - - /** - * Flush the callback queue. - */ - public function tick() - { - while (!$this->queue->isEmpty()) { - call_user_func( - $this->queue->dequeue(), - $this->eventLoop - ); - } - } - - /** - * Check if the next tick queue is empty. - * - * @return boolean - */ - public function isEmpty() - { - return $this->queue->isEmpty(); - } -} diff --git a/tests/AbstractLoopTest.php b/tests/AbstractLoopTest.php index b4095379..7c2eb0b4 100644 --- a/tests/AbstractLoopTest.php +++ b/tests/AbstractLoopTest.php @@ -200,7 +200,7 @@ function () { } ); - $this->loop->nextTick( + $this->loop->futureTick( function () { $this->loop->stop(); } @@ -231,126 +231,19 @@ public function testIgnoreRemovedCallback() $loop->run(); } - public function testNextTick() - { - $called = false; - - $callback = function ($loop) use (&$called) { - $this->assertSame($this->loop, $loop); - $called = true; - }; - - $this->loop->nextTick($callback); - - $this->assertFalse($called); - - $this->loop->tick(); - - $this->assertTrue($called); - } - - public function testNextTickFiresBeforeIO() + public function testFutureTickEventGeneratedByFutureTick() { - $stream = $this->createStream(); - - $this->loop->addWriteStream( - $stream, - function () { - echo 'stream' . PHP_EOL; - } - ); - - $this->loop->nextTick( - function () { - echo 'next-tick' . PHP_EOL; - } - ); - - $this->expectOutputString('next-tick' . PHP_EOL . 'stream' . PHP_EOL); - - $this->loop->tick(); - } - - public function testRecursiveNextTick() - { - $stream = $this->createStream(); - - $this->loop->addWriteStream( - $stream, - function () { - echo 'stream' . PHP_EOL; - } - ); - - $this->loop->nextTick( - function () { - $this->loop->nextTick( - function () { - echo 'next-tick' . PHP_EOL; - } - ); - } - ); - - $this->expectOutputString('next-tick' . PHP_EOL . 'stream' . PHP_EOL); - - $this->loop->tick(); - } - - public function testRunWaitsForNextTickEvents() - { - $stream = $this->createStream(); - - $this->loop->addWriteStream( - $stream, - function () use ($stream) { - $this->loop->removeStream($stream); - $this->loop->nextTick( - function () { - echo 'next-tick' . PHP_EOL; - } - ); - } - ); - - $this->expectOutputString('next-tick' . PHP_EOL); - - $this->loop->run(); - } - - public function testNextTickEventGeneratedByFutureTick() - { - $stream = $this->createStream(); - $this->loop->futureTick( function () { - $this->loop->nextTick( - function () { - echo 'next-tick' . PHP_EOL; - } - ); - } - ); - - $this->expectOutputString('next-tick' . PHP_EOL); - - $this->loop->run(); - } - - public function testNextTickEventGeneratedByTimer() - { - $this->loop->addTimer( - 0.001, - function () { - $this->loop->nextTick( + $this->loop->futureTick( function () { - echo 'next-tick' . PHP_EOL; + echo 'future-tick' . PHP_EOL; } ); } ); - $this->expectOutputString('next-tick' . PHP_EOL); + $this->expectOutputString('future-tick' . PHP_EOL); $this->loop->run(); } @@ -444,25 +337,6 @@ function () { $this->loop->run(); } - public function testFutureTickEventGeneratedByNextTick() - { - $stream = $this->createStream(); - - $this->loop->nextTick( - function () { - $this->loop->futureTick( - function () { - echo 'future-tick' . PHP_EOL; - } - ); - } - ); - - $this->expectOutputString('future-tick' . PHP_EOL); - - $this->loop->run(); - } - public function testFutureTickEventGeneratedByTimer() { $this->loop->addTimer(