CommandHashStrategyTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // ---- HELPER METHODS ------------------------------------------------ //
  124. // ******************************************************************** //
  125. /**
  126. * Returns the list of expected supported commands.
  127. *
  128. * @param string $type Optional type of command (based on its keys)
  129. * @return array
  130. */
  131. protected function getExpectedCommands($type = null)
  132. {
  133. $commands = array(
  134. /* commands operating on the key space */
  135. 'EXISTS' => 'keys-first',
  136. 'DEL' => 'keys-all',
  137. 'TYPE' => 'keys-first',
  138. 'EXPIRE' => 'keys-first',
  139. 'EXPIREAT' => 'keys-first',
  140. 'PERSIST' => 'keys-first',
  141. 'PEXPIRE' => 'keys-first',
  142. 'PEXPIREAT' => 'keys-first',
  143. 'TTL' => 'keys-first',
  144. 'PTTL' => 'keys-first',
  145. 'SORT' => 'keys-first', // TODO
  146. /* commands operating on string values */
  147. 'APPEND' => 'keys-first',
  148. 'DECR' => 'keys-first',
  149. 'DECRBY' => 'keys-first',
  150. 'GET' => 'keys-first',
  151. 'GETBIT' => 'keys-first',
  152. 'MGET' => 'keys-all',
  153. 'SET' => 'keys-first',
  154. 'GETRANGE' => 'keys-first',
  155. 'GETSET' => 'keys-first',
  156. 'INCR' => 'keys-first',
  157. 'INCRBY' => 'keys-first',
  158. 'SETBIT' => 'keys-first',
  159. 'SETEX' => 'keys-first',
  160. 'MSET' => 'keys-interleaved',
  161. 'MSETNX' => 'keys-interleaved',
  162. 'SETNX' => 'keys-first',
  163. 'SETRANGE' => 'keys-first',
  164. 'STRLEN' => 'keys-first',
  165. 'SUBSTR' => 'keys-first',
  166. /* commands operating on lists */
  167. 'LINSERT' => 'keys-first',
  168. 'LINDEX' => 'keys-first',
  169. 'LLEN' => 'keys-first',
  170. 'LPOP' => 'keys-first',
  171. 'RPOP' => 'keys-first',
  172. 'RPOPLPUSH' => 'keys-all',
  173. 'BLPOP' => 'keys-blockinglist',
  174. 'BRPOP' => 'keys-blockinglist',
  175. 'BRPOPLPUSH' => 'keys-blockinglist',
  176. 'LPUSH' => 'keys-first',
  177. 'LPUSHX' => 'keys-first',
  178. 'RPUSH' => 'keys-first',
  179. 'RPUSHX' => 'keys-first',
  180. 'LRANGE' => 'keys-first',
  181. 'LREM' => 'keys-first',
  182. 'LSET' => 'keys-first',
  183. 'LTRIM' => 'keys-first',
  184. /* commands operating on sets */
  185. 'SADD' => 'keys-first',
  186. 'SCARD' => 'keys-first',
  187. 'SDIFF' => 'keys-all',
  188. 'SDIFFSTORE' => 'keys-all',
  189. 'SINTER' => 'keys-all',
  190. 'SINTERSTORE' => 'keys-all',
  191. 'SUNION' => 'keys-all',
  192. 'SUNIONSTORE' => 'keys-all',
  193. 'SISMEMBER' => 'keys-first',
  194. 'SMEMBERS' => 'keys-first',
  195. 'SPOP' => 'keys-first',
  196. 'SRANDMEMBER' => 'keys-first',
  197. 'SREM' => 'keys-first',
  198. /* commands operating on sorted sets */
  199. 'ZADD' => 'keys-first',
  200. 'ZCARD' => 'keys-first',
  201. 'ZCOUNT' => 'keys-first',
  202. 'ZINCRBY' => 'keys-first',
  203. 'ZINTERSTORE' => 'keys-zaggregated',
  204. 'ZRANGE' => 'keys-first',
  205. 'ZRANGEBYSCORE' => 'keys-first',
  206. 'ZRANK' => 'keys-first',
  207. 'ZREM' => 'keys-first',
  208. 'ZREMRANGEBYRANK' => 'keys-first',
  209. 'ZREMRANGEBYSCORE' => 'keys-first',
  210. 'ZREVRANGE' => 'keys-first',
  211. 'ZREVRANGEBYSCORE' => 'keys-first',
  212. 'ZREVRANK' => 'keys-first',
  213. 'ZSCORE' => 'keys-first',
  214. 'ZUNIONSTORE' => 'keys-zaggregated',
  215. /* commands operating on hashes */
  216. 'HDEL' => 'keys-first',
  217. 'HEXISTS' => 'keys-first',
  218. 'HGET' => 'keys-first',
  219. 'HGETALL' => 'keys-first',
  220. 'HMGET' => 'keys-first',
  221. 'HINCRBY' => 'keys-first',
  222. 'HINCRBYFLOAT' => 'keys-first',
  223. 'HKEYS' => 'keys-first',
  224. 'HLEN' => 'keys-first',
  225. 'HSET' => 'keys-first',
  226. 'HSETNX' => 'keys-first',
  227. 'HVALS' => 'keys-first',
  228. );
  229. if (isset($type)) {
  230. $commands = array_filter($commands, function($expectedType) use($type) {
  231. return $expectedType === $type;
  232. });
  233. }
  234. return array_keys($commands);
  235. }
  236. }