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 #7 from PSchwisow/issue-6-event-queue-ordering
Browse files Browse the repository at this point in the history
Issue #6 - Fixing bugs in event queue priority logic.
  • Loading branch information
elazar committed Nov 26, 2014
2 parents 71c1463 + a715e5b commit fad1679
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/EventQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class EventQueue extends \SplPriorityQueue implements EventQueueInterface
*/
protected $priorities;

/**
* Track the last timestamp used for priority so we can avoid duplicate values
*
* @var int
*/
protected $lastTimestamp = 0;

/**
* Initializes the list of event priorities.
*/
Expand Down Expand Up @@ -129,7 +136,11 @@ protected function getPriority($command, array $params)
{
$priority = new EventQueuePriority;
$priority->value = $this->priorities[$command];
$priority->timestamp = microtime(true) * 10000;
$priority->timestamp = (int) (microtime(true) * 10000);
if ($priority->timestamp <= $this->lastTimestamp) {
$priority->timestamp = $this->lastTimestamp + 1;
}
$this->lastTimestamp = $priority->timestamp;
return $priority;
}

Expand Down Expand Up @@ -158,7 +169,7 @@ protected function queueIrcRequest($command, array $params = array())
*/
public function compare($priority1, $priority2)
{
$priority = $priority2->value - $priority1->value;
$priority = $priority1->value - $priority2->value;
if (!$priority) {
$priority = $priority2->timestamp - $priority1->timestamp;
}
Expand Down
2 changes: 1 addition & 1 deletion src/EventQueuePriority.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class EventQueuePriority
* same command in order to assign higher priority to events inserted
* earlier
*
* @var float
* @var int
*/
public $timestamp;
}
38 changes: 38 additions & 0 deletions tests/EventQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,42 @@ public function testExtract()
$this->assertInstanceOf('\Phergie\Irc\Event\EventInterface', $this->queue->extract());
$this->assertNull($this->queue->extract());
}

/**
* Tests ordering by command priority then FIFO.
*/
public function testPriorities()
{
// start with empty queue
$this->assertNull($this->queue->extract());

// queue a bunch of stuff
$this->queue->ircQuit('Bye!');
$this->queue->ircPrivmsg('#channel', 'text1');
$this->queue->ircPrivmsg('#channel', 'text2');
$this->queue->ircPrivmsg('#channel', 'text3');

// verify order of output
$event = $this->queue->extract();
$this->assertInstanceOf('\Phergie\Irc\Event\EventInterface', $event);
$this->assertEquals('PRIVMSG', $event->getCommand());
$this->assertEquals(['#channel', 'text1'], $event->getParams());

$event = $this->queue->extract();
$this->assertInstanceOf('\Phergie\Irc\Event\EventInterface', $event);
$this->assertEquals('PRIVMSG', $event->getCommand());
$this->assertEquals(['#channel', 'text2'], $event->getParams());

$event = $this->queue->extract();
$this->assertInstanceOf('\Phergie\Irc\Event\EventInterface', $event);
$this->assertEquals('PRIVMSG', $event->getCommand());
$this->assertEquals(['#channel', 'text3'], $event->getParams());

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

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

0 comments on commit fad1679

Please sign in to comment.