ClientFeaturesTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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\Protocols\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\Protocols\TextResponseReader */
  213. function testResponseReader_OptionIterableMultiBulkReplies() {
  214. $protocol = new Predis\Protocols\Text\ComposableTextProtocol();
  215. $reader = $protocol->getReader();
  216. $connection = new \Predis\Network\ComposableStreamConnection(RC::getConnectionParameters(), $protocol);
  217. $reader->setHandler(
  218. \Predis\Protocols\Text\TextProtocol::PREFIX_MULTI_BULK,
  219. new \Predis\Protocols\Text\ResponseMultiBulkHandler()
  220. );
  221. $connection->writeBytes("KEYS *\r\n");
  222. $this->assertInternalType('array', $reader->read($connection));
  223. $reader->setHandler(
  224. \Predis\Protocols\Text\TextProtocol::PREFIX_MULTI_BULK,
  225. new \Predis\Protocols\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\Protocols\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\Protocols\Text\TextProtocol::PREFIX_ERROR,
  239. new \Predis\Protocols\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\Protocols\Text\TextProtocol::PREFIX_ERROR,
  247. new \Predis\Protocols\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\Protocols\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. $this->assertInstanceOf('\Predis\MultiExecContext', $multi);
  388. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->set('foo', 'bar'));
  389. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->set('hoge', 'piyo'));
  390. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->mset(array(
  391. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  392. )));
  393. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->mget(array(
  394. 'foo', 'hoge', 'foofoo', 'hogehoge'
  395. )));
  396. $replies = $multi->execute();
  397. $this->assertInternalType('array', $replies);
  398. $this->assertEquals(4, count($replies));
  399. $this->assertEquals(4, count($replies[3]));
  400. $this->assertEquals('barbar', $replies[3][2]);
  401. }
  402. function testMultiExecContext_FluentInterface() {
  403. $client = RC::getConnection();
  404. $client->flushdb();
  405. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  406. $this->assertInternalType('array', $replies);
  407. $this->assertEquals('bar', $replies[2]);
  408. }
  409. function testMultiExecContext_CallableAnonymousBlock() {
  410. $client = RC::getConnection();
  411. $client->flushdb();
  412. $replies = $client->multiExec(function($multi) {
  413. $multi->ping();
  414. $multi->set('foo', 'bar');
  415. $multi->get('foo');
  416. });
  417. $this->assertInternalType('array', $replies);
  418. $this->assertEquals('bar', $replies[2]);
  419. }
  420. /**
  421. * @expectedException Predis\ClientException
  422. */
  423. function testMultiExecContext_CannotMixFluentInterfaceAndAnonymousBlock() {
  424. $emptyBlock = function($tx) { };
  425. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  426. }
  427. function testMultiExecContext_EmptyCallableBlock() {
  428. $client = RC::getConnection();
  429. $client->flushdb();
  430. $replies = $client->multiExec(function($multi) { });
  431. $this->assertEquals(0, count($replies));
  432. $options = array('cas' => true);
  433. $replies = $client->multiExec($options, function($multi) { });
  434. $this->assertEquals(0, count($replies));
  435. $options = array('cas' => true);
  436. $replies = $client->multiExec($options, function($multi) {
  437. $multi->multi();
  438. });
  439. $this->assertEquals(0, count($replies));
  440. }
  441. function testMultiExecContext_ClientExceptionInCallableBlock() {
  442. $client = RC::getConnection();
  443. $client->flushdb();
  444. RC::testForClientException($this, 'TEST', function() use($client) {
  445. $client->multiExec(function($multi) {
  446. $multi->ping();
  447. $multi->set('foo', 'bar');
  448. throw new \Predis\ClientException("TEST");
  449. });
  450. });
  451. $this->assertFalse($client->exists('foo'));
  452. }
  453. function testMultiExecContext_ServerExceptionInCallableBlock() {
  454. $client = RC::createConnection(array('throw_errors' => false));
  455. $client->flushdb();
  456. $replies = $client->multiExec(function($multi) {
  457. $multi->set('foo', 'bar');
  458. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  459. $multi->set('hoge', 'piyo');
  460. });
  461. $this->assertInternalType('array', $replies);
  462. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  463. $this->assertTrue($client->exists('foo'));
  464. $this->assertTrue($client->exists('hoge'));
  465. }
  466. function testMultiExecContext_Discard() {
  467. $client = RC::getConnection();
  468. $client->flushdb();
  469. $replies = $client->multiExec(function($multi) {
  470. $multi->set('foo', 'bar');
  471. $multi->discard();
  472. $multi->set('hoge', 'piyo');
  473. });
  474. $this->assertEquals(1, count($replies));
  475. $this->assertFalse($client->exists('foo'));
  476. $this->assertTrue($client->exists('hoge'));
  477. }
  478. function testMultiExecContext_DiscardEmpty() {
  479. $client = RC::getConnection();
  480. $client->flushdb();
  481. $replies = $client->multiExec(function($multi) {
  482. $multi->discard();
  483. });
  484. $this->assertEquals(0, count($replies));
  485. }
  486. function testMultiExecContext_Watch() {
  487. $client1 = RC::getConnection();
  488. $client2 = RC::getConnection(true);
  489. $client1->flushdb();
  490. RC::testForAbortedMultiExecException($this, function()
  491. use($client1, $client2) {
  492. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  493. use ($client2) {
  494. $multi->set('sentinel', 'client1');
  495. $multi->get('sentinel');
  496. $client2->set('sentinel', 'client2');
  497. });
  498. });
  499. $this->assertEquals('client2', $client1->get('sentinel'));
  500. }
  501. function testMultiExecContext_CheckAndSet() {
  502. $client = RC::getConnection();
  503. $client->flushdb();
  504. $client->set('foo', 'bar');
  505. $options = array('watch' => 'foo', 'cas' => true);
  506. $replies = $client->multiExec($options, function($tx) {
  507. $tx->watch('foobar');
  508. $foo = $tx->get('foo');
  509. $tx->multi();
  510. $tx->set('foobar', $foo);
  511. $tx->mget('foo', 'foobar');
  512. });
  513. $this->assertInternalType('array', $replies);
  514. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  515. $tx = $client->multiExec($options);
  516. $tx->watch('foobar');
  517. $foo = $tx->get('foo');
  518. $replies = $tx->multi()
  519. ->set('foobar', $foo)
  520. ->mget('foo', 'foobar')
  521. ->execute();
  522. $this->assertInternalType('array', $replies);
  523. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  524. }
  525. function testMultiExecContext_RetryOnServerAbort() {
  526. $client1 = RC::getConnection();
  527. $client2 = RC::getConnection(true);
  528. $client1->flushdb();
  529. $retry = 3;
  530. $attempts = 0;
  531. RC::testForAbortedMultiExecException($this, function()
  532. use($client1, $client2, $retry, &$attempts) {
  533. $options = array('watch' => 'sentinel', 'retry' => $retry);
  534. $client1->multiExec($options, function($tx)
  535. use ($client2, &$attempts) {
  536. $attempts++;
  537. $tx->set('sentinel', 'client1');
  538. $tx->get('sentinel');
  539. $client2->set('sentinel', 'client2');
  540. });
  541. });
  542. $this->assertEquals('client2', $client1->get('sentinel'));
  543. $this->assertEquals($retry + 1, $attempts);
  544. $retry = 3;
  545. $attempts = 0;
  546. RC::testForAbortedMultiExecException($this, function()
  547. use($client1, $client2, $retry, &$attempts) {
  548. $options = array(
  549. 'watch' => 'sentinel',
  550. 'cas' => true,
  551. 'retry' => $retry
  552. );
  553. $client1->multiExec($options, function($tx)
  554. use ($client2, &$attempts) {
  555. $attempts++;
  556. $tx->incr('attempts');
  557. $tx->multi();
  558. $tx->set('sentinel', 'client1');
  559. $tx->get('sentinel');
  560. $client2->set('sentinel', 'client2');
  561. });
  562. });
  563. $this->assertEquals('client2', $client1->get('sentinel'));
  564. $this->assertEquals($retry + 1, $attempts);
  565. $this->assertEquals($attempts, $client1->get('attempts'));
  566. }
  567. /**
  568. * @expectedException InvalidArgumentException
  569. */
  570. function testMultiExecContext_RetryNotAvailableWithoutBlock() {
  571. $options = array('watch' => 'foo', 'retry' => 1);
  572. $tx = RC::getConnection()->multiExec($options);
  573. $tx->multi()->get('foo')->exec();
  574. }
  575. function testMultiExecContext_CheckAndSet_Discard() {
  576. $client = RC::getConnection();
  577. $client->flushdb();
  578. $client->set('foo', 'bar');
  579. $options = array('watch' => 'foo', 'cas' => true);
  580. $replies = $client->multiExec($options, function($tx) {
  581. $tx->watch('foobar');
  582. $foo = $tx->get('foo');
  583. $tx->multi();
  584. $tx->set('foobar', $foo);
  585. $tx->discard();
  586. $tx->mget('foo', 'foobar');
  587. });
  588. $this->assertInternalType('array', $replies);
  589. $this->assertEquals(array(array('bar', null)), $replies);
  590. $hijack = true;
  591. $client->set('foo', 'bar');
  592. $client2 = RC::getConnection(true);
  593. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  594. $replies = $client->multiExec($options, function($tx)
  595. use ($client2, &$hijack) {
  596. $foo = $tx->get('foo');
  597. $tx->multi();
  598. $tx->set('foobar', $foo);
  599. $tx->discard();
  600. if ($hijack) {
  601. $hijack = false;
  602. $client2->set('foo', 'hijacked!');
  603. }
  604. $tx->mget('foo', 'foobar');
  605. });
  606. $this->assertInternalType('array', $replies);
  607. $this->assertEquals(array(array('hijacked!', null)), $replies);
  608. }
  609. }
  610. ?>