PredisStrategyTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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
  192. ->expects($this->once())
  193. ->method('getScript')
  194. ->will($this->returnValue('return true'));
  195. $command
  196. ->expects($this->exactly(2))
  197. ->method('getKeysCount')
  198. ->will($this->returnValue(2));
  199. $command->setArguments($arguments);
  200. $this->assertNotNull($strategy->getSlot($command), "Script Command [{$command->getId()}]");
  201. }
  202. /**
  203. * @group disconnected
  204. */
  205. public function testUnsettingCommandHandler()
  206. {
  207. $strategy = $this->getClusterStrategy();
  208. $commands = $this->getCommandFactory();
  209. $strategy->setCommandHandler('set');
  210. $strategy->setCommandHandler('get', null);
  211. $command = $commands->createCommand('set', array('key', 'value'));
  212. $this->assertNull($strategy->getSlot($command));
  213. $command = $commands->createCommand('get', array('key'));
  214. $this->assertNull($strategy->getSlot($command));
  215. }
  216. /**
  217. * @group disconnected
  218. */
  219. public function testSettingCustomCommandHandler()
  220. {
  221. $strategy = $this->getClusterStrategy();
  222. $commands = $this->getCommandFactory();
  223. $callable = $this->getMock('stdClass', array('__invoke'));
  224. $callable
  225. ->expects($this->once())
  226. ->method('__invoke')
  227. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  228. ->will($this->returnValue('key'));
  229. $strategy->setCommandHandler('get', $callable);
  230. $command = $commands->createCommand('get', array('key'));
  231. $this->assertNotNull($strategy->getSlot($command));
  232. }
  233. // ******************************************************************** //
  234. // ---- HELPER METHODS ------------------------------------------------ //
  235. // ******************************************************************** //
  236. /**
  237. * Creates the default cluster strategy object.
  238. *
  239. * @return StrategyInterface
  240. */
  241. protected function getClusterStrategy()
  242. {
  243. $strategy = new PredisStrategy();
  244. $connection = $this->getMock('Predis\Connection\NodeConnectionInterface');
  245. $strategy->getDistributor()->add($connection);
  246. return $strategy;
  247. }
  248. /**
  249. * Returns the list of expected supported commands.
  250. *
  251. * @param string $type Optional type of command (based on its keys)
  252. *
  253. * @return array
  254. */
  255. protected function getExpectedCommands($type = null)
  256. {
  257. $commands = array(
  258. /* commands operating on the key space */
  259. 'EXISTS' => 'keys-all',
  260. 'DEL' => 'keys-all',
  261. 'TYPE' => 'keys-first',
  262. 'EXPIRE' => 'keys-first',
  263. 'EXPIREAT' => 'keys-first',
  264. 'PERSIST' => 'keys-first',
  265. 'PEXPIRE' => 'keys-first',
  266. 'PEXPIREAT' => 'keys-first',
  267. 'TTL' => 'keys-first',
  268. 'PTTL' => 'keys-first',
  269. 'SORT' => 'variable',
  270. 'DUMP' => 'keys-first',
  271. 'RESTORE' => 'keys-first',
  272. /* commands operating on string values */
  273. 'APPEND' => 'keys-first',
  274. 'DECR' => 'keys-first',
  275. 'DECRBY' => 'keys-first',
  276. 'GET' => 'keys-first',
  277. 'GETBIT' => 'keys-first',
  278. 'MGET' => 'keys-all',
  279. 'SET' => 'keys-first',
  280. 'GETRANGE' => 'keys-first',
  281. 'GETSET' => 'keys-first',
  282. 'INCR' => 'keys-first',
  283. 'INCRBY' => 'keys-first',
  284. 'INCRBYFLOAT' => 'keys-first',
  285. 'SETBIT' => 'keys-first',
  286. 'SETEX' => 'keys-first',
  287. 'MSET' => 'keys-interleaved',
  288. 'MSETNX' => 'keys-interleaved',
  289. 'SETNX' => 'keys-first',
  290. 'SETRANGE' => 'keys-first',
  291. 'STRLEN' => 'keys-first',
  292. 'SUBSTR' => 'keys-first',
  293. 'BITOP' => 'keys-bitop',
  294. 'BITCOUNT' => 'keys-first',
  295. 'BITFIELD' => 'keys-first',
  296. /* commands operating on lists */
  297. 'LINSERT' => 'keys-first',
  298. 'LINDEX' => 'keys-first',
  299. 'LLEN' => 'keys-first',
  300. 'LPOP' => 'keys-first',
  301. 'RPOP' => 'keys-first',
  302. 'RPOPLPUSH' => 'keys-all',
  303. 'BLPOP' => 'keys-blockinglist',
  304. 'BRPOP' => 'keys-blockinglist',
  305. 'BRPOPLPUSH' => 'keys-blockinglist',
  306. 'LPUSH' => 'keys-first',
  307. 'LPUSHX' => 'keys-first',
  308. 'RPUSH' => 'keys-first',
  309. 'RPUSHX' => 'keys-first',
  310. 'LRANGE' => 'keys-first',
  311. 'LREM' => 'keys-first',
  312. 'LSET' => 'keys-first',
  313. 'LTRIM' => 'keys-first',
  314. /* commands operating on sets */
  315. 'SADD' => 'keys-first',
  316. 'SCARD' => 'keys-first',
  317. 'SDIFF' => 'keys-all',
  318. 'SDIFFSTORE' => 'keys-all',
  319. 'SINTER' => 'keys-all',
  320. 'SINTERSTORE' => 'keys-all',
  321. 'SUNION' => 'keys-all',
  322. 'SUNIONSTORE' => 'keys-all',
  323. 'SISMEMBER' => 'keys-first',
  324. 'SMEMBERS' => 'keys-first',
  325. 'SSCAN' => 'keys-first',
  326. 'SPOP' => 'keys-first',
  327. 'SRANDMEMBER' => 'keys-first',
  328. 'SREM' => 'keys-first',
  329. /* commands operating on sorted sets */
  330. 'ZADD' => 'keys-first',
  331. 'ZCARD' => 'keys-first',
  332. 'ZCOUNT' => 'keys-first',
  333. 'ZINCRBY' => 'keys-first',
  334. 'ZINTERSTORE' => 'keys-zaggregated',
  335. 'ZRANGE' => 'keys-first',
  336. 'ZRANGEBYSCORE' => 'keys-first',
  337. 'ZRANK' => 'keys-first',
  338. 'ZREM' => 'keys-first',
  339. 'ZREMRANGEBYRANK' => 'keys-first',
  340. 'ZREMRANGEBYSCORE' => 'keys-first',
  341. 'ZREVRANGE' => 'keys-first',
  342. 'ZREVRANGEBYSCORE' => 'keys-first',
  343. 'ZREVRANK' => 'keys-first',
  344. 'ZSCORE' => 'keys-first',
  345. 'ZUNIONSTORE' => 'keys-zaggregated',
  346. 'ZSCAN' => 'keys-first',
  347. 'ZLEXCOUNT' => 'keys-first',
  348. 'ZRANGEBYLEX' => 'keys-first',
  349. 'ZREMRANGEBYLEX' => 'keys-first',
  350. 'ZREVRANGEBYLEX' => 'keys-first',
  351. /* commands operating on hashes */
  352. 'HDEL' => 'keys-first',
  353. 'HEXISTS' => 'keys-first',
  354. 'HGET' => 'keys-first',
  355. 'HGETALL' => 'keys-first',
  356. 'HMGET' => 'keys-first',
  357. 'HMSET' => 'keys-first',
  358. 'HINCRBY' => 'keys-first',
  359. 'HINCRBYFLOAT' => 'keys-first',
  360. 'HKEYS' => 'keys-first',
  361. 'HLEN' => 'keys-first',
  362. 'HSET' => 'keys-first',
  363. 'HSETNX' => 'keys-first',
  364. 'HVALS' => 'keys-first',
  365. 'HSCAN' => 'keys-first',
  366. 'HSTRLEN' => 'keys-first',
  367. /* commands operating on HyperLogLog */
  368. 'PFADD' => 'keys-first',
  369. 'PFCOUNT' => 'keys-all',
  370. 'PFMERGE' => 'keys-all',
  371. /* scripting */
  372. 'EVAL' => 'keys-script',
  373. 'EVALSHA' => 'keys-script',
  374. /* commands performing geospatial operations */
  375. 'GEOADD' => 'keys-first',
  376. 'GEOHASH' => 'keys-first',
  377. 'GEOPOS' => 'keys-first',
  378. 'GEODIST' => 'keys-first',
  379. 'GEORADIUS' => 'keys-georadius',
  380. 'GEORADIUSBYMEMBER' => 'keys-georadius',
  381. );
  382. if (isset($type)) {
  383. $commands = array_filter($commands, function ($expectedType) use ($type) {
  384. return $expectedType === $type;
  385. });
  386. }
  387. return array_keys($commands);
  388. }
  389. }