ClientFeaturesTest.php 29 KB

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