function testMultiExecContext_CheckAndSet_Discard() { $client = RC::getConnection(); $client->flushdb(); $client->set('foo', 'bar'); $options = array('watch' => 'foo', 'cas' => true); $replies = $client->multiExec($options, function ($tx) { $tx->watch('foobar'); $foo = $tx->get('foo'); $tx->multi(); $tx->set('foobar', $foo); $tx->discard(); $tx->mget('foo', 'foobar'); }); $this->assertInternalType('array', $replies); $this->assertEquals(array(array('bar', null)), $replies); $hijack = true; $client->set('foo', 'bar'); $client2 = RC::getConnection(true); $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1); $replies = $client->multiExec($options, function ($tx) use($client2, &$hijack) { $foo = $tx->get('foo'); $tx->multi(); $tx->set('foobar', $foo); $tx->discard(); if ($hijack) { $hijack = false; $client2->set('foo', 'hijacked!'); } $tx->mget('foo', 'foobar'); }); $this->assertInternalType('array', $replies); $this->assertEquals(array(array('hijacked!', null)), $replies); }
protected function setUp() { $this->redis = RC::getConnection(); $this->redis->flushdb(); }
function testMultiExecBlock_Watch() { $client1 = RC::getConnection(); $client2 = RC::getConnection(true); $client1->flushdb(); RC::testForAbortedMultiExecException($this, function () use($client1, $client2) { $client1->multiExec(array('watch' => 'sentinel'), function ($multi) use($client2) { $multi->set('sentinel', 'client1'); $multi->get('sentinel'); $client2->set('sentinel', 'client2'); }); }); $this->assertEquals('client2', $client1->get('sentinel')); }
function testCommandPipeline_Flush() { $client = RC::getConnection(); $client->flushdb(); $pipe = $client->pipeline(); $pipe->set('foo', 'bar')->set('hoge', 'piyo'); $pipe->flushPipeline(); $pipe->ping()->mget(array('foo', 'hoge')); $replies = $pipe->execute(); $this->assertType('array', $replies); $this->assertEquals(4, count($replies)); $this->assertEquals('bar', $replies[3][0]); $this->assertEquals('piyo', $replies[3][1]); }
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 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')); }