ClientFeaturesTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  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. $this->assertInstanceOf('\Predis\PipelineContext', $pipe);
  311. $this->assertInstanceOf('\Predis\PipelineContext', $pipe->set('foo', 'bar'));
  312. $this->assertInstanceOf('\Predis\PipelineContext', $pipe->set('hoge', 'piyo'));
  313. $this->assertInstanceOf('\Predis\PipelineContext', $pipe->mset(array(
  314. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  315. )));
  316. $this->assertInstanceOf('\Predis\PipelineContext', $pipe->mget(array(
  317. 'foo', 'hoge', 'foofoo', 'hogehoge'
  318. )));
  319. $replies = $pipe->execute();
  320. $this->assertInternalType('array', $replies);
  321. $this->assertEquals(4, count($replies));
  322. $this->assertEquals(4, count($replies[3]));
  323. $this->assertEquals('barbar', $replies[3][2]);
  324. }
  325. function testPipelineContext_FluentInterface() {
  326. $client = RC::getConnection();
  327. $client->flushdb();
  328. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  329. $this->assertInternalType('array', $replies);
  330. $this->assertEquals('bar', $replies[2]);
  331. }
  332. function testPipelineContext_CallableAnonymousBlock() {
  333. $client = RC::getConnection();
  334. $client->flushdb();
  335. $replies = $client->pipeline(function($pipe) {
  336. $pipe->ping();
  337. $pipe->set('foo', 'bar');
  338. $pipe->get('foo');
  339. });
  340. $this->assertInternalType('array', $replies);
  341. $this->assertEquals('bar', $replies[2]);
  342. }
  343. function testPipelineContext_ClientExceptionInCallableBlock() {
  344. $client = RC::getConnection();
  345. $client->flushdb();
  346. RC::testForClientException($this, 'TEST', function() use($client) {
  347. $client->pipeline(function($pipe) {
  348. $pipe->ping();
  349. $pipe->set('foo', 'bar');
  350. throw new \Predis\ClientException("TEST");
  351. });
  352. });
  353. $this->assertFalse($client->exists('foo'));
  354. }
  355. function testPipelineContext_ServerExceptionInCallableBlock() {
  356. $client = RC::createConnection(array('throw_errors' => false));
  357. $client->flushdb();
  358. $replies = $client->pipeline(function($pipe) {
  359. $pipe->set('foo', 'bar');
  360. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  361. $pipe->set('hoge', 'piyo');
  362. });
  363. $this->assertInternalType('array', $replies);
  364. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  365. $this->assertTrue($client->exists('foo'));
  366. $this->assertTrue($client->exists('hoge'));
  367. }
  368. function testPipelineContext_Flush() {
  369. $client = RC::getConnection();
  370. $client->flushdb();
  371. $pipe = $client->pipeline();
  372. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  373. $pipe->flushPipeline();
  374. $pipe->ping()->mget(array('foo', 'hoge'));
  375. $replies = $pipe->execute();
  376. $this->assertInternalType('array', $replies);
  377. $this->assertEquals(4, count($replies));
  378. $this->assertEquals('bar', $replies[3][0]);
  379. $this->assertEquals('piyo', $replies[3][1]);
  380. }
  381. /* Predis\Client + Predis\MultiExecContext */
  382. function testMultiExecContext_Simple() {
  383. $client = RC::getConnection();
  384. $client->flushdb();
  385. $multi = $client->multiExec();
  386. $this->assertInstanceOf('\Predis\MultiExecContext', $multi);
  387. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->set('foo', 'bar'));
  388. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->set('hoge', 'piyo'));
  389. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->mset(array(
  390. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  391. )));
  392. $this->assertInstanceOf('\Predis\MultiExecContext', $multi->mget(array(
  393. 'foo', 'hoge', 'foofoo', 'hogehoge'
  394. )));
  395. $replies = $multi->execute();
  396. $this->assertInternalType('array', $replies);
  397. $this->assertEquals(4, count($replies));
  398. $this->assertEquals(4, count($replies[3]));
  399. $this->assertEquals('barbar', $replies[3][2]);
  400. }
  401. function testMultiExecContext_FluentInterface() {
  402. $client = RC::getConnection();
  403. $client->flushdb();
  404. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  405. $this->assertInternalType('array', $replies);
  406. $this->assertEquals('bar', $replies[2]);
  407. }
  408. function testMultiExecContext_CallableAnonymousBlock() {
  409. $client = RC::getConnection();
  410. $client->flushdb();
  411. $replies = $client->multiExec(function($multi) {
  412. $multi->ping();
  413. $multi->set('foo', 'bar');
  414. $multi->get('foo');
  415. });
  416. $this->assertInternalType('array', $replies);
  417. $this->assertEquals('bar', $replies[2]);
  418. }
  419. /**
  420. * @expectedException Predis\ClientException
  421. */
  422. function testMultiExecContext_CannotMixFluentInterfaceAndAnonymousBlock() {
  423. $emptyBlock = function($tx) { };
  424. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  425. }
  426. function testMultiExecContext_EmptyCallableBlock() {
  427. $client = RC::getConnection();
  428. $client->flushdb();
  429. $replies = $client->multiExec(function($multi) { });
  430. $this->assertEquals(0, count($replies));
  431. $options = array('cas' => true);
  432. $replies = $client->multiExec($options, function($multi) { });
  433. $this->assertEquals(0, count($replies));
  434. $options = array('cas' => true);
  435. $replies = $client->multiExec($options, function($multi) {
  436. $multi->multi();
  437. });
  438. $this->assertEquals(0, count($replies));
  439. }
  440. function testMultiExecContext_ClientExceptionInCallableBlock() {
  441. $client = RC::getConnection();
  442. $client->flushdb();
  443. RC::testForClientException($this, 'TEST', function() use($client) {
  444. $client->multiExec(function($multi) {
  445. $multi->ping();
  446. $multi->set('foo', 'bar');
  447. throw new \Predis\ClientException("TEST");
  448. });
  449. });
  450. $this->assertFalse($client->exists('foo'));
  451. }
  452. function testMultiExecContext_ServerExceptionInCallableBlock() {
  453. $client = RC::createConnection(array('throw_errors' => false));
  454. $client->flushdb();
  455. $replies = $client->multiExec(function($multi) {
  456. $multi->set('foo', 'bar');
  457. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  458. $multi->set('hoge', 'piyo');
  459. });
  460. $this->assertInternalType('array', $replies);
  461. $this->assertInstanceOf('\Predis\ResponseError', $replies[1]);
  462. $this->assertTrue($client->exists('foo'));
  463. $this->assertTrue($client->exists('hoge'));
  464. }
  465. function testMultiExecContext_Discard() {
  466. $client = RC::getConnection();
  467. $client->flushdb();
  468. $replies = $client->multiExec(function($multi) {
  469. $multi->set('foo', 'bar');
  470. $multi->discard();
  471. $multi->set('hoge', 'piyo');
  472. });
  473. $this->assertEquals(1, count($replies));
  474. $this->assertFalse($client->exists('foo'));
  475. $this->assertTrue($client->exists('hoge'));
  476. }
  477. function testMultiExecContext_DiscardEmpty() {
  478. $client = RC::getConnection();
  479. $client->flushdb();
  480. $replies = $client->multiExec(function($multi) {
  481. $multi->discard();
  482. });
  483. $this->assertEquals(0, count($replies));
  484. }
  485. function testMultiExecContext_Watch() {
  486. $client1 = RC::getConnection();
  487. $client2 = RC::getConnection(true);
  488. $client1->flushdb();
  489. RC::testForAbortedMultiExecException($this, function()
  490. use($client1, $client2) {
  491. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  492. use ($client2) {
  493. $multi->set('sentinel', 'client1');
  494. $multi->get('sentinel');
  495. $client2->set('sentinel', 'client2');
  496. });
  497. });
  498. $this->assertEquals('client2', $client1->get('sentinel'));
  499. }
  500. function testMultiExecContext_CheckAndSet() {
  501. $client = RC::getConnection();
  502. $client->flushdb();
  503. $client->set('foo', 'bar');
  504. $options = array('watch' => 'foo', 'cas' => true);
  505. $replies = $client->multiExec($options, function($tx) {
  506. $tx->watch('foobar');
  507. $foo = $tx->get('foo');
  508. $tx->multi();
  509. $tx->set('foobar', $foo);
  510. $tx->mget('foo', 'foobar');
  511. });
  512. $this->assertInternalType('array', $replies);
  513. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  514. $tx = $client->multiExec($options);
  515. $tx->watch('foobar');
  516. $foo = $tx->get('foo');
  517. $replies = $tx->multi()
  518. ->set('foobar', $foo)
  519. ->mget('foo', 'foobar')
  520. ->execute();
  521. $this->assertInternalType('array', $replies);
  522. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  523. }
  524. function testMultiExecContext_RetryOnServerAbort() {
  525. $client1 = RC::getConnection();
  526. $client2 = RC::getConnection(true);
  527. $client1->flushdb();
  528. $retry = 3;
  529. $attempts = 0;
  530. RC::testForAbortedMultiExecException($this, function()
  531. use($client1, $client2, $retry, &$attempts) {
  532. $options = array('watch' => 'sentinel', 'retry' => $retry);
  533. $client1->multiExec($options, function($tx)
  534. use ($client2, &$attempts) {
  535. $attempts++;
  536. $tx->set('sentinel', 'client1');
  537. $tx->get('sentinel');
  538. $client2->set('sentinel', 'client2');
  539. });
  540. });
  541. $this->assertEquals('client2', $client1->get('sentinel'));
  542. $this->assertEquals($retry + 1, $attempts);
  543. $retry = 3;
  544. $attempts = 0;
  545. RC::testForAbortedMultiExecException($this, function()
  546. use($client1, $client2, $retry, &$attempts) {
  547. $options = array(
  548. 'watch' => 'sentinel',
  549. 'cas' => true,
  550. 'retry' => $retry
  551. );
  552. $client1->multiExec($options, function($tx)
  553. use ($client2, &$attempts) {
  554. $attempts++;
  555. $tx->incr('attempts');
  556. $tx->multi();
  557. $tx->set('sentinel', 'client1');
  558. $tx->get('sentinel');
  559. $client2->set('sentinel', 'client2');
  560. });
  561. });
  562. $this->assertEquals('client2', $client1->get('sentinel'));
  563. $this->assertEquals($retry + 1, $attempts);
  564. $this->assertEquals($attempts, $client1->get('attempts'));
  565. }
  566. /**
  567. * @expectedException InvalidArgumentException
  568. */
  569. function testMultiExecContext_RetryNotAvailableWithoutBlock() {
  570. $options = array('watch' => 'foo', 'retry' => 1);
  571. $tx = RC::getConnection()->multiExec($options);
  572. $tx->multi()->get('foo')->exec();
  573. }
  574. function testMultiExecContext_CheckAndSet_Discard() {
  575. $client = RC::getConnection();
  576. $client->flushdb();
  577. $client->set('foo', 'bar');
  578. $options = array('watch' => 'foo', 'cas' => true);
  579. $replies = $client->multiExec($options, function($tx) {
  580. $tx->watch('foobar');
  581. $foo = $tx->get('foo');
  582. $tx->multi();
  583. $tx->set('foobar', $foo);
  584. $tx->discard();
  585. $tx->mget('foo', 'foobar');
  586. });
  587. $this->assertInternalType('array', $replies);
  588. $this->assertEquals(array(array('bar', null)), $replies);
  589. $hijack = true;
  590. $client->set('foo', 'bar');
  591. $client2 = RC::getConnection(true);
  592. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  593. $replies = $client->multiExec($options, function($tx)
  594. use ($client2, &$hijack) {
  595. $foo = $tx->get('foo');
  596. $tx->multi();
  597. $tx->set('foobar', $foo);
  598. $tx->discard();
  599. if ($hijack) {
  600. $hijack = false;
  601. $client2->set('foo', 'hijacked!');
  602. }
  603. $tx->mget('foo', 'foobar');
  604. });
  605. $this->assertInternalType('array', $replies);
  606. $this->assertEquals(array(array('hijacked!', null)), $replies);
  607. }
  608. }
  609. ?>