123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743 |
- <?php
- class ClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
- public $redis;
- protected function setUp() {
- $this->redis = RC::getConnection();
- $this->redis->flushdb();
- }
- protected function tearDown() {
- }
- protected function onNotSuccessfulTest(Exception $exception) {
- // drops and reconnect to a redis server on uncaught exceptions
- RC::resetConnection();
- parent::onNotSuccessfulTest($exception);
- }
- /* ConnectionParameters */
- function testConnectionParametersDefaultValues() {
- $params = new \Predis\ConnectionParameters();
- $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_HOST, $params->host);
- $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_PORT, $params->port);
- $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout);
- $this->assertNull($params->read_write_timeout);
- $this->assertNull($params->database);
- $this->assertNull($params->password);
- $this->assertNull($params->alias);
- }
- function testConnectionParametersSetupValuesArray() {
- $paramsArray = RC::getConnectionParametersArgumentsArray();
- $params = new \Predis\ConnectionParameters($paramsArray);
- $this->assertEquals($paramsArray['host'], $params->host);
- $this->assertEquals($paramsArray['port'], $params->port);
- $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
- $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
- $this->assertEquals($paramsArray['database'], $params->database);
- $this->assertEquals($paramsArray['password'], $params->password);
- $this->assertEquals($paramsArray['alias'], $params->alias);
- }
- function testConnectionParametersSetupValuesString() {
- $paramsArray = RC::getConnectionParametersArgumentsArray();
- $paramsString = RC::getConnectionParametersArgumentsString($paramsArray);
- $params = new \Predis\ConnectionParameters($paramsArray);
- $this->assertEquals($paramsArray['host'], $params->host);
- $this->assertEquals($paramsArray['port'], $params->port);
- $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
- $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
- $this->assertEquals($paramsArray['database'], $params->database);
- $this->assertEquals($paramsArray['password'], $params->password);
- $this->assertEquals($paramsArray['alias'], $params->alias);
- }
- /* Command and derivates */
- function testCommand_TestArguments() {
- $cmdArgs = array('key1', 'key2', 'key3');
- $cmd = new \Predis\Commands\GetMultiple();
- $cmd->setArgumentsArray($cmdArgs);
- $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
- $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
- $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
- $cmd = new \Predis\Commands\GetMultiple();
- $cmd->setArguments('key1', 'key2', 'key3');
- $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
- $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
- $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
- $cmd = new \Predis\Commands\Ping();
- $this->assertNull($cmd->getArgument(0));
- }
- function testCommand_InlineWithNoArguments() {
- $cmd = new \Predis\Compatibility\v1_0\Commands\Ping();
- $this->assertInstanceOf('\Predis\InlineCommand', $cmd);
- $this->assertEquals('PING', $cmd->getCommandId());
- $this->assertFalse($cmd->closesConnection());
- $this->assertFalse($cmd->canBeHashed());
- $this->assertNull($cmd->getHash(new \Predis\Distribution\HashRing()));
- $this->assertEquals("PING\r\n", $cmd());
- }
- function testCommand_InlineWithArguments() {
- $cmd = new \Predis\Compatibility\v1_0\Commands\Get();
- $cmd->setArgumentsArray(array('key'));
- $this->assertInstanceOf('\Predis\InlineCommand', $cmd);
- $this->assertEquals('GET', $cmd->getCommandId());
- $this->assertFalse($cmd->closesConnection());
- $this->assertTrue($cmd->canBeHashed());
- $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
- $this->assertEquals("GET key\r\n", $cmd());
- }
- function testCommand_BulkWithArguments() {
- $cmd = new \Predis\Compatibility\v1_0\Commands\Set();
- $cmd->setArgumentsArray(array('key', 'value'));
- $this->assertInstanceOf('\Predis\BulkCommand', $cmd);
- $this->assertEquals('SET', $cmd->getCommandId());
- $this->assertFalse($cmd->closesConnection());
- $this->assertTrue($cmd->canBeHashed());
- $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
- $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd());
- }
- function testCommand_MultiBulkWithArguments() {
- $cmd = new \Predis\Commands\SetMultiple();
- $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2'));
- $this->assertInstanceOf('\Predis\MultiBulkCommand', $cmd);
- $this->assertEquals('MSET', $cmd->getCommandId());
- $this->assertFalse($cmd->closesConnection());
- $this->assertFalse($cmd->canBeHashed());
- $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
- $this->assertEquals("*5\r\n$4\r\nMSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n$4\r\nkey2\r\n$6\r\nvalue2\r\n", $cmd());
- }
- function testCommand_ParseResponse() {
- // default parser
- $cmd = new \Predis\Commands\Get();
- $this->assertEquals('test', $cmd->parseResponse('test'));
- // overridden parser (boolean)
- $cmd = new \Predis\Commands\Exists();
- $this->assertTrue($cmd->parseResponse('1'));
- $this->assertFalse($cmd->parseResponse('0'));
- // overridden parser (boolean)
- $cmd = new \Predis\Commands\Ping();
- $this->assertTrue($cmd->parseResponse('PONG'));
- // overridden parser (complex)
- // TODO: emulate a respons to INFO
- }
- /* RedisServerProfile and derivates */
- function testRedisServerProfile_GetSpecificVersions() {
- $this->assertInstanceOf('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0'));
- $this->assertInstanceOf('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2'));
- $this->assertInstanceOf('\Predis\RedisServer_v2_0', \Predis\RedisServerProfile::get('2.0'));
- $this->assertInstanceOf('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev'));
- $this->assertInstanceOf('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default'));
- $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault());
- }
- function testRedisServerProfile_SupportedCommands() {
- $profile_10 = \Predis\RedisServerProfile::get('1.0');
- $profile_12 = \Predis\RedisServerProfile::get('1.2');
- $this->assertTrue($profile_10->supportsCommand('info'));
- $this->assertTrue($profile_12->supportsCommand('info'));
- $this->assertFalse($profile_10->supportsCommand('mset'));
- $this->assertTrue($profile_12->supportsCommand('mset'));
- $this->assertFalse($profile_10->supportsCommand('multi'));
- $this->assertFalse($profile_12->supportsCommand('multi'));
- }
- function testRedisServerProfile_CommandsCreation() {
- $profile = \Predis\RedisServerProfile::get('1.0');
- $cmdNoArgs = $profile->createCommand('info');
- $this->assertInstanceOf('\Predis\Compatibility\v1_0\Commands\Info', $cmdNoArgs);
- $this->assertNull($cmdNoArgs->getArgument());
- $args = array('key1', 'key2');
- $cmdWithArgs = $profile->createCommand('mget', $args);
- $this->assertInstanceOf('\Predis\Compatibility\v1_0\Commands\GetMultiple', $cmdWithArgs);
- $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
- $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
- $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
- $bogusCommand = 'not_existing_command';
- $expectedMessage = "'$bogusCommand' is not a registered Redis command";
- RC::testForClientException($this, $expectedMessage, function()
- use($profile, $bogusCommand) {
- $profile->createCommand($bogusCommand);
- });
- }
- function testRedisServerProfile_CommandsRegistration() {
- $profile = \Predis\RedisServerProfile::get('1.0');
- $cmdId = 'mset';
- $cmdClass = '\Predis\Commands\SetMultiple';
- $this->assertFalse($profile->supportsCommand($cmdId));
- $profile->registerCommand(new $cmdClass(), $cmdId);
- $this->assertTrue($profile->supportsCommand($cmdId));
- $this->assertInstanceOf($cmdClass, $profile->createCommand($cmdId));
- }
- /* ResponseQueued */
- function testResponseQueued() {
- $response = new \Predis\ResponseQueued();
- $this->assertTrue($response->skipParse);
- $this->assertTrue($response->queued);
- $this->assertEquals(\Predis\Protocol::QUEUED, (string)$response);
- }
- /* ResponseError */
- function testResponseError() {
- $errorMessage = 'ERROR MESSAGE';
- $response = new \Predis\ResponseError($errorMessage);
- $this->assertTrue($response->skipParse);
- $this->assertTrue($response->error);
- $this->assertEquals($errorMessage, $response->message);
- $this->assertEquals($errorMessage, (string)$response);
- }
- /* Connection */
- function testConnection_StringCastReturnsIPAndPort() {
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
- }
- function testConnection_ConnectDisconnect() {
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $this->assertFalse($connection->isConnected());
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- $connection->disconnect();
- $this->assertFalse($connection->isConnected());
- }
- function testConnection_WriteAndReadCommand() {
- $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $connection->connect();
- $connection->writeCommand($cmd);
- $this->assertTrue($connection->readResponse($cmd));
- }
- function testConnection_WriteCommandAndCloseConnection() {
- $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit');
- $connection = new \Predis\Connection(new \Predis\ConnectionParameters(
- RC::getConnectionArguments() + array('read_write_timeout' => 0.5)
- ));
- $connection->connect();
- $this->assertTrue($connection->isConnected());
- $connection->writeCommand($cmd);
- $connection->disconnect();
- $exceptionMessage = 'Error while reading line from the server';
- RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
- $connection->readResponse($cmd);
- });
- }
- function testConnection_GetSocketOpensConnection() {
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $this->assertFalse($connection->isConnected());
- $this->assertInternalType('resource', $connection->getSocket());
- $this->assertTrue($connection->isConnected());
- }
- function testConnection_LazyConnect() {
- $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $this->assertFalse($connection->isConnected());
- $connection->writeCommand($cmd);
- $this->assertTrue($connection->isConnected());
- $this->assertTrue($connection->readResponse($cmd));
- }
- function testConnection_RawCommand() {
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
- }
- function testConnection_Alias() {
- $connection1 = new \Predis\Connection(RC::getConnectionParameters());
- $this->assertNull($connection1->getParameters()->alias);
- $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
- $connection2 = new \Predis\Connection(new \Predis\ConnectionParameters($args));
- $this->assertEquals('servername', $connection2->getParameters()->alias);
- }
- function testConnection_ConnectionTimeout() {
- $timeout = 3;
- $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
- $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
- $start = time();
- RC::testForCommunicationException($this, null, function() use($connection) {
- $connection->connect();
- });
- $this->assertEquals((float)(time() - $start), $timeout, '', 1);
- }
- function testConnection_ReadTimeout() {
- $timeout = 1;
- $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
- $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
- $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
- $expectedMessage = 'Error while reading line from the server';
- $start = time();
- RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) {
- $connection->readResponse($cmdFake);
- });
- $this->assertEquals((float)(time() - $start), $timeout, '', 1);
- }
- /* ResponseReader */
- function testResponseReader_OptionIterableMultiBulkReplies() {
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $connection->getResponseReader()->setOption('iterable_multibulk', false);
- $this->assertInternalType('array', $connection->rawCommand("KEYS *\r\n"));
- $connection->getResponseReader()->setOption('iterable_multibulk', true);
- $this->assertInstanceOf('\Iterator', $connection->rawCommand("KEYS *\r\n"));
- }
- function testResponseReader_OptionExceptionOnError() {
- $connection = new \Predis\Connection(RC::getConnectionParameters());
- $responseReader = $connection->getResponseReader();
- $connection->rawCommand("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
- $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
- $responseReader->setOption('throw_on_error', false);
- $errorReply = $connection->rawCommand($rawCmdUnexpected);
- $this->assertInstanceOf('\Predis\ResponseError', $errorReply);
- $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
- $responseReader->setOption('throw_on_error', true);
- RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
- use ($connection, $rawCmdUnexpected) {
- $connection->rawCommand($rawCmdUnexpected);
- });
- }
- function testResponseReader_EmptyBulkResponse() {
- $this->assertTrue($this->redis->set('foo', ''));
- $this->assertEquals('', $this->redis->get('foo'));
- $this->assertEquals('', $this->redis->get('foo'));
- }
- /* Client + CommandPipeline */
- function testCommandPipeline_Simple() {
- $client = RC::getConnection();
- $client->flushdb();
- $pipe = $client->pipeline();
- $this->assertInstanceOf('\Predis\CommandPipeline', $pipe);
- $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->set('foo', 'bar'));
- $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo'));
- $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->mset(array(
- 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
- )));
- $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->mget(array(
- 'foo', 'hoge', 'foofoo', 'hogehoge'
- )));
- $replies = $pipe->execute();
- $this->assertInternalType('array', $replies);
- $this->assertEquals(4, count($replies));
- $this->assertEquals(4, count($replies[3]));
- $this->assertEquals('barbar', $replies[3][2]);
- }
- function testCommandPipeline_FluentInterface() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
- $this->assertInternalType('array', $replies);
- $this->assertEquals('bar', $replies[2]);
- }
- function testCommandPipeline_CallableAnonymousBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->pipeline(function($pipe) {
- $pipe->ping();
- $pipe->set('foo', 'bar');
- $pipe->get('foo');
- });
- $this->assertInternalType('array', $replies);
- $this->assertEquals('bar', $replies[2]);
- }
- function testCommandPipeline_ClientExceptionInCallableBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- RC::testForClientException($this, 'TEST', function() use($client) {
- $client->pipeline(function($pipe) {
- $pipe->ping();
- $pipe->set('foo', 'bar');
- throw new \Predis\ClientException("TEST");
- });
- });
- $this->assertFalse($client->exists('foo'));
- }
- function testCommandPipeline_ServerExceptionInCallableBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- $client->getResponseReader()->setOption('throw_on_error', false);
- $replies = $client->pipeline(function($pipe) {
- $pipe->set('foo', 'bar');
- $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
- $pipe->set('hoge', 'piyo');
- });
- $this->assertInternalType('array', $replies);
- $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
- $this->assertTrue($client->exists('foo'));
- $this->assertTrue($client->exists('hoge'));
- }
- 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->assertInternalType('array', $replies);
- $this->assertEquals(4, count($replies));
- $this->assertEquals('bar', $replies[3][0]);
- $this->assertEquals('piyo', $replies[3][1]);
- }
- /* Client + MultiExecBlock */
- function testMultiExecBlock_Simple() {
- $client = RC::getConnection();
- $client->flushdb();
- $multi = $client->multiExec();
- $this->assertInstanceOf('\Predis\MultiExecBlock', $multi);
- $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->set('foo', 'bar'));
- $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->set('hoge', 'piyo'));
- $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->mset(array(
- 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
- )));
- $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->mget(array(
- 'foo', 'hoge', 'foofoo', 'hogehoge'
- )));
- $replies = $multi->execute();
- $this->assertInternalType('array', $replies);
- $this->assertEquals(4, count($replies));
- $this->assertEquals(4, count($replies[3]));
- $this->assertEquals('barbar', $replies[3][2]);
- }
- function testMultiExecBlock_FluentInterface() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
- $this->assertInternalType('array', $replies);
- $this->assertEquals('bar', $replies[2]);
- }
- function testMultiExecBlock_CallableAnonymousBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->multiExec(function($multi) {
- $multi->ping();
- $multi->set('foo', 'bar');
- $multi->get('foo');
- });
- $this->assertInternalType('array', $replies);
- $this->assertEquals('bar', $replies[2]);
- }
- /**
- * @expectedException Predis\ClientException
- */
- function testMultiExecBlock_CannotMixFluentInterfaceAndAnonymousBlock() {
- $emptyBlock = function($tx) { };
- $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
- }
- function testMultiExecBlock_EmptyCallableBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->multiExec(function($multi) { });
- $this->assertEquals(0, count($replies));
- $options = array('cas' => true);
- $replies = $client->multiExec($options, function($multi) { });
- $this->assertEquals(0, count($replies));
- $options = array('cas' => true);
- $replies = $client->multiExec($options, function($multi) {
- $multi->multi();
- });
- $this->assertEquals(0, count($replies));
- }
- function testMultiExecBlock_ClientExceptionInCallableBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- RC::testForClientException($this, 'TEST', function() use($client) {
- $client->multiExec(function($multi) {
- $multi->ping();
- $multi->set('foo', 'bar');
- throw new \Predis\ClientException("TEST");
- });
- });
- $this->assertFalse($client->exists('foo'));
- }
- function testMultiExecBlock_ServerExceptionInCallableBlock() {
- $client = RC::getConnection();
- $client->flushdb();
- $client->getResponseReader()->setOption('throw_on_error', false);
- $replies = $client->multiExec(function($multi) {
- $multi->set('foo', 'bar');
- $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
- $multi->set('hoge', 'piyo');
- });
- $this->assertInternalType('array', $replies);
- $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
- $this->assertTrue($client->exists('foo'));
- $this->assertTrue($client->exists('hoge'));
- }
- function testMultiExecBlock_Discard() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->multiExec(function($multi) {
- $multi->set('foo', 'bar');
- $multi->discard();
- $multi->set('hoge', 'piyo');
- });
- $this->assertEquals(1, count($replies));
- $this->assertFalse($client->exists('foo'));
- $this->assertTrue($client->exists('hoge'));
- }
- function testMultiExecBlock_DiscardEmpty() {
- $client = RC::getConnection();
- $client->flushdb();
- $replies = $client->multiExec(function($multi) {
- $multi->discard();
- });
- $this->assertEquals(0, count($replies));
- }
- 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 testMultiExecBlock_CheckAndSet() {
- $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->mget('foo', 'foobar');
- });
- $this->assertInternalType('array', $replies);
- $this->assertEquals(array(true, array('bar', 'bar')), $replies);
- $tx = $client->multiExec($options);
- $tx->watch('foobar');
- $foo = $tx->get('foo');
- $replies = $tx->multi()
- ->set('foobar', $foo)
- ->mget('foo', 'foobar')
- ->execute();
- $this->assertInternalType('array', $replies);
- $this->assertEquals(array(true, array('bar', 'bar')), $replies);
- }
- function testMultiExecBlock_RetryOnServerAbort() {
- $client1 = RC::getConnection();
- $client2 = RC::getConnection(true);
- $client1->flushdb();
- $retry = 3;
- $attempts = 0;
- RC::testForAbortedMultiExecException($this, function()
- use($client1, $client2, $retry, &$attempts) {
- $options = array('watch' => 'sentinel', 'retry' => $retry);
- $client1->multiExec($options, function($tx)
- use ($client2, &$attempts) {
- $attempts++;
- $tx->set('sentinel', 'client1');
- $tx->get('sentinel');
- $client2->set('sentinel', 'client2');
- });
- });
- $this->assertEquals('client2', $client1->get('sentinel'));
- $this->assertEquals($retry + 1, $attempts);
- $retry = 3;
- $attempts = 0;
- RC::testForAbortedMultiExecException($this, function()
- use($client1, $client2, $retry, &$attempts) {
- $options = array(
- 'watch' => 'sentinel',
- 'cas' => true,
- 'retry' => $retry
- );
- $client1->multiExec($options, function($tx)
- use ($client2, &$attempts) {
- $attempts++;
- $tx->incr('attempts');
- $tx->multi();
- $tx->set('sentinel', 'client1');
- $tx->get('sentinel');
- $client2->set('sentinel', 'client2');
- });
- });
- $this->assertEquals('client2', $client1->get('sentinel'));
- $this->assertEquals($retry + 1, $attempts);
- $this->assertEquals($attempts, $client1->get('attempts'));
- }
- /**
- * @expectedException InvalidArgumentException
- */
- function testMultiExecBlock_RetryNotAvailableWithoutBlock() {
- $options = array('watch' => 'foo', 'retry' => 1);
- $tx = RC::getConnection()->multiExec($options);
- $tx->multi()->get('foo')->exec();
- }
- function testMultiExecBlock_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);
- }
- }
- ?>
|