RedisStrategyTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Cluster;
  11. use Predis\Profile;
  12. use PredisTestCase;
  13. /**
  14. *
  15. */
  16. class RedisStrategyTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testSupportsKeyTags()
  22. {
  23. $strategy = $this->getClusterStrategy();
  24. $this->assertSame(12182, $strategy->getSlotByKey('{foo}'));
  25. $this->assertSame(12182, $strategy->getSlotByKey('{foo}:bar'));
  26. $this->assertSame(12182, $strategy->getSlotByKey('{foo}:baz'));
  27. $this->assertSame(12182, $strategy->getSlotByKey('bar:{foo}:baz'));
  28. $this->assertSame(12182, $strategy->getSlotByKey('bar:{foo}:{baz}'));
  29. $this->assertSame(12182, $strategy->getSlotByKey('bar:{foo}:baz{}'));
  30. $this->assertSame(9415, $strategy->getSlotByKey('{}bar:{foo}:baz'));
  31. $this->assertSame(0, $strategy->getSlotByKey(''));
  32. $this->assertSame(15257, $strategy->getSlotByKey('{}'));
  33. }
  34. /**
  35. * @group disconnected
  36. */
  37. public function testSupportedCommands()
  38. {
  39. $strategy = $this->getClusterStrategy();
  40. $this->assertSame($this->getExpectedCommands(), $strategy->getSupportedCommands());
  41. }
  42. /**
  43. * @group disconnected
  44. */
  45. public function testReturnsNullOnUnsupportedCommand()
  46. {
  47. $strategy = $this->getClusterStrategy();
  48. $command = Profile\Factory::getDevelopment()->createCommand('ping');
  49. $this->assertNull($strategy->getSlot($command));
  50. }
  51. /**
  52. * @group disconnected
  53. */
  54. public function testFirstKeyCommands()
  55. {
  56. $strategy = $this->getClusterStrategy();
  57. $profile = Profile\Factory::getDevelopment();
  58. $arguments = array('key');
  59. foreach ($this->getExpectedCommands('keys-first') as $commandID) {
  60. $command = $profile->createCommand($commandID, $arguments);
  61. $this->assertNotNull($strategy->getSlot($command), $commandID);
  62. }
  63. }
  64. /**
  65. * @group disconnected
  66. */
  67. public function testAllKeysCommandsWithOneKey()
  68. {
  69. $strategy = $this->getClusterStrategy();
  70. $profile = Profile\Factory::getDevelopment();
  71. $arguments = array('key');
  72. foreach ($this->getExpectedCommands('keys-all') as $commandID) {
  73. $command = $profile->createCommand($commandID, $arguments);
  74. $this->assertNotNull($strategy->getSlot($command), $commandID);
  75. }
  76. }
  77. /**
  78. * @group disconnected
  79. */
  80. public function testAllKeysCommandsWithMoreKeys()
  81. {
  82. $strategy = $this->getClusterStrategy();
  83. $profile = Profile\Factory::getDevelopment();
  84. $arguments = array('key1', 'key2');
  85. foreach ($this->getExpectedCommands('keys-all') as $commandID) {
  86. $command = $profile->createCommand($commandID, $arguments);
  87. $this->assertNull($strategy->getSlot($command), $commandID);
  88. }
  89. }
  90. /**
  91. * @group disconnected
  92. */
  93. public function testInterleavedKeysCommandsWithOneKey()
  94. {
  95. $strategy = $this->getClusterStrategy();
  96. $profile = Profile\Factory::getDevelopment();
  97. $arguments = array('key:1', 'value1');
  98. foreach ($this->getExpectedCommands('keys-interleaved') as $commandID) {
  99. $command = $profile->createCommand($commandID, $arguments);
  100. $this->assertNotNull($strategy->getSlot($command), $commandID);
  101. }
  102. }
  103. /**
  104. * @group disconnected
  105. */
  106. public function testInterleavedKeysCommandsWithMoreKeys()
  107. {
  108. $strategy = $this->getClusterStrategy();
  109. $profile = Profile\Factory::getDevelopment();
  110. $arguments = array('key:1', 'value1', 'key:2', 'value2');
  111. foreach ($this->getExpectedCommands('keys-interleaved') as $commandID) {
  112. $command = $profile->createCommand($commandID, $arguments);
  113. $this->assertNull($strategy->getSlot($command), $commandID);
  114. }
  115. }
  116. /**
  117. * @group disconnected
  118. */
  119. public function testKeysForSortCommand()
  120. {
  121. $strategy = $this->getClusterStrategy();
  122. $profile = Profile\Factory::getDevelopment();
  123. $arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
  124. $commandID = 'SORT';
  125. $command = $profile->createCommand($commandID, array('{key}:1'));
  126. $this->assertNotNull($strategy->getSlot($command), $commandID);
  127. $command = $profile->createCommand($commandID, array('{key}:1', array('STORE' => '{key}:2')));
  128. $this->assertNotNull($strategy->getSlot($command), $commandID);
  129. }
  130. /**
  131. * @group disconnected
  132. */
  133. public function testKeysForBlockingListCommandsWithOneKey()
  134. {
  135. $strategy = $this->getClusterStrategy();
  136. $profile = Profile\Factory::getDevelopment();
  137. $arguments = array('key:1', 10);
  138. foreach ($this->getExpectedCommands('keys-blockinglist') as $commandID) {
  139. $command = $profile->createCommand($commandID, $arguments);
  140. $this->assertNotNull($strategy->getSlot($command), $commandID);
  141. }
  142. }
  143. /**
  144. * @group disconnected
  145. */
  146. public function testKeysForBlockingListCommandsWithMoreKeys()
  147. {
  148. $strategy = $this->getClusterStrategy();
  149. $profile = Profile\Factory::getDevelopment();
  150. $arguments = array('key:1', 'key:2', 10);
  151. foreach ($this->getExpectedCommands('keys-blockinglist') as $commandID) {
  152. $command = $profile->createCommand($commandID, $arguments);
  153. $this->assertNull($strategy->getSlot($command), $commandID);
  154. }
  155. }
  156. /**
  157. * @group disconnected
  158. */
  159. public function testKeysForGeoradiusCommand()
  160. {
  161. $strategy = $this->getClusterStrategy();
  162. $profile = Profile\Factory::getDevelopment();
  163. $commandID = 'GEORADIUS';
  164. $command = $profile->createCommand($commandID, array('{key}:1', 10, 10, 1, 'km'));
  165. $this->assertNotNull($strategy->getSlot($command), $commandID);
  166. $command = $profile->createCommand($commandID, array('{key}:1', 10, 10, 1, 'km', 'store', '{key}:2', 'storedist', '{key}:3'));
  167. $this->assertNotNull($strategy->getSlot($command), $commandID);
  168. }
  169. /**
  170. * @group disconnected
  171. */
  172. public function testKeysForGeoradiusByMemberCommand()
  173. {
  174. $strategy = $this->getClusterStrategy();
  175. $profile = Profile\Factory::getDevelopment();
  176. $commandID = 'GEORADIUSBYMEMBER';
  177. $command = $profile->createCommand($commandID, array('{key}:1', 'member', 1, 'km'));
  178. $this->assertNotNull($strategy->getSlot($command), $commandID);
  179. $command = $profile->createCommand($commandID, array('{key}:1', 'member', 1, 'km', 'store', '{key}:2', 'storedist', '{key}:3'));
  180. $this->assertNotNull($strategy->getSlot($command), $commandID);
  181. }
  182. /**
  183. * @group disconnected
  184. */
  185. public function testKeysForEvalCommand()
  186. {
  187. $strategy = $this->getClusterStrategy();
  188. $profile = Profile\Factory::getDevelopment();
  189. $arguments = array('%SCRIPT%', 1, 'key:1', 'value1');
  190. foreach ($this->getExpectedCommands('keys-script') as $commandID) {
  191. $command = $profile->createCommand($commandID, $arguments);
  192. $this->assertNotNull($strategy->getSlot($command), $commandID);
  193. }
  194. }
  195. /**
  196. * @group disconnected
  197. */
  198. public function testKeysForScriptCommand()
  199. {
  200. $strategy = $this->getClusterStrategy();
  201. $arguments = array('key:1', 'value1');
  202. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript', 'getKeysCount'));
  203. $command->expects($this->once())
  204. ->method('getScript')
  205. ->will($this->returnValue('return true'));
  206. $command->expects($this->exactly(2))
  207. ->method('getKeysCount')
  208. ->will($this->returnValue(1));
  209. $command->setArguments($arguments);
  210. $this->assertNotNull($strategy->getSlot($command), "Script Command [{$command->getId()}]");
  211. }
  212. /**
  213. * @group disconnected
  214. */
  215. public function testUnsettingCommandHandler()
  216. {
  217. $strategy = $this->getClusterStrategy();
  218. $profile = Profile\Factory::getDevelopment();
  219. $strategy->setCommandHandler('set');
  220. $strategy->setCommandHandler('get', null);
  221. $command = $profile->createCommand('set', array('key', 'value'));
  222. $this->assertNull($strategy->getSlot($command));
  223. $command = $profile->createCommand('get', array('key'));
  224. $this->assertNull($strategy->getSlot($command));
  225. }
  226. /**
  227. * @group disconnected
  228. */
  229. public function testSettingCustomCommandHandler()
  230. {
  231. $strategy = $this->getClusterStrategy();
  232. $profile = Profile\Factory::getDevelopment();
  233. $callable = $this->getMock('stdClass', array('__invoke'));
  234. $callable->expects($this->once())
  235. ->method('__invoke')
  236. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  237. ->will($this->returnValue('key'));
  238. $strategy->setCommandHandler('get', $callable);
  239. $command = $profile->createCommand('get', array('key'));
  240. $this->assertNotNull($strategy->getSlot($command));
  241. }
  242. // ******************************************************************** //
  243. // ---- HELPER METHODS ------------------------------------------------ //
  244. // ******************************************************************** //
  245. /**
  246. * Creates the default cluster strategy object.
  247. *
  248. * @return StrategyInterface
  249. */
  250. protected function getClusterStrategy()
  251. {
  252. $strategy = new RedisStrategy();
  253. return $strategy;
  254. }
  255. /**
  256. * Returns the list of expected supported commands.
  257. *
  258. * @param string $type Optional type of command (based on its keys)
  259. *
  260. * @return array
  261. */
  262. protected function getExpectedCommands($type = null)
  263. {
  264. $commands = array(
  265. /* commands operating on the key space */
  266. 'EXISTS' => 'keys-all',
  267. 'DEL' => 'keys-all',
  268. 'TYPE' => 'keys-first',
  269. 'EXPIRE' => 'keys-first',
  270. 'EXPIREAT' => 'keys-first',
  271. 'PERSIST' => 'keys-first',
  272. 'PEXPIRE' => 'keys-first',
  273. 'PEXPIREAT' => 'keys-first',
  274. 'TTL' => 'keys-first',
  275. 'PTTL' => 'keys-first',
  276. 'SORT' => 'keys-first', // TODO
  277. 'DUMP' => 'keys-first',
  278. 'RESTORE' => 'keys-first',
  279. /* commands operating on string values */
  280. 'APPEND' => 'keys-first',
  281. 'DECR' => 'keys-first',
  282. 'DECRBY' => 'keys-first',
  283. 'GET' => 'keys-first',
  284. 'GETBIT' => 'keys-first',
  285. 'MGET' => 'keys-all',
  286. 'SET' => 'keys-first',
  287. 'GETRANGE' => 'keys-first',
  288. 'GETSET' => 'keys-first',
  289. 'INCR' => 'keys-first',
  290. 'INCRBY' => 'keys-first',
  291. 'INCRBYFLOAT' => 'keys-first',
  292. 'SETBIT' => 'keys-first',
  293. 'SETEX' => 'keys-first',
  294. 'MSET' => 'keys-interleaved',
  295. 'MSETNX' => 'keys-interleaved',
  296. 'SETNX' => 'keys-first',
  297. 'SETRANGE' => 'keys-first',
  298. 'STRLEN' => 'keys-first',
  299. 'SUBSTR' => 'keys-first',
  300. 'BITOP' => 'keys-bitop',
  301. 'BITCOUNT' => 'keys-first',
  302. 'BITFIELD' => 'keys-first',
  303. /* commands operating on lists */
  304. 'LINSERT' => 'keys-first',
  305. 'LINDEX' => 'keys-first',
  306. 'LLEN' => 'keys-first',
  307. 'LPOP' => 'keys-first',
  308. 'RPOP' => 'keys-first',
  309. 'RPOPLPUSH' => 'keys-all',
  310. 'BLPOP' => 'keys-blockinglist',
  311. 'BRPOP' => 'keys-blockinglist',
  312. 'BRPOPLPUSH' => 'keys-blockinglist',
  313. 'LPUSH' => 'keys-first',
  314. 'LPUSHX' => 'keys-first',
  315. 'RPUSH' => 'keys-first',
  316. 'RPUSHX' => 'keys-first',
  317. 'LRANGE' => 'keys-first',
  318. 'LREM' => 'keys-first',
  319. 'LSET' => 'keys-first',
  320. 'LTRIM' => 'keys-first',
  321. /* commands operating on sets */
  322. 'SADD' => 'keys-first',
  323. 'SCARD' => 'keys-first',
  324. 'SDIFF' => 'keys-all',
  325. 'SDIFFSTORE' => 'keys-all',
  326. 'SINTER' => 'keys-all',
  327. 'SINTERSTORE' => 'keys-all',
  328. 'SUNION' => 'keys-all',
  329. 'SUNIONSTORE' => 'keys-all',
  330. 'SISMEMBER' => 'keys-first',
  331. 'SMEMBERS' => 'keys-first',
  332. 'SSCAN' => 'keys-first',
  333. 'SPOP' => 'keys-first',
  334. 'SRANDMEMBER' => 'keys-first',
  335. 'SREM' => 'keys-first',
  336. /* commands operating on sorted sets */
  337. 'ZADD' => 'keys-first',
  338. 'ZCARD' => 'keys-first',
  339. 'ZCOUNT' => 'keys-first',
  340. 'ZINCRBY' => 'keys-first',
  341. 'ZINTERSTORE' => 'keys-zaggregated',
  342. 'ZRANGE' => 'keys-first',
  343. 'ZRANGEBYSCORE' => 'keys-first',
  344. 'ZRANK' => 'keys-first',
  345. 'ZREM' => 'keys-first',
  346. 'ZREMRANGEBYRANK' => 'keys-first',
  347. 'ZREMRANGEBYSCORE' => 'keys-first',
  348. 'ZREVRANGE' => 'keys-first',
  349. 'ZREVRANGEBYSCORE' => 'keys-first',
  350. 'ZREVRANK' => 'keys-first',
  351. 'ZSCORE' => 'keys-first',
  352. 'ZUNIONSTORE' => 'keys-zaggregated',
  353. 'ZSCAN' => 'keys-first',
  354. 'ZLEXCOUNT' => 'keys-first',
  355. 'ZRANGEBYLEX' => 'keys-first',
  356. 'ZREMRANGEBYLEX' => 'keys-first',
  357. 'ZREVRANGEBYLEX' => 'keys-first',
  358. /* commands operating on hashes */
  359. 'HDEL' => 'keys-first',
  360. 'HEXISTS' => 'keys-first',
  361. 'HGET' => 'keys-first',
  362. 'HGETALL' => 'keys-first',
  363. 'HMGET' => 'keys-first',
  364. 'HMSET' => 'keys-first',
  365. 'HINCRBY' => 'keys-first',
  366. 'HINCRBYFLOAT' => 'keys-first',
  367. 'HKEYS' => 'keys-first',
  368. 'HLEN' => 'keys-first',
  369. 'HSET' => 'keys-first',
  370. 'HSETNX' => 'keys-first',
  371. 'HVALS' => 'keys-first',
  372. 'HSCAN' => 'keys-first',
  373. 'HSTRLEN' => 'keys-first',
  374. /* commands operating on HyperLogLog */
  375. 'PFADD' => 'keys-first',
  376. 'PFCOUNT' => 'keys-all',
  377. 'PFMERGE' => 'keys-all',
  378. /* scripting */
  379. 'EVAL' => 'keys-script',
  380. 'EVALSHA' => 'keys-script',
  381. /* commands performing geospatial operations */
  382. 'GEOADD' => 'keys-first',
  383. 'GEOHASH' => 'keys-first',
  384. 'GEOPOS' => 'keys-first',
  385. 'GEODIST' => 'keys-first',
  386. 'GEORADIUS' => 'keys-georadius',
  387. 'GEORADIUSBYMEMBER' => 'keys-georadius',
  388. );
  389. if (isset($type)) {
  390. $commands = array_filter($commands, function ($expectedType) use ($type) {
  391. return $expectedType === $type;
  392. });
  393. }
  394. return array_keys($commands);
  395. }
  396. }