RedisClusterHashStrategyTest.php 14 KB

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