RedisClusterHashStrategyTest.php 12 KB

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