PredisStrategyTest.php 15 KB

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