ReplicationStrategyTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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\Replication;
  11. use Predis\Profile;
  12. use PredisTestCase;
  13. /**
  14. *
  15. */
  16. class ReplicationStrategyTest extends PredisTestCase
  17. {
  18. /**
  19. * @group disconnected
  20. */
  21. public function testReadCommands()
  22. {
  23. $profile = Profile\Factory::getDevelopment();
  24. $strategy = new ReplicationStrategy();
  25. foreach ($this->getExpectedCommands('read') as $commandId) {
  26. $command = $profile->createCommand($commandId);
  27. $this->assertTrue(
  28. $strategy->isReadOperation($command),
  29. "$commandId is expected to be a read operation."
  30. );
  31. }
  32. }
  33. /**
  34. * @group disconnected
  35. */
  36. public function testWriteRequests()
  37. {
  38. $profile = Profile\Factory::getDevelopment();
  39. $strategy = new ReplicationStrategy();
  40. foreach ($this->getExpectedCommands('write') as $commandId) {
  41. $command = $profile->createCommand($commandId);
  42. $this->assertFalse(
  43. $strategy->isReadOperation($command),
  44. "$commandId is expected to be a write operation."
  45. );
  46. }
  47. }
  48. /**
  49. * @group disconnected
  50. */
  51. public function testDisallowedCommands()
  52. {
  53. $profile = Profile\Factory::getDevelopment();
  54. $strategy = new ReplicationStrategy();
  55. foreach ($this->getExpectedCommands('disallowed') as $commandId) {
  56. $command = $profile->createCommand($commandId);
  57. $this->assertTrue(
  58. $strategy->isDisallowedOperation($command),
  59. "$commandId is expected to be a disallowed operation."
  60. );
  61. }
  62. }
  63. /**
  64. * @group disconnected
  65. */
  66. public function testSortCommand()
  67. {
  68. $profile = Profile\Factory::getDevelopment();
  69. $strategy = new ReplicationStrategy();
  70. $cmdReadSort = $profile->createCommand('SORT', array('key:list'));
  71. $this->assertTrue(
  72. $strategy->isReadOperation($cmdReadSort),
  73. 'SORT is expected to be a read operation.'
  74. );
  75. $cmdWriteSort = $profile->createCommand('SORT', array('key:list', array('store' => 'key:stored')));
  76. $this->assertFalse(
  77. $strategy->isReadOperation($cmdWriteSort),
  78. 'SORT with STORE is expected to be a write operation.'
  79. );
  80. }
  81. /**
  82. * @group disconnected
  83. * @expectedException \Predis\NotSupportedException
  84. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  85. */
  86. public function testUsingDisallowedCommandThrowsException()
  87. {
  88. $profile = Profile\Factory::getDevelopment();
  89. $strategy = new ReplicationStrategy();
  90. $command = $profile->createCommand('INFO');
  91. $strategy->isReadOperation($command);
  92. }
  93. /**
  94. * @group disconnected
  95. */
  96. public function testDefaultIsWriteOperation()
  97. {
  98. $strategy = new ReplicationStrategy();
  99. $command = $this->getMock('Predis\Command\CommandInterface');
  100. $command->expects($this->any())
  101. ->method('getId')
  102. ->will($this->returnValue('CMDTEST'));
  103. $this->assertFalse($strategy->isReadOperation($command));
  104. }
  105. /**
  106. * @group disconnected
  107. */
  108. public function testCanSetCommandAsReadOperation()
  109. {
  110. $strategy = new ReplicationStrategy();
  111. $command = $this->getMock('Predis\Command\CommandInterface');
  112. $command->expects($this->any())
  113. ->method('getId')
  114. ->will($this->returnValue('CMDTEST'));
  115. $strategy->setCommandReadOnly('CMDTEST', true);
  116. $this->assertTrue($strategy->isReadOperation($command));
  117. }
  118. /**
  119. * @group disconnected
  120. */
  121. public function testCanSetCommandAsWriteOperation()
  122. {
  123. $strategy = new ReplicationStrategy();
  124. $command = $this->getMock('Predis\Command\CommandInterface');
  125. $command->expects($this->any())
  126. ->method('getId')
  127. ->will($this->returnValue('CMDTEST'));
  128. $strategy->setCommandReadOnly('CMDTEST', false);
  129. $this->assertFalse($strategy->isReadOperation($command));
  130. $strategy->setCommandReadOnly('GET', false);
  131. $this->assertFalse($strategy->isReadOperation($command));
  132. }
  133. /**
  134. * @group disconnected
  135. */
  136. public function testCanUseCallableToCheckCommand()
  137. {
  138. $strategy = new ReplicationStrategy();
  139. $profile = Profile\Factory::getDevelopment();
  140. $strategy->setCommandReadOnly('SET', function ($command) {
  141. return $command->getArgument(1) === true;
  142. });
  143. $command = $profile->createCommand('SET', array('trigger', false));
  144. $this->assertFalse($strategy->isReadOperation($command));
  145. $command = $profile->createCommand('SET', array('trigger', true));
  146. $this->assertTrue($strategy->isReadOperation($command));
  147. }
  148. /**
  149. * @group disconnected
  150. */
  151. public function testSetLuaScriptAsReadOperation()
  152. {
  153. $strategy = new ReplicationStrategy();
  154. $profile = Profile\Factory::getDevelopment();
  155. $writeScript = 'redis.call("set", "foo", "bar")';
  156. $readScript = 'return true';
  157. $strategy->setScriptReadOnly($readScript, true);
  158. $cmdEval = $profile->createCommand('EVAL', array($writeScript));
  159. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($writeScript)));
  160. $this->assertFalse($strategy->isReadOperation($cmdEval));
  161. $this->assertFalse($strategy->isReadOperation($cmdEvalSHA));
  162. $cmdEval = $profile->createCommand('EVAL', array($readScript));
  163. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($readScript)));
  164. $this->assertTrue($strategy->isReadOperation($cmdEval));
  165. $this->assertTrue($strategy->isReadOperation($cmdEvalSHA));
  166. }
  167. /**
  168. * @group disconnected
  169. */
  170. public function testSetLuaScriptAsReadOperationWorksWithScriptCommand()
  171. {
  172. $strategy = new ReplicationStrategy();
  173. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  174. $command->expects($this->any())
  175. ->method('getScript')
  176. ->will($this->returnValue($script = 'return true'));
  177. $strategy->setScriptReadOnly($script, function ($command) {
  178. return $command->getArgument(2) === true;
  179. });
  180. $command->setArguments(array(false));
  181. $this->assertFalse($strategy->isReadOperation($command));
  182. $command->setArguments(array(true));
  183. $this->assertTrue($strategy->isReadOperation($command));
  184. }
  185. /**
  186. * @group disconnected
  187. */
  188. public function testSetLuaScriptAsReadOperationWorksWithScriptCommandAndCallableCheck()
  189. {
  190. $strategy = new ReplicationStrategy();
  191. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  192. $command->expects($this->any())
  193. ->method('getScript')
  194. ->will($this->returnValue($script = 'return true'));
  195. $command->setArguments(array('trigger', false));
  196. $strategy->setScriptReadOnly($script, true);
  197. $this->assertTrue($strategy->isReadOperation($command));
  198. }
  199. // ******************************************************************** //
  200. // ---- HELPER METHODS ------------------------------------------------ //
  201. // ******************************************************************** //
  202. /**
  203. * Returns the list of expected supported commands.
  204. *
  205. * @param string $type Optional type of command (based on its keys)
  206. *
  207. * @return array
  208. */
  209. protected function getExpectedCommands($type = null)
  210. {
  211. $commands = array(
  212. /* commands operating on the connection */
  213. 'AUTH' => 'read',
  214. 'SELECT' => 'read',
  215. 'ECHO' => 'read',
  216. 'QUIT' => 'read',
  217. 'OBJECT' => 'read',
  218. 'TIME' => 'read',
  219. 'SHUTDOWN' => 'disallowed',
  220. 'INFO' => 'disallowed',
  221. 'DBSIZE' => 'disallowed',
  222. 'LASTSAVE' => 'disallowed',
  223. 'CONFIG' => 'disallowed',
  224. 'MONITOR' => 'disallowed',
  225. 'SLAVEOF' => 'disallowed',
  226. 'SAVE' => 'disallowed',
  227. 'BGSAVE' => 'disallowed',
  228. 'BGREWRITEAOF' => 'disallowed',
  229. 'SLOWLOG' => 'disallowed',
  230. /* commands operating on the key space */
  231. 'EXISTS' => 'read',
  232. 'DEL' => 'write',
  233. 'TYPE' => 'read',
  234. 'EXPIRE' => 'write',
  235. 'EXPIREAT' => 'write',
  236. 'PERSIST' => 'write',
  237. 'PEXPIRE' => 'write',
  238. 'PEXPIREAT' => 'write',
  239. 'TTL' => 'read',
  240. 'PTTL' => 'write',
  241. 'SORT' => 'variable',
  242. 'KEYS' => 'read',
  243. 'SCAN' => 'read',
  244. 'RANDOMKEY' => 'read',
  245. /* commands operating on string values */
  246. 'APPEND' => 'write',
  247. 'DECR' => 'write',
  248. 'DECRBY' => 'write',
  249. 'GET' => 'read',
  250. 'GETBIT' => 'read',
  251. 'BITCOUNT' => 'read',
  252. 'BITPOS' => 'read',
  253. 'BITOP' => 'write',
  254. 'MGET' => 'read',
  255. 'SET' => 'write',
  256. 'GETRANGE' => 'read',
  257. 'GETSET' => 'write',
  258. 'INCR' => 'write',
  259. 'INCRBY' => 'write',
  260. 'INCRBYFLOAT' => 'write',
  261. 'SETBIT' => 'write',
  262. 'SETEX' => 'write',
  263. 'MSET' => 'write',
  264. 'MSETNX' => 'write',
  265. 'SETNX' => 'write',
  266. 'SETRANGE' => 'write',
  267. 'STRLEN' => 'read',
  268. 'SUBSTR' => 'read',
  269. /* commands operating on lists */
  270. 'LINSERT' => 'write',
  271. 'LINDEX' => 'read',
  272. 'LLEN' => 'read',
  273. 'LPOP' => 'write',
  274. 'RPOP' => 'write',
  275. 'BLPOP' => 'write',
  276. 'BRPOP' => 'write',
  277. 'LPUSH' => 'write',
  278. 'LPUSHX' => 'write',
  279. 'RPUSH' => 'write',
  280. 'RPUSHX' => 'write',
  281. 'LRANGE' => 'read',
  282. 'LREM' => 'write',
  283. 'LSET' => 'write',
  284. 'LTRIM' => 'write',
  285. /* commands operating on sets */
  286. 'SADD' => 'write',
  287. 'SCARD' => 'read',
  288. 'SISMEMBER' => 'read',
  289. 'SMEMBERS' => 'read',
  290. 'SSCAN' => 'read',
  291. 'SRANDMEMBER' => 'read',
  292. 'SPOP' => 'write',
  293. 'SREM' => 'write',
  294. 'SINTER' => 'read',
  295. 'SUNION' => 'read',
  296. 'SDIFF' => 'read',
  297. /* commands operating on sorted sets */
  298. 'ZADD' => 'write',
  299. 'ZCARD' => 'read',
  300. 'ZCOUNT' => 'read',
  301. 'ZINCRBY' => 'write',
  302. 'ZRANGE' => 'read',
  303. 'ZRANGEBYSCORE' => 'read',
  304. 'ZRANK' => 'read',
  305. 'ZREM' => 'write',
  306. 'ZREMRANGEBYRANK' => 'write',
  307. 'ZREMRANGEBYSCORE' => 'write',
  308. 'ZREVRANGE' => 'read',
  309. 'ZREVRANGEBYSCORE' => 'read',
  310. 'ZREVRANK' => 'read',
  311. 'ZSCORE' => 'read',
  312. 'ZSCAN' => 'read',
  313. 'ZLEXCOUNT' => 'read',
  314. 'ZRANGEBYLEX' => 'read',
  315. 'ZREMRANGEBYLEX' => 'write',
  316. 'ZREVRANGEBYLEX' => 'read',
  317. /* commands operating on hashes */
  318. 'HDEL' => 'write',
  319. 'HEXISTS' => 'read',
  320. 'HGET' => 'read',
  321. 'HGETALL' => 'read',
  322. 'HMGET' => 'read',
  323. 'HINCRBY' => 'write',
  324. 'HINCRBYFLOAT' => 'write',
  325. 'HKEYS' => 'read',
  326. 'HLEN' => 'read',
  327. 'HSET' => 'write',
  328. 'HSETNX' => 'write',
  329. 'HVALS' => 'read',
  330. 'HSCAN' => 'read',
  331. /* commands operating on HyperLogLog */
  332. 'PFADD' => 'write',
  333. 'PFMERGE' => 'write',
  334. 'PFCOUNT' => 'read',
  335. /* scripting */
  336. 'EVAL' => 'write',
  337. 'EVALSHA' => 'write',
  338. );
  339. if (isset($type)) {
  340. $commands = array_filter($commands, function ($expectedType) use ($type) {
  341. return $expectedType === $type;
  342. });
  343. }
  344. return array_keys($commands);
  345. }
  346. }