PredisClientFeatures.php 29 KB

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