PredisClusterHashStrategyTest.php 13 KB

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