Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added PEIP_Redis\Queue\Queue + tests

  • Loading branch information...
commit d9de53c1b66e58917e5aaa1020049f2ebd155420 1 parent 529394a
@tidal authored
View
46 src/Queue/Queue.php
@@ -0,0 +1,46 @@
+<?php
+
+namespace PEIP_Redis\Queue;
+
+/*
+ * This file is part of the PEIP_Redis package.
+ * (c) 2009-2011 Timo Michna <timomichna/yahoo.de>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+/**
+ * Queue
+ * Basic implementation of a Queue using a redis list
+ *
+ * @author Timo Michna <timomichna/yahoo.de>
+ * @package PEIP_Redis
+ */
+
+class Queue {
+
+ protected
+ $client,
+ $key,
+ $timeout;
+
+ public function __construct(\Predis\Client $client, $key, $timeout = 1){
+ $this->client = $client;
+ $this->key = $key;
+ $this->timeout = $timeout;
+ }
+
+ public function enqueue($value){
+ $this->client->rpush($this->key, $value);
+ }
+
+ public function dequeue(){
+ $res = $this->client->blpop($this->key, $this->timeout);
+ return $res[1];
+ }
+
+
+
+
+}
View
44 tests/Queue/QueueTest.php
@@ -0,0 +1,44 @@
+<?php
+require_once __DIR__.'/../../src/Queue/Queue.php';
+require_once __DIR__.'/../bootstrap.php';
+
+class RedisCommandTestSuite extends PHPUnit_Framework_TestCase {
+ public $redis;
+
+ protected function setUp() {
+ $this->redis = RC::getConnection();
+ $this->redis->flushdb();
+ }
+
+ public function testEnqueue(){
+ $key = 'bar';
+ $value = 'foo';
+ $queue = new PEIP_Redis\Queue\Queue($this->redis, $key);
+
+ $queue->enqueue($value);
+
+ $this->assertEquals($value, $queue->dequeue());
+ }
+
+ public function testEnqueueError(){
+ $key = 'bar';
+ $value = 'foo';
+ $queue = new PEIP_Redis\Queue\Queue($this->redis, $key);
+
+ $this->assertNotEquals($value, $queue->dequeue());
+ }
+
+ public function testEnqueueTimeout(){
+ $key = 'bar';
+ $value = 'foo';
+ $timeout = 1;
+ $time1 = microtime(1);
+ $queue = new PEIP_Redis\Queue\Queue($this->redis, $key);
+ $queue->dequeue();
+ $this->assertGreaterThanOrEqual($time1 + $timeout, microtime(1));
+ }
+
+
+
+}
+
View
6 tests/bootstrap.php
@@ -0,0 +1,6 @@
+<?php
+
+require_once(__DIR__.'/../src/vendor/predis/test/bootstrap.php');
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.