PredisClientFeatures.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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. $client = new \Predis\Client(array($params1, $params2, $params3, $params4));
  327. foreach ($client->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. }
  336. /* Client + CommandPipeline */
  337. function testCommandPipeline_Simple() {
  338. $client = RC::getConnection();
  339. $client->flushdb();
  340. $pipe = $client->pipeline();
  341. $this->assertType('\Predis\CommandPipeline', $pipe);
  342. $this->assertType('\Predis\CommandPipeline', $pipe->set('foo', 'bar'));
  343. $this->assertType('\Predis\CommandPipeline', $pipe->set('hoge', 'piyo'));
  344. $this->assertType('\Predis\CommandPipeline', $pipe->mset(array(
  345. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  346. )));
  347. $this->assertType('\Predis\CommandPipeline', $pipe->mget(array(
  348. 'foo', 'hoge', 'foofoo', 'hogehoge'
  349. )));
  350. $replies = $pipe->execute();
  351. $this->assertType('array', $replies);
  352. $this->assertEquals(4, count($replies));
  353. $this->assertEquals(4, count($replies[3]));
  354. $this->assertEquals('barbar', $replies[3][2]);
  355. }
  356. function testCommandPipeline_FluentInterface() {
  357. $client = RC::getConnection();
  358. $client->flushdb();
  359. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  360. $this->assertType('array', $replies);
  361. $this->assertEquals('bar', $replies[2]);
  362. }
  363. function testCommandPipeline_CallableAnonymousBlock() {
  364. $client = RC::getConnection();
  365. $client->flushdb();
  366. $replies = $client->pipeline(function($pipe) {
  367. $pipe->ping();
  368. $pipe->set('foo', 'bar');
  369. $pipe->get('foo');
  370. });
  371. $this->assertType('array', $replies);
  372. $this->assertEquals('bar', $replies[2]);
  373. }
  374. function testCommandPipeline_ClientExceptionInCallableBlock() {
  375. $client = RC::getConnection();
  376. $client->flushdb();
  377. RC::testForClientException($this, 'TEST', function() use($client) {
  378. $client->pipeline(function($pipe) {
  379. $pipe->ping();
  380. $pipe->set('foo', 'bar');
  381. throw new \Predis\ClientException("TEST");
  382. });
  383. });
  384. $this->assertFalse($client->exists('foo'));
  385. }
  386. function testCommandPipeline_ServerExceptionInCallableBlock() {
  387. $client = RC::getConnection();
  388. $client->flushdb();
  389. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  390. $replies = $client->pipeline(function($pipe) {
  391. $pipe->set('foo', 'bar');
  392. $pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  393. $pipe->set('hoge', 'piyo');
  394. });
  395. $this->assertType('array', $replies);
  396. $this->assertType('\Predis\ResponseError', $replies[1]);
  397. $this->assertTrue($client->exists('foo'));
  398. $this->assertTrue($client->exists('hoge'));
  399. }
  400. function testCommandPipeline_Flush() {
  401. $client = RC::getConnection();
  402. $client->flushdb();
  403. $pipe = $client->pipeline();
  404. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  405. $pipe->flushPipeline();
  406. $pipe->ping()->mget(array('foo', 'hoge'));
  407. $replies = $pipe->execute();
  408. $this->assertType('array', $replies);
  409. $this->assertEquals(4, count($replies));
  410. $this->assertEquals('bar', $replies[3][0]);
  411. $this->assertEquals('piyo', $replies[3][1]);
  412. }
  413. /* Client + MultiExecBlock */
  414. function testMultiExecBlock_Simple() {
  415. $client = RC::getConnection();
  416. $client->flushdb();
  417. $multi = $client->multiExec();
  418. $this->assertType('\Predis\MultiExecBlock', $multi);
  419. $this->assertType('\Predis\MultiExecBlock', $multi->set('foo', 'bar'));
  420. $this->assertType('\Predis\MultiExecBlock', $multi->set('hoge', 'piyo'));
  421. $this->assertType('\Predis\MultiExecBlock', $multi->mset(array(
  422. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  423. )));
  424. $this->assertType('\Predis\MultiExecBlock', $multi->mget(array(
  425. 'foo', 'hoge', 'foofoo', 'hogehoge'
  426. )));
  427. $replies = $multi->execute();
  428. $this->assertType('array', $replies);
  429. $this->assertEquals(4, count($replies));
  430. $this->assertEquals(4, count($replies[3]));
  431. $this->assertEquals('barbar', $replies[3][2]);
  432. }
  433. function testMultiExecBlock_FluentInterface() {
  434. $client = RC::getConnection();
  435. $client->flushdb();
  436. $replies = $client->multiExec()->ping()->set('foo', 'bar')->get('foo')->execute();
  437. $this->assertType('array', $replies);
  438. $this->assertEquals('bar', $replies[2]);
  439. }
  440. function testMultiExecBlock_CallableAnonymousBlock() {
  441. $client = RC::getConnection();
  442. $client->flushdb();
  443. $replies = $client->multiExec(function($multi) {
  444. $multi->ping();
  445. $multi->set('foo', 'bar');
  446. $multi->get('foo');
  447. });
  448. $this->assertType('array', $replies);
  449. $this->assertEquals('bar', $replies[2]);
  450. }
  451. function testMultiExecBlock_EmptyCallableBlock() {
  452. $client = RC::getConnection();
  453. $client->flushdb();
  454. $replies = $client->multiExec(function($multi) { });
  455. $this->assertEquals(0, count($replies));
  456. $options = array('cas' => true);
  457. $replies = $client->multiExec($options, function($multi) { });
  458. $this->assertEquals(0, count($replies));
  459. $options = array('cas' => true);
  460. $replies = $client->multiExec($options, function($multi) {
  461. $multi->multi();
  462. });
  463. $this->assertEquals(0, count($replies));
  464. }
  465. function testMultiExecBlock_ClientExceptionInCallableBlock() {
  466. $client = RC::getConnection();
  467. $client->flushdb();
  468. RC::testForClientException($this, 'TEST', function() use($client) {
  469. $client->multiExec(function($multi) {
  470. $multi->ping();
  471. $multi->set('foo', 'bar');
  472. throw new \Predis\ClientException("TEST");
  473. });
  474. });
  475. $this->assertFalse($client->exists('foo'));
  476. }
  477. function testMultiExecBlock_ServerExceptionInCallableBlock() {
  478. $client = RC::getConnection();
  479. $client->flushdb();
  480. $client->getResponseReader()->setHandler('-', new \Predis\ResponseErrorSilentHandler());
  481. $replies = $client->multiExec(function($multi) {
  482. $multi->set('foo', 'bar');
  483. $multi->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  484. $multi->set('hoge', 'piyo');
  485. });
  486. $this->assertType('array', $replies);
  487. $this->assertType('\Predis\ResponseError', $replies[1]);
  488. $this->assertTrue($client->exists('foo'));
  489. $this->assertTrue($client->exists('hoge'));
  490. }
  491. function testMultiExecBlock_Discard() {
  492. $client = RC::getConnection();
  493. $client->flushdb();
  494. $replies = $client->multiExec(function($multi) {
  495. $multi->set('foo', 'bar');
  496. $multi->discard();
  497. $multi->set('hoge', 'piyo');
  498. });
  499. $this->assertEquals(1, count($replies));
  500. $this->assertFalse($client->exists('foo'));
  501. $this->assertTrue($client->exists('hoge'));
  502. }
  503. function testMultiExecBlock_DiscardEmpty() {
  504. $client = RC::getConnection();
  505. $client->flushdb();
  506. $replies = $client->multiExec(function($multi) {
  507. $multi->discard();
  508. });
  509. $this->assertEquals(0, count($replies));
  510. }
  511. function testMultiExecBlock_Watch() {
  512. $client1 = RC::getConnection();
  513. $client2 = RC::getConnection(true);
  514. $client1->flushdb();
  515. RC::testForAbortedMultiExecException($this, function()
  516. use($client1, $client2) {
  517. $client1->multiExec(array('watch' => 'sentinel'), function($multi)
  518. use ($client2) {
  519. $multi->set('sentinel', 'client1');
  520. $multi->get('sentinel');
  521. $client2->set('sentinel', 'client2');
  522. });
  523. });
  524. $this->assertEquals('client2', $client1->get('sentinel'));
  525. }
  526. function testMultiExecBlock_CheckAndSet() {
  527. $client = RC::getConnection();
  528. $client->flushdb();
  529. $client->set('foo', 'bar');
  530. $options = array('watch' => 'foo', 'cas' => true);
  531. $replies = $client->multiExec($options, function($tx) {
  532. $tx->watch('foobar');
  533. $foo = $tx->get('foo');
  534. $tx->multi();
  535. $tx->set('foobar', $foo);
  536. $tx->mget('foo', 'foobar');
  537. });
  538. $this->assertType('array', $replies);
  539. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  540. $tx = $client->multiExec($options);
  541. $tx->watch('foobar');
  542. $foo = $tx->get('foo');
  543. $replies = $tx->multi()
  544. ->set('foobar', $foo)
  545. ->mget('foo', 'foobar')
  546. ->execute();
  547. $this->assertType('array', $replies);
  548. $this->assertEquals(array(true, array('bar', 'bar')), $replies);
  549. }
  550. function testMultiExecBlock_RetryOnServerAbort() {
  551. $client1 = RC::getConnection();
  552. $client2 = RC::getConnection(true);
  553. $client1->flushdb();
  554. $retry = 3;
  555. $attempts = 0;
  556. RC::testForAbortedMultiExecException($this, function()
  557. use($client1, $client2, $retry, &$attempts) {
  558. $options = array('watch' => 'sentinel', 'retry' => $retry);
  559. $client1->multiExec($options, function($tx)
  560. use ($client2, &$attempts) {
  561. $attempts++;
  562. $tx->set('sentinel', 'client1');
  563. $tx->get('sentinel');
  564. $client2->set('sentinel', 'client2');
  565. });
  566. });
  567. $this->assertEquals('client2', $client1->get('sentinel'));
  568. $this->assertEquals($retry + 1, $attempts);
  569. $retry = 3;
  570. $attempts = 0;
  571. RC::testForAbortedMultiExecException($this, function()
  572. use($client1, $client2, $retry, &$attempts) {
  573. $options = array(
  574. 'watch' => 'sentinel',
  575. 'cas' => true,
  576. 'retry' => $retry
  577. );
  578. $client1->multiExec($options, function($tx)
  579. use ($client2, &$attempts) {
  580. $attempts++;
  581. $tx->incr('attempts');
  582. $tx->multi();
  583. $tx->set('sentinel', 'client1');
  584. $tx->get('sentinel');
  585. $client2->set('sentinel', 'client2');
  586. });
  587. });
  588. $this->assertEquals('client2', $client1->get('sentinel'));
  589. $this->assertEquals($retry + 1, $attempts);
  590. $this->assertEquals($attempts, $client1->get('attempts'));
  591. }
  592. function testMultiExecBlock_CheckAndSet_Discard() {
  593. $client = RC::getConnection();
  594. $client->flushdb();
  595. $client->set('foo', 'bar');
  596. $options = array('watch' => 'foo', 'cas' => true);
  597. $replies = $client->multiExec($options, function($tx) {
  598. $tx->watch('foobar');
  599. $foo = $tx->get('foo');
  600. $tx->multi();
  601. $tx->set('foobar', $foo);
  602. $tx->discard();
  603. $tx->mget('foo', 'foobar');
  604. });
  605. $this->assertType('array', $replies);
  606. $this->assertEquals(array(array('bar', null)), $replies);
  607. $hijack = true;
  608. $client->set('foo', 'bar');
  609. $client2 = RC::getConnection(true);
  610. $options = array('watch' => 'foo', 'cas' => true, 'retry' => 1);
  611. $replies = $client->multiExec($options, function($tx)
  612. use ($client2, &$hijack) {
  613. $foo = $tx->get('foo');
  614. $tx->multi();
  615. $tx->set('foobar', $foo);
  616. $tx->discard();
  617. if ($hijack) {
  618. $hijack = false;
  619. $client2->set('foo', 'hijacked!');
  620. }
  621. $tx->mget('foo', 'foobar');
  622. });
  623. $this->assertType('array', $replies);
  624. $this->assertEquals(array(array('hijacked!', null)), $replies);
  625. }
  626. }
  627. ?>