diff --git a/test/PredisShared.php b/test/PredisShared.php index e22e84c7..14fa4c1d 100644 --- a/test/PredisShared.php +++ b/test/PredisShared.php @@ -25,7 +25,8 @@ class RC { private static $_connection; private static function createConnection() { - $connection = new Predis\Client(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT)); + $serverProfile = new Predis\RedisServer__Futures(); + $connection = new Predis\Client(array('host' => RC::SERVER_HOST, 'port' => RC::SERVER_PORT), $serverProfile); $connection->connect(); $connection->selectDatabase(RC::DEFAULT_DATABASE); return $connection; @@ -45,6 +46,22 @@ class RC { } } + public static function helperForBlockingPops($op) { + // TODO: I admit that this helper is kinda lame and it does not run + // in a separate process to properly test BLPOP/BRPOP + $redisUri = sprintf('redis://%s:%d/?database=%d', RC::SERVER_HOST, RC::SERVER_PORT, RC::DEFAULT_DATABASE); + $handle = popen('php', 'w'); + fwrite($handle, "rpush('{$op}1', 'a'); + \$redis->rpush('{$op}2', 'b'); + \$redis->rpush('{$op}3', 'c'); + \$redis->rpush('{$op}1', 'd'); + ?>"); + pclose($handle); + } + public static function getArrayOfNumbers() { return array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); } diff --git a/test/RedisCommandsTest.php b/test/RedisCommandsTest.php index ed637df0..2c050ebc 100644 --- a/test/RedisCommandsTest.php +++ b/test/RedisCommandsTest.php @@ -535,6 +535,60 @@ class RedisCommandTestSuite extends PHPUnit_Framework_TestCase { }); } + function testListBlockingPopFirst() { + // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it + // does not run with a concurrent client pushing items on lists. + RC::helperForBlockingPops('blpop'); + + // BLPOP on one key + $start = time(); + $item = $this->redis->blpop('blpop3', 5); + $this->assertEquals((float)(time() - $start), 0, '', 1); + $this->assertEquals($item, array('blpop3', 'c')); + + // BLPOP on more than one key + $poppedItems = array(); + while ($item = $this->redis->blpop('blpop1', 'blpop2', 1)) { + $poppedItems[] = $item; + } + $this->assertEquals( + array(array('blpop1', 'a'), array('blpop1', 'd'), array('blpop2', 'b')), + $poppedItems + ); + + // check if BLPOP timeouts as expected on empty lists + $start = time(); + $this->redis->blpop('blpop4', 2); + $this->assertEquals((float)(time() - $start), 2, '', 1); + } + + function testListBlockingPopLast() { + // TODO: this test does not cover all the aspects of BLPOP/BRPOP as it + // does not run with a concurrent client pushing items on lists. + RC::helperForBlockingPops('brpop'); + + // BRPOP on one key + $start = time(); + $item = $this->redis->brpop('brpop3', 5); + $this->assertEquals((float)(time() - $start), 0, '', 1); + $this->assertEquals($item, array('brpop3', 'c')); + + // BRPOP on more than one key + $poppedItems = array(); + while ($item = $this->redis->brpop('brpop1', 'brpop2', 1)) { + $poppedItems[] = $item; + } + $this->assertEquals( + array(array('brpop1', 'd'), array('brpop1', 'a'), array('brpop2', 'b')), + $poppedItems + ); + + // check if BRPOP timeouts as expected on empty lists + $start = time(); + $this->redis->brpop('brpop4', 2); + $this->assertEquals((float)(time() - $start), 2, '', 1); + } + /* commands operating on sets */