PredisClientFeatures.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <?php
  2. define('I_AM_AWARE_OF_THE_DESTRUCTIVE_POWER_OF_THIS_TEST_SUITE', false);
  3. require_once 'PHPUnit/Framework.php';
  4. require_once 'PredisShared.php';
  5. require_once '../lib/Predis_Compatibility.php';
  6. class PredisClientFeaturesTestSuite extends PHPUnit_Framework_TestCase {
  7. public $redis;
  8. protected function setUp() {
  9. $this->redis = RC::getConnection();
  10. $this->redis->flushDatabase();
  11. }
  12. protected function tearDown() {
  13. }
  14. protected function onNotSuccessfulTest(Exception $exception) {
  15. // drops and reconnect to a redis server on uncaught exceptions
  16. RC::resetConnection();
  17. parent::onNotSuccessfulTest($exception);
  18. }
  19. /* ConnectionParameters */
  20. function testConnectionParametersDefaultValues() {
  21. $params = new Predis_ConnectionParameters();
  22. $this->assertEquals(Predis_ConnectionParameters::DEFAULT_HOST, $params->host);
  23. $this->assertEquals(Predis_ConnectionParameters::DEFAULT_PORT, $params->port);
  24. $this->assertEquals(Predis_ConnectionParameters::DEFAULT_TIMEOUT, $params->connection_timeout);
  25. $this->assertNull($params->read_write_timeout);
  26. $this->assertNull($params->database);
  27. $this->assertNull($params->password);
  28. $this->assertNull($params->alias);
  29. }
  30. function testConnectionParametersSetupValuesArray() {
  31. $paramsArray = RC::getConnectionParametersArgumentsArray();
  32. $params = new Predis_ConnectionParameters($paramsArray);
  33. $this->assertEquals($paramsArray['host'], $params->host);
  34. $this->assertEquals($paramsArray['port'], $params->port);
  35. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  36. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  37. $this->assertEquals($paramsArray['database'], $params->database);
  38. $this->assertEquals($paramsArray['password'], $params->password);
  39. $this->assertEquals($paramsArray['alias'], $params->alias);
  40. }
  41. function testConnectionParametersSetupValuesString() {
  42. $paramsArray = RC::getConnectionParametersArgumentsArray();
  43. $paramsString = RC::getConnectionParametersArgumentsString($paramsArray);
  44. $params = new Predis_ConnectionParameters($paramsArray);
  45. $this->assertEquals($paramsArray['host'], $params->host);
  46. $this->assertEquals($paramsArray['port'], $params->port);
  47. $this->assertEquals($paramsArray['connection_timeout'], $params->connection_timeout);
  48. $this->assertEquals($paramsArray['read_write_timeout'], $params->read_write_timeout);
  49. $this->assertEquals($paramsArray['database'], $params->database);
  50. $this->assertEquals($paramsArray['password'], $params->password);
  51. $this->assertEquals($paramsArray['alias'], $params->alias);
  52. }
  53. /* Command and derivates */
  54. function testCommand_TestArguments() {
  55. $cmdArgs = array('key1', 'key2', 'key3');
  56. $cmd = new Predis_Commands_GetMultiple();
  57. $cmd->setArgumentsArray($cmdArgs);
  58. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  59. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  60. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  61. $cmd = new Predis_Commands_GetMultiple();
  62. $cmd->setArguments('key1', 'key2', 'key3');
  63. $this->assertEquals($cmdArgs[0], $cmd->getArgument(0));
  64. $this->assertEquals($cmdArgs[1], $cmd->getArgument(1));
  65. $this->assertEquals($cmdArgs[2], $cmd->getArgument(2));
  66. $cmd = new Predis_Commands_Ping();
  67. $this->assertNull($cmd->getArgument(0));
  68. }
  69. function testCommand_InlineWithNoArguments() {
  70. $cmd = new Predis_Compatibility_v1_0_Commands_Ping();
  71. $this->assertType('Predis_InlineCommand', $cmd);
  72. $this->assertEquals('PING', $cmd->getCommandId());
  73. $this->assertFalse($cmd->closesConnection());
  74. $this->assertFalse($cmd->canBeHashed());
  75. $this->assertNull($cmd->getHash(new Predis_Distribution_HashRing()));
  76. $this->assertEquals("PING\r\n", $cmd->invoke());
  77. }
  78. function testCommand_InlineWithArguments() {
  79. $cmd = new Predis_Compatibility_v1_0_Commands_Get();
  80. $cmd->setArgumentsArray(array('key'));
  81. $this->assertType('Predis_InlineCommand', $cmd);
  82. $this->assertEquals('GET', $cmd->getCommandId());
  83. $this->assertFalse($cmd->closesConnection());
  84. $this->assertTrue($cmd->canBeHashed());
  85. $this->assertNotNull($cmd->getHash(new Predis_Distribution_HashRing()));
  86. $this->assertEquals("GET key\r\n", $cmd->invoke());
  87. }
  88. function testCommand_BulkWithArguments() {
  89. $cmd = new Predis_Compatibility_v1_0_Commands_Set();
  90. $cmd->setArgumentsArray(array('key', 'value'));
  91. $this->assertType('Predis_BulkCommand', $cmd);
  92. $this->assertEquals('SET', $cmd->getCommandId());
  93. $this->assertFalse($cmd->closesConnection());
  94. $this->assertTrue($cmd->canBeHashed());
  95. $this->assertNotNull($cmd->getHash(new Predis_Distribution_HashRing()));
  96. $this->assertEquals("SET key 5\r\nvalue\r\n", $cmd->invoke());
  97. }
  98. function testCommand_MultiBulkWithArguments() {
  99. $cmd = new Predis_Commands_SetMultiple();
  100. $cmd->setArgumentsArray(array('key1', 'value1', 'key2', 'value2'));
  101. $this->assertType('Predis_MultiBulkCommand', $cmd);
  102. $this->assertEquals('MSET', $cmd->getCommandId());
  103. $this->assertFalse($cmd->closesConnection());
  104. $this->assertFalse($cmd->canBeHashed());
  105. $this->assertNotNull($cmd->getHash(new Predis_Distribution_HashRing()));
  106. $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->invoke());
  107. }
  108. function testCommand_ParseResponse() {
  109. // default parser
  110. $cmd = new Predis_Commands_Get();
  111. $this->assertEquals('test', $cmd->parseResponse('test'));
  112. // overridden parser (boolean)
  113. $cmd = new Predis_Commands_Exists();
  114. $this->assertTrue($cmd->parseResponse('1'));
  115. $this->assertFalse($cmd->parseResponse('0'));
  116. // overridden parser (boolean)
  117. $cmd = new Predis_Commands_Ping();
  118. $this->assertTrue($cmd->parseResponse('PONG'));
  119. // overridden parser (complex)
  120. // TODO: emulate a respons to INFO
  121. }
  122. /* RedisServerProfile and derivates */
  123. function testRedisServerProfile_GetSpecificVersions() {
  124. $this->assertType('Predis_RedisServer_v1_0', Predis_RedisServerProfile::get('1.0'));
  125. $this->assertType('Predis_RedisServer_v1_2', Predis_RedisServerProfile::get('1.2'));
  126. $this->assertType('Predis_RedisServer_v2_0', Predis_RedisServerProfile::get('2.0'));
  127. $this->assertType('Predis_RedisServer_vNext', Predis_RedisServerProfile::get('dev'));
  128. $this->assertType('Predis_RedisServerProfile', Predis_RedisServerProfile::get('default'));
  129. $this->assertEquals(Predis_RedisServerProfile::get('default'), Predis_RedisServerProfile::getDefault());
  130. }
  131. function testRedisServerProfile_SupportedCommands() {
  132. $profile_10 = Predis_RedisServerProfile::get('1.0');
  133. $profile_12 = Predis_RedisServerProfile::get('1.2');
  134. $this->assertTrue($profile_10->supportsCommand('info'));
  135. $this->assertTrue($profile_12->supportsCommand('info'));
  136. $this->assertFalse($profile_10->supportsCommand('mset'));
  137. $this->assertTrue($profile_12->supportsCommand('mset'));
  138. $this->assertFalse($profile_10->supportsCommand('multi'));
  139. $this->assertFalse($profile_12->supportsCommand('multi'));
  140. }
  141. function testRedisServerProfile_CommandsCreation() {
  142. $profile = Predis_RedisServerProfile::get('1.0');
  143. $cmdNoArgs = $profile->createCommand('info');
  144. $this->assertType('Predis_Compatibility_v1_0_Commands_Info', $cmdNoArgs);
  145. $this->assertNull($cmdNoArgs->getArgument());
  146. $args = array('key1', 'key2');
  147. $cmdWithArgs = $profile->createCommand('mget', $args);
  148. $this->assertType('Predis_Compatibility_v1_0_Commands_GetMultiple', $cmdWithArgs);
  149. $this->assertEquals($args[0], $cmdWithArgs->getArgument()); // TODO: why?
  150. $this->assertEquals($args[0], $cmdWithArgs->getArgument(0));
  151. $this->assertEquals($args[1], $cmdWithArgs->getArgument(1));
  152. $bogusCommand = 'not_existing_command';
  153. $expectedMessage = "'$bogusCommand' is not a registered Redis command";
  154. RC::testForClientException($this, $expectedMessage, p_anon("\$test", "
  155. \$profile = Predis_RedisServerProfile::getDefault();
  156. \$profile->createCommand('$bogusCommand');
  157. "));
  158. }
  159. function testRedisServerProfile_CommandsRegistration() {
  160. $profile = Predis_RedisServerProfile::get('1.0');
  161. $cmdId = 'mset';
  162. $cmdClass = 'Predis_Commands_SetMultiple';
  163. $this->assertFalse($profile->supportsCommand($cmdId));
  164. $profile->registerCommand(new $cmdClass(), $cmdId);
  165. $this->assertTrue($profile->supportsCommand($cmdId));
  166. $this->assertType($cmdClass, $profile->createCommand($cmdId));
  167. }
  168. /* ResponseQueued */
  169. function testResponseQueued() {
  170. $response = new Predis_ResponseQueued();
  171. $this->assertTrue($response->queued);
  172. $this->assertEquals(Predis_Protocol::QUEUED, (string)$response);
  173. }
  174. /* ResponseError */
  175. function testResponseError() {
  176. $errorMessage = 'ERROR MESSAGE';
  177. $response = new Predis_ResponseError($errorMessage);
  178. $this->assertTrue($response->error);
  179. $this->assertEquals($errorMessage, $response->message);
  180. $this->assertEquals($errorMessage, (string)$response);
  181. }
  182. /* Connection */
  183. function testConnection_StringCastReturnsIPAndPort() {
  184. $connection = new Predis_Connection(RC::getConnectionParameters());
  185. $this->assertEquals(RC::SERVER_HOST . ':' . RC::SERVER_PORT, (string) $connection);
  186. }
  187. function testConnection_ConnectDisconnect() {
  188. $connection = new Predis_Connection(RC::getConnectionParameters());
  189. $this->assertFalse($connection->isConnected());
  190. $connection->connect();
  191. $this->assertTrue($connection->isConnected());
  192. $connection->disconnect();
  193. $this->assertFalse($connection->isConnected());
  194. }
  195. function testConnection_WriteAndReadCommand() {
  196. $cmd = Predis_RedisServerProfile::getDefault()->createCommand('ping');
  197. $connection = new Predis_Connection(RC::getConnectionParameters());
  198. $connection->connect();
  199. $connection->writeCommand($cmd);
  200. $this->assertTrue($connection->readResponse($cmd));
  201. }
  202. function testConnection_WriteCommandAndCloseConnection() {
  203. $cmd = Predis_RedisServerProfile::getDefault()->createCommand('quit');
  204. $connection = new Predis_Connection(RC::getConnectionParameters());
  205. $connection->connect();
  206. $this->assertTrue($connection->isConnected());
  207. $connection->writeCommand($cmd);
  208. $expectedMessage = 'Error while reading line from the server';
  209. $thrownException = null;
  210. try {
  211. $connection->readResponse($cmd);
  212. }
  213. catch (Predis_CommunicationException $exception) {
  214. $thrownException = $exception;
  215. }
  216. $this->assertType('Predis_CommunicationException', $thrownException);
  217. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  218. //$this->assertFalse($connection->isConnected());
  219. }
  220. function testConnection_GetSocketOpensConnection() {
  221. $connection = new Predis_Connection(RC::getConnectionParameters());
  222. $this->assertFalse($connection->isConnected());
  223. $this->assertType('resource', $connection->getSocket());
  224. $this->assertTrue($connection->isConnected());
  225. }
  226. function testConnection_LazyConnect() {
  227. $cmd = Predis_RedisServerProfile::getDefault()->createCommand('ping');
  228. $connection = new Predis_Connection(RC::getConnectionParameters());
  229. $this->assertFalse($connection->isConnected());
  230. $connection->writeCommand($cmd);
  231. $this->assertTrue($connection->isConnected());
  232. $this->assertTrue($connection->readResponse($cmd));
  233. }
  234. function testConnection_RawCommand() {
  235. $connection = new Predis_Connection(RC::getConnectionParameters());
  236. $this->assertEquals('PONG', $connection->rawCommand("PING\r\n"));
  237. }
  238. function testConnection_Alias() {
  239. $connection1 = new Predis_Connection(RC::getConnectionParameters());
  240. $this->assertNull($connection1->getParameters()->alias);
  241. $args = array_merge(RC::getConnectionArguments(), array('alias' => 'servername'));
  242. $connection2 = new Predis_Connection(new Predis_ConnectionParameters($args));
  243. $this->assertEquals('servername', $connection2->getParameters()->alias);
  244. }
  245. function testConnection_ConnectionTimeout() {
  246. $timeout = 3;
  247. $args = array('host' => '1.0.0.1', 'connection_timeout' => $timeout);
  248. $connection = new Predis_Connection(new Predis_ConnectionParameters($args));
  249. $start = time();
  250. $thrownException = null;
  251. try {
  252. $connection->connect();
  253. }
  254. catch (Predis_CommunicationException $exception) {
  255. $thrownException = $exception;
  256. }
  257. $this->assertType('Predis_CommunicationException', $thrownException);
  258. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  259. }
  260. function testConnection_ReadTimeout() {
  261. $timeout = 1;
  262. $args = array_merge(RC::getConnectionArguments(), array('read_write_timeout' => $timeout));
  263. $cmdFake = Predis_RedisServerProfile::getDefault()->createCommand('ping');
  264. $connection = new Predis_Connection(new Predis_ConnectionParameters($args));
  265. $expectedMessage = 'Error while reading line from the server';
  266. $start = time();
  267. $thrownException = null;
  268. try {
  269. $connection->readResponse($cmdFake);
  270. }
  271. catch (Predis_CommunicationException $exception) {
  272. $thrownException = $exception;
  273. }
  274. $this->assertType('Predis_CommunicationException', $thrownException);
  275. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  276. $this->assertEquals((float)(time() - $start), $timeout, '', 1);
  277. }
  278. /* ResponseReader */
  279. function testResponseReader_OptionIterableMultiBulkReplies() {
  280. $connection = new Predis_Connection(RC::getConnectionParameters());
  281. $responseReader = $connection->getResponseReader();
  282. $responseReader->setHandler(
  283. Predis_Protocol::PREFIX_MULTI_BULK,
  284. new Predis_ResponseMultiBulkHandler()
  285. );
  286. $this->assertType('array', $connection->rawCommand("KEYS *\r\n"));
  287. $responseReader->setHandler(
  288. Predis_Protocol::PREFIX_MULTI_BULK,
  289. new Predis_ResponseMultiBulkStreamHandler()
  290. );
  291. $this->assertType('Iterator', $connection->rawCommand("KEYS *\r\n"));
  292. }
  293. function testResponseReader_OptionExceptionOnError() {
  294. $connection = new Predis_Connection(RC::getConnectionParameters());
  295. $responseReader = $connection->getResponseReader();
  296. $connection->rawCommand("SET key 5\r\nvalue\r\n");
  297. $rawCmdUnexpected = "LPUSH key 5\r\nvalue\r\n";
  298. $responseReader->setHandler(
  299. Predis_Protocol::PREFIX_ERROR,
  300. new Predis_ResponseErrorSilentHandler()
  301. );
  302. $errorReply = $connection->rawCommand($rawCmdUnexpected);
  303. $this->assertType('Predis_ResponseError', $errorReply);
  304. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $errorReply->message);
  305. $responseReader->setHandler(
  306. Predis_Protocol::PREFIX_ERROR,
  307. new Predis_ResponseErrorHandler()
  308. );
  309. $thrownException = null;
  310. try {
  311. $connection->rawCommand($rawCmdUnexpected);
  312. }
  313. catch (Predis_ServerException $exception) {
  314. $thrownException = $exception;
  315. }
  316. $this->assertType('Predis_ServerException', $thrownException);
  317. $this->assertEquals(RC::EXCEPTION_WRONG_TYPE, $thrownException->getMessage());
  318. }
  319. /* Client + CommandPipeline */
  320. function testCommandPipeline_Simple() {
  321. $client = RC::getConnection();
  322. $client->flushdb();
  323. $pipe = $client->pipeline();
  324. $this->assertType('Predis_CommandPipeline', $pipe);
  325. $this->assertType('Predis_CommandPipeline', $pipe->set('foo', 'bar'));
  326. $this->assertType('Predis_CommandPipeline', $pipe->set('hoge', 'piyo'));
  327. $this->assertType('Predis_CommandPipeline', $pipe->mset(array(
  328. 'foofoo' => 'barbar', 'hogehoge' => 'piyopiyo'
  329. )));
  330. $this->assertType('Predis_CommandPipeline', $pipe->mget(array(
  331. 'foo', 'hoge', 'foofoo', 'hogehoge'
  332. )));
  333. $replies = $pipe->execute();
  334. $this->assertType('array', $replies);
  335. $this->assertEquals(4, count($replies));
  336. $this->assertEquals(4, count($replies[3]));
  337. $this->assertEquals('barbar', $replies[3][2]);
  338. }
  339. function testCommandPipeline_FluentInterface() {
  340. $client = RC::getConnection();
  341. $client->flushdb();
  342. $replies = $client->pipeline()->ping()->set('foo', 'bar')->get('foo')->execute();
  343. $this->assertType('array', $replies);
  344. $this->assertEquals('bar', $replies[2]);
  345. }
  346. function testCommandPipeline_CallableAnonymousBlock() {
  347. $client = RC::getConnection();
  348. $client->flushdb();
  349. $replies = $client->pipeline(p_anon("\$pipe", "
  350. \$pipe->ping();
  351. \$pipe->set('foo', 'bar');
  352. \$pipe->get('foo');
  353. "));
  354. $this->assertType('array', $replies);
  355. $this->assertEquals('bar', $replies[2]);
  356. }
  357. function testCommandPipeline_ClientExceptionInCallableBlock() {
  358. $client = RC::getConnection();
  359. $client->flushdb();
  360. $expectedMessage = 'TEST';
  361. $thrownException = null;
  362. try {
  363. $client->pipeline(p_anon("\$pipe", "
  364. \$pipe->ping();
  365. \$pipe->set('foo', 'bar');
  366. throw new Predis_ClientException('$expectedMessage');
  367. "));
  368. }
  369. catch (Predis_ClientException $exception) {
  370. $thrownException = $exception;
  371. }
  372. $this->assertType('Predis_ClientException', $thrownException);
  373. $this->assertEquals($expectedMessage, $thrownException->getMessage());
  374. $this->assertFalse($client->exists('foo'));
  375. }
  376. function testCommandPipeline_ServerExceptionInCallableBlock() {
  377. $client = RC::getConnection();
  378. $client->flushdb();
  379. $client->getResponseReader()->setHandler('-', new Predis_ResponseErrorSilentHandler());
  380. $replies = $client->pipeline(p_anon("\$pipe", "
  381. \$pipe->set('foo', 'bar');
  382. \$pipe->lpush('foo', 'piyo'); // LIST operation on STRING type returns an ERROR
  383. \$pipe->set('hoge', 'piyo');
  384. "));
  385. $this->assertType('array', $replies);
  386. $this->assertType('Predis_ResponseError', $replies[1]);
  387. $this->assertTrue($client->exists('foo'));
  388. $this->assertTrue($client->exists('hoge'));
  389. }
  390. function testCommandPipeline_Flush() {
  391. $client = RC::getConnection();
  392. $client->flushdb();
  393. $pipe = $client->pipeline();
  394. $pipe->set('foo', 'bar')->set('hoge', 'piyo');
  395. $pipe->flushPipeline();
  396. $pipe->ping()->mget(array('foo', 'hoge'));
  397. $replies = $pipe->execute();
  398. $this->assertType('array', $replies);
  399. $this->assertEquals(4, count($replies));
  400. $this->assertEquals('bar', $replies[3][0]);
  401. $this->assertEquals('piyo', $replies[3][1]);
  402. }
  403. }
  404. ?>