CommandHashStrategyTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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\HashRing;
  13. use Predis\Profile\ServerProfile;
  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. * @group disconnected
  138. */
  139. public function testUnsettingCommandHandler()
  140. {
  141. $distribution = new HashRing();
  142. $hashstrategy = new CommandHashStrategy();
  143. $profile = ServerProfile::getDevelopment();
  144. $hashstrategy->setCommandHandler('set');
  145. $hashstrategy->setCommandHandler('get', null);
  146. $command = $profile->createCommand('set', array('key', 'value'));
  147. $this->assertNull($hashstrategy->getHash($distribution, $command));
  148. $command = $profile->createCommand('get', array('key'));
  149. $this->assertNull($hashstrategy->getHash($distribution, $command));
  150. }
  151. /**
  152. * @group disconnected
  153. */
  154. public function testSettingCustomCommandHandler()
  155. {
  156. $distribution = new HashRing();
  157. $hashstrategy = new CommandHashStrategy();
  158. $profile = ServerProfile::getDevelopment();
  159. $callable = $this->getMock('stdClass', array('__invoke'));
  160. $callable->expects($this->once())
  161. ->method('__invoke')
  162. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  163. ->will($this->returnValue('key'));
  164. $hashstrategy->setCommandHandler('get', $callable);
  165. $command = $profile->createCommand('get', array('key'));
  166. $this->assertNotNull($hashstrategy->getHash($distribution, $command));
  167. }
  168. // ******************************************************************** //
  169. // ---- HELPER METHODS ------------------------------------------------ //
  170. // ******************************************************************** //
  171. /**
  172. * Returns the list of expected supported commands.
  173. *
  174. * @param string $type Optional type of command (based on its keys)
  175. * @return array
  176. */
  177. protected function getExpectedCommands($type = null)
  178. {
  179. $commands = array(
  180. /* commands operating on the key space */
  181. 'EXISTS' => 'keys-first',
  182. 'DEL' => 'keys-all',
  183. 'TYPE' => 'keys-first',
  184. 'EXPIRE' => 'keys-first',
  185. 'EXPIREAT' => 'keys-first',
  186. 'PERSIST' => 'keys-first',
  187. 'PEXPIRE' => 'keys-first',
  188. 'PEXPIREAT' => 'keys-first',
  189. 'TTL' => 'keys-first',
  190. 'PTTL' => 'keys-first',
  191. 'SORT' => 'keys-first', // TODO
  192. /* commands operating on string values */
  193. 'APPEND' => 'keys-first',
  194. 'DECR' => 'keys-first',
  195. 'DECRBY' => 'keys-first',
  196. 'GET' => 'keys-first',
  197. 'GETBIT' => 'keys-first',
  198. 'MGET' => 'keys-all',
  199. 'SET' => 'keys-first',
  200. 'GETRANGE' => 'keys-first',
  201. 'GETSET' => 'keys-first',
  202. 'INCR' => 'keys-first',
  203. 'INCRBY' => 'keys-first',
  204. 'SETBIT' => 'keys-first',
  205. 'SETEX' => 'keys-first',
  206. 'MSET' => 'keys-interleaved',
  207. 'MSETNX' => 'keys-interleaved',
  208. 'SETNX' => 'keys-first',
  209. 'SETRANGE' => 'keys-first',
  210. 'STRLEN' => 'keys-first',
  211. 'SUBSTR' => 'keys-first',
  212. 'BITOP' => 'keys-bitop',
  213. 'BITCOUNT' => 'keys-first',
  214. /* commands operating on lists */
  215. 'LINSERT' => 'keys-first',
  216. 'LINDEX' => 'keys-first',
  217. 'LLEN' => 'keys-first',
  218. 'LPOP' => 'keys-first',
  219. 'RPOP' => 'keys-first',
  220. 'RPOPLPUSH' => 'keys-all',
  221. 'BLPOP' => 'keys-blockinglist',
  222. 'BRPOP' => 'keys-blockinglist',
  223. 'BRPOPLPUSH' => 'keys-blockinglist',
  224. 'LPUSH' => 'keys-first',
  225. 'LPUSHX' => 'keys-first',
  226. 'RPUSH' => 'keys-first',
  227. 'RPUSHX' => 'keys-first',
  228. 'LRANGE' => 'keys-first',
  229. 'LREM' => 'keys-first',
  230. 'LSET' => 'keys-first',
  231. 'LTRIM' => 'keys-first',
  232. /* commands operating on sets */
  233. 'SADD' => 'keys-first',
  234. 'SCARD' => 'keys-first',
  235. 'SDIFF' => 'keys-all',
  236. 'SDIFFSTORE' => 'keys-all',
  237. 'SINTER' => 'keys-all',
  238. 'SINTERSTORE' => 'keys-all',
  239. 'SUNION' => 'keys-all',
  240. 'SUNIONSTORE' => 'keys-all',
  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. 'ZINTERSTORE' => 'keys-zaggregated',
  252. 'ZRANGE' => 'keys-first',
  253. 'ZRANGEBYSCORE' => 'keys-first',
  254. 'ZRANK' => 'keys-first',
  255. 'ZREM' => 'keys-first',
  256. 'ZREMRANGEBYRANK' => 'keys-first',
  257. 'ZREMRANGEBYSCORE' => 'keys-first',
  258. 'ZREVRANGE' => 'keys-first',
  259. 'ZREVRANGEBYSCORE' => 'keys-first',
  260. 'ZREVRANK' => 'keys-first',
  261. 'ZSCORE' => 'keys-first',
  262. 'ZUNIONSTORE' => 'keys-zaggregated',
  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. }