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