ClientFeaturesTest.php 29 KB

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