PredisClusterHashStrategyTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Cluster\Distribution\HashRing;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class PredisClusterHashStrategyTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testSupportsKeyTags()
  23. {
  24. $expected = -1938594527;
  25. $strategy = $this->getHashStrategy();
  26. $this->assertSame($expected, $strategy->getKeyHash('{foo}'));
  27. $this->assertSame($expected, $strategy->getKeyHash('{foo}:bar'));
  28. $this->assertSame($expected, $strategy->getKeyHash('{foo}:baz'));
  29. $this->assertSame($expected, $strategy->getKeyHash('bar:{foo}:bar'));
  30. $this->assertSame(0, $strategy->getKeyHash(''));
  31. $this->assertSame(0, $strategy->getKeyHash('{}'));
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testSupportedCommands()
  37. {
  38. $strategy = $this->getHashStrategy();
  39. $this->assertSame($this->getExpectedCommands(), $strategy->getSupportedCommands());
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testReturnsNullOnUnsupportedCommand()
  45. {
  46. $strategy = $this->getHashStrategy();
  47. $command = ServerProfile::getDevelopment()->createCommand('ping');
  48. $this->assertNull($strategy->getHash($command));
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testFirstKeyCommands()
  54. {
  55. $strategy = $this->getHashStrategy();
  56. $profile = ServerProfile::getDevelopment();
  57. $arguments = array('key');
  58. foreach ($this->getExpectedCommands('keys-first') as $commandID) {
  59. $command = $profile->createCommand($commandID, $arguments);
  60. $this->assertNotNull($strategy->getHash($command), $commandID);
  61. }
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testAllKeysCommands()
  67. {
  68. $strategy = $this->getHashStrategy();
  69. $profile = ServerProfile::getDevelopment();
  70. $arguments = array('{key}:1', '{key}:2', '{key}:3', '{key}:4');
  71. foreach ($this->getExpectedCommands('keys-all') as $commandID) {
  72. $command = $profile->createCommand($commandID, $arguments);
  73. $this->assertNotNull($strategy->getHash($command), $commandID);
  74. }
  75. }
  76. /**
  77. * @group disconnected
  78. */
  79. public function testInterleavedKeysCommands()
  80. {
  81. $strategy = $this->getHashStrategy();
  82. $profile = ServerProfile::getDevelopment();
  83. $arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
  84. foreach ($this->getExpectedCommands('keys-interleaved') as $commandID) {
  85. $command = $profile->createCommand($commandID, $arguments);
  86. $this->assertNotNull($strategy->getHash($command), $commandID);
  87. }
  88. }
  89. /**
  90. * @group disconnected
  91. */
  92. public function testKeysForBlockingListCommands()
  93. {
  94. $strategy = $this->getHashStrategy();
  95. $profile = ServerProfile::getDevelopment();
  96. $arguments = array('{key}:1', '{key}:2', 10);
  97. foreach ($this->getExpectedCommands('keys-blockinglist') as $commandID) {
  98. $command = $profile->createCommand($commandID, $arguments);
  99. $this->assertNotNull($strategy->getHash($command), $commandID);
  100. }
  101. }
  102. /**
  103. * @group disconnected
  104. */
  105. public function testKeysForZsetAggregationCommands()
  106. {
  107. $strategy = $this->getHashStrategy();
  108. $profile = ServerProfile::getDevelopment();
  109. $arguments = array('{key}:destination', 2, '{key}:1', '{key}:1', array('aggregate' => 'SUM'));
  110. foreach ($this->getExpectedCommands('keys-zaggregated') as $commandID) {
  111. $command = $profile->createCommand($commandID, $arguments);
  112. $this->assertNotNull($strategy->getHash($command), $commandID);
  113. }
  114. }
  115. /**
  116. * @group disconnected
  117. */
  118. public function testKeysForBitOpCommand()
  119. {
  120. $strategy = $this->getHashStrategy();
  121. $profile = ServerProfile::getDevelopment();
  122. $arguments = array('AND', '{key}:destination', '{key}:src:1', '{key}:src:2');
  123. foreach ($this->getExpectedCommands('keys-bitop') as $commandID) {
  124. $command = $profile->createCommand($commandID, $arguments);
  125. $this->assertNotNull($strategy->getHash($command), $commandID);
  126. }
  127. }
  128. /**
  129. * @group disconnected
  130. */
  131. public function testUnsettingCommandHandler()
  132. {
  133. $strategy = $this->getHashStrategy();
  134. $profile = ServerProfile::getDevelopment();
  135. $strategy->setCommandHandler('set');
  136. $strategy->setCommandHandler('get', null);
  137. $command = $profile->createCommand('set', array('key', 'value'));
  138. $this->assertNull($strategy->getHash($command));
  139. $command = $profile->createCommand('get', array('key'));
  140. $this->assertNull($strategy->getHash($command));
  141. }
  142. /**
  143. * @group disconnected
  144. */
  145. public function testSettingCustomCommandHandler()
  146. {
  147. $strategy = $this->getHashStrategy();
  148. $profile = ServerProfile::getDevelopment();
  149. $callable = $this->getMock('stdClass', array('__invoke'));
  150. $callable->expects($this->once())
  151. ->method('__invoke')
  152. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  153. ->will($this->returnValue('key'));
  154. $strategy->setCommandHandler('get', $callable);
  155. $command = $profile->createCommand('get', array('key'));
  156. $this->assertNotNull($strategy->getHash($command));
  157. }
  158. // ******************************************************************** //
  159. // ---- HELPER METHODS ------------------------------------------------ //
  160. // ******************************************************************** //
  161. /**
  162. * Creates the default hash strategy object.
  163. *
  164. * @return CommandHashStrategyInterface
  165. */
  166. protected function getHashStrategy()
  167. {
  168. $distributor = new HashRing();
  169. $hashGenerator = $distributor->getHashGenerator();
  170. $strategy = new PredisClusterHashStrategy($hashGenerator);
  171. return $strategy;
  172. }
  173. /**
  174. * Returns the list of expected supported commands.
  175. *
  176. * @param string $type Optional type of command (based on its keys)
  177. * @return array
  178. */
  179. protected function getExpectedCommands($type = null)
  180. {
  181. $commands = array(
  182. /* commands operating on the key space */
  183. 'EXISTS' => 'keys-first',
  184. 'DEL' => 'keys-all',
  185. 'TYPE' => 'keys-first',
  186. 'EXPIRE' => 'keys-first',
  187. 'EXPIREAT' => 'keys-first',
  188. 'PERSIST' => 'keys-first',
  189. 'PEXPIRE' => 'keys-first',
  190. 'PEXPIREAT' => 'keys-first',
  191. 'TTL' => 'keys-first',
  192. 'PTTL' => 'keys-first',
  193. 'SORT' => 'keys-first', // TODO
  194. /* commands operating on string values */
  195. 'APPEND' => 'keys-first',
  196. 'DECR' => 'keys-first',
  197. 'DECRBY' => 'keys-first',
  198. 'GET' => 'keys-first',
  199. 'GETBIT' => 'keys-first',
  200. 'MGET' => 'keys-all',
  201. 'SET' => 'keys-first',
  202. 'GETRANGE' => 'keys-first',
  203. 'GETSET' => 'keys-first',
  204. 'INCR' => 'keys-first',
  205. 'INCRBY' => 'keys-first',
  206. 'SETBIT' => 'keys-first',
  207. 'SETEX' => 'keys-first',
  208. 'MSET' => 'keys-interleaved',
  209. 'MSETNX' => 'keys-interleaved',
  210. 'SETNX' => 'keys-first',
  211. 'SETRANGE' => 'keys-first',
  212. 'STRLEN' => 'keys-first',
  213. 'SUBSTR' => 'keys-first',
  214. 'BITOP' => 'keys-bitop',
  215. 'BITCOUNT' => 'keys-first',
  216. /* commands operating on lists */
  217. 'LINSERT' => 'keys-first',
  218. 'LINDEX' => 'keys-first',
  219. 'LLEN' => 'keys-first',
  220. 'LPOP' => 'keys-first',
  221. 'RPOP' => 'keys-first',
  222. 'RPOPLPUSH' => 'keys-all',
  223. 'BLPOP' => 'keys-blockinglist',
  224. 'BRPOP' => 'keys-blockinglist',
  225. 'BRPOPLPUSH' => 'keys-blockinglist',
  226. 'LPUSH' => 'keys-first',
  227. 'LPUSHX' => 'keys-first',
  228. 'RPUSH' => 'keys-first',
  229. 'RPUSHX' => 'keys-first',
  230. 'LRANGE' => 'keys-first',
  231. 'LREM' => 'keys-first',
  232. 'LSET' => 'keys-first',
  233. 'LTRIM' => 'keys-first',
  234. /* commands operating on sets */
  235. 'SADD' => 'keys-first',
  236. 'SCARD' => 'keys-first',
  237. 'SDIFF' => 'keys-all',
  238. 'SDIFFSTORE' => 'keys-all',
  239. 'SINTER' => 'keys-all',
  240. 'SINTERSTORE' => 'keys-all',
  241. 'SUNION' => 'keys-all',
  242. 'SUNIONSTORE' => 'keys-all',
  243. 'SISMEMBER' => 'keys-first',
  244. 'SMEMBERS' => 'keys-first',
  245. 'SPOP' => 'keys-first',
  246. 'SRANDMEMBER' => 'keys-first',
  247. 'SREM' => 'keys-first',
  248. /* commands operating on sorted sets */
  249. 'ZADD' => 'keys-first',
  250. 'ZCARD' => 'keys-first',
  251. 'ZCOUNT' => 'keys-first',
  252. 'ZINCRBY' => 'keys-first',
  253. 'ZINTERSTORE' => 'keys-zaggregated',
  254. 'ZRANGE' => 'keys-first',
  255. 'ZRANGEBYSCORE' => 'keys-first',
  256. 'ZRANK' => 'keys-first',
  257. 'ZREM' => 'keys-first',
  258. 'ZREMRANGEBYRANK' => 'keys-first',
  259. 'ZREMRANGEBYSCORE' => 'keys-first',
  260. 'ZREVRANGE' => 'keys-first',
  261. 'ZREVRANGEBYSCORE' => 'keys-first',
  262. 'ZREVRANK' => 'keys-first',
  263. 'ZSCORE' => 'keys-first',
  264. 'ZUNIONSTORE' => 'keys-zaggregated',
  265. /* commands operating on hashes */
  266. 'HDEL' => 'keys-first',
  267. 'HEXISTS' => 'keys-first',
  268. 'HGET' => 'keys-first',
  269. 'HGETALL' => 'keys-first',
  270. 'HMGET' => 'keys-first',
  271. 'HINCRBY' => 'keys-first',
  272. 'HINCRBYFLOAT' => 'keys-first',
  273. 'HKEYS' => 'keys-first',
  274. 'HLEN' => 'keys-first',
  275. 'HSET' => 'keys-first',
  276. 'HSETNX' => 'keys-first',
  277. 'HVALS' => 'keys-first',
  278. );
  279. if (isset($type)) {
  280. $commands = array_filter($commands, function ($expectedType) use ($type) {
  281. return $expectedType === $type;
  282. });
  283. }
  284. return array_keys($commands);
  285. }
  286. }