function testMultiExecBlock_CheckAndSet_Discard() { $client = RC::getConnection(); $client->flushdb(); $client->set('foo', 'bar'); $options = array('watch' => 'foo', 'cas' => true); $replies = $client->multiExec($options, p_anon("\$tx", "\n \$tx->watch('foobar');\n \$foo = \$tx->get('foo');\n \$tx->multi();\n \$tx->set('foobar', \$foo);\n \$tx->discard();\n \$tx->mget('foo', 'foobar');\n ")); $this->assertInternalType('array', $replies); $this->assertEquals(array(array('bar', null)), $replies); $hijack = true; $client->set('foo', 'bar'); $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1); $replies = $client->multiExec($options, p_anon("\$tx", "\n \$client2 = RC::getConnection(true);\n \$hijack = \$client2->get('foo') !== 'hijacked';\n \$foo = \$tx->get('foo');\n \$tx->multi();\n \$tx->set('foobar', \$foo);\n \$tx->discard();\n if (\$hijack) {\n \$client2->set('foo', 'hijacked!');\n }\n \$tx->mget('foo', 'foobar');\n ")); $this->assertInternalType('array', $replies); $this->assertEquals(array(array('hijacked!', null)), $replies); }
function testSort() { $unorderedList = RC::pushTailAndReturn($this->redis, 'unordered', array(2, 100, 3, 1, 30, 10)); // without parameters $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->sort('unordered')); // with parameter ASC/DESC $this->assertEquals(array(100, 30, 10, 3, 2, 1), $this->redis->sort('unordered', array('sort' => 'desc'))); // with parameter LIMIT $this->assertEquals(array(1, 2, 3), $this->redis->sort('unordered', array('limit' => array(0, 3)))); $this->assertEquals(array(10, 30), $this->redis->sort('unordered', array('limit' => array(3, 2)))); // with parameter ALPHA $this->assertEquals(array(1, 10, 100, 2, 3, 30), $this->redis->sort('unordered', array('alpha' => true))); // with combined parameters $this->assertEquals(array(30, 10, 3, 2), $this->redis->sort('unordered', array('alpha' => false, 'sort' => 'desc', 'limit' => array(1, 4)))); // with parameter ALPHA $this->assertEquals(array(1, 10, 100, 2, 3, 30), $this->redis->sort('unordered', array('alpha' => true))); // with parameter STORE $this->assertEquals(count($unorderedList), $this->redis->sort('unordered', array('store' => 'ordered'))); $this->assertEquals(array(1, 2, 3, 10, 30, 100), $this->redis->lrange('ordered', 0, -1)); // with parameter GET $this->redis->rpush('uids', 1003); $this->redis->rpush('uids', 1001); $this->redis->rpush('uids', 1002); $this->redis->rpush('uids', 1000); $sortget = array('uid:1000' => 'foo', 'uid:1001' => 'bar', 'uid:1002' => 'hoge', 'uid:1003' => 'piyo'); $this->redis->mset($sortget); $this->assertEquals(array_values($sortget), $this->redis->sort('uids', array('get' => 'uid:*'))); // wrong type RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, p_anon("\$test", "\n \$test->redis->set('foo', 'bar');\n \$test->redis->sort('foo');\n ")); }
function testMultiExecBlock_RetryOnServerAbort() { $client1 = RC::getConnection(); $client1->flushdb(); $retry = 3; $thrownException = null; try { $options = array('watch' => 'sentinel', 'retry' => $retry); $client1->multiExec($options, p_anon("\$tx", "\n \$tx->set('sentinel', 'client1');\n \$tx->get('sentinel');\n \$client2 = RC::getConnection(true);\n \$client2->incr('attempts');\n \$client2->set('sentinel', 'client2');\n ")); } catch (Predis_AbortedMultiExec $exception) { $thrownException = $exception; } $this->assertType('Predis_AbortedMultiExec', $thrownException); $this->assertEquals('The current transaction has been aborted by the server', $thrownException->getMessage()); $this->assertEquals('client2', $client1->get('sentinel')); $this->assertEquals($retry + 1, $client1->get('attempts')); $client1->del('attempts', 'sentinel'); $thrownException = null; try { $options = array('watch' => 'sentinel', 'cas' => true, 'retry' => $retry); $client1->multiExec($options, p_anon("\$tx", "\n \$tx->incr('attempts');\n \$tx->multi();\n \$tx->set('sentinel', 'client1');\n \$tx->get('sentinel');\n \$client2 = RC::getConnection(true);\n \$client2->set('sentinel', 'client2');\n ")); } catch (Predis_AbortedMultiExec $exception) { $thrownException = $exception; } $this->assertType('Predis_AbortedMultiExec', $thrownException); $this->assertEquals('The current transaction has been aborted by the server', $thrownException->getMessage()); $this->assertEquals('client2', $client1->get('sentinel')); $this->assertEquals($retry + 1, $client1->get('attempts')); }