PredisClientFeatures.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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->invoke());
  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->invoke());
  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->invoke());
  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->invoke());
  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, p_anon("\$test", "
  155. \$profile = Predis_RedisServerProfile::getDefault();
  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. $expectedMessage = 'Error while reading line from the server';
  214. $thrownException = null;
  215. try {
  216. $connection->readResponse($cmd);
  217. }
  218. catch (Predis_CommunicationException $exception) {
  219. $thrownException = $exception;
  220. }
  221. $this->assertType('Predis_CommunicationException', $thrownException);
  222. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  223. }
  224. function testConnection_GetSocketOpensConnection() {
  225. $connection = new Predis_Connection(RC::getConnectionParameters());
  226. $this->assertFalse($connection->isConnected());
  227. $this->assertType('resource', $connection->getSocket());
  228. $this->assertTrue($connection->isConnected());
  229. }
  230. function testConnection_LazyConnect() {
  231. $cmd = Predis_RedisServerProfile::getDefault()->createCommand('ping');
  232. $connection = new Predis_Connection(RC::getConnectionParameters());
  233. $this->assertFalse($connection->isConnected());
  234. $connection->writeCommand($cmd);
  235. $this->assertTrue($connection->isConnected());
  236. $this->assertTrue($connection->readResponse($cmd));
  237. }
  238. function testConnection_RawCommand() {
  239. $connection = new Predis_Connection(RC::getConnectionParameters());
  240. $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
  241. }
  242. function testConnection_Alias() {
  243. $connection1 = new Predis_Connection(RC::getConnectionParameters());
  244. $this->assertNull($connection1->getParameters()->alias);
  245. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  246. $connection2 = new Predis_Connection(new Predis_ConnectionParameters($args));
  247. $this->assertEquals('servername', $connection2->getParameters()->alias);
  248. }
  249. function testConnection_ConnectionTimeout() {
  250. $timeout = 3;
  251. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  252. $connection = new Predis_Connection(new Predis_ConnectionParameters($args));
  253. $start = time();
  254. $thrownException = null;
  255. try {
  256. $connection->connect();
  257. }
  258. catch (Predis_CommunicationException $exception) {
  259. $thrownException = $exception;
  260. }
  261. $this->assertType('Predis_CommunicationException', $thrownException);
  262. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  263. }
  264. function testConnection_ReadTimeout() {
  265. $timeout = 1;
  266. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  267. $cmdFake = Predis_RedisServerProfile::getDefault()->createCommand('ping');
  268. $connection = new Predis_Connection(new Predis_ConnectionParameters($args));
  269. $expectedMessage = 'Error while reading line from the server';
  270. $start = time();
  271. $thrownException = null;
  272. try {
  273. $connection->readResponse($cmdFake);
  274. }
  275. catch (Predis_CommunicationException $exception) {
  276. $thrownException = $exception;
  277. }
  278. $this->assertType('Predis_CommunicationException', $thrownException);
  279. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  280. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  281. }
  282. /* ResponseReader */
  283. function testResponseReader_OptionIterableMultiBulkReplies() {
  284. $connection = new Predis_Connection(RC::getConnectionParameters());
  285. $responseReader = $connection->getResponseReader();
  286. $responseReader->setHandler(
  287. Predis_Protocol::PREFIX_MULTI_BULK,
  288. new Predis_ResponseMultiBulkHandler()
  289. );
  290. $this->assertType('array', $connection->rawCommand("KEYS *\r\n"));
  291. $responseReader->setHandler(
  292. Predis_Protocol::PREFIX_MULTI_BULK,
  293. new Predis_ResponseMultiBulkStreamHandler()
  294. );
  295. $this->assertType('Iterator', $connection->rawCommand("KEYS *\r\n"));
  296. }
  297. function testResponseReader_OptionExceptionOnError() {
  298. $connection = new Predis_Connection(RC::getConnectionParameters());
  299. $responseReader = $connection->getResponseReader();
  300. $connection->rawCommand("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
  301. $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
  302. $responseReader->setHandler(
  303. Predis_Protocol::PREFIX_ERROR,
  304. new Predis_ResponseErrorSilentHandler()
  305. );
  306. $errorReply = $connection->rawCommand($rawCmdUnexpected);
  307. $this->assertType('Predis_ResponseError', $errorReply);
  308. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
  309. $responseReader->setHandler(
  310. Predis_Protocol::PREFIX_ERROR,
  311. new Predis_ResponseErrorHandler()
  312. );
  313. $thrownException = null;
  314. try {
  315. $connection->rawCommand($rawCmdUnexpected);
  316. }
  317. catch (Predis_ServerException $exception) {
  318. $thrownException = $exception;
  319. }
  320. $this->assertType('Predis_ServerException', $thrownException);
  321. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $thrownException->getMessage());
  322. }
  323. /* Client + CommandPipeline */
  324. function testCommandPipeline_Simple() {
  325. $client = RC::getConnection();
  326. $client->flushdb();
  327. $pipe = $client->pipeline();
  328. $this->assertType('Predis_CommandPipeline', $pipe);
  329. $this->assertType('Predis_CommandPipeline', $pipe->set('foo', 'bar'));
  330. $this->assertType('Predis_CommandPipeline', $pipe->set('hoge', 'piyo'));
  331. $this->assertType('Predis_CommandPipeline', $pipe->mset(array(
  332. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  333. )));
  334. $this->assertType('Predis_CommandPipeline', $pipe->mget(array(
  335. 'foo', 'hoge', 'foofoo', 'hogehoge'
  336. )));
  337. $replies = $pipe->execute();
  338. $this->assertType('array', $replies);
  339. $this->assertEquals(4, count($replies));
  340. $this->assertEquals(4, count($replies[3]));
  341. $this->assertEquals('barbar', $replies[3][2]);
  342. }
  343. function testCommandPipeline_FluentInterface() {
  344. $client = RC::getConnection();
  345. $client->flushdb();
  346. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  347. $this->assertType('array', $replies);
  348. $this->assertEquals('bar', $replies[2]);
  349. }
  350. function testCommandPipeline_CallableAnonymousBlock() {
  351. $client = RC::getConnection();
  352. $client->flushdb();
  353. $replies = $client->pipeline(p_anon("\$pipe", "
  354. \$pipe->ping();
  355. \$pipe->set('foo', 'bar');
  356. \$pipe->get('foo');
  357. "));
  358. $this->assertType('array', $replies);
  359. $this->assertEquals('bar', $replies[2]);
  360. }
  361. function testCommandPipeline_ClientExceptionInCallableBlock() {
  362. $client = RC::getConnection();
  363. $client->flushdb();
  364. $expectedMessage = 'TEST';
  365. $thrownException = null;
  366. try {
  367. $client->pipeline(p_anon("\$pipe", "
  368. \$pipe->ping();
  369. \$pipe->set('foo', 'bar');
  370. throw new Predis_ClientException('$expectedMessage');
  371. "));
  372. }
  373. catch (Predis_ClientException $exception) {
  374. $thrownException = $exception;
  375. }
  376. $this->assertType('Predis_ClientException', $thrownException);
  377. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  378. $this->assertFalse($client->exists('foo'));
  379. }
  380. function testCommandPipeline_ServerExceptionInCallableBlock() {
  381. $client = RC::getConnection();
  382. $client->flushdb();
  383. $client->getResponseReader()->setHandler('-', new Predis_ResponseErrorSilentHandler());
  384. $replies = $client->pipeline(p_anon("\$pipe", "
  385. \$pipe->set('foo', 'bar');
  386. \$pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  387. \$pipe->set('hoge', 'piyo');
  388. "));
  389. $this->assertType('array', $replies);
  390. $this->assertType('Predis_ResponseError', $replies[1]);
  391. $this->assertTrue($client->exists('foo'));
  392. $this->assertTrue($client->exists('hoge'));
  393. }
  394. function testCommandPipeline_Flush() {
  395. $client = RC::getConnection();
  396. $client->flushdb();
  397. $pipe = $client->pipeline();
  398. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  399. $pipe->flushPipeline();
  400. $pipe->ping()->mget(array('foo', 'hoge'));
  401. $replies = $pipe->execute();
  402. $this->assertType('array', $replies);
  403. $this->assertEquals(4, count($replies));
  404. $this->assertEquals('bar', $replies[3][0]);
  405. $this->assertEquals('piyo', $replies[3][1]);
  406. }
  407. /* Client + MultiExecBlock */
  408. function testMultiExecBlock_Simple() {
  409. $client = RC::getConnection();
  410. $client->flushdb();
  411. $multi = $client->multiExec();
  412. $this->assertType('Predis_MultiExecBlock', $multi);
  413. $this->assertType('Predis_MultiExecBlock', $multi->set('foo', 'bar'));
  414. $this->assertType('Predis_MultiExecBlock', $multi->set('hoge', 'piyo'));
  415. $this->assertType('Predis_MultiExecBlock', $multi->mset(array(
  416. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  417. )));
  418. $this->assertType('Predis_MultiExecBlock', $multi->mget(array(
  419. 'foo', 'hoge', 'foofoo', 'hogehoge'
  420. )));
  421. $replies = $multi->execute();
  422. $this->assertType('array', $replies);
  423. $this->assertEquals(4, count($replies));
  424. $this->assertEquals(4, count($replies[3]));
  425. $this->assertEquals('barbar', $replies[3][2]);
  426. }
  427. function testMultiExecBlock_FluentInterface() {
  428. $client = RC::getConnection();
  429. $client->flushdb();
  430. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  431. $this->assertType('array', $replies);
  432. $this->assertEquals('bar', $replies[2]);
  433. }
  434. function testMultiExecBlock_CallableAnonymousBlock() {
  435. $client = RC::getConnection();
  436. $client->flushdb();
  437. $replies = $client->multiExec(p_anon("\$multi", "
  438. \$multi->ping();
  439. \$multi->set('foo', 'bar');
  440. \$multi->get('foo');
  441. "));
  442. $this->assertType('array', $replies);
  443. $this->assertEquals('bar', $replies[2]);
  444. }
  445. /**
  446. * @expectedException Predis_ClientException
  447. */
  448. function testMultiExecBlock_CannotMixFluentInterfaceAndAnonymousBlock() {
  449. $emptyBlock = p_anon("\$tx", "");
  450. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  451. }
  452. function testMultiExecBlock_EmptyCallableBlock() {
  453. $client = RC::getConnection();
  454. $client->flushdb();
  455. $replies = $client->multiExec(p_anon("\$multi", ""));
  456. $this->assertEquals(0, count($replies));
  457. $options = array('cas' => true);
  458. $replies = $client->multiExec($options, p_anon("\$multi", ""));
  459. $this->assertEquals(0, count($replies));
  460. $options = array('cas' => true);
  461. $replies = $client->multiExec($options, p_anon("\$multi", "
  462. \$multi->multi();
  463. "));
  464. $this->assertEquals(0, count($replies));
  465. }
  466. function testMultiExecBlock_ClientExceptionInCallableBlock() {
  467. $client = RC::getConnection();
  468. $client->flushdb();
  469. $expectedMessage = 'TEST';
  470. $thrownException = null;
  471. try {
  472. $client->multiExec(p_anon("\$multi", "
  473. \$multi->ping();
  474. \$multi->set('foo', 'bar');
  475. throw new Predis_ClientException('$expectedMessage');
  476. "));
  477. }
  478. catch (Predis_ClientException $exception) {
  479. $thrownException = $exception;
  480. }
  481. $this->assertType('Predis_ClientException', $thrownException);
  482. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  483. $this->assertFalse($client->exists('foo'));
  484. }
  485. function testMultiExecBlock_ServerExceptionInCallableBlock() {
  486. $client = RC::getConnection();
  487. $client->flushdb();
  488. $client->getResponseReader()->setHandler('-', new Predis_ResponseErrorSilentHandler());
  489. $multi = $client->multiExec();
  490. $multi->set('foo', 'bar');
  491. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  492. $multi->set('hoge', 'piyo');
  493. $replies = $multi->execute();
  494. $this->assertType('array', $replies);
  495. $this->assertType('Predis_ResponseError', $replies[1]);
  496. $this->assertTrue($client->exists('foo'));
  497. $this->assertTrue($client->exists('hoge'));
  498. }
  499. function testMultiExecBlock_Discard() {
  500. $client = RC::getConnection();
  501. $client->flushdb();
  502. $multi = $client->multiExec();
  503. $multi->set('foo', 'bar');
  504. $multi->discard();
  505. $multi->set('hoge', 'piyo');
  506. $replies = $multi->execute();
  507. $this->assertEquals(1, count($replies));
  508. $this->assertFalse($client->exists('foo'));
  509. $this->assertTrue($client->exists('hoge'));
  510. }
  511. function testMultiExecBlock_DiscardEmpty() {
  512. $client = RC::getConnection();
  513. $client->flushdb();
  514. $replies = $client->multiExec()->discard()->execute();
  515. $this->assertEquals(0, count($replies));
  516. }
  517. function testMultiExecBlock_Watch() {
  518. $client1 = RC::getConnection();
  519. $client2 = RC::getConnection(true);
  520. $client1->flushdb();
  521. $thrownException = null;
  522. try {
  523. $multi = $client1->multiExec(array('watch' => 'sentinel'));
  524. $multi->set('sentinel', 'client1');
  525. $multi->get('sentinel');
  526. $client2->set('sentinel', 'client2');
  527. $multi->execute();
  528. }
  529. catch (PredisException $exception) {
  530. $thrownException = $exception;
  531. }
  532. $this->assertType('Predis_AbortedMultiExec', $thrownException);
  533. $this->assertEquals('The current transaction has been aborted by the server', $thrownException->getMessage());
  534. $this->assertEquals('client2', $client1->get('sentinel'));
  535. }
  536. function testMultiExecBlock_CheckAndSet() {
  537. $client = RC::getConnection();
  538. $client->flushdb();
  539. $client->set('foo', 'bar');
  540. $options = array('watch' => 'foo', 'cas' => true);
  541. $replies = $client->multiExec($options, p_anon("\$tx", "
  542. \$tx->watch('foobar');
  543. \$foo = \$tx->get('foo');
  544. \$tx->multi();
  545. \$tx->set('foobar', \$foo);
  546. \$tx->mget('foo', 'foobar');
  547. "));
  548. $this->assertType('array', $replies);
  549. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  550. $tx = $client->multiExec($options);
  551. $tx->watch('foobar');
  552. $foo = $tx->get('foo');
  553. $replies = $tx->multi()
  554. ->set('foobar', $foo)
  555. ->mget('foo', 'foobar')
  556. ->execute();
  557. $this->assertType('array', $replies);
  558. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  559. }
  560. function testMultiExecBlock_RetryOnServerAbort() {
  561. $client1 = RC::getConnection();
  562. $client1->flushdb();
  563. $retry = 3;
  564. $thrownException = null;
  565. try {
  566. $options = array('watch' => 'sentinel', 'retry' => $retry);
  567. $client1->multiExec($options, p_anon("\$tx", "
  568. \$tx->set('sentinel', 'client1');
  569. \$tx->get('sentinel');
  570. \$client2 = RC::getConnection(true);
  571. \$client2->incr('attempts');
  572. \$client2->set('sentinel', 'client2');
  573. "));
  574. }
  575. catch (Predis_AbortedMultiExec $exception) {
  576. $thrownException = $exception;
  577. }
  578. $this->assertType('Predis_AbortedMultiExec', $thrownException);
  579. $this->assertEquals('The current transaction has been aborted by the server', $thrownException->getMessage());
  580. $this->assertEquals('client2', $client1->get('sentinel'));
  581. $this->assertEquals($retry + 1, $client1->get('attempts'));
  582. $client1->del('attempts', 'sentinel');
  583. $thrownException = null;
  584. try {
  585. $options = array(
  586. 'watch' => 'sentinel',
  587. 'cas' => true,
  588. 'retry' => $retry
  589. );
  590. $client1->multiExec($options, p_anon("\$tx", "
  591. \$tx->incr('attempts');
  592. \$tx->multi();
  593. \$tx->set('sentinel', 'client1');
  594. \$tx->get('sentinel');
  595. \$client2 = RC::getConnection(true);
  596. \$client2->set('sentinel', 'client2');
  597. "));
  598. }
  599. catch (Predis_AbortedMultiExec $exception) {
  600. $thrownException = $exception;
  601. }
  602. $this->assertType('Predis_AbortedMultiExec', $thrownException);
  603. $this->assertEquals('The current transaction has been aborted by the server', $thrownException->getMessage());
  604. $this->assertEquals('client2', $client1->get('sentinel'));
  605. $this->assertEquals($retry + 1, $client1->get('attempts'));
  606. }
  607. }
  608. ?>