CommandHashStrategyTest.php 11 KB

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