PredisStrategyTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 PredisTestCase;
  12. use Predis\Profile;
  13. /**
  14. *
  15. */
  16. class PredisStrategyTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testSupportsKeyTags()
  22. {
  23. // NOTE: 32 and 64 bits PHP runtimes can produce different hash values.
  24. $expected = PHP_INT_SIZE == 4 ? -1938594527 : 2356372769;
  25. $strategy = $this->getClusterStrategy();
  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->getClusterStrategy();
  39. $this->assertSame($this->getExpectedCommands(), $strategy->getSupportedCommands());
  40. }
  41. /**
  42. * @group disconnected
  43. */
  44. public function testReturnsNullOnUnsupportedCommand()
  45. {
  46. $strategy = $this->getClusterStrategy();
  47. $command = Profile\Factory::getDevelopment()->createCommand('ping');
  48. $this->assertNull($strategy->getHash($command));
  49. }
  50. /**
  51. * @group disconnected
  52. */
  53. public function testFirstKeyCommands()
  54. {
  55. $strategy = $this->getClusterStrategy();
  56. $profile = Profile\Factory::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->getClusterStrategy();
  69. $profile = Profile\Factory::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->getClusterStrategy();
  82. $profile = Profile\Factory::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->getClusterStrategy();
  95. $profile = Profile\Factory::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->getClusterStrategy();
  108. $profile = Profile\Factory::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->getClusterStrategy();
  121. $profile = Profile\Factory::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 testKeysForEvalCommand()
  132. {
  133. $strategy = $this->getClusterStrategy();
  134. $profile = Profile\Factory::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 testKeysForScriptCommand()
  145. {
  146. $strategy = $this->getClusterStrategy();
  147. $arguments = array('{key}:1', '{key}:2', 'value1', 'value2');
  148. $command = $this->getMock('Predis\Command\ScriptCommand', 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), "Script Command [{$command->getId()}]");
  157. }
  158. /**
  159. * @group disconnected
  160. */
  161. public function testUnsettingCommandHandler()
  162. {
  163. $strategy = $this->getClusterStrategy();
  164. $profile = Profile\Factory::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->getClusterStrategy();
  178. $profile = Profile\Factory::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 cluster strategy object.
  193. *
  194. * @return StrategyInterface
  195. */
  196. protected function getClusterStrategy()
  197. {
  198. $distributor = new Distributor\HashRing();
  199. $hashGenerator = $distributor->getHashGenerator();
  200. $strategy = new PredisStrategy($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. 'DUMP' => 'keys-first',
  225. 'RESTORE' => 'keys-first',
  226. /* commands operating on string values */
  227. 'APPEND' => 'keys-first',
  228. 'DECR' => 'keys-first',
  229. 'DECRBY' => 'keys-first',
  230. 'GET' => 'keys-first',
  231. 'GETBIT' => 'keys-first',
  232. 'MGET' => 'keys-all',
  233. 'SET' => 'keys-first',
  234. 'GETRANGE' => 'keys-first',
  235. 'GETSET' => 'keys-first',
  236. 'INCR' => 'keys-first',
  237. 'INCRBY' => 'keys-first',
  238. 'SETBIT' => 'keys-first',
  239. 'SETEX' => 'keys-first',
  240. 'MSET' => 'keys-interleaved',
  241. 'MSETNX' => 'keys-interleaved',
  242. 'SETNX' => 'keys-first',
  243. 'SETRANGE' => 'keys-first',
  244. 'STRLEN' => 'keys-first',
  245. 'SUBSTR' => 'keys-first',
  246. 'BITOP' => 'keys-bitop',
  247. 'BITCOUNT' => 'keys-first',
  248. /* commands operating on lists */
  249. 'LINSERT' => 'keys-first',
  250. 'LINDEX' => 'keys-first',
  251. 'LLEN' => 'keys-first',
  252. 'LPOP' => 'keys-first',
  253. 'RPOP' => 'keys-first',
  254. 'RPOPLPUSH' => 'keys-all',
  255. 'BLPOP' => 'keys-blockinglist',
  256. 'BRPOP' => 'keys-blockinglist',
  257. 'BRPOPLPUSH' => 'keys-blockinglist',
  258. 'LPUSH' => 'keys-first',
  259. 'LPUSHX' => 'keys-first',
  260. 'RPUSH' => 'keys-first',
  261. 'RPUSHX' => 'keys-first',
  262. 'LRANGE' => 'keys-first',
  263. 'LREM' => 'keys-first',
  264. 'LSET' => 'keys-first',
  265. 'LTRIM' => 'keys-first',
  266. /* commands operating on sets */
  267. 'SADD' => 'keys-first',
  268. 'SCARD' => 'keys-first',
  269. 'SDIFF' => 'keys-all',
  270. 'SDIFFSTORE' => 'keys-all',
  271. 'SINTER' => 'keys-all',
  272. 'SINTERSTORE' => 'keys-all',
  273. 'SUNION' => 'keys-all',
  274. 'SUNIONSTORE' => 'keys-all',
  275. 'SISMEMBER' => 'keys-first',
  276. 'SMEMBERS' => 'keys-first',
  277. 'SSCAN' => 'keys-first',
  278. 'SPOP' => 'keys-first',
  279. 'SRANDMEMBER' => 'keys-first',
  280. 'SREM' => 'keys-first',
  281. /* commands operating on sorted sets */
  282. 'ZADD' => 'keys-first',
  283. 'ZCARD' => 'keys-first',
  284. 'ZCOUNT' => 'keys-first',
  285. 'ZINCRBY' => 'keys-first',
  286. 'ZINTERSTORE' => 'keys-zaggregated',
  287. 'ZRANGE' => 'keys-first',
  288. 'ZRANGEBYSCORE' => 'keys-first',
  289. 'ZRANK' => 'keys-first',
  290. 'ZREM' => 'keys-first',
  291. 'ZREMRANGEBYRANK' => 'keys-first',
  292. 'ZREMRANGEBYSCORE' => 'keys-first',
  293. 'ZREVRANGE' => 'keys-first',
  294. 'ZREVRANGEBYSCORE' => 'keys-first',
  295. 'ZREVRANK' => 'keys-first',
  296. 'ZSCORE' => 'keys-first',
  297. 'ZUNIONSTORE' => 'keys-zaggregated',
  298. 'ZSCAN' => 'keys-first',
  299. /* commands operating on hashes */
  300. 'HDEL' => 'keys-first',
  301. 'HEXISTS' => 'keys-first',
  302. 'HGET' => 'keys-first',
  303. 'HGETALL' => 'keys-first',
  304. 'HMGET' => 'keys-first',
  305. 'HMSET' => 'keys-first',
  306. 'HINCRBY' => 'keys-first',
  307. 'HINCRBYFLOAT' => 'keys-first',
  308. 'HKEYS' => 'keys-first',
  309. 'HLEN' => 'keys-first',
  310. 'HSET' => 'keys-first',
  311. 'HSETNX' => 'keys-first',
  312. 'HVALS' => 'keys-first',
  313. 'HSCAN' => 'keys-first',
  314. /* scripting */
  315. 'EVAL' => 'keys-script',
  316. 'EVALSHA' => 'keys-script',
  317. );
  318. if (isset($type)) {
  319. $commands = array_filter($commands, function ($expectedType) use ($type) {
  320. return $expectedType === $type;
  321. });
  322. }
  323. return array_keys($commands);
  324. }
  325. }