From 0d4099585188c8f3b8ea59970dfd56c24da6bf54 Mon Sep 17 00:00:00 2001 From: elazar Date: Tue, 31 Mar 2015 18:59:47 -0500 Subject: [PATCH] #9: Implement an event queue factory to resolve issues with multiple connections --- src/Bot.php | 34 +++++++++---------- src/EventQueueFactory.php | 50 ++++++++++++++++++++++++++++ src/EventQueueFactoryInterface.php | 28 ++++++++++++++++ tests/BotTest.php | 53 ++++++++++++++++++++---------- 4 files changed, 131 insertions(+), 34 deletions(-) create mode 100644 src/EventQueueFactory.php create mode 100644 src/EventQueueFactoryInterface.php diff --git a/src/Bot.php b/src/Bot.php index a5ad221..6d0b174 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -76,11 +76,11 @@ class Bot protected $converter; /** - * Queue for events generated by plugins to be sent to servers + * Event queue factory for creating connection-specific event queues * - * @var \Phergie\Irc\Bot\React\EventQueueInterface + * @var \Phergie\Irc\Bot\React\EventQueueFactoryInterface */ - protected $queue; + protected $queueFactory; /** * Sets the IRC client for the bot to use. @@ -198,26 +198,26 @@ public function getConverter() } /** - * Sets the event queue for the bot to use. + * Sets the event queue factory for the bot to use. * - * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue + * @param \Phergie\Irc\Bot\React\EventQueueFactoryInterface $queueFactory */ - public function setEventQueue(EventQueueInterface $queue) + public function setEventQueueFactory(EventQueueFactoryInterface $queueFactory) { - $this->queue = $queue; + $this->queueFactory = $queueFactory; } /** - * Returns the event queue in use by the bot. + * Returns the event queue factory in use by the bot. * - * @return \Phergie\Irc\Bot\React\EventQueueInterface + * @return \Phergie\Irc\Bot\React\EventQueueFactoryInterface */ - public function getEventQueue() + public function getEventQueueFactory() { - if (!$this->queue) { - $this->queue = new EventQueue; + if (!$this->queueFactory) { + $this->queueFactory = new EventQueueFactory; } - return $this->queue; + return $this->queueFactory; } /** @@ -258,8 +258,8 @@ protected function setDependencyOverrides(array $config) $this->setConverter($config['converter']); } - if (isset($config['eventQueue'])) { - $this->setEventQueue($config['eventQueue']); + if (isset($config['eventQueueFactory'])) { + $this->setEventQueueFactory($config['eventQueueFactory']); } } @@ -472,7 +472,7 @@ public function processClientEvent($event, array $message, ConnectionInterface $ $converted->setConnection($connection); $client = $this->getClient(); - $queue = $this->getEventQueue(); + $queue = $this->getEventQueueFactory()->getEventQueue($connection); $params = array($converted, $queue); $subtype = $this->getEventSubtype($converted); $client->emit($event . '.each', $params); @@ -493,7 +493,7 @@ public function processClientEvent($event, array $message, ConnectionInterface $ public function processOutgoingEvents(ConnectionInterface $connection, WriteStream $write) { $client = $this->getClient(); - $queue = $this->getEventQueue(); + $queue = $this->getEventQueueFactory()->getEventQueue($connection); $client->emit('irc.sending.all', array($queue)); while ($extracted = $queue->extract()) { diff --git a/src/EventQueueFactory.php b/src/EventQueueFactory.php new file mode 100644 index 0000000..04fb98c --- /dev/null +++ b/src/EventQueueFactory.php @@ -0,0 +1,50 @@ +queues = new \SplObjectStorage; + } + + /** + * Returns the event queue for a specified connection. + * + * @param \Phergie\Irc\ConnectionInterface $connection + * @return \Phergie\Irc\Bot\React\EventQueueInterface + */ + public function getEventQueue(ConnectionInterface $connection) + { + if (!isset($this->queues[$connection])) { + $this->queues[$connection] = new EventQueue; + } + return $this->queues[$connection]; + } +} diff --git a/src/EventQueueFactoryInterface.php b/src/EventQueueFactoryInterface.php new file mode 100644 index 0000000..a2c3bcb --- /dev/null +++ b/src/EventQueueFactoryInterface.php @@ -0,0 +1,28 @@ +getMockEventQueue(); - $this->bot->setEventQueue($queue); - $this->assertSame($queue, $this->bot->getEventQueue()); + $queue = $this->getMockEventQueueFactory(); + $this->bot->setEventQueueFactory($queue); + $this->assertSame($queue, $this->bot->getEventQueueFactory()); } /** - * Tests getEventQueue(). + * Tests getEventQueueFactory(). */ - public function testGetEventQueue() + public function testGetEventQueueFactory() { $this->assertInstanceOf( - '\Phergie\Irc\Bot\React\EventQueueInterface', - $this->bot->getEventQueue() + '\Phergie\Irc\Bot\React\EventQueueFactoryInterface', + $this->bot->getEventQueueFactory() ); } @@ -442,9 +443,6 @@ public function testEventCallbacks(EventInterface $eventObject, $eventType, $eve Phake::when($parser)->parse($message)->thenReturn($message); $this->bot->setParser($parser); - $queue = $this->getMockEventQueue(); - $this->bot->setEventQueue($queue); - $client = new \Phergie\Irc\Client\React\Client; $this->bot->setClient($client); @@ -452,6 +450,11 @@ public function testEventCallbacks(EventInterface $eventObject, $eventType, $eve $connection = $params[] = $this->getMockConnection(); $logger = $params[] = $this->getMockLogger(); + $queue = $this->getMockEventQueue(); + $queueFactory = $this->getMockEventQueueFactory(); + Phake::when($queueFactory)->getEventQueue($connection)->thenReturn($queue); + $this->bot->setEventQueueFactory($queueFactory); + $test = $this; $allCalled = false; $typeCalled = false; @@ -485,7 +488,6 @@ public function testTickEvent() $queue = $this->getMockEventQueue(); Phake::when($queue)->extract()->thenReturn($eventObject)->thenReturn(false); - $this->bot->setEventQueue($queue); $client = new \Phergie\Irc\Client\React\Client; $this->bot->setClient($client); @@ -522,6 +524,11 @@ function($otherEvent, $otherQueue) $write = $params[] = $this->getMockWriteStream(); $connection = $params[] = $this->getMockConnection(); + + $queueFactory = $this->getMockEventQueueFactory(); + Phake::when($queueFactory)->getEventQueue($connection)->thenReturn($queue); + $this->bot->setEventQueueFactory($queueFactory); + $client->emit('irc.tick', $params); Phake::verify($eventObject)->setConnection($connection); @@ -565,7 +572,9 @@ public function testPluginEmittedEvents($event, $class, $method) $connections = array($connection); $queue = new EventQueue; - $this->bot->setEventQueue($queue); + $queueFactory = $this->getMockEventQueueFactory(); + Phake::when($queueFactory)->getEventQueue($connection)->thenReturn($queue); + $this->bot->setEventQueueFactory($queueFactory); $eventObject = Phake::mock('\Phergie\Irc\Event\UserEvent'); $eventParams = array('#channel', 'message'); @@ -622,14 +631,14 @@ public function testOverrideDependencies() $logger = $this->getMockLogger(); $parser = $this->getMockParser(); $converter = $this->getMockConverter(); - $eventQueue = $this->getMockEventQueue(); + $eventQueueFactory = $this->getMockEventQueueFactory(); $config = array( 'client' => $client, 'logger' => $logger, 'parser' => $parser, 'converter' => $converter, - 'eventQueue' => $eventQueue, + 'eventQueueFactory' => $eventQueueFactory, 'plugins' => array(), 'connections' => array($this->getMockConnection()), ); @@ -641,7 +650,7 @@ public function testOverrideDependencies() $this->assertSame($logger, $this->bot->getLogger()); $this->assertSame($parser, $this->bot->getParser()); $this->assertSame($converter, $this->bot->getConverter()); - $this->assertSame($eventQueue, $this->bot->getEventQueue()); + $this->assertSame($eventQueueFactory, $this->bot->getEventQueueFactory()); } /*** SUPPORTING METHODS ***/ @@ -696,6 +705,16 @@ protected function getMockEventQueue() return Phake::mock('\Phergie\Irc\Bot\React\EventQueueInterface'); } + /** + * Returns a mock event queue factory. + * + * @return \Phergie\Irc\Bot\React\EventQueueFactoryInterface + */ + protected function getMockEventQueueFactory() + { + return Phake::mock('\Phergie\Irc\Bot\React\EventQueueFactoryInterface'); + } + /** * Returns a mock plugin. *