PredisClientFeatures.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. <?php
  2. require_once 'PredisShared.php';
  3. require_once '../lib/addons/RedisVersion1_0.php';
  4. class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
  5. public $redis;
  6. protected function setUp() {
  7. $this->redis = RC::getConnection();
  8. $this->redis->flushdb();
  9. }
  10. protected function tearDown() {
  11. }
  12. protected function onNotSuccessfulTest(Exception $exception) {
  13. // drops and reconnect to a redis server on uncaught exceptions
  14. RC::resetConnection();
  15. parent::onNotSuccessfulTest($exception);
  16. }
  17. /* ConnectionParameters */
  18. function testConnectionParametersDefaultValues() {
  19. $params = new \Predis\ConnectionParameters();
  20. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_HOST, $params->host);
  21. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_PORT, $params->port);
  22. $this->assertEquals(\Predis\ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout);
  23. $this->assertNull($params->read_write_timeout);
  24. $this->assertNull($params->database);
  25. $this->assertNull($params->password);
  26. $this->assertNull($params->alias);
  27. }
  28. function testConnectionParametersSetupValuesArray() {
  29. $paramsArray = RC::getConnectionParametersArgumentsArray();
  30. $params = new \Predis\ConnectionParameters($paramsArray);
  31. $this->assertEquals($paramsArray['host'], $params->host);
  32. $this->assertEquals($paramsArray['port'], $params->port);
  33. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  34. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  35. $this->assertEquals($paramsArray['database'], $params->database);
  36. $this->assertEquals($paramsArray['password'], $params->password);
  37. $this->assertEquals($paramsArray['alias'], $params->alias);
  38. }
  39. function testConnectionParametersSetupValuesString() {
  40. $paramsArray = RC::getConnectionParametersArgumentsArray();
  41. $paramsString = RC::getConnectionParametersArgumentsString($paramsArray);
  42. $params = new \Predis\ConnectionParameters($paramsArray);
  43. $this->assertEquals($paramsArray['host'], $params->host);
  44. $this->assertEquals($paramsArray['port'], $params->port);
  45. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  46. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  47. $this->assertEquals($paramsArray['database'], $params->database);
  48. $this->assertEquals($paramsArray['password'], $params->password);
  49. $this->assertEquals($paramsArray['alias'], $params->alias);
  50. }
  51. /* Command and derivates */
  52. function testCommand_TestArguments() {
  53. $cmdArgs = array('key1', 'key2', 'key3');
  54. $cmd = new \Predis\Commands\GetMultiple();
  55. $cmd->setArgumentsArray($cmdArgs);
  56. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  57. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  58. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  59. $cmd = new \Predis\Commands\GetMultiple();
  60. $cmd->setArguments('key1', 'key2', 'key3');
  61. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  62. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  63. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  64. $cmd = new \Predis\Commands\Ping();
  65. $this->assertNull($cmd->getArgument(0));
  66. }
  67. function testCommand_InlineWithNoArguments() {
  68. $cmd = new \Predis\Compatibility\v1_0\Commands\Ping();
  69. $this->assertType('\Predis\InlineCommand', $cmd);
  70. $this->assertEquals('PING', $cmd->getCommandId());
  71. $this->assertFalse($cmd->closesConnection());
  72. $this->assertFalse($cmd->canBeHashed());
  73. $this->assertNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  74. $this->assertEquals("PING\r\n", $cmd->serialize());
  75. }
  76. function testCommand_InlineWithArguments() {
  77. $cmd = new \Predis\Compatibility\v1_0\Commands\Get();
  78. $cmd->setArgumentsArray(array('key'));
  79. $this->assertType('\Predis\InlineCommand', $cmd);
  80. $this->assertEquals('GET', $cmd->getCommandId());
  81. $this->assertFalse($cmd->closesConnection());
  82. $this->assertTrue($cmd->canBeHashed());
  83. $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  84. $this->assertEquals("GET key\r\n", $cmd->serialize());
  85. }
  86. function testCommand_BulkWithArguments() {
  87. $cmd = new \Predis\Compatibility\v1_0\Commands\Set();
  88. $cmd->setArgumentsArray(array('key', 'value'));
  89. $this->assertType('\Predis\BulkCommand', $cmd);
  90. $this->assertEquals('SET', $cmd->getCommandId());
  91. $this->assertFalse($cmd->closesConnection());
  92. $this->assertTrue($cmd->canBeHashed());
  93. $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  94. $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd->serialize());
  95. }
  96. function testCommand_MultiBulkWithArguments() {
  97. $cmd = new \Predis\Commands\SetMultiple();
  98. $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2'));
  99. $this->assertType('\Predis\Command', $cmd);
  100. $this->assertEquals('MSET', $cmd->getCommandId());
  101. $this->assertFalse($cmd->closesConnection());
  102. $this->assertFalse($cmd->canBeHashed());
  103. $this->assertNotNull($cmd->getHash(new \Predis\Distribution\HashRing()));
  104. $this->assertEquals("*5\r\n$4\r\nMSET\r\n$4\r\nkey1\r\n$6\r\nvalue1\r\n$4\r\nkey2\r\n$6\r\nvalue2\r\n", $cmd->serialize());
  105. }
  106. function testCommand_ParseResponse() {
  107. // default parser
  108. $cmd = new \Predis\Commands\Get();
  109. $this->assertEquals('test', $cmd->parseResponse('test'));
  110. // overridden parser (boolean)
  111. $cmd = new \Predis\Commands\Exists();
  112. $this->assertTrue($cmd->parseResponse('1'));
  113. $this->assertFalse($cmd->parseResponse('0'));
  114. // overridden parser (boolean)
  115. $cmd = new \Predis\Commands\Ping();
  116. $this->assertTrue($cmd->parseResponse('PONG'));
  117. // overridden parser (complex)
  118. // TODO: emulate a respons to INFO
  119. }
  120. /* RedisServerProfile and derivates */
  121. function testRedisServerProfile_GetSpecificVersions() {
  122. $this->assertType('\Predis\RedisServer_v1_0', \Predis\RedisServerProfile::get('1.0'));
  123. $this->assertType('\Predis\RedisServer_v1_2', \Predis\RedisServerProfile::get('1.2'));
  124. $this->assertType('\Predis\RedisServer_v2_0', \Predis\RedisServerProfile::get('2.0'));
  125. $this->assertType('\Predis\RedisServer_vNext', \Predis\RedisServerProfile::get('dev'));
  126. $this->assertType('\Predis\RedisServerProfile', \Predis\RedisServerProfile::get('default'));
  127. $this->assertEquals(\Predis\RedisServerProfile::get('default'), \Predis\RedisServerProfile::getDefault());
  128. }
  129. function testRedisServerProfile_SupportedCommands() {
  130. $profile_10 = \Predis\RedisServerProfile::get('1.0');
  131. $profile_12 = \Predis\RedisServerProfile::get('1.2');
  132. $this->assertTrue($profile_10->supportsCommand('info'));
  133. $this->assertTrue($profile_12->supportsCommand('info'));
  134. $this->assertFalse($profile_10->supportsCommand('mset'));
  135. $this->assertTrue($profile_12->supportsCommand('mset'));
  136. $this->assertFalse($profile_10->supportsCommand('multi'));
  137. $this->assertFalse($profile_12->supportsCommand('multi'));
  138. }
  139. function testRedisServerProfile_CommandsCreation() {
  140. $profile = \Predis\RedisServerProfile::get('1.0');
  141. $cmdNoArgs = $profile->createCommand('info');
  142. $this->assertType('\Predis\Compatibility\v1_0\Commands\Info', $cmdNoArgs);
  143. $this->assertNull($cmdNoArgs->getArgument());
  144. $args = array('key1', 'key2');
  145. $cmdWithArgs = $profile->createCommand('mget', $args);
  146. $this->assertType('\Predis\Compatibility\v1_0\Commands\GetMultiple', $cmdWithArgs);
  147. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  148. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  149. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  150. $bogusCommand = 'not_existing_command';
  151. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  152. RC::testForClientException($this, $expectedMessage, function()
  153. use($profile, $bogusCommand) {
  154. $profile->createCommand($bogusCommand);
  155. });
  156. }
  157. function testRedisServerProfile_CommandsRegistration() {
  158. $profile = \Predis\RedisServerProfile::get('1.0');
  159. $cmdId = 'mset';
  160. $cmdClass = '\Predis\Commands\SetMultiple';
  161. $this->assertFalse($profile->supportsCommand($cmdId));
  162. $profile->registerCommand(new $cmdClass(), $cmdId);
  163. $this->assertTrue($profile->supportsCommand($cmdId));
  164. $this->assertType($cmdClass, $profile->createCommand($cmdId));
  165. }
  166. /* ResponseQueued */
  167. function testResponseQueued() {
  168. $response = new \Predis\ResponseQueued();
  169. $this->assertTrue($response->queued);
  170. $this->assertEquals(\Predis\Protocol::QUEUED, (string)$response);
  171. }
  172. /* ResponseError */
  173. function testResponseError() {
  174. $errorMessage = 'ERROR MESSAGE';
  175. $response = new \Predis\ResponseError($errorMessage);
  176. $this->assertTrue($response->error);
  177. $this->assertEquals($errorMessage, $response->message);
  178. $this->assertEquals($errorMessage, (string)$response);
  179. }
  180. /* Connection */
  181. function testConnection_StringCastReturnsIPAndPort() {
  182. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  183. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  184. }
  185. function testConnection_ConnectDisconnect() {
  186. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  187. $this->assertFalse($connection->isConnected());
  188. $connection->connect();
  189. $this->assertTrue($connection->isConnected());
  190. $connection->disconnect();
  191. $this->assertFalse($connection->isConnected());
  192. }
  193. function testConnection_WriteAndReadCommand() {
  194. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  195. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  196. $connection->connect();
  197. $connection->writeCommand($cmd);
  198. $this->assertTrue($connection->readResponse($cmd));
  199. }
  200. function testConnection_WriteCommandAndCloseConnection() {
  201. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('quit');
  202. $connection = new \Predis\TcpConnection(new \Predis\ConnectionParameters(
  203. RC::getConnectionArguments() + array('read_write_timeout' => 0.5)
  204. ));
  205. $connection->connect();
  206. $this->assertTrue($connection->isConnected());
  207. $connection->writeCommand($cmd);
  208. $connection->disconnect();
  209. $exceptionMessage = 'Error while reading line from the server';
  210. RC::testForCommunicationException($this, $exceptionMessage, function() use($connection, $cmd) {
  211. $connection->readResponse($cmd);
  212. });
  213. }
  214. function testConnection_GetSocketOpensConnection() {
  215. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  216. $this->assertFalse($connection->isConnected());
  217. $this->assertType('resource', $connection->getSocket());
  218. $this->assertTrue($connection->isConnected());
  219. }
  220. function testConnection_LazyConnect() {
  221. $cmd = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  222. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  223. $this->assertFalse($connection->isConnected());
  224. $connection->writeCommand($cmd);
  225. $this->assertTrue($connection->isConnected());
  226. $this->assertTrue($connection->readResponse($cmd));
  227. }
  228. function testConnection_RawCommand() {
  229. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  230. $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
  231. }
  232. function testConnection_Alias() {
  233. $connection1 = new \Predis\TcpConnection(RC::getConnectionParameters());
  234. $this->assertNull($connection1->getParameters()->alias);
  235. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  236. $connection2 = new \Predis\TcpConnection(new \Predis\ConnectionParameters($args));
  237. $this->assertEquals('servername', $connection2->getParameters()->alias);
  238. }
  239. function testConnection_ConnectionTimeout() {
  240. $timeout = 3;
  241. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  242. $connection = new \Predis\TcpConnection(new \Predis\ConnectionParameters($args));
  243. $start = time();
  244. RC::testForCommunicationException($this, null, function() use($connection) {
  245. $connection->connect();
  246. });
  247. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  248. }
  249. function testConnection_ReadTimeout() {
  250. $timeout = 1;
  251. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  252. $cmdFake = \Predis\RedisServerProfile::getDefault()->createCommand('ping');
  253. $connection = new \Predis\TcpConnection(new \Predis\ConnectionParameters($args));
  254. $expectedMessage = 'Error while reading line from the server';
  255. $start = time();
  256. RC::testForCommunicationException($this, $expectedMessage, function() use($connection, $cmdFake) {
  257. $connection->readResponse($cmdFake);
  258. });
  259. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  260. }
  261. /* ResponseReader */
  262. function testResponseReader_OptionIterableMultiBulkReplies() {
  263. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  264. $responseReader = $connection->getResponseReader();
  265. $responseReader->setHandler(
  266. \Predis\Protocol::PREFIX_MULTI_BULK,
  267. new \Predis\ResponseMultiBulkHandler()
  268. );
  269. $this->assertType('array', $connection->rawCommand("KEYS *\r\n"));
  270. $responseReader->setHandler(
  271. \Predis\Protocol::PREFIX_MULTI_BULK,
  272. new \Predis\ResponseMultiBulkStreamHandler()
  273. );
  274. $this->assertType('\Iterator', $connection->rawCommand("KEYS *\r\n"));
  275. }
  276. function testResponseReader_OptionExceptionOnError() {
  277. $connection = new \Predis\TcpConnection(RC::getConnectionParameters());
  278. $responseReader = $connection->getResponseReader();
  279. $connection->rawCommand("*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n");
  280. $rawCmdUnexpected = "*3\r\n$5\r\nLPUSH\r\n$3\r\nkey\r\n$5\r\nvalue\r\n";
  281. $responseReader->setHandler(
  282. \Predis\Protocol::PREFIX_ERROR,
  283. new \Predis\ResponseErrorSilentHandler()
  284. );
  285. $errorReply = $connection->rawCommand($rawCmdUnexpected);
  286. $this->assertType('\Predis\ResponseError', $errorReply);
  287. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
  288. $responseReader->setHandler(
  289. \Predis\Protocol::PREFIX_ERROR,
  290. new \Predis\ResponseErrorHandler()
  291. );
  292. RC::testForServerException($this, RC::EXCEPTION_WRONG_TYPE, function()
  293. use ($connection, $rawCmdUnexpected) {
  294. $connection->rawCommand($rawCmdUnexpected);
  295. });
  296. }
  297. /* Client initialization */
  298. function testClientInitialization_SingleConnectionParameters() {
  299. $params1 = array_merge(RC::getConnectionArguments(), array(
  300. 'connection_timeout' => 10,
  301. 'read_write_timeout' => 30,
  302. 'alias' => 'connection_alias',
  303. ));
  304. $params2 = RC::getConnectionParametersArgumentsString($params1);
  305. $params3 = new \Predis\ConnectionParameters($params1);
  306. $params4 = new \Predis\TcpConnection($params3);
  307. foreach (array($params1, $params2, $params3, $params4) as $params) {
  308. $client = new \Predis\Client($params);
  309. $parameters = $client->getConnection()->getParameters();
  310. $this->assertEquals($params1['host'], $parameters->host);
  311. $this->assertEquals($params1['port'], $parameters->port);
  312. $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
  313. $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
  314. $this->assertEquals($params1['alias'], $parameters->alias);
  315. $this->assertNull($parameters->password);
  316. }
  317. }
  318. function testClientInitialization_ClusterConnectionParameters() {
  319. $params1 = array_merge(RC::getConnectionArguments(), array(
  320. 'connection_timeout' => 10,
  321. 'read_write_timeout' => 30,
  322. ));
  323. $params2 = RC::getConnectionParametersArgumentsString($params1);
  324. $params3 = new \Predis\ConnectionParameters($params1);
  325. $params4 = new \Predis\TcpConnection($params3);
  326. $client1 = new \Predis\Client(array($params1, $params2, $params3, $params4));
  327. foreach ($client1->getConnection() as $connection) {
  328. $parameters = $connection->getParameters();
  329. $this->assertEquals($params1['host'], $parameters->host);
  330. $this->assertEquals($params1['port'], $parameters->port);
  331. $this->assertEquals($params1['connection_timeout'], $parameters->connection_timeout);
  332. $this->assertEquals($params1['read_write_timeout'], $parameters->read_write_timeout);
  333. $this->assertNull($parameters->password);
  334. }
  335. $connectionCluster = $client1->getConnection();
  336. $client2 = new \Predis\Client($connectionCluster);
  337. $this->assertSame($connectionCluster, $client2->getConnection());
  338. }
  339. /* Client + CommandPipeline */
  340. function testCommandPipeline_Simple() {
  341. $client = RC::getConnection();
  342. $client->flushdb();
  343. $pipe = $client->pipeline();
  344. $this->assertType('\Predis\CommandPipeline', $pipe);
  345. $this->assertType('\Predis\CommandPipeline', $pipe->set('foo', 'bar'));
  346. $this->assertType('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo'));
  347. $this->assertType('\Predis\CommandPipeline', $pipe->mset(array(
  348. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  349. )));
  350. $this->assertType('\Predis\CommandPipeline', $pipe->mget(array(
  351. 'foo', 'hoge', 'foofoo', 'hogehoge'
  352. )));
  353. $replies = $pipe->execute();
  354. $this->assertType('array', $replies);
  355. $this->assertEquals(4, count($replies));
  356. $this->assertEquals(4, count($replies[3]));
  357. $this->assertEquals('barbar', $replies[3][2]);
  358. }
  359. function testCommandPipeline_FluentInterface() {
  360. $client = RC::getConnection();
  361. $client->flushdb();
  362. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  363. $this->assertType('array', $replies);
  364. $this->assertEquals('bar', $replies[2]);
  365. }
  366. function testCommandPipeline_CallableAnonymousBlock() {
  367. $client = RC::getConnection();
  368. $client->flushdb();
  369. $replies = $client->pipeline(function($pipe) {
  370. $pipe->ping();
  371. $pipe->set('foo', 'bar');
  372. $pipe->get('foo');
  373. });
  374. $this->assertType('array', $replies);
  375. $this->assertEquals('bar', $replies[2]);
  376. }
  377. function testCommandPipeline_ClientExceptionInCallableBlock() {
  378. $client = RC::getConnection();
  379. $client->flushdb();
  380. RC::testForClientException($this, 'TEST', function() use($client) {
  381. $client->pipeline(function($pipe) {
  382. $pipe->ping();
  383. $pipe->set('foo', 'bar');
  384. throw new \Predis\ClientException("TEST");
  385. });
  386. });
  387. $this->assertFalse($client->exists('foo'));
  388. }
  389. function testCommandPipeline_ServerExceptionInCallableBlock() {
  390. $client = RC::getConnection();
  391. $client->flushdb();
  392. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  393. $replies = $client->pipeline(function($pipe) {
  394. $pipe->set('foo', 'bar');
  395. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  396. $pipe->set('hoge', 'piyo');
  397. });
  398. $this->assertType('array', $replies);
  399. $this->assertType('\Predis\ResponseError', $replies[1]);
  400. $this->assertTrue($client->exists('foo'));
  401. $this->assertTrue($client->exists('hoge'));
  402. }
  403. function testCommandPipeline_Flush() {
  404. $client = RC::getConnection();
  405. $client->flushdb();
  406. $pipe = $client->pipeline();
  407. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  408. $pipe->flushPipeline();
  409. $pipe->ping()->mget(array('foo', 'hoge'));
  410. $replies = $pipe->execute();
  411. $this->assertType('array', $replies);
  412. $this->assertEquals(4, count($replies));
  413. $this->assertEquals('bar', $replies[3][0]);
  414. $this->assertEquals('piyo', $replies[3][1]);
  415. }
  416. /* Client + MultiExecBlock */
  417. function testMultiExecBlock_Simple() {
  418. $client = RC::getConnection();
  419. $client->flushdb();
  420. $multi = $client->multiExec();
  421. $this->assertType('\Predis\MultiExecBlock', $multi);
  422. $this->assertType('\Predis\MultiExecBlock', $multi->set('foo', 'bar'));
  423. $this->assertType('\Predis\MultiExecBlock', $multi->set('hoge', 'piyo'));
  424. $this->assertType('\Predis\MultiExecBlock', $multi->mset(array(
  425. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  426. )));
  427. $this->assertType('\Predis\MultiExecBlock', $multi->mget(array(
  428. 'foo', 'hoge', 'foofoo', 'hogehoge'
  429. )));
  430. $replies = $multi->execute();
  431. $this->assertType('array', $replies);
  432. $this->assertEquals(4, count($replies));
  433. $this->assertEquals(4, count($replies[3]));
  434. $this->assertEquals('barbar', $replies[3][2]);
  435. }
  436. function testMultiExecBlock_FluentInterface() {
  437. $client = RC::getConnection();
  438. $client->flushdb();
  439. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  440. $this->assertType('array', $replies);
  441. $this->assertEquals('bar', $replies[2]);
  442. }
  443. function testMultiExecBlock_CallableAnonymousBlock() {
  444. $client = RC::getConnection();
  445. $client->flushdb();
  446. $replies = $client->multiExec(function($multi) {
  447. $multi->ping();
  448. $multi->set('foo', 'bar');
  449. $multi->get('foo');
  450. });
  451. $this->assertType('array', $replies);
  452. $this->assertEquals('bar', $replies[2]);
  453. }
  454. /**
  455. * @expectedException Predis\ClientException
  456. */
  457. function testMultiExecBlock_CannotMixFluentInterfaceAndAnonymousBlock() {
  458. $emptyBlock = function($tx) { };
  459. $tx = RC::getConnection()->multiExec()->get('foo')->execute($emptyBlock);
  460. }
  461. function testMultiExecBlock_EmptyCallableBlock() {
  462. $client = RC::getConnection();
  463. $client->flushdb();
  464. $replies = $client->multiExec(function($multi) { });
  465. $this->assertEquals(0, count($replies));
  466. $options = array('cas' => true);
  467. $replies = $client->multiExec($options, function($multi) { });
  468. $this->assertEquals(0, count($replies));
  469. $options = array('cas' => true);
  470. $replies = $client->multiExec($options, function($multi) {
  471. $multi->multi();
  472. });
  473. $this->assertEquals(0, count($replies));
  474. }
  475. function testMultiExecBlock_ClientExceptionInCallableBlock() {
  476. $client = RC::getConnection();
  477. $client->flushdb();
  478. RC::testForClientException($this, 'TEST', function() use($client) {
  479. $client->multiExec(function($multi) {
  480. $multi->ping();
  481. $multi->set('foo', 'bar');
  482. throw new \Predis\ClientException("TEST");
  483. });
  484. });
  485. $this->assertFalse($client->exists('foo'));
  486. }
  487. function testMultiExecBlock_ServerExceptionInCallableBlock() {
  488. $client = RC::getConnection();
  489. $client->flushdb();
  490. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  491. $replies = $client->multiExec(function($multi) {
  492. $multi->set('foo', 'bar');
  493. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  494. $multi->set('hoge', 'piyo');
  495. });
  496. $this->assertType('array', $replies);
  497. $this->assertType('\Predis\ResponseError', $replies[1]);
  498. $this->assertTrue($client->exists('foo'));
  499. $this->assertTrue($client->exists('hoge'));
  500. }
  501. function testMultiExecBlock_Discard() {
  502. $client = RC::getConnection();
  503. $client->flushdb();
  504. $replies = $client->multiExec(function($multi) {
  505. $multi->set('foo', 'bar');
  506. $multi->discard();
  507. $multi->set('hoge', 'piyo');
  508. });
  509. $this->assertEquals(1, count($replies));
  510. $this->assertFalse($client->exists('foo'));
  511. $this->assertTrue($client->exists('hoge'));
  512. }
  513. function testMultiExecBlock_DiscardEmpty() {
  514. $client = RC::getConnection();
  515. $client->flushdb();
  516. $replies = $client->multiExec(function($multi) {
  517. $multi->discard();
  518. });
  519. $this->assertEquals(0, count($replies));
  520. }
  521. function testMultiExecBlock_Watch() {
  522. $client1 = RC::getConnection();
  523. $client2 = RC::getConnection(true);
  524. $client1->flushdb();
  525. RC::testForAbortedMultiExecException($this, function()
  526. use($client1, $client2) {
  527. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  528. use ($client2) {
  529. $multi->set('sentinel', 'client1');
  530. $multi->get('sentinel');
  531. $client2->set('sentinel', 'client2');
  532. });
  533. });
  534. $this->assertEquals('client2', $client1->get('sentinel'));
  535. }
  536. function testMultiExecBlock_CheckAndSet() {
  537. $client = RC::getConnection();
  538. $client->flushdb();
  539. $client->set('foo', 'bar');
  540. $options = array('watch' => 'foo', 'cas' => true);
  541. $replies = $client->multiExec($options, function($tx) {
  542. $tx->watch('foobar');
  543. $foo = $tx->get('foo');
  544. $tx->multi();
  545. $tx->set('foobar', $foo);
  546. $tx->mget('foo', 'foobar');
  547. });
  548. $this->assertType('array', $replies);
  549. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  550. $tx = $client->multiExec($options);
  551. $tx->watch('foobar');
  552. $foo = $tx->get('foo');
  553. $replies = $tx->multi()
  554. ->set('foobar', $foo)
  555. ->mget('foo', 'foobar')
  556. ->execute();
  557. $this->assertType('array', $replies);
  558. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  559. }
  560. function testMultiExecBlock_RetryOnServerAbort() {
  561. $client1 = RC::getConnection();
  562. $client2 = RC::getConnection(true);
  563. $client1->flushdb();
  564. $retry = 3;
  565. $attempts = 0;
  566. RC::testForAbortedMultiExecException($this, function()
  567. use($client1, $client2, $retry, &$attempts) {
  568. $options = array('watch' => 'sentinel', 'retry' => $retry);
  569. $client1->multiExec($options, function($tx)
  570. use ($client2, &$attempts) {
  571. $attempts++;
  572. $tx->set('sentinel', 'client1');
  573. $tx->get('sentinel');
  574. $client2->set('sentinel', 'client2');
  575. });
  576. });
  577. $this->assertEquals('client2', $client1->get('sentinel'));
  578. $this->assertEquals($retry + 1, $attempts);
  579. $retry = 3;
  580. $attempts = 0;
  581. RC::testForAbortedMultiExecException($this, function()
  582. use($client1, $client2, $retry, &$attempts) {
  583. $options = array(
  584. 'watch' => 'sentinel',
  585. 'cas' => true,
  586. 'retry' => $retry
  587. );
  588. $client1->multiExec($options, function($tx)
  589. use ($client2, &$attempts) {
  590. $attempts++;
  591. $tx->incr('attempts');
  592. $tx->multi();
  593. $tx->set('sentinel', 'client1');
  594. $tx->get('sentinel');
  595. $client2->set('sentinel', 'client2');
  596. });
  597. });
  598. $this->assertEquals('client2', $client1->get('sentinel'));
  599. $this->assertEquals($retry + 1, $attempts);
  600. $this->assertEquals($attempts, $client1->get('attempts'));
  601. }
  602. /**
  603. * @expectedException InvalidArgumentException
  604. */
  605. function testMultiExecBlock_RetryNotAvailableWithoutBlock() {
  606. $options = array('watch' => 'foo', 'retry' => 1);
  607. $tx = RC::getConnection()->multiExec($options);
  608. $tx->multi()->get('foo')->exec();
  609. }
  610. function testMultiExecBlock_CheckAndSet_Discard() {
  611. $client = RC::getConnection();
  612. $client->flushdb();
  613. $client->set('foo', 'bar');
  614. $options = array('watch' => 'foo', 'cas' => true);
  615. $replies = $client->multiExec($options, function($tx) {
  616. $tx->watch('foobar');
  617. $foo = $tx->get('foo');
  618. $tx->multi();
  619. $tx->set('foobar', $foo);
  620. $tx->discard();
  621. $tx->mget('foo', 'foobar');
  622. });
  623. $this->assertType('array', $replies);
  624. $this->assertEquals(array(array('bar', null)), $replies);
  625. $hijack = true;
  626. $client->set('foo', 'bar');
  627. $client2 = RC::getConnection(true);
  628. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  629. $replies = $client->multiExec($options, function($tx)
  630. use ($client2, &$hijack) {
  631. $foo = $tx->get('foo');
  632. $tx->multi();
  633. $tx->set('foobar', $foo);
  634. $tx->discard();
  635. if ($hijack) {
  636. $hijack = false;
  637. $client2->set('foo', 'hijacked!');
  638. }
  639. $tx->mget('foo', 'foobar');
  640. });
  641. $this->assertType('array', $replies);
  642. $this->assertEquals(array(array('hijacked!', null)), $replies);
  643. }
  644. }
  645. ?>