RedisStrategyTest.php 15 KB

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