PredisClientFeatures.php 28 KB

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