ClientFeaturesTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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. /* Predis\ResponseQueued */
  121. function testResponseQueued() {
  122. $response = new \Predis\ResponseQueued();
  123. $this->assertInstanceOf('\Predis\IReplyObject', $response);
  124. $this->assertEquals(\Predis\Protocol\Text\TextProtocol::QUEUED, (string)$response);
  125. }
  126. /* Predis\ResponseError */
  127. function testResponseError() {
  128. $errorMessage = 'ERROR MESSAGE';
  129. $response = new \Predis\ResponseError($errorMessage);
  130. $this->assertInstanceOf('\Predis\IReplyObject', $response);
  131. $this->assertInstanceOf('\Predis\IRedisServerError', $response);
  132. $this->assertEquals($errorMessage, $response->getMessage());
  133. $this->assertEquals($errorMessage, (string)$response);
  134. }
  135. /* Predis\Network\StreamConnection */
  136. function testStreamConnection_StringCastReturnsIPAndPort() {
  137. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  138. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  139. }
  140. function testStreamConnection_ConnectDisconnect() {
  141. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  142. $this->assertFalse($connection->isConnected());
  143. $connection->connect();
  144. $this->assertTrue($connection->isConnected());
  145. $connection->disconnect();
  146. $this->assertFalse($connection->isConnected());
  147. }
  148. function testStreamConnection_WriteAndReadCommand() {
  149. $cmd = \Predis\Profiles\ServerProfile::getDefault()->createCommand('ping');
  150. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  151. $connection->connect();
  152. $connection->writeCommand($cmd);
  153. $this->assertTrue($connection->readResponse($cmd));
  154. }
  155. function testStreamConnection_WriteCommandAndCloseConnection() {
  156. $cmd = \Predis\Profiles\ServerProfile::getDefault()->createCommand('quit');
  157. $connection = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters(
  158. RC::getConnectionArguments() + array('read_write_timeout' => 0.5)
  159. ));
  160. $connection->connect();
  161. $this->assertTrue($connection->isConnected());
  162. $connection->writeCommand($cmd);
  163. $connection->disconnect();
  164. $exceptionMessage = 'Error while reading line from the server';
  165. RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
  166. $connection->readResponse($cmd);
  167. });
  168. }
  169. function testStreamConnection_GetSocketOpensConnection() {
  170. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  171. $this->assertFalse($connection->isConnected());
  172. $this->assertInternalType('resource', $connection->getResource());
  173. $this->assertTrue($connection->isConnected());
  174. }
  175. function testStreamConnection_LazyConnect() {
  176. $cmd = \Predis\Profiles\ServerProfile::getDefault()->createCommand('ping');
  177. $connection = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  178. $this->assertFalse($connection->isConnected());
  179. $connection->writeCommand($cmd);
  180. $this->assertTrue($connection->isConnected());
  181. $this->assertTrue($connection->readResponse($cmd));
  182. }
  183. function testStreamConnection_Alias() {
  184. $connection1 = new \Predis\Network\StreamConnection(RC::getConnectionParameters());
  185. $this->assertNull($connection1->getParameters()->alias);
  186. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  187. $connection2 = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters($args));
  188. $this->assertEquals('servername', $connection2->getParameters()->alias);
  189. }
  190. function testStreamConnection_ConnectionTimeout() {
  191. $timeout = 3;
  192. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  193. $connection = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters($args));
  194. $start = time();
  195. RC::testForCommunicationException($this, null, function() use($connection) {
  196. $connection->connect();
  197. });
  198. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  199. }
  200. function testStreamConnection_ReadTimeout() {
  201. $timeout = 1;
  202. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  203. $cmdFake = \Predis\Profiles\ServerProfile::getDefault()->createCommand('ping');
  204. $connection = new \Predis\Network\StreamConnection(new \Predis\ConnectionParameters($args));
  205. $expectedMessage = 'Error while reading line from the server';
  206. $start = time();
  207. RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) {
  208. $connection->readResponse($cmdFake);
  209. });
  210. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  211. }
  212. /* Predis\Protocol\TextResponseReader */
  213. function testResponseReader_OptionIterableMultiBulkReplies() {
  214. $protocol = new Predis\Protocol\Text\ComposableTextProtocol();
  215. $reader = $protocol->getReader();
  216. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  217. $reader->setHandler(
  218. \Predis\Protocol\Text\TextProtocol::PREFIX_MULTI_BULK,
  219. new \Predis\Protocol\Text\ResponseMultiBulkHandler()
  220. );
  221. $connection->writeBytes("KEYS *\r\n");
  222. $this->assertInternalType('array', $reader->read($connection));
  223. $reader->setHandler(
  224. \Predis\Protocol\Text\TextProtocol::PREFIX_MULTI_BULK,
  225. new \Predis\Protocol\Text\ResponseMultiBulkStreamHandler()
  226. );
  227. $connection->writeBytes("KEYS *\r\n");
  228. $this->assertInstanceOf('\Iterator', $reader->read($connection));
  229. }
  230. function testResponseReader_OptionExceptionOnError() {
  231. $protocol = new Predis\Protocol\Text\ComposableTextProtocol();
  232. $reader = $protocol->getReader();
  233. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  234. $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
  235. $connection->writeBytes("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
  236. $reader->read($connection);
  237. $reader->setHandler(
  238. \Predis\Protocol\Text\TextProtocol::PREFIX_ERROR,
  239. new \Predis\Protocol\Text\ResponseErrorSilentHandler()
  240. );
  241. $connection->writeBytes($rawCmdUnexpected);
  242. $errorReply = $reader->read($connection);
  243. $this->assertInstanceOf('\Predis\ResponseError', $errorReply);
  244. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->getMessage());
  245. $reader->setHandler(
  246. \Predis\Protocol\Text\TextProtocol::PREFIX_ERROR,
  247. new \Predis\Protocol\Text\ResponseErrorHandler()
  248. );
  249. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
  250. use ($connection, $rawCmdUnexpected) {
  251. $connection->writeBytes($rawCmdUnexpected);
  252. $connection->getProtocol()->read($connection);
  253. });
  254. }
  255. function testResponseReader_EmptyBulkResponse() {
  256. $protocol = new \Predis\Protocol\Text\ComposableTextProtocol();
  257. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  258. $client = new \Predis\Client($connection);
  259. $this->assertTrue($client->set('foo', ''));
  260. $this->assertEquals('', $client->get('foo'));
  261. $this->assertEquals('', $client->get('foo'));
  262. }
  263. /* Client initialization */
  264. function testClientInitialization_SingleConnectionParameters() {
  265. $params1 = array_merge(RC::getConnectionArguments(), array(
  266. 'connection_timeout' => 10,
  267. 'read_write_timeout' => 30,
  268. 'alias' => 'connection_alias',
  269. ));
  270. $params2 = RC::getConnectionParametersArgumentsString($params1);
  271. $params3 = new \Predis\ConnectionParameters($params1);
  272. $params4 = new \Predis\Network\StreamConnection($params3);
  273. foreach (array($params1, $params2, $params3, $params4) as $params) {
  274. $client = new \Predis\Client($params);
  275. $parameters = $client->getConnection()->getParameters();
  276. $this->assertEquals($params1['host'], $parameters->host);
  277. $this->assertEquals($params1['port'], $parameters->port);
  278. $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
  279. $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
  280. $this->assertEquals($params1['alias'], $parameters->alias);
  281. $this->assertNull($parameters->password);
  282. }
  283. }
  284. function testClientInitialization_ClusterConnectionParameters() {
  285. $params1 = array_merge(RC::getConnectionArguments(), array(
  286. 'connection_timeout' => 10,
  287. 'read_write_timeout' => 30,
  288. ));
  289. $params2 = RC::getConnectionParametersArgumentsString($params1);
  290. $params3 = new \Predis\ConnectionParameters($params1);
  291. $params4 = new \Predis\Network\StreamConnection($params3);
  292. $client1 = new \Predis\Client(array($params1, $params2, $params3, $params4));
  293. foreach ($client1->getConnection() as $connection) {
  294. $parameters = $connection->getParameters();
  295. $this->assertEquals($params1['host'], $parameters->host);
  296. $this->assertEquals($params1['port'], $parameters->port);
  297. $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
  298. $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
  299. $this->assertNull($parameters->password);
  300. }
  301. $connectionCluster = $client1->getConnection();
  302. $client2 = new \Predis\Client($connectionCluster);
  303. $this->assertSame($connectionCluster, $client2->getConnection());
  304. }
  305. /* Client + PipelineContext */
  306. function testPipelineContext_Simple() {
  307. $client = RC::getConnection();
  308. $client->flushdb();
  309. $pipe = $client->pipeline();
  310. $pipelineClass = '\Predis\Pipeline\PipelineContext';
  311. $this->assertInstanceOf($pipelineClass, $pipe);
  312. $this->assertInstanceOf($pipelineClass, $pipe->set('foo', 'bar'));
  313. $this->assertInstanceOf($pipelineClass, $pipe->set('hoge', 'piyo'));
  314. $this->assertInstanceOf($pipelineClass, $pipe->mset(array(
  315. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  316. )));
  317. $this->assertInstanceOf($pipelineClass, $pipe->mget(array(
  318. 'foo', 'hoge', 'foofoo', 'hogehoge'
  319. )));
  320. $replies = $pipe->execute();
  321. $this->assertInternalType('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 testPipelineContext_FluentInterface() {
  327. $client = RC::getConnection();
  328. $client->flushdb();
  329. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  330. $this->assertInternalType('array', $replies);
  331. $this->assertEquals('bar', $replies[2]);
  332. }
  333. function testPipelineContext_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->assertInternalType('array', $replies);
  342. $this->assertEquals('bar', $replies[2]);
  343. }
  344. function testPipelineContext_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 testPipelineContext_ServerExceptionInCallableBlock() {
  357. $client = RC::createConnection(array('throw_errors' => false));
  358. $client->flushdb();
  359. $replies = $client->pipeline(function($pipe) {
  360. $pipe->set('foo', 'bar');
  361. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  362. $pipe->set('hoge', 'piyo');
  363. });
  364. $this->assertInternalType('array', $replies);
  365. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  366. $this->assertTrue($client->exists('foo'));
  367. $this->assertTrue($client->exists('hoge'));
  368. }
  369. function testPipelineContext_Flush() {
  370. $client = RC::getConnection();
  371. $client->flushdb();
  372. $pipe = $client->pipeline();
  373. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  374. $pipe->flushPipeline();
  375. $pipe->ping()->mget(array('foo', 'hoge'));
  376. $replies = $pipe->execute();
  377. $this->assertInternalType('array', $replies);
  378. $this->assertEquals(4, count($replies));
  379. $this->assertEquals('bar', $replies[3][0]);
  380. $this->assertEquals('piyo', $replies[3][1]);
  381. }
  382. /* Predis\Client + Predis\MultiExecContext */
  383. function testMultiExecContext_Simple() {
  384. $client = RC::getConnection();
  385. $client->flushdb();
  386. $multi = $client->multiExec();
  387. $transactionClass = '\Predis\Transaction\MultiExecContext';
  388. $this->assertInstanceOf($transactionClass, $multi);
  389. $this->assertInstanceOf($transactionClass, $multi->set('foo', 'bar'));
  390. $this->assertInstanceOf($transactionClass, $multi->set('hoge', 'piyo'));
  391. $this->assertInstanceOf($transactionClass, $multi->mset(array(
  392. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  393. )));
  394. $this->assertInstanceOf($transactionClass, $multi->mget(array(
  395. 'foo', 'hoge', 'foofoo', 'hogehoge'
  396. )));
  397. $replies = $multi->execute();
  398. $this->assertInternalType('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 testMultiExecContext_FluentInterface() {
  404. $client = RC::getConnection();
  405. $client->flushdb();
  406. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  407. $this->assertInternalType('array', $replies);
  408. $this->assertEquals('bar', $replies[2]);
  409. }
  410. function testMultiExecContext_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->assertInternalType('array', $replies);
  419. $this->assertEquals('bar', $replies[2]);
  420. }
  421. /**
  422. * @expectedException Predis\ClientException
  423. */
  424. function testMultiExecContext_CannotMixFluentInterfaceAndAnonymousBlock() {
  425. $emptyBlock = function($tx) { };
  426. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  427. }
  428. function testMultiExecContext_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 testMultiExecContext_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 testMultiExecContext_ServerExceptionInCallableBlock() {
  455. $client = RC::createConnection(array('throw_errors' => false));
  456. $client->flushdb();
  457. $replies = $client->multiExec(function($multi) {
  458. $multi->set('foo', 'bar');
  459. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  460. $multi->set('hoge', 'piyo');
  461. });
  462. $this->assertInternalType('array', $replies);
  463. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  464. $this->assertTrue($client->exists('foo'));
  465. $this->assertTrue($client->exists('hoge'));
  466. }
  467. function testMultiExecContext_Discard() {
  468. $client = RC::getConnection();
  469. $client->flushdb();
  470. $replies = $client->multiExec(function($multi) {
  471. $multi->set('foo', 'bar');
  472. $multi->discard();
  473. $multi->set('hoge', 'piyo');
  474. });
  475. $this->assertEquals(1, count($replies));
  476. $this->assertFalse($client->exists('foo'));
  477. $this->assertTrue($client->exists('hoge'));
  478. }
  479. function testMultiExecContext_DiscardEmpty() {
  480. $client = RC::getConnection();
  481. $client->flushdb();
  482. $replies = $client->multiExec(function($multi) {
  483. $multi->discard();
  484. });
  485. $this->assertEquals(0, count($replies));
  486. }
  487. function testMultiExecContext_Watch() {
  488. $client1 = RC::getConnection();
  489. $client2 = RC::getConnection(true);
  490. $client1->flushdb();
  491. RC::testForAbortedMultiExecException($this, function()
  492. use($client1, $client2) {
  493. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  494. use ($client2) {
  495. $multi->set('sentinel', 'client1');
  496. $multi->get('sentinel');
  497. $client2->set('sentinel', 'client2');
  498. });
  499. });
  500. $this->assertEquals('client2', $client1->get('sentinel'));
  501. }
  502. function testMultiExecContext_CheckAndSet() {
  503. $client = RC::getConnection();
  504. $client->flushdb();
  505. $client->set('foo', 'bar');
  506. $options = array('watch' => 'foo', 'cas' => true);
  507. $replies = $client->multiExec($options, function($tx) {
  508. $tx->watch('foobar');
  509. $foo = $tx->get('foo');
  510. $tx->multi();
  511. $tx->set('foobar', $foo);
  512. $tx->mget('foo', 'foobar');
  513. });
  514. $this->assertInternalType('array', $replies);
  515. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  516. $tx = $client->multiExec($options);
  517. $tx->watch('foobar');
  518. $foo = $tx->get('foo');
  519. $replies = $tx->multi()
  520. ->set('foobar', $foo)
  521. ->mget('foo', 'foobar')
  522. ->execute();
  523. $this->assertInternalType('array', $replies);
  524. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  525. }
  526. function testMultiExecContext_RetryOnServerAbort() {
  527. $client1 = RC::getConnection();
  528. $client2 = RC::getConnection(true);
  529. $client1->flushdb();
  530. $retry = 3;
  531. $attempts = 0;
  532. RC::testForAbortedMultiExecException($this, function()
  533. use($client1, $client2, $retry, &$attempts) {
  534. $options = array('watch' => 'sentinel', 'retry' => $retry);
  535. $client1->multiExec($options, function($tx)
  536. use ($client2, &$attempts) {
  537. $attempts++;
  538. $tx->set('sentinel', 'client1');
  539. $tx->get('sentinel');
  540. $client2->set('sentinel', 'client2');
  541. });
  542. });
  543. $this->assertEquals('client2', $client1->get('sentinel'));
  544. $this->assertEquals($retry + 1, $attempts);
  545. $retry = 3;
  546. $attempts = 0;
  547. RC::testForAbortedMultiExecException($this, function()
  548. use($client1, $client2, $retry, &$attempts) {
  549. $options = array(
  550. 'watch' => 'sentinel',
  551. 'cas' => true,
  552. 'retry' => $retry
  553. );
  554. $client1->multiExec($options, function($tx)
  555. use ($client2, &$attempts) {
  556. $attempts++;
  557. $tx->incr('attempts');
  558. $tx->multi();
  559. $tx->set('sentinel', 'client1');
  560. $tx->get('sentinel');
  561. $client2->set('sentinel', 'client2');
  562. });
  563. });
  564. $this->assertEquals('client2', $client1->get('sentinel'));
  565. $this->assertEquals($retry + 1, $attempts);
  566. $this->assertEquals($attempts, $client1->get('attempts'));
  567. }
  568. /**
  569. * @expectedException InvalidArgumentException
  570. */
  571. function testMultiExecContext_RetryNotAvailableWithoutBlock() {
  572. $options = array('watch' => 'foo', 'retry' => 1);
  573. $tx = RC::getConnection()->multiExec($options);
  574. $tx->multi()->get('foo')->exec();
  575. }
  576. function testMultiExecContext_CheckAndSet_Discard() {
  577. $client = RC::getConnection();
  578. $client->flushdb();
  579. $client->set('foo', 'bar');
  580. $options = array('watch' => 'foo', 'cas' => true);
  581. $replies = $client->multiExec($options, function($tx) {
  582. $tx->watch('foobar');
  583. $foo = $tx->get('foo');
  584. $tx->multi();
  585. $tx->set('foobar', $foo);
  586. $tx->discard();
  587. $tx->mget('foo', 'foobar');
  588. });
  589. $this->assertInternalType('array', $replies);
  590. $this->assertEquals(array(array('bar', null)), $replies);
  591. $hijack = true;
  592. $client->set('foo', 'bar');
  593. $client2 = RC::getConnection(true);
  594. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  595. $replies = $client->multiExec($options, function($tx)
  596. use ($client2, &$hijack) {
  597. $foo = $tx->get('foo');
  598. $tx->multi();
  599. $tx->set('foobar', $foo);
  600. $tx->discard();
  601. if ($hijack) {
  602. $hijack = false;
  603. $client2->set('foo', 'hijacked!');
  604. }
  605. $tx->mget('foo', 'foobar');
  606. });
  607. $this->assertInternalType('array', $replies);
  608. $this->assertEquals(array(array('hijacked!', null)), $replies);
  609. }
  610. }
  611. ?>