RedisStrategyTest.php 13 KB

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