ClientFeaturesTest.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. <?php
  2. class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
  3. public $redis;
  4. protected function setUp() {
  5. $this->redis = RC::getConnection();
  6. $this->redis->flushDatabase();
  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. $responseReader = $connection->getResponseReader();
  265. $responseReader->setHandler(
  266. \Predis\Protocol::PREFIX_MULTI_BULK,
  267. new \Predis\ResponseMultiBulkHandler()
  268. );
  269. $this->assertInternalType('array', $connection->rawCommand("KEYS *\r\n"));
  270. $responseReader->setHandler(
  271. \Predis\Protocol::PREFIX_MULTI_BULK,
  272. new \Predis\ResponseMultiBulkStreamHandler()
  273. );
  274. $this->assertInstanceOf('\Iterator', $connection->rawCommand("KEYS *\r\n"));
  275. }
  276. function testResponseReader_OptionExceptionOnError() {
  277. $connection = new \Predis\Connection(RC::getConnectionParameters());
  278. $responseReader = $connection->getResponseReader();
  279. $connection->rawCommand("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
  280. $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
  281. $responseReader->setHandler(
  282. \Predis\Protocol::PREFIX_ERROR,
  283. new \Predis\ResponseErrorSilentHandler()
  284. );
  285. $errorReply = $connection->rawCommand($rawCmdUnexpected);
  286. $this->assertInstanceOf('\Predis\ResponseError', $errorReply);
  287. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
  288. $responseReader->setHandler(
  289. \Predis\Protocol::PREFIX_ERROR,
  290. new \Predis\ResponseErrorHandler()
  291. );
  292. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
  293. use ($connection, $rawCmdUnexpected) {
  294. $connection->rawCommand($rawCmdUnexpected);
  295. });
  296. }
  297. function testResponseReader_EmptyBulkResponse() {
  298. $this->assertTrue($this->redis->set('foo', ''));
  299. $this->assertEquals('', $this->redis->get('foo'));
  300. $this->assertEquals('', $this->redis->get('foo'));
  301. }
  302. /* Client + CommandPipeline */
  303. function testCommandPipeline_Simple() {
  304. $client = RC::getConnection();
  305. $client->flushdb();
  306. $pipe = $client->pipeline();
  307. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe);
  308. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->set('foo', 'bar'));
  309. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo'));
  310. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->mset(array(
  311. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  312. )));
  313. $this->assertInstanceOf('\Predis\CommandPipeline', $pipe->mget(array(
  314. 'foo', 'hoge', 'foofoo', 'hogehoge'
  315. )));
  316. $replies = $pipe->execute();
  317. $this->assertInternalType('array', $replies);
  318. $this->assertEquals(4, count($replies));
  319. $this->assertEquals(4, count($replies[3]));
  320. $this->assertEquals('barbar', $replies[3][2]);
  321. }
  322. function testCommandPipeline_FluentInterface() {
  323. $client = RC::getConnection();
  324. $client->flushdb();
  325. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  326. $this->assertInternalType('array', $replies);
  327. $this->assertEquals('bar', $replies[2]);
  328. }
  329. function testCommandPipeline_CallableAnonymousBlock() {
  330. $client = RC::getConnection();
  331. $client->flushdb();
  332. $replies = $client->pipeline(function($pipe) {
  333. $pipe->ping();
  334. $pipe->set('foo', 'bar');
  335. $pipe->get('foo');
  336. });
  337. $this->assertInternalType('array', $replies);
  338. $this->assertEquals('bar', $replies[2]);
  339. }
  340. function testCommandPipeline_ClientExceptionInCallableBlock() {
  341. $client = RC::getConnection();
  342. $client->flushdb();
  343. RC::testForClientException($this, 'TEST', function() use($client) {
  344. $client->pipeline(function($pipe) {
  345. $pipe->ping();
  346. $pipe->set('foo', 'bar');
  347. throw new \Predis\ClientException("TEST");
  348. });
  349. });
  350. $this->assertFalse($client->exists('foo'));
  351. }
  352. function testCommandPipeline_ServerExceptionInCallableBlock() {
  353. $client = RC::getConnection();
  354. $client->flushdb();
  355. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  356. $replies = $client->pipeline(function($pipe) {
  357. $pipe->set('foo', 'bar');
  358. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  359. $pipe->set('hoge', 'piyo');
  360. });
  361. $this->assertInternalType('array', $replies);
  362. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  363. $this->assertTrue($client->exists('foo'));
  364. $this->assertTrue($client->exists('hoge'));
  365. }
  366. function testCommandPipeline_Flush() {
  367. $client = RC::getConnection();
  368. $client->flushdb();
  369. $pipe = $client->pipeline();
  370. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  371. $pipe->flushPipeline();
  372. $pipe->ping()->mget(array('foo', 'hoge'));
  373. $replies = $pipe->execute();
  374. $this->assertInternalType('array', $replies);
  375. $this->assertEquals(4, count($replies));
  376. $this->assertEquals('bar', $replies[3][0]);
  377. $this->assertEquals('piyo', $replies[3][1]);
  378. }
  379. /* Client + MultiExecBlock */
  380. function testMultiExecBlock_Simple() {
  381. $client = RC::getConnection();
  382. $client->flushdb();
  383. $multi = $client->multiExec();
  384. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi);
  385. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->set('foo', 'bar'));
  386. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->set('hoge', 'piyo'));
  387. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->mset(array(
  388. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  389. )));
  390. $this->assertInstanceOf('\Predis\MultiExecBlock', $multi->mget(array(
  391. 'foo', 'hoge', 'foofoo', 'hogehoge'
  392. )));
  393. $replies = $multi->execute();
  394. $this->assertInternalType('array', $replies);
  395. $this->assertEquals(4, count($replies));
  396. $this->assertEquals(4, count($replies[3]));
  397. $this->assertEquals('barbar', $replies[3][2]);
  398. }
  399. function testMultiExecBlock_FluentInterface() {
  400. $client = RC::getConnection();
  401. $client->flushdb();
  402. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  403. $this->assertInternalType('array', $replies);
  404. $this->assertEquals('bar', $replies[2]);
  405. }
  406. function testMultiExecBlock_CallableAnonymousBlock() {
  407. $client = RC::getConnection();
  408. $client->flushdb();
  409. $replies = $client->multiExec(function($multi) {
  410. $multi->ping();
  411. $multi->set('foo', 'bar');
  412. $multi->get('foo');
  413. });
  414. $this->assertInternalType('array', $replies);
  415. $this->assertEquals('bar', $replies[2]);
  416. }
  417. /**
  418. * @expectedException Predis\ClientException
  419. */
  420. function testMultiExecBlock_CannotMixFluentInterfaceAndAnonymousBlock() {
  421. $emptyBlock = function($tx) { };
  422. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  423. }
  424. function testMultiExecBlock_EmptyCallableBlock() {
  425. $client = RC::getConnection();
  426. $client->flushdb();
  427. $replies = $client->multiExec(function($multi) { });
  428. $this->assertEquals(0, count($replies));
  429. $options = array('cas' => true);
  430. $replies = $client->multiExec($options, function($multi) { });
  431. $this->assertEquals(0, count($replies));
  432. $options = array('cas' => true);
  433. $replies = $client->multiExec($options, function($multi) {
  434. $multi->multi();
  435. });
  436. $this->assertEquals(0, count($replies));
  437. }
  438. function testMultiExecBlock_ClientExceptionInCallableBlock() {
  439. $client = RC::getConnection();
  440. $client->flushdb();
  441. RC::testForClientException($this, 'TEST', function() use($client) {
  442. $client->multiExec(function($multi) {
  443. $multi->ping();
  444. $multi->set('foo', 'bar');
  445. throw new \Predis\ClientException("TEST");
  446. });
  447. });
  448. $this->assertFalse($client->exists('foo'));
  449. }
  450. function testMultiExecBlock_ServerExceptionInCallableBlock() {
  451. $client = RC::getConnection();
  452. $client->flushdb();
  453. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  454. $replies = $client->multiExec(function($multi) {
  455. $multi->set('foo', 'bar');
  456. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  457. $multi->set('hoge', 'piyo');
  458. });
  459. $this->assertInternalType('array', $replies);
  460. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  461. $this->assertTrue($client->exists('foo'));
  462. $this->assertTrue($client->exists('hoge'));
  463. }
  464. function testMultiExecBlock_Discard() {
  465. $client = RC::getConnection();
  466. $client->flushdb();
  467. $replies = $client->multiExec(function($multi) {
  468. $multi->set('foo', 'bar');
  469. $multi->discard();
  470. $multi->set('hoge', 'piyo');
  471. });
  472. $this->assertEquals(1, count($replies));
  473. $this->assertFalse($client->exists('foo'));
  474. $this->assertTrue($client->exists('hoge'));
  475. }
  476. function testMultiExecBlock_DiscardEmpty() {
  477. $client = RC::getConnection();
  478. $client->flushdb();
  479. $replies = $client->multiExec(function($multi) {
  480. $multi->discard();
  481. });
  482. $this->assertEquals(0, count($replies));
  483. }
  484. function testMultiExecBlock_Watch() {
  485. $client1 = RC::getConnection();
  486. $client2 = RC::getConnection(true);
  487. $client1->flushdb();
  488. RC::testForAbortedMultiExecException($this, function()
  489. use($client1, $client2) {
  490. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  491. use ($client2) {
  492. $multi->set('sentinel', 'client1');
  493. $multi->get('sentinel');
  494. $client2->set('sentinel', 'client2');
  495. });
  496. });
  497. $this->assertEquals('client2', $client1->get('sentinel'));
  498. }
  499. function testMultiExecBlock_CheckAndSet() {
  500. $client = RC::getConnection();
  501. $client->flushdb();
  502. $client->set('foo', 'bar');
  503. $options = array('watch' => 'foo', 'cas' => true);
  504. $replies = $client->multiExec($options, function($tx) {
  505. $tx->watch('foobar');
  506. $foo = $tx->get('foo');
  507. $tx->multi();
  508. $tx->set('foobar', $foo);
  509. $tx->mget('foo', 'foobar');
  510. });
  511. $this->assertInternalType('array', $replies);
  512. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  513. $tx = $client->multiExec($options);
  514. $tx->watch('foobar');
  515. $foo = $tx->get('foo');
  516. $replies = $tx->multi()
  517. ->set('foobar', $foo)
  518. ->mget('foo', 'foobar')
  519. ->execute();
  520. $this->assertInternalType('array', $replies);
  521. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  522. }
  523. function testMultiExecBlock_RetryOnServerAbort() {
  524. $client1 = RC::getConnection();
  525. $client2 = RC::getConnection(true);
  526. $client1->flushdb();
  527. $retry = 3;
  528. $attempts = 0;
  529. RC::testForAbortedMultiExecException($this, function()
  530. use($client1, $client2, $retry, &$attempts) {
  531. $options = array('watch' => 'sentinel', 'retry' => $retry);
  532. $client1->multiExec($options, function($tx)
  533. use ($client2, &$attempts) {
  534. $attempts++;
  535. $tx->set('sentinel', 'client1');
  536. $tx->get('sentinel');
  537. $client2->set('sentinel', 'client2');
  538. });
  539. });
  540. $this->assertEquals('client2', $client1->get('sentinel'));
  541. $this->assertEquals($retry + 1, $attempts);
  542. $retry = 3;
  543. $attempts = 0;
  544. RC::testForAbortedMultiExecException($this, function()
  545. use($client1, $client2, $retry, &$attempts) {
  546. $options = array(
  547. 'watch' => 'sentinel',
  548. 'cas' => true,
  549. 'retry' => $retry
  550. );
  551. $client1->multiExec($options, function($tx)
  552. use ($client2, &$attempts) {
  553. $attempts++;
  554. $tx->incr('attempts');
  555. $tx->multi();
  556. $tx->set('sentinel', 'client1');
  557. $tx->get('sentinel');
  558. $client2->set('sentinel', 'client2');
  559. });
  560. });
  561. $this->assertEquals('client2', $client1->get('sentinel'));
  562. $this->assertEquals($retry + 1, $attempts);
  563. $this->assertEquals($attempts, $client1->get('attempts'));
  564. }
  565. /**
  566. * @expectedException InvalidArgumentException
  567. */
  568. function testMultiExecBlock_RetryNotAvailableWithoutBlock() {
  569. $options = array('watch' => 'foo', 'retry' => 1);
  570. $tx = RC::getConnection()->multiExec($options);
  571. $tx->multi()->get('foo')->exec();
  572. }
  573. function testMultiExecBlock_CheckAndSet_Discard() {
  574. $client = RC::getConnection();
  575. $client->flushdb();
  576. $client->set('foo', 'bar');
  577. $options = array('watch' => 'foo', 'cas' => true);
  578. $replies = $client->multiExec($options, function($tx) {
  579. $tx->watch('foobar');
  580. $foo = $tx->get('foo');
  581. $tx->multi();
  582. $tx->set('foobar', $foo);
  583. $tx->discard();
  584. $tx->mget('foo', 'foobar');
  585. });
  586. $this->assertInternalType('array', $replies);
  587. $this->assertEquals(array(array('bar', null)), $replies);
  588. $hijack = true;
  589. $client->set('foo', 'bar');
  590. $client2 = RC::getConnection(true);
  591. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  592. $replies = $client->multiExec($options, function($tx)
  593. use ($client2, &$hijack) {
  594. $foo = $tx->get('foo');
  595. $tx->multi();
  596. $tx->set('foobar', $foo);
  597. $tx->discard();
  598. if ($hijack) {
  599. $hijack = false;
  600. $client2->set('foo', 'hijacked!');
  601. }
  602. $tx->mget('foo', 'foobar');
  603. });
  604. $this->assertInternalType('array', $replies);
  605. $this->assertEquals(array(array('hijacked!', null)), $replies);
  606. }
  607. }
  608. ?>