ClientFeaturesTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. /* Predis\ConnectionParameters */
  16. function testConnectionParametersDefaultValues() {
  17. $params = new \Predis\ConnectionParameters();
  18. $this->assertEquals('127.0.0.1', $params->host);
  19. $this->assertEquals(6379, $params->port);
  20. $this->assertEquals(5, $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. /* Predis\Commands\Command and derivates */
  50. function testCommand_TestArguments() {
  51. $cmdArgs = array('key1', 'key2', 'key3');
  52. $cmd = new \Predis\Commands\StringGetMultiple();
  53. $cmd->setArguments($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\ConnectionPing();
  58. $this->assertNull($cmd->getArgument(0));
  59. }
  60. function testCommand_ParseResponse() {
  61. // default parser
  62. $cmd = new \Predis\Commands\StringGet();
  63. $this->assertEquals('test', $cmd->parseResponse('test'));
  64. // overridden parser (boolean)
  65. $cmd = new \Predis\Commands\KeyExists();
  66. $this->assertTrue($cmd->parseResponse('1'));
  67. $this->assertFalse($cmd->parseResponse('0'));
  68. // overridden parser (boolean)
  69. $cmd = new \Predis\Commands\ConnectionPing();
  70. $this->assertTrue($cmd->parseResponse('PONG'));
  71. // overridden parser (complex)
  72. // TODO: emulate a respons to INFO
  73. }
  74. /* Predis\Profiles\ServerProfile and derivates */
  75. function testServerProfile_GetSpecificVersions() {
  76. $this->assertInstanceOf('\Predis\Profiles\ServerVersion12', \Predis\Profiles\ServerProfile::get('1.2'));
  77. $this->assertInstanceOf('\Predis\Profiles\ServerVersion20', \Predis\Profiles\ServerProfile::get('2.0'));
  78. $this->assertInstanceOf('\Predis\Profiles\ServerVersion22', \Predis\Profiles\ServerProfile::get('2.2'));
  79. $this->assertInstanceOf('\Predis\Profiles\ServerVersionNext', \Predis\Profiles\ServerProfile::get('dev'));
  80. $this->assertInstanceOf('\Predis\Profiles\ServerProfile', \Predis\Profiles\ServerProfile::get('default'));
  81. $this->assertEquals(\Predis\Profiles\ServerProfile::get('default'), \Predis\Profiles\ServerProfile::getDefault());
  82. }
  83. function testServerProfile_SupportedCommands() {
  84. $profile_12 = \Predis\Profiles\ServerProfile::get('1.2');
  85. $profile_20 = \Predis\Profiles\ServerProfile::get('2.0');
  86. $this->assertTrue($profile_12->supportsCommand('info'));
  87. $this->assertTrue($profile_20->supportsCommand('info'));
  88. $this->assertFalse($profile_12->supportsCommand('multi'));
  89. $this->assertTrue($profile_20->supportsCommand('multi'));
  90. $this->assertFalse($profile_12->supportsCommand('watch'));
  91. $this->assertFalse($profile_20->supportsCommand('watch'));
  92. }
  93. function testServerProfile_CommandsCreation() {
  94. $profile = \Predis\Profiles\ServerProfile::get('2.0');
  95. $cmdNoArgs = $profile->createCommand('info');
  96. $this->assertInstanceOf('\Predis\Commands\ServerInfo', $cmdNoArgs);
  97. $this->assertNull($cmdNoArgs->getArgument());
  98. $args = array('key1', 'key2');
  99. $cmdWithArgs = $profile->createCommand('mget', $args);
  100. $this->assertInstanceOf('\Predis\Commands\StringGetMultiple', $cmdWithArgs);
  101. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  102. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  103. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  104. $bogusCommand = 'not_existing_command';
  105. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  106. RC::testForClientException($this, $expectedMessage, function()
  107. use($profile, $bogusCommand) {
  108. $profile->createCommand($bogusCommand);
  109. });
  110. }
  111. function testServerProfile_CommandsRegistration() {
  112. $profile = \Predis\Profiles\ServerProfile::get('1.2');
  113. $cmdId = 'multi';
  114. $cmdClass = '\Predis\Commands\TransactionMulti';
  115. $this->assertFalse($profile->supportsCommand($cmdId));
  116. $profile->defineCommand($cmdId, new $cmdClass());
  117. $this->assertTrue($profile->supportsCommand($cmdId));
  118. $this->assertInstanceOf($cmdClass, $profile->createCommand($cmdId));
  119. }
  120. function testServerProfile_CaseInsensitiveCommandsNames() {
  121. $profile = $this->redis->getProfile();
  122. $uppercase = $profile->supportsCommand('INFO');
  123. $lowercase = $profile->supportsCommand('info');
  124. $this->assertEquals($uppercase, $lowercase);
  125. $uppercase = $profile->createCommand('INFO');
  126. $lowercase = $profile->createCommand('info');
  127. $this->assertEquals($uppercase, $lowercase);
  128. }
  129. /* Predis\ResponseQueued */
  130. function testResponseQueued() {
  131. $response = new \Predis\ResponseQueued();
  132. $this->assertInstanceOf('\Predis\IReplyObject', $response);
  133. $this->assertEquals(\Predis\Protocol\Text\TextProtocol::QUEUED, (string)$response);
  134. }
  135. /* Predis\ResponseError */
  136. function testResponseError() {
  137. $errorMessage = 'ERROR MESSAGE';
  138. $response = new \Predis\ResponseError($errorMessage);
  139. $this->assertInstanceOf('\Predis\IReplyObject', $response);
  140. $this->assertInstanceOf('\Predis\IRedisServerError', $response);
  141. $this->assertEquals($errorMessage, $response->getMessage());
  142. $this->assertEquals($errorMessage, (string)$response);
  143. }
  144. /* Predis\Network\StreamConnection */
  145. function testStreamConnection_StringCastReturnsIPAndPort() {
  146. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  147. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  148. }
  149. function testStreamConnection_ConnectDisconnect() {
  150. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  151. $this->assertFalse($connection->isConnected());
  152. $connection->connect();
  153. $this->assertTrue($connection->isConnected());
  154. $connection->disconnect();
  155. $this->assertFalse($connection->isConnected());
  156. }
  157. function testStreamConnection_WriteAndReadCommand() {
  158. $cmd = \Predis\Profiles\ServerProfile::getDefault()->createCommand('ping');
  159. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  160. $connection->connect();
  161. $connection->writeCommand($cmd);
  162. $this->assertTrue($connection->readResponse($cmd));
  163. }
  164. function testStreamConnection_WriteCommandAndCloseConnection() {
  165. $cmd = \Predis\Profiles\ServerProfile::getDefault()->createCommand('quit');
  166. $connection = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters(
  167. RC::getConnectionArguments() + array('read_write_timeout' => 0.5)
  168. ));
  169. $connection->connect();
  170. $this->assertTrue($connection->isConnected());
  171. $connection->writeCommand($cmd);
  172. $connection->disconnect();
  173. $exceptionMessage = 'Error while reading line from the server';
  174. RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
  175. $connection->readResponse($cmd);
  176. });
  177. }
  178. function testStreamConnection_GetSocketOpensConnection() {
  179. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  180. $this->assertFalse($connection->isConnected());
  181. $this->assertInternalType('resource', $connection->getResource());
  182. $this->assertTrue($connection->isConnected());
  183. }
  184. function testStreamConnection_LazyConnect() {
  185. $cmd = \Predis\Profiles\ServerProfile::getDefault()->createCommand('ping');
  186. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  187. $this->assertFalse($connection->isConnected());
  188. $connection->writeCommand($cmd);
  189. $this->assertTrue($connection->isConnected());
  190. $this->assertTrue($connection->readResponse($cmd));
  191. }
  192. function testStreamConnection_Alias() {
  193. $connection1 = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  194. $this->assertNull($connection1->getParameters()->alias);
  195. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  196. $connection2 = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters($args));
  197. $this->assertEquals('servername', $connection2->getParameters()->alias);
  198. }
  199. function testStreamConnection_ConnectionTimeout() {
  200. $timeout = 3;
  201. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  202. $connection = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters($args));
  203. $start = time();
  204. RC::testForCommunicationException($this, null, function() use($connection) {
  205. $connection->connect();
  206. });
  207. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  208. }
  209. function testStreamConnection_ReadTimeout() {
  210. $timeout = 1;
  211. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  212. $cmdFake = \Predis\Profiles\ServerProfile::getDefault()->createCommand('ping');
  213. $connection = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters($args));
  214. $expectedMessage = 'Error while reading line from the server';
  215. $start = time();
  216. RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) {
  217. $connection->readResponse($cmdFake);
  218. });
  219. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  220. }
  221. /* Predis\Protocol\TextResponseReader */
  222. function testResponseReader_OptionIterableMultiBulkReplies() {
  223. $protocol = new Predis\Protocol\Text\ComposableTextProtocol();
  224. $reader = $protocol->getReader();
  225. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  226. $reader->setHandler(
  227. \Predis\Protocol\Text\TextProtocol::PREFIX_MULTI_BULK,
  228. new \Predis\Protocol\Text\ResponseMultiBulkHandler()
  229. );
  230. $connection->writeBytes("KEYS *\r\n");
  231. $this->assertInternalType('array', $reader->read($connection));
  232. $reader->setHandler(
  233. \Predis\Protocol\Text\TextProtocol::PREFIX_MULTI_BULK,
  234. new \Predis\Protocol\Text\ResponseMultiBulkStreamHandler()
  235. );
  236. $connection->writeBytes("KEYS *\r\n");
  237. $this->assertInstanceOf('\Iterator', $reader->read($connection));
  238. }
  239. function testResponseReader_OptionExceptionOnError() {
  240. $protocol = new Predis\Protocol\Text\ComposableTextProtocol();
  241. $reader = $protocol->getReader();
  242. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  243. $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
  244. $connection->writeBytes("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
  245. $reader->read($connection);
  246. $reader->setHandler(
  247. \Predis\Protocol\Text\TextProtocol::PREFIX_ERROR,
  248. new \Predis\Protocol\Text\ResponseErrorSilentHandler()
  249. );
  250. $connection->writeBytes($rawCmdUnexpected);
  251. $errorReply = $reader->read($connection);
  252. $this->assertInstanceOf('\Predis\ResponseError', $errorReply);
  253. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->getMessage());
  254. $reader->setHandler(
  255. \Predis\Protocol\Text\TextProtocol::PREFIX_ERROR,
  256. new \Predis\Protocol\Text\ResponseErrorHandler()
  257. );
  258. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
  259. use ($connection, $rawCmdUnexpected) {
  260. $connection->writeBytes($rawCmdUnexpected);
  261. $connection->getProtocol()->read($connection);
  262. });
  263. }
  264. function testResponseReader_EmptyBulkResponse() {
  265. $protocol = new \Predis\Protocol\Text\ComposableTextProtocol();
  266. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  267. $client = new \Predis\Client($connection);
  268. $this->assertTrue($client->set('foo', ''));
  269. $this->assertEquals('', $client->get('foo'));
  270. $this->assertEquals('', $client->get('foo'));
  271. }
  272. /* Client initialization */
  273. function testClientInitialization_SingleConnectionParameters() {
  274. $params1 = array_merge(RC::getConnectionArguments(), array(
  275. 'connection_timeout' => 10,
  276. 'read_write_timeout' => 30,
  277. 'alias' => 'connection_alias',
  278. ));
  279. $params2 = RC::getConnectionParametersArgumentsString($params1);
  280. $params3 = new \Predis\ConnectionParameters($params1);
  281. $params4 = new \Predis\Network\StreamConnection($params3);
  282. foreach (array($params1, $params2, $params3, $params4) as $params) {
  283. $client = new \Predis\Client($params);
  284. $parameters = $client->getConnection()->getParameters();
  285. $this->assertEquals($params1['host'], $parameters->host);
  286. $this->assertEquals($params1['port'], $parameters->port);
  287. $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
  288. $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
  289. $this->assertEquals($params1['alias'], $parameters->alias);
  290. $this->assertNull($parameters->password);
  291. }
  292. }
  293. function testClientInitialization_ClusterConnectionParameters() {
  294. $params1 = array_merge(RC::getConnectionArguments(), array(
  295. 'connection_timeout' => 10,
  296. 'read_write_timeout' => 30,
  297. ));
  298. $params2 = RC::getConnectionParametersArgumentsString($params1);
  299. $params3 = new \Predis\ConnectionParameters($params1);
  300. $params4 = new \Predis\Network\StreamConnection($params3);
  301. $client1 = new \Predis\Client(array($params1, $params2, $params3, $params4));
  302. foreach ($client1->getConnection() as $connection) {
  303. $parameters = $connection->getParameters();
  304. $this->assertEquals($params1['host'], $parameters->host);
  305. $this->assertEquals($params1['port'], $parameters->port);
  306. $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
  307. $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
  308. $this->assertNull($parameters->password);
  309. }
  310. $connectionCluster = $client1->getConnection();
  311. $client2 = new \Predis\Client($connectionCluster);
  312. $this->assertSame($connectionCluster, $client2->getConnection());
  313. }
  314. /* Client + PipelineContext */
  315. function testPipelineContext_Simple() {
  316. $client = RC::getConnection();
  317. $client->flushdb();
  318. $pipe = $client->pipeline();
  319. $pipelineClass = '\Predis\Pipeline\PipelineContext';
  320. $this->assertInstanceOf($pipelineClass, $pipe);
  321. $this->assertInstanceOf($pipelineClass, $pipe->set('foo', 'bar'));
  322. $this->assertInstanceOf($pipelineClass, $pipe->set('hoge', 'piyo'));
  323. $this->assertInstanceOf($pipelineClass, $pipe->mset(array(
  324. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  325. )));
  326. $this->assertInstanceOf($pipelineClass, $pipe->mget(array(
  327. 'foo', 'hoge', 'foofoo', 'hogehoge'
  328. )));
  329. $replies = $pipe->execute();
  330. $this->assertInternalType('array', $replies);
  331. $this->assertEquals(4, count($replies));
  332. $this->assertEquals(4, count($replies[3]));
  333. $this->assertEquals('barbar', $replies[3][2]);
  334. }
  335. function testPipelineContext_FluentInterface() {
  336. $client = RC::getConnection();
  337. $client->flushdb();
  338. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  339. $this->assertInternalType('array', $replies);
  340. $this->assertEquals('bar', $replies[2]);
  341. }
  342. function testPipelineContext_CallableAnonymousBlock() {
  343. $client = RC::getConnection();
  344. $client->flushdb();
  345. $replies = $client->pipeline(function($pipe) {
  346. $pipe->ping();
  347. $pipe->set('foo', 'bar');
  348. $pipe->get('foo');
  349. });
  350. $this->assertInternalType('array', $replies);
  351. $this->assertEquals('bar', $replies[2]);
  352. }
  353. function testPipelineContext_ClientExceptionInCallableBlock() {
  354. $client = RC::getConnection();
  355. $client->flushdb();
  356. RC::testForClientException($this, 'TEST', function() use($client) {
  357. $client->pipeline(function($pipe) {
  358. $pipe->ping();
  359. $pipe->set('foo', 'bar');
  360. throw new \Predis\ClientException("TEST");
  361. });
  362. });
  363. $this->assertFalse($client->exists('foo'));
  364. }
  365. function testPipelineContext_ServerExceptionInCallableBlock() {
  366. $client = RC::createConnection(array('throw_errors' => false));
  367. $client->flushdb();
  368. $replies = $client->pipeline(function($pipe) {
  369. $pipe->set('foo', 'bar');
  370. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  371. $pipe->set('hoge', 'piyo');
  372. });
  373. $this->assertInternalType('array', $replies);
  374. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  375. $this->assertTrue($client->exists('foo'));
  376. $this->assertTrue($client->exists('hoge'));
  377. }
  378. function testPipelineContext_Flush() {
  379. $client = RC::getConnection();
  380. $client->flushdb();
  381. $pipe = $client->pipeline();
  382. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  383. $pipe->flushPipeline();
  384. $pipe->ping()->mget(array('foo', 'hoge'));
  385. $replies = $pipe->execute();
  386. $this->assertInternalType('array', $replies);
  387. $this->assertEquals(4, count($replies));
  388. $this->assertEquals('bar', $replies[3][0]);
  389. $this->assertEquals('piyo', $replies[3][1]);
  390. }
  391. /* Predis\Client + Predis\MultiExecContext */
  392. function testMultiExecContext_Simple() {
  393. $client = RC::getConnection();
  394. $client->flushdb();
  395. $multi = $client->multiExec();
  396. $transactionClass = '\Predis\Transaction\MultiExecContext';
  397. $this->assertInstanceOf($transactionClass, $multi);
  398. $this->assertInstanceOf($transactionClass, $multi->set('foo', 'bar'));
  399. $this->assertInstanceOf($transactionClass, $multi->set('hoge', 'piyo'));
  400. $this->assertInstanceOf($transactionClass, $multi->mset(array(
  401. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  402. )));
  403. $this->assertInstanceOf($transactionClass, $multi->mget(array(
  404. 'foo', 'hoge', 'foofoo', 'hogehoge'
  405. )));
  406. $replies = $multi->execute();
  407. $this->assertInternalType('array', $replies);
  408. $this->assertEquals(4, count($replies));
  409. $this->assertEquals(4, count($replies[3]));
  410. $this->assertEquals('barbar', $replies[3][2]);
  411. }
  412. function testMultiExecContext_FluentInterface() {
  413. $client = RC::getConnection();
  414. $client->flushdb();
  415. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  416. $this->assertInternalType('array', $replies);
  417. $this->assertEquals('bar', $replies[2]);
  418. }
  419. function testMultiExecContext_CallableAnonymousBlock() {
  420. $client = RC::getConnection();
  421. $client->flushdb();
  422. $replies = $client->multiExec(function($multi) {
  423. $multi->ping();
  424. $multi->set('foo', 'bar');
  425. $multi->get('foo');
  426. });
  427. $this->assertInternalType('array', $replies);
  428. $this->assertEquals('bar', $replies[2]);
  429. }
  430. /**
  431. * @expectedException Predis\ClientException
  432. */
  433. function testMultiExecContext_CannotMixFluentInterfaceAndAnonymousBlock() {
  434. $emptyBlock = function($tx) { };
  435. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  436. }
  437. function testMultiExecContext_EmptyCallableBlock() {
  438. $client = RC::getConnection();
  439. $client->flushdb();
  440. $replies = $client->multiExec(function($multi) { });
  441. $this->assertEquals(0, count($replies));
  442. $options = array('cas' => true);
  443. $replies = $client->multiExec($options, function($multi) { });
  444. $this->assertEquals(0, count($replies));
  445. $options = array('cas' => true);
  446. $replies = $client->multiExec($options, function($multi) {
  447. $multi->multi();
  448. });
  449. $this->assertEquals(0, count($replies));
  450. }
  451. function testMultiExecContext_ClientExceptionInCallableBlock() {
  452. $client = RC::getConnection();
  453. $client->flushdb();
  454. RC::testForClientException($this, 'TEST', function() use($client) {
  455. $client->multiExec(function($multi) {
  456. $multi->ping();
  457. $multi->set('foo', 'bar');
  458. throw new \Predis\ClientException("TEST");
  459. });
  460. });
  461. $this->assertFalse($client->exists('foo'));
  462. }
  463. function testMultiExecContext_ServerExceptionInCallableBlock() {
  464. $client = RC::createConnection(array('throw_errors' => false));
  465. $client->flushdb();
  466. $replies = $client->multiExec(function($multi) {
  467. $multi->set('foo', 'bar');
  468. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  469. $multi->set('hoge', 'piyo');
  470. });
  471. $this->assertInternalType('array', $replies);
  472. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  473. $this->assertTrue($client->exists('foo'));
  474. $this->assertTrue($client->exists('hoge'));
  475. }
  476. function testMultiExecContext_Discard() {
  477. $client = RC::getConnection();
  478. $client->flushdb();
  479. $replies = $client->multiExec(function($multi) {
  480. $multi->set('foo', 'bar');
  481. $multi->discard();
  482. $multi->set('hoge', 'piyo');
  483. });
  484. $this->assertEquals(1, count($replies));
  485. $this->assertFalse($client->exists('foo'));
  486. $this->assertTrue($client->exists('hoge'));
  487. }
  488. function testMultiExecContext_DiscardEmpty() {
  489. $client = RC::getConnection();
  490. $client->flushdb();
  491. $replies = $client->multiExec(function($multi) {
  492. $multi->discard();
  493. });
  494. $this->assertEquals(0, count($replies));
  495. }
  496. function testMultiExecContext_Watch() {
  497. $client1 = RC::getConnection();
  498. $client2 = RC::getConnection(true);
  499. $client1->flushdb();
  500. RC::testForAbortedMultiExecException($this, function()
  501. use($client1, $client2) {
  502. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  503. use ($client2) {
  504. $multi->set('sentinel', 'client1');
  505. $multi->get('sentinel');
  506. $client2->set('sentinel', 'client2');
  507. });
  508. });
  509. $this->assertEquals('client2', $client1->get('sentinel'));
  510. }
  511. function testMultiExecContext_CheckAndSet() {
  512. $client = RC::getConnection();
  513. $client->flushdb();
  514. $client->set('foo', 'bar');
  515. $options = array('watch' => 'foo', 'cas' => true);
  516. $replies = $client->multiExec($options, function($tx) {
  517. $tx->watch('foobar');
  518. $foo = $tx->get('foo');
  519. $tx->multi();
  520. $tx->set('foobar', $foo);
  521. $tx->mget('foo', 'foobar');
  522. });
  523. $this->assertInternalType('array', $replies);
  524. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  525. $tx = $client->multiExec($options);
  526. $tx->watch('foobar');
  527. $foo = $tx->get('foo');
  528. $replies = $tx->multi()
  529. ->set('foobar', $foo)
  530. ->mget('foo', 'foobar')
  531. ->execute();
  532. $this->assertInternalType('array', $replies);
  533. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  534. }
  535. function testMultiExecContext_RetryOnServerAbort() {
  536. $client1 = RC::getConnection();
  537. $client2 = RC::getConnection(true);
  538. $client1->flushdb();
  539. $retry = 3;
  540. $attempts = 0;
  541. RC::testForAbortedMultiExecException($this, function()
  542. use($client1, $client2, $retry, &$attempts) {
  543. $options = array('watch' => 'sentinel', 'retry' => $retry);
  544. $client1->multiExec($options, function($tx)
  545. use ($client2, &$attempts) {
  546. $attempts++;
  547. $tx->set('sentinel', 'client1');
  548. $tx->get('sentinel');
  549. $client2->set('sentinel', 'client2');
  550. });
  551. });
  552. $this->assertEquals('client2', $client1->get('sentinel'));
  553. $this->assertEquals($retry + 1, $attempts);
  554. $retry = 3;
  555. $attempts = 0;
  556. RC::testForAbortedMultiExecException($this, function()
  557. use($client1, $client2, $retry, &$attempts) {
  558. $options = array(
  559. 'watch' => 'sentinel',
  560. 'cas' => true,
  561. 'retry' => $retry
  562. );
  563. $client1->multiExec($options, function($tx)
  564. use ($client2, &$attempts) {
  565. $attempts++;
  566. $tx->incr('attempts');
  567. $tx->multi();
  568. $tx->set('sentinel', 'client1');
  569. $tx->get('sentinel');
  570. $client2->set('sentinel', 'client2');
  571. });
  572. });
  573. $this->assertEquals('client2', $client1->get('sentinel'));
  574. $this->assertEquals($retry + 1, $attempts);
  575. $this->assertEquals($attempts, $client1->get('attempts'));
  576. }
  577. /**
  578. * @expectedException InvalidArgumentException
  579. */
  580. function testMultiExecContext_RetryNotAvailableWithoutBlock() {
  581. $options = array('watch' => 'foo', 'retry' => 1);
  582. $tx = RC::getConnection()->multiExec($options);
  583. $tx->multi()->get('foo')->exec();
  584. }
  585. function testMultiExecContext_CheckAndSet_Discard() {
  586. $client = RC::getConnection();
  587. $client->flushdb();
  588. $client->set('foo', 'bar');
  589. $options = array('watch' => 'foo', 'cas' => true);
  590. $replies = $client->multiExec($options, function($tx) {
  591. $tx->watch('foobar');
  592. $foo = $tx->get('foo');
  593. $tx->multi();
  594. $tx->set('foobar', $foo);
  595. $tx->discard();
  596. $tx->mget('foo', 'foobar');
  597. });
  598. $this->assertInternalType('array', $replies);
  599. $this->assertEquals(array(array('bar', null)), $replies);
  600. $hijack = true;
  601. $client->set('foo', 'bar');
  602. $client2 = RC::getConnection(true);
  603. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  604. $replies = $client->multiExec($options, function($tx)
  605. use ($client2, &$hijack) {
  606. $foo = $tx->get('foo');
  607. $tx->multi();
  608. $tx->set('foobar', $foo);
  609. $tx->discard();
  610. if ($hijack) {
  611. $hijack = false;
  612. $client2->set('foo', 'hijacked!');
  613. }
  614. $tx->mget('foo', 'foobar');
  615. });
  616. $this->assertInternalType('array', $replies);
  617. $this->assertEquals(array(array('hijacked!', null)), $replies);
  618. }
  619. }
  620. ?>