Skip to content
This repository has been archived by the owner on Mar 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #17 from Renegade334/devel-eventqueue-internal
Browse files Browse the repository at this point in the history
Partition the priority queue as an internal property of EventQueue
  • Loading branch information
elazar committed Apr 3, 2015
2 parents 0d40995 + 9f8db09 commit aef6a00
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 30 deletions.
61 changes: 32 additions & 29 deletions src/EventQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@
* @category Phergie
* @package Phergie\Irc\Bot\React
*/
class EventQueue extends \SplPriorityQueue implements EventQueueInterface
class EventQueue implements EventQueueInterface
{
/**
* Internal priority queue.
*
* @var \Phergie\Irc\Bot\React\EventQueueInternal
*/
protected $queue;

/**
* Prefix for queued messages
*
Expand All @@ -48,18 +55,31 @@ class EventQueue extends \SplPriorityQueue implements EventQueueInterface
*/
public function __construct()
{
// For HHVM compatibility
// @see https://github.com/facebook/hhvm/issues/1695
// @see http://stackoverflow.com/a/4650617/906821
// @codeCoverageIgnoreStart
if (method_exists('\SplPriorityQueue', '__construct')) {
parent::__construct();
}
// @codeCoverageIgnoreEnd
$this->queue = new EventQueueInternal;

$this->priorities = $this->getPriorities();
}

/**
* Allows iteration over the event queue.
*
* @return \Phergie\Irc\Bot\React\EventQueueInternal
*/
public function getIterator()
{
return clone $this->queue;
}

/**
* Wrapper for the queue's internal count method.
*
* @return int
*/
public function count()
{
return $this->queue->count();
}

/**
* Removes and returns an event from the front of the queue.
*
Expand All @@ -68,10 +88,10 @@ public function __construct()
*/
public function extract()
{
if ($this->isEmpty()) {
if ($this->queue->isEmpty()) {
return null;
}
return parent::extract();
return $this->queue->extract();
}

/**
Expand Down Expand Up @@ -115,7 +135,7 @@ protected function queueRequest(UserEventInterface $event, $command, array $para
$event->setPrefix($this->prefix);
$event->setCommand($command);
$event->setParams(array_filter($params));
$this->insert($event, $this->getPriority($command, $params));
$this->queue->insert($event, $this->getPriority($command, $params));
}

/**
Expand All @@ -129,23 +149,6 @@ protected function queueIrcRequest($command, array $params = array())
$this->queueRequest(new UserEvent, $command, $params);
}

/**
* Overrides native default comparison logic to assign higher priority to
* events inserted earlier.
*
* @param \Phergie\Irc\Bot\React\EventQueuePriority $priority1
* @param \Phergie\Irc\Bot\React\EventQueuePriority $priority2
* @return int
*/
public function compare($priority1, $priority2)
{
$priority = $priority1->value - $priority2->value;
if (!$priority) {
$priority = $priority2->timestamp - $priority1->timestamp;
}
return $priority;
}

/**
* Enqueues a new CTCP event.
*
Expand Down
2 changes: 1 addition & 1 deletion src/EventQueueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @category Phergie
* @package Phergie\Irc\Bot\React
*/
interface EventQueueInterface extends GeneratorInterface, \Iterator, \Countable
interface EventQueueInterface extends GeneratorInterface, \IteratorAggregate, \Countable
{
/**
* Removes and returns an event from the front of the queue.
Expand Down
40 changes: 40 additions & 0 deletions src/EventQueueInternal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* Phergie (http://phergie.org)
* @copyright Copyright (c) 2008-2015 Phergie Development Team (http://phergie.org)
* @license http://phergie.org/license Simplified BSD License
* @package Phergie\Irc\Bot\React
*/

namespace Phergie\Irc\Bot\React;

/**
* Implementation of SplPriorityQueue specifically for internal use within
* the EventQueue class.
*
* @category Phergie
* @package Phergie\Irc\Bot\React
*/
class EventQueueInternal extends \SplPriorityQueue
{
/**
* Overrides native default comparison logic to assign higher priority to
* events inserted earlier.
*
* @param \Phergie\Irc\Bot\React\EventQueuePriority $priority1
* @param \Phergie\Irc\Bot\React\EventQueuePriority $priority2
* @return int
*/
public function compare($priority1, $priority2)
{
if (!$priority1 instanceof EventQueuePriority || !$priority2 instanceof EventQueuePriority) {
return parent::compare($priority1, $priority2);
}

$priority = $priority1->value - $priority2->value;
if (!$priority) {
$priority = $priority2->timestamp - $priority1->timestamp;
}
return $priority;
}
}
21 changes: 21 additions & 0 deletions tests/EventQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,25 @@ public function testPriorities()

$this->assertNull($this->queue->extract());
}

/**
* Tests that iterating over the event queue does not truncate its contents.
*/
public function testNonDestructiveIteration()
{
$this->assertNull($this->queue->extract());

$this->queue->ircQuit();
$contents = [];
foreach ($this->queue as $value) {
$contents[] = $value;
}

$event = $this->queue->extract();
$this->assertInstanceOf('\Phergie\Irc\Event\EventInterface', $event);
$this->assertEquals('QUIT', $event->getCommand());
$this->assertEmpty($event->getParams());

$this->assertEquals([$event], $contents);
}
}

0 comments on commit aef6a00

Please sign in to comment.