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