ClientFeaturesTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. class ClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
  3. public $redis;
  4. protected function setUp() {
  5. $this->redis = RC::getConnection();
  6. $this->redis->flushdb();
  7. }
  8. protected function tearDown() {
  9. }
  10. protected function onNotSuccessfulTest(Exception $exception) {
  11. // drops and reconnect to a redis server on uncaught exceptions
  12. RC::resetConnection();
  13. parent::onNotSuccessfulTest($exception);
  14. }
  15. /* ConnectionParameters */
  16. function testConnectionParametersDefaultValues() {
  17. $params = new \Predis\ConnectionParameters();
  18. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_HOST, $params->host);
  19. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_PORT, $params->port);
  20. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout);
  21. $this->assertNull($params->read_write_timeout);
  22. $this->assertNull($params->database);
  23. $this->assertNull($params->password);
  24. $this->assertNull($params->alias);
  25. }
  26. function testConnectionParametersSetupValuesArray() {
  27. $paramsArray = RC::getConnectionParametersArgumentsArray();
  28. $params = new \Predis\ConnectionParameters($paramsArray);
  29. $this->assertEquals($paramsArray['host'], $params->host);
  30. $this->assertEquals($paramsArray['port'], $params->port);
  31. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  32. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  33. $this->assertEquals($paramsArray['database'], $params->database);
  34. $this->assertEquals($paramsArray['password'], $params->password);
  35. $this->assertEquals($paramsArray['alias'], $params->alias);
  36. }
  37. function testConnectionParametersSetupValuesString() {
  38. $paramsArray = RC::getConnectionParametersArgumentsArray();
  39. $paramsString = RC::getConnectionParametersArgumentsString($paramsArray);
  40. $params = new \Predis\ConnectionParameters($paramsArray);
  41. $this->assertEquals($paramsArray['host'], $params->host);
  42. $this->assertEquals($paramsArray['port'], $params->port);
  43. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  44. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  45. $this->assertEquals($paramsArray['database'], $params->database);
  46. $this->assertEquals($paramsArray['password'], $params->password);
  47. $this->assertEquals($paramsArray['alias'], $params->alias);
  48. }
  49. /* Command and derivates */
  50. function testCommand_TestArguments() {
  51. $cmdArgs = array('key1', 'key2', 'key3');
  52. $cmd = new \Predis\Commands\GetMultiple();
  53. $cmd->setArgumentsArray($cmdArgs);
  54. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  55. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  56. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  57. $cmd = new \Predis\Commands\GetMultiple();
  58. $cmd->setArguments('key1', 'key2', 'key3');
  59. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  60. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  61. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  62. $cmd = new \Predis\Commands\Ping();
  63. $this->assertNull($cmd->getArgument(0));
  64. }
  65. function testCommand_InlineWithNoArguments() {
  66. $cmd = new \Predis\Compatibility\v1_0\Commands\Ping();
  67. $this->assertInstanceOf('\Predis\InlineCommand', $cmd);
  68. $this->assertEquals('PING', $cmd->getCommandId());
  69. $this->assertFalse($cmd->closesConnection());
  70. $this->assertFalse($cmd->canBeHashed());
  71. $this->assertNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  72. $this->assertEquals("PING\r\n", $cmd());
  73. }
  74. function testCommand_InlineWithArguments() {
  75. $cmd = new \Predis\Compatibility\v1_0\Commands\Get();
  76. $cmd->setArgumentsArray(array('key'));
  77. $this->assertInstanceOf('\Predis\InlineCommand', $cmd);
  78. $this->assertEquals('GET', $cmd->getCommandId());
  79. $this->assertFalse($cmd->closesConnection());
  80. $this->assertTrue($cmd->canBeHashed());
  81. $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  82. $this->assertEquals("GET key\r\n", $cmd());
  83. }
  84. function testCommand_BulkWithArguments() {
  85. $cmd = new \Predis\Compatibility\v1_0\Commands\Set();
  86. $cmd->setArgumentsArray(array('key', 'value'));
  87. $this->assertInstanceOf('\Predis\BulkCommand', $cmd);
  88. $this->assertEquals('SET', $cmd->getCommandId());
  89. $this->assertFalse($cmd->closesConnection());
  90. $this->assertTrue($cmd->canBeHashed());
  91. $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  92. $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd());
  93. }
  94. function testCommand_MultiBulkWithArguments() {
  95. $cmd = new \Predis\Commands\SetMultiple();
  96. $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2'));
  97. $this->assertInstanceOf('\Predis\MultiBulkCommand', $cmd);
  98. $this->assertEquals('MSET', $cmd->getCommandId());
  99. $this->assertFalse($cmd->closesConnection());
  100. $this->assertFalse($cmd->canBeHashed());
  101. $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  102. $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());
  103. }
  104. function testCommand_ParseResponse() {
  105. // default parser
  106. $cmd = new \Predis\Commands\Get();
  107. $this->assertEquals('test', $cmd->parseResponse('test'));
  108. // overridden parser (boolean)
  109. $cmd = new \Predis\Commands\Exists();
  110. $this->assertTrue($cmd->parseResponse('1'));
  111. $this->assertFalse($cmd->parseResponse('0'));
  112. // overridden parser (boolean)
  113. $cmd = new \Predis\Commands\Ping();
  114. $this->assertTrue($cmd->parseResponse('PONG'));
  115. // overridden parser (complex)
  116. // TODO: emulate a respons to INFO
  117. }
  118. /* RedisServerProfile and derivates */
  119. function testRedisServerProfile_GetSpecificVersions() {
  120. $this->assertInstanceOf('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0'));
  121. $this->assertInstanceOf('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2'));
  122. $this->assertInstanceOf('\Predis\RedisServer_v2_0', \Predis\RedisServerProfile::get('2.0'));
  123. $this->assertInstanceOf('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev'));
  124. $this->assertInstanceOf('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default'));
  125. $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault());
  126. }
  127. function testRedisServerProfile_SupportedCommands() {
  128. $profile_10 = \Predis\RedisServerProfile::get('1.0');
  129. $profile_12 = \Predis\RedisServerProfile::get('1.2');
  130. $this->assertTrue($profile_10->supportsCommand('info'));
  131. $this->assertTrue($profile_12->supportsCommand('info'));
  132. $this->assertFalse($profile_10->supportsCommand('mset'));
  133. $this->assertTrue($profile_12->supportsCommand('mset'));
  134. $this->assertFalse($profile_10->supportsCommand('multi'));
  135. $this->assertFalse($profile_12->supportsCommand('multi'));
  136. }
  137. function testRedisServerProfile_CommandsCreation() {
  138. $profile = \Predis\RedisServerProfile::get('1.0');
  139. $cmdNoArgs = $profile->createCommand('info');
  140. $this->assertInstanceOf('\Predis\Compatibility\v1_0\Commands\Info', $cmdNoArgs);
  141. $this->assertNull($cmdNoArgs->getArgument());
  142. $args = array('key1', 'key2');
  143. $cmdWithArgs = $profile->createCommand('mget', $args);
  144. $this->assertInstanceOf('\Predis\Compatibility\v1_0\Commands\GetMultiple', $cmdWithArgs);
  145. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  146. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  147. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  148. $bogusCommand = 'not_existing_command';
  149. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  150. RC::testForClientException($this, $expectedMessage, function()
  151. use($profile, $bogusCommand) {
  152. $profile->createCommand($bogusCommand);
  153. });
  154. }
  155. function testRedisServerProfile_CommandsRegistration() {
  156. $profile = \Predis\RedisServerProfile::get('1.0');
  157. $cmdId = 'mset';
  158. $cmdClass = '\Predis\Commands\SetMultiple';
  159. $this->assertFalse($profile->supportsCommand($cmdId));
  160. $profile->registerCommand(new $cmdClass(), $cmdId);
  161. $this->assertTrue($profile->supportsCommand($cmdId));
  162. $this->assertInstanceOf($cmdClass, $profile->createCommand($cmdId));
  163. }
  164. /* ResponseQueued */
  165. function testResponseQueued() {
  166. $response = new \Predis\ResponseQueued();
  167. $this->assertTrue($response->skipParse);
  168. $this->assertTrue($response->queued);
  169. $this->assertEquals(\Predis\Protocol::QUEUED, (string)$response);
  170. }
  171. /* ResponseError */
  172. function testResponseError() {
  173. $errorMessage = 'ERROR MESSAGE';
  174. $response = new \Predis\ResponseError($errorMessage);
  175. $this->assertTrue($response->skipParse);
  176. $this->assertTrue($response->error);
  177. $this->assertEquals($errorMessage, $response->message);
  178. $this->assertEquals($errorMessage, (string)$response);
  179. }
  180. /* Connection */
  181. function testConnection_StringCastReturnsIPAndPort() {
  182. $connection = new \Predis\Connection(RC::getConnectionParameters());
  183. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  184. }
  185. function testConnection_ConnectDisconnect() {
  186. $connection = new \Predis\Connection(RC::getConnectionParameters());
  187. $this->assertFalse($connection->isConnected());
  188. $connection->connect();
  189. $this->assertTrue($connection->isConnected());
  190. $connection->disconnect();
  191. $this->assertFalse($connection->isConnected());
  192. }
  193. function testConnection_WriteAndReadCommand() {
  194. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  195. $connection = new \Predis\Connection(RC::getConnectionParameters());
  196. $connection->connect();
  197. $connection->writeCommand($cmd);
  198. $this->assertTrue($connection->readResponse($cmd));
  199. }
  200. function testConnection_WriteCommandAndCloseConnection() {
  201. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit');
  202. $connection = new \Predis\Connection(new \Predis\ConnectionParameters(
  203. RC::getConnectionArguments() + array('read_write_timeout' => 0.5)
  204. ));
  205. $connection->connect();
  206. $this->assertTrue($connection->isConnected());
  207. $connection->writeCommand($cmd);
  208. $connection->disconnect();
  209. $exceptionMessage = 'Error while reading line from the server';
  210. RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
  211. $connection->readResponse($cmd);
  212. });
  213. }
  214. function testConnection_GetSocketOpensConnection() {
  215. $connection = new \Predis\Connection(RC::getConnectionParameters());
  216. $this->assertFalse($connection->isConnected());
  217. $this->assertInternalType('resource', $connection->getSocket());
  218. $this->assertTrue($connection->isConnected());
  219. }
  220. function testConnection_LazyConnect() {
  221. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  222. $connection = new \Predis\Connection(RC::getConnectionParameters());
  223. $this->assertFalse($connection->isConnected());
  224. $connection->writeCommand($cmd);
  225. $this->assertTrue($connection->isConnected());
  226. $this->assertTrue($connection->readResponse($cmd));
  227. }
  228. function testConnection_RawCommand() {
  229. $connection = new \Predis\Connection(RC::getConnectionParameters());
  230. $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
  231. }
  232. function testConnection_Alias() {
  233. $connection1 = new \Predis\Connection(RC::getConnectionParameters());
  234. $this->assertNull($connection1->getParameters()->alias);
  235. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  236. $connection2 = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  237. $this->assertEquals('servername', $connection2->getParameters()->alias);
  238. }
  239. function testConnection_ConnectionTimeout() {
  240. $timeout = 3;
  241. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  242. $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  243. $start = time();
  244. RC::testForCommunicationException($this, null, function() use($connection) {
  245. $connection->connect();
  246. });
  247. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  248. }
  249. function testConnection_ReadTimeout() {
  250. $timeout = 1;
  251. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  252. $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  253. $connection = new \Predis\Connection(new \Predis\ConnectionParameters($args));
  254. $expectedMessage = 'Error while reading line from the server';
  255. $start = time();
  256. RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) {
  257. $connection->readResponse($cmdFake);
  258. });
  259. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  260. }
  261. /* ResponseReader */
  262. function testResponseReader_OptionIterableMultiBulkReplies() {
  263. $connection = new \Predis\Connection(RC::getConnectionParameters());
  264. $connection->getResponseReader()->setOption('iterable_multibulk', false);
  265. $this->assertInternalType('array', $connection->rawCommand("KEYS *\r\n"));
  266. $connection->getResponseReader()->setOption('iterable_multibulk', true);
  267. $this->assertInstanceOf('\Iterator', $connection->rawCommand("KEYS *\r\n"));
  268. }
  269. function testResponseReader_OptionExceptionOnError() {
  270. $connection = new \Predis\Connection(RC::getConnectionParameters());
  271. $responseReader = $connection->getResponseReader();
  272. $connection->rawCommand("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
  273. $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
  274. $responseReader->setOption('throw_on_error', false);
  275. $errorReply = $connection->rawCommand($rawCmdUnexpected);
  276. $this->assertInstanceOf('\Predis\ResponseError', $errorReply);
  277. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
  278. $responseReader->setOption('throw_on_error', true);
  279. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
  280. use ($connection, $rawCmdUnexpected) {
  281. $connection->rawCommand($rawCmdUnexpected);
  282. });
  283. }
  284. function testResponseReader_EmptyBulkResponse() {
  285. $this->assertTrue($this->redis->set('foo', ''));
  286. $this->assertEquals('', $this->redis->get('foo'));
  287. $this->assertEquals('', $this->redis->get('foo'));
  288. }
  289. /* Client + CommandPipeline */
  290. function testCommandPipeline_Simple() {
  291. $client = RC::getConnection();
  292. $client->flushdb();
  293. $pipe = $client->pipeline();
  294. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe);
  295. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->set('foo', 'bar'));
  296. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo'));
  297. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->mset(array(
  298. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  299. )));
  300. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->mget(array(
  301. 'foo', 'hoge', 'foofoo', 'hogehoge'
  302. )));
  303. $replies = $pipe->execute();
  304. $this->assertInternalType('array', $replies);
  305. $this->assertEquals(4, count($replies));
  306. $this->assertEquals(4, count($replies[3]));
  307. $this->assertEquals('barbar', $replies[3][2]);
  308. }
  309. function testCommandPipeline_FluentInterface() {
  310. $client = RC::getConnection();
  311. $client->flushdb();
  312. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  313. $this->assertInternalType('array', $replies);
  314. $this->assertEquals('bar', $replies[2]);
  315. }
  316. function testCommandPipeline_CallableAnonymousBlock() {
  317. $client = RC::getConnection();
  318. $client->flushdb();
  319. $replies = $client->pipeline(function($pipe) {
  320. $pipe->ping();
  321. $pipe->set('foo', 'bar');
  322. $pipe->get('foo');
  323. });
  324. $this->assertInternalType('array', $replies);
  325. $this->assertEquals('bar', $replies[2]);
  326. }
  327. function testCommandPipeline_ClientExceptionInCallableBlock() {
  328. $client = RC::getConnection();
  329. $client->flushdb();
  330. RC::testForClientException($this, 'TEST', function() use($client) {
  331. $client->pipeline(function($pipe) {
  332. $pipe->ping();
  333. $pipe->set('foo', 'bar');
  334. throw new \Predis\ClientException("TEST");
  335. });
  336. });
  337. $this->assertFalse($client->exists('foo'));
  338. }
  339. function testCommandPipeline_ServerExceptionInCallableBlock() {
  340. $client = RC::getConnection();
  341. $client->flushdb();
  342. $client->getResponseReader()->setOption('throw_on_error', false);
  343. $replies = $client->pipeline(function($pipe) {
  344. $pipe->set('foo', 'bar');
  345. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  346. $pipe->set('hoge', 'piyo');
  347. });
  348. $this->assertInternalType('array', $replies);
  349. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  350. $this->assertTrue($client->exists('foo'));
  351. $this->assertTrue($client->exists('hoge'));
  352. }
  353. function testCommandPipeline_Flush() {
  354. $client = RC::getConnection();
  355. $client->flushdb();
  356. $pipe = $client->pipeline();
  357. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  358. $pipe->flushPipeline();
  359. $pipe->ping()->mget(array('foo', 'hoge'));
  360. $replies = $pipe->execute();
  361. $this->assertInternalType('array', $replies);
  362. $this->assertEquals(4, count($replies));
  363. $this->assertEquals('bar', $replies[3][0]);
  364. $this->assertEquals('piyo', $replies[3][1]);
  365. }
  366. /* Client + MultiExecBlock */
  367. function testMultiExecBlock_Simple() {
  368. $client = RC::getConnection();
  369. $client->flushdb();
  370. $multi = $client->multiExec();
  371. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi);
  372. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->set('foo', 'bar'));
  373. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->set('hoge', 'piyo'));
  374. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->mset(array(
  375. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  376. )));
  377. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->mget(array(
  378. 'foo', 'hoge', 'foofoo', 'hogehoge'
  379. )));
  380. $replies = $multi->execute();
  381. $this->assertInternalType('array', $replies);
  382. $this->assertEquals(4, count($replies));
  383. $this->assertEquals(4, count($replies[3]));
  384. $this->assertEquals('barbar', $replies[3][2]);
  385. }
  386. function testMultiExecBlock_FluentInterface() {
  387. $client = RC::getConnection();
  388. $client->flushdb();
  389. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  390. $this->assertInternalType('array', $replies);
  391. $this->assertEquals('bar', $replies[2]);
  392. }
  393. function testMultiExecBlock_CallableAnonymousBlock() {
  394. $client = RC::getConnection();
  395. $client->flushdb();
  396. $replies = $client->multiExec(function($multi) {
  397. $multi->ping();
  398. $multi->set('foo', 'bar');
  399. $multi->get('foo');
  400. });
  401. $this->assertInternalType('array', $replies);
  402. $this->assertEquals('bar', $replies[2]);
  403. }
  404. /**
  405. * @expectedException Predis\ClientException
  406. */
  407. function testMultiExecBlock_CannotMixFluentInterfaceAndAnonymousBlock() {
  408. $emptyBlock = function($tx) { };
  409. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  410. }
  411. function testMultiExecBlock_EmptyCallableBlock() {
  412. $client = RC::getConnection();
  413. $client->flushdb();
  414. $replies = $client->multiExec(function($multi) { });
  415. $this->assertEquals(0, count($replies));
  416. $options = array('cas' => true);
  417. $replies = $client->multiExec($options, function($multi) { });
  418. $this->assertEquals(0, count($replies));
  419. $options = array('cas' => true);
  420. $replies = $client->multiExec($options, function($multi) {
  421. $multi->multi();
  422. });
  423. $this->assertEquals(0, count($replies));
  424. }
  425. function testMultiExecBlock_ClientExceptionInCallableBlock() {
  426. $client = RC::getConnection();
  427. $client->flushdb();
  428. RC::testForClientException($this, 'TEST', function() use($client) {
  429. $client->multiExec(function($multi) {
  430. $multi->ping();
  431. $multi->set('foo', 'bar');
  432. throw new \Predis\ClientException("TEST");
  433. });
  434. });
  435. $this->assertFalse($client->exists('foo'));
  436. }
  437. function testMultiExecBlock_ServerExceptionInCallableBlock() {
  438. $client = RC::getConnection();
  439. $client->flushdb();
  440. $client->getResponseReader()->setOption('throw_on_error', false);
  441. $replies = $client->multiExec(function($multi) {
  442. $multi->set('foo', 'bar');
  443. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  444. $multi->set('hoge', 'piyo');
  445. });
  446. $this->assertInternalType('array', $replies);
  447. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  448. $this->assertTrue($client->exists('foo'));
  449. $this->assertTrue($client->exists('hoge'));
  450. }
  451. function testMultiExecBlock_Discard() {
  452. $client = RC::getConnection();
  453. $client->flushdb();
  454. $replies = $client->multiExec(function($multi) {
  455. $multi->set('foo', 'bar');
  456. $multi->discard();
  457. $multi->set('hoge', 'piyo');
  458. });
  459. $this->assertEquals(1, count($replies));
  460. $this->assertFalse($client->exists('foo'));
  461. $this->assertTrue($client->exists('hoge'));
  462. }
  463. function testMultiExecBlock_DiscardEmpty() {
  464. $client = RC::getConnection();
  465. $client->flushdb();
  466. $replies = $client->multiExec(function($multi) {
  467. $multi->discard();
  468. });
  469. $this->assertEquals(0, count($replies));
  470. }
  471. function testMultiExecBlock_Watch() {
  472. $client1 = RC::getConnection();
  473. $client2 = RC::getConnection(true);
  474. $client1->flushdb();
  475. RC::testForAbortedMultiExecException($this, function()
  476. use($client1, $client2) {
  477. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  478. use ($client2) {
  479. $multi->set('sentinel', 'client1');
  480. $multi->get('sentinel');
  481. $client2->set('sentinel', 'client2');
  482. });
  483. });
  484. $this->assertEquals('client2', $client1->get('sentinel'));
  485. }
  486. function testMultiExecBlock_CheckAndSet() {
  487. $client = RC::getConnection();
  488. $client->flushdb();
  489. $client->set('foo', 'bar');
  490. $options = array('watch' => 'foo', 'cas' => true);
  491. $replies = $client->multiExec($options, function($tx) {
  492. $tx->watch('foobar');
  493. $foo = $tx->get('foo');
  494. $tx->multi();
  495. $tx->set('foobar', $foo);
  496. $tx->mget('foo', 'foobar');
  497. });
  498. $this->assertInternalType('array', $replies);
  499. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  500. $tx = $client->multiExec($options);
  501. $tx->watch('foobar');
  502. $foo = $tx->get('foo');
  503. $replies = $tx->multi()
  504. ->set('foobar', $foo)
  505. ->mget('foo', 'foobar')
  506. ->execute();
  507. $this->assertInternalType('array', $replies);
  508. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  509. }
  510. function testMultiExecBlock_RetryOnServerAbort() {
  511. $client1 = RC::getConnection();
  512. $client2 = RC::getConnection(true);
  513. $client1->flushdb();
  514. $retry = 3;
  515. $attempts = 0;
  516. RC::testForAbortedMultiExecException($this, function()
  517. use($client1, $client2, $retry, &$attempts) {
  518. $options = array('watch' => 'sentinel', 'retry' => $retry);
  519. $client1->multiExec($options, function($tx)
  520. use ($client2, &$attempts) {
  521. $attempts++;
  522. $tx->set('sentinel', 'client1');
  523. $tx->get('sentinel');
  524. $client2->set('sentinel', 'client2');
  525. });
  526. });
  527. $this->assertEquals('client2', $client1->get('sentinel'));
  528. $this->assertEquals($retry + 1, $attempts);
  529. $retry = 3;
  530. $attempts = 0;
  531. RC::testForAbortedMultiExecException($this, function()
  532. use($client1, $client2, $retry, &$attempts) {
  533. $options = array(
  534. 'watch' => 'sentinel',
  535. 'cas' => true,
  536. 'retry' => $retry
  537. );
  538. $client1->multiExec($options, function($tx)
  539. use ($client2, &$attempts) {
  540. $attempts++;
  541. $tx->incr('attempts');
  542. $tx->multi();
  543. $tx->set('sentinel', 'client1');
  544. $tx->get('sentinel');
  545. $client2->set('sentinel', 'client2');
  546. });
  547. });
  548. $this->assertEquals('client2', $client1->get('sentinel'));
  549. $this->assertEquals($retry + 1, $attempts);
  550. $this->assertEquals($attempts, $client1->get('attempts'));
  551. }
  552. /**
  553. * @expectedException InvalidArgumentException
  554. */
  555. function testMultiExecBlock_RetryNotAvailableWithoutBlock() {
  556. $options = array('watch' => 'foo', 'retry' => 1);
  557. $tx = RC::getConnection()->multiExec($options);
  558. $tx->multi()->get('foo')->exec();
  559. }
  560. function testMultiExecBlock_CheckAndSet_Discard() {
  561. $client = RC::getConnection();
  562. $client->flushdb();
  563. $client->set('foo', 'bar');
  564. $options = array('watch' => 'foo', 'cas' => true);
  565. $replies = $client->multiExec($options, function($tx) {
  566. $tx->watch('foobar');
  567. $foo = $tx->get('foo');
  568. $tx->multi();
  569. $tx->set('foobar', $foo);
  570. $tx->discard();
  571. $tx->mget('foo', 'foobar');
  572. });
  573. $this->assertInternalType('array', $replies);
  574. $this->assertEquals(array(array('bar', null)), $replies);
  575. $hijack = true;
  576. $client->set('foo', 'bar');
  577. $client2 = RC::getConnection(true);
  578. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  579. $replies = $client->multiExec($options, function($tx)
  580. use ($client2, &$hijack) {
  581. $foo = $tx->get('foo');
  582. $tx->multi();
  583. $tx->set('foobar', $foo);
  584. $tx->discard();
  585. if ($hijack) {
  586. $hijack = false;
  587. $client2->set('foo', 'hijacked!');
  588. }
  589. $tx->mget('foo', 'foobar');
  590. });
  591. $this->assertInternalType('array', $replies);
  592. $this->assertEquals(array(array('hijacked!', null)), $replies);
  593. }
  594. }
  595. ?>