PredisStrategyTest.php 13 KB

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