RedisClusterHashStrategyTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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\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 testKeysForScriptCommand()
  141. {
  142. $strategy = $this->getHashStrategy();
  143. $profile = ServerProfile::getDevelopment();
  144. $arguments = array('%SCRIPT%', 1, 'key:1', 'value1');
  145. foreach ($this->getExpectedCommands('keys-script') as $commandID) {
  146. $command = $profile->createCommand($commandID, $arguments);
  147. $this->assertNotNull($strategy->getHash($command), $commandID);
  148. }
  149. }
  150. /**
  151. * @group disconnected
  152. */
  153. public function testKeysForScriptedCommand()
  154. {
  155. $strategy = $this->getHashStrategy();
  156. $arguments = array('key:1', 'value1');
  157. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
  158. $command->expects($this->once())
  159. ->method('getScript')
  160. ->will($this->returnValue('return true'));
  161. $command->expects($this->exactly(1))
  162. ->method('getKeysCount')
  163. ->will($this->returnValue(1));
  164. $command->setArguments($arguments);
  165. $this->assertNotNull($strategy->getHash($command), "Scripted Command [{$command->getId()}]");
  166. }
  167. /**
  168. * @group disconnected
  169. */
  170. public function testUnsettingCommandHandler()
  171. {
  172. $strategy = $this->getHashStrategy();
  173. $profile = ServerProfile::getDevelopment();
  174. $strategy->setCommandHandler('set');
  175. $strategy->setCommandHandler('get', null);
  176. $command = $profile->createCommand('set', array('key', 'value'));
  177. $this->assertNull($strategy->getHash($command));
  178. $command = $profile->createCommand('get', array('key'));
  179. $this->assertNull($strategy->getHash($command));
  180. }
  181. /**
  182. * @group disconnected
  183. */
  184. public function testSettingCustomCommandHandler()
  185. {
  186. $strategy = $this->getHashStrategy();
  187. $profile = ServerProfile::getDevelopment();
  188. $callable = $this->getMock('stdClass', array('__invoke'));
  189. $callable->expects($this->once())
  190. ->method('__invoke')
  191. ->with($this->isInstanceOf('Predis\Command\CommandInterface'))
  192. ->will($this->returnValue('key'));
  193. $strategy->setCommandHandler('get', $callable);
  194. $command = $profile->createCommand('get', array('key'));
  195. $this->assertNotNull($strategy->getHash($command));
  196. }
  197. // ******************************************************************** //
  198. // ---- HELPER METHODS ------------------------------------------------ //
  199. // ******************************************************************** //
  200. /**
  201. * Creates the default hash strategy object.
  202. *
  203. * @return CommandHashStrategyInterface
  204. */
  205. protected function getHashStrategy()
  206. {
  207. $strategy = new RedisClusterHashStrategy();
  208. return $strategy;
  209. }
  210. /**
  211. * Returns the list of expected supported commands.
  212. *
  213. * @param string $type Optional type of command (based on its keys)
  214. * @return array
  215. */
  216. protected function getExpectedCommands($type = null)
  217. {
  218. $commands = array(
  219. /* commands operating on the key space */
  220. 'EXISTS' => 'keys-first',
  221. 'DEL' => 'keys-all',
  222. 'TYPE' => 'keys-first',
  223. 'EXPIRE' => 'keys-first',
  224. 'EXPIREAT' => 'keys-first',
  225. 'PERSIST' => 'keys-first',
  226. 'PEXPIRE' => 'keys-first',
  227. 'PEXPIREAT' => 'keys-first',
  228. 'TTL' => 'keys-first',
  229. 'PTTL' => 'keys-first',
  230. 'SORT' => 'keys-first', // TODO
  231. /* commands operating on string values */
  232. 'APPEND' => 'keys-first',
  233. 'DECR' => 'keys-first',
  234. 'DECRBY' => 'keys-first',
  235. 'GET' => 'keys-first',
  236. 'GETBIT' => 'keys-first',
  237. 'MGET' => 'keys-all',
  238. 'SET' => 'keys-first',
  239. 'GETRANGE' => 'keys-first',
  240. 'GETSET' => 'keys-first',
  241. 'INCR' => 'keys-first',
  242. 'INCRBY' => 'keys-first',
  243. 'SETBIT' => 'keys-first',
  244. 'SETEX' => 'keys-first',
  245. 'MSET' => 'keys-interleaved',
  246. 'MSETNX' => 'keys-interleaved',
  247. 'SETNX' => 'keys-first',
  248. 'SETRANGE' => 'keys-first',
  249. 'STRLEN' => 'keys-first',
  250. 'SUBSTR' => 'keys-first',
  251. 'BITCOUNT' => 'keys-first',
  252. /* commands operating on lists */
  253. 'LINSERT' => 'keys-first',
  254. 'LINDEX' => 'keys-first',
  255. 'LLEN' => 'keys-first',
  256. 'LPOP' => 'keys-first',
  257. 'RPOP' => 'keys-first',
  258. 'BLPOP' => 'keys-blockinglist',
  259. 'BRPOP' => 'keys-blockinglist',
  260. 'LPUSH' => 'keys-first',
  261. 'LPUSHX' => 'keys-first',
  262. 'RPUSH' => 'keys-first',
  263. 'RPUSHX' => 'keys-first',
  264. 'LRANGE' => 'keys-first',
  265. 'LREM' => 'keys-first',
  266. 'LSET' => 'keys-first',
  267. 'LTRIM' => 'keys-first',
  268. /* commands operating on sets */
  269. 'SADD' => 'keys-first',
  270. 'SCARD' => 'keys-first',
  271. 'SISMEMBER' => 'keys-first',
  272. 'SMEMBERS' => 'keys-first',
  273. 'SPOP' => 'keys-first',
  274. 'SRANDMEMBER' => 'keys-first',
  275. 'SREM' => 'keys-first',
  276. /* commands operating on sorted sets */
  277. 'ZADD' => 'keys-first',
  278. 'ZCARD' => 'keys-first',
  279. 'ZCOUNT' => 'keys-first',
  280. 'ZINCRBY' => 'keys-first',
  281. 'ZRANGE' => 'keys-first',
  282. 'ZRANGEBYSCORE' => 'keys-first',
  283. 'ZRANK' => 'keys-first',
  284. 'ZREM' => 'keys-first',
  285. 'ZREMRANGEBYRANK' => 'keys-first',
  286. 'ZREMRANGEBYSCORE' => 'keys-first',
  287. 'ZREVRANGE' => 'keys-first',
  288. 'ZREVRANGEBYSCORE' => 'keys-first',
  289. 'ZREVRANK' => 'keys-first',
  290. 'ZSCORE' => 'keys-first',
  291. /* commands operating on hashes */
  292. 'HDEL' => 'keys-first',
  293. 'HEXISTS' => 'keys-first',
  294. 'HGET' => 'keys-first',
  295. 'HGETALL' => 'keys-first',
  296. 'HMGET' => 'keys-first',
  297. 'HINCRBY' => 'keys-first',
  298. 'HINCRBYFLOAT' => 'keys-first',
  299. 'HKEYS' => 'keys-first',
  300. 'HLEN' => 'keys-first',
  301. 'HSET' => 'keys-first',
  302. 'HSETNX' => 'keys-first',
  303. 'HVALS' => 'keys-first',
  304. /* scripting */
  305. 'EVAL' => 'keys-script',
  306. 'EVALSHA' => 'keys-script',
  307. );
  308. if (isset($type)) {
  309. $commands = array_filter($commands, function ($expectedType) use ($type) {
  310. return $expectedType === $type;
  311. });
  312. }
  313. return array_keys($commands);
  314. }
  315. }