ReplicationStrategyTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. */
  84. public function testBitFieldCommand()
  85. {
  86. $profile = Profile\Factory::getDevelopment();
  87. $strategy = new ReplicationStrategy();
  88. $command = $profile->createCommand('BITFIELD', array('key'));
  89. $this->assertTrue(
  90. $strategy->isReadOperation($command),
  91. 'BITFIELD with no modifiers is expected to be a read operation.'
  92. );
  93. $command = $profile->createCommand('BITFIELD', array('key', 'GET', 'u4', '0'));
  94. $this->assertTrue(
  95. $strategy->isReadOperation($command),
  96. 'BITFIELD with GET only is expected to be a read operation.'
  97. );
  98. $command = $profile->createCommand('BITFIELD', array('key', 'SET', 'u4', '0', 1));
  99. $this->assertFalse(
  100. $strategy->isReadOperation($command),
  101. 'BITFIELD with SET is expected to be a write operation.'
  102. );
  103. $command = $profile->createCommand('BITFIELD', array('key', 'INCRBY', 'u4', '0', 1));
  104. $this->assertFalse(
  105. $strategy->isReadOperation($command),
  106. 'BITFIELD with INCRBY is expected to be a write operation.'
  107. );
  108. $command = $profile->createCommand('BITFIELD', array('key', 'GET', 'u4', '0', 'INCRBY', 'u4', '0', 1));
  109. $this->assertFalse(
  110. $strategy->isReadOperation($command),
  111. 'BITFIELD with GET and INCRBY is expected to be a write operation.'
  112. );
  113. $command = $profile->createCommand('BITFIELD', array('key', 'GET', 'u4', '0', 'SET', 'u4', '0', 1));
  114. $this->assertFalse(
  115. $strategy->isReadOperation($command),
  116. 'BITFIELD with GET and SET is expected to be a write operation.'
  117. );
  118. }
  119. /**
  120. * @group disconnected
  121. * @expectedException \Predis\NotSupportedException
  122. * @expectedExceptionMessage The command 'INFO' is not allowed in replication mode.
  123. */
  124. public function testUsingDisallowedCommandThrowsException()
  125. {
  126. $profile = Profile\Factory::getDevelopment();
  127. $strategy = new ReplicationStrategy();
  128. $command = $profile->createCommand('INFO');
  129. $strategy->isReadOperation($command);
  130. }
  131. /**
  132. * @group disconnected
  133. */
  134. public function testDefaultIsWriteOperation()
  135. {
  136. $strategy = new ReplicationStrategy();
  137. $command = $this->getMock('Predis\Command\CommandInterface');
  138. $command->expects($this->any())
  139. ->method('getId')
  140. ->will($this->returnValue('CMDTEST'));
  141. $this->assertFalse($strategy->isReadOperation($command));
  142. }
  143. /**
  144. * @group disconnected
  145. */
  146. public function testCanSetCommandAsReadOperation()
  147. {
  148. $strategy = new ReplicationStrategy();
  149. $command = $this->getMock('Predis\Command\CommandInterface');
  150. $command->expects($this->any())
  151. ->method('getId')
  152. ->will($this->returnValue('CMDTEST'));
  153. $strategy->setCommandReadOnly('CMDTEST', true);
  154. $this->assertTrue($strategy->isReadOperation($command));
  155. }
  156. /**
  157. * @group disconnected
  158. */
  159. public function testCanSetCommandAsWriteOperation()
  160. {
  161. $strategy = new ReplicationStrategy();
  162. $command = $this->getMock('Predis\Command\CommandInterface');
  163. $command->expects($this->any())
  164. ->method('getId')
  165. ->will($this->returnValue('CMDTEST'));
  166. $strategy->setCommandReadOnly('CMDTEST', false);
  167. $this->assertFalse($strategy->isReadOperation($command));
  168. $strategy->setCommandReadOnly('GET', false);
  169. $this->assertFalse($strategy->isReadOperation($command));
  170. }
  171. /**
  172. * @group disconnected
  173. */
  174. public function testCanUseCallableToCheckCommand()
  175. {
  176. $strategy = new ReplicationStrategy();
  177. $profile = Profile\Factory::getDevelopment();
  178. $strategy->setCommandReadOnly('SET', function ($command) {
  179. return $command->getArgument(1) === true;
  180. });
  181. $command = $profile->createCommand('SET', array('trigger', false));
  182. $this->assertFalse($strategy->isReadOperation($command));
  183. $command = $profile->createCommand('SET', array('trigger', true));
  184. $this->assertTrue($strategy->isReadOperation($command));
  185. }
  186. /**
  187. * @group disconnected
  188. */
  189. public function testSetLuaScriptAsReadOperation()
  190. {
  191. $strategy = new ReplicationStrategy();
  192. $profile = Profile\Factory::getDevelopment();
  193. $writeScript = 'redis.call("set", "foo", "bar")';
  194. $readScript = 'return true';
  195. $strategy->setScriptReadOnly($readScript, true);
  196. $cmdEval = $profile->createCommand('EVAL', array($writeScript));
  197. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($writeScript)));
  198. $this->assertFalse($strategy->isReadOperation($cmdEval));
  199. $this->assertFalse($strategy->isReadOperation($cmdEvalSHA));
  200. $cmdEval = $profile->createCommand('EVAL', array($readScript));
  201. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($readScript)));
  202. $this->assertTrue($strategy->isReadOperation($cmdEval));
  203. $this->assertTrue($strategy->isReadOperation($cmdEvalSHA));
  204. }
  205. /**
  206. * @group disconnected
  207. */
  208. public function testSetLuaScriptAsReadOperationWorksWithScriptCommand()
  209. {
  210. $strategy = new ReplicationStrategy();
  211. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  212. $command->expects($this->any())
  213. ->method('getScript')
  214. ->will($this->returnValue($script = 'return true'));
  215. $strategy->setScriptReadOnly($script, function ($command) {
  216. return $command->getArgument(2) === true;
  217. });
  218. $command->setArguments(array(false));
  219. $this->assertFalse($strategy->isReadOperation($command));
  220. $command->setArguments(array(true));
  221. $this->assertTrue($strategy->isReadOperation($command));
  222. }
  223. /**
  224. * @group disconnected
  225. */
  226. public function testSetLuaScriptAsReadOperationWorksWithScriptCommandAndCallableCheck()
  227. {
  228. $strategy = new ReplicationStrategy();
  229. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  230. $command->expects($this->any())
  231. ->method('getScript')
  232. ->will($this->returnValue($script = 'return true'));
  233. $command->setArguments(array('trigger', false));
  234. $strategy->setScriptReadOnly($script, true);
  235. $this->assertTrue($strategy->isReadOperation($command));
  236. }
  237. // ******************************************************************** //
  238. // ---- HELPER METHODS ------------------------------------------------ //
  239. // ******************************************************************** //
  240. /**
  241. * Returns the list of expected supported commands.
  242. *
  243. * @param string $type Optional type of command (based on its keys)
  244. *
  245. * @return array
  246. */
  247. protected function getExpectedCommands($type = null)
  248. {
  249. $commands = array(
  250. /* commands operating on the connection */
  251. 'AUTH' => 'read',
  252. 'SELECT' => 'read',
  253. 'ECHO' => 'read',
  254. 'QUIT' => 'read',
  255. 'OBJECT' => 'read',
  256. 'TIME' => 'read',
  257. 'SHUTDOWN' => 'disallowed',
  258. 'INFO' => 'disallowed',
  259. 'DBSIZE' => 'disallowed',
  260. 'LASTSAVE' => 'disallowed',
  261. 'CONFIG' => 'disallowed',
  262. 'MONITOR' => 'disallowed',
  263. 'SLAVEOF' => 'disallowed',
  264. 'SAVE' => 'disallowed',
  265. 'BGSAVE' => 'disallowed',
  266. 'BGREWRITEAOF' => 'disallowed',
  267. 'SLOWLOG' => 'disallowed',
  268. /* commands operating on the key space */
  269. 'EXISTS' => 'read',
  270. 'DEL' => 'write',
  271. 'TYPE' => 'read',
  272. 'EXPIRE' => 'write',
  273. 'EXPIREAT' => 'write',
  274. 'PERSIST' => 'write',
  275. 'PEXPIRE' => 'write',
  276. 'PEXPIREAT' => 'write',
  277. 'TTL' => 'read',
  278. 'PTTL' => 'write',
  279. 'SORT' => 'variable',
  280. 'KEYS' => 'read',
  281. 'SCAN' => 'read',
  282. 'RANDOMKEY' => 'read',
  283. /* commands operating on string values */
  284. 'APPEND' => 'write',
  285. 'DECR' => 'write',
  286. 'DECRBY' => 'write',
  287. 'GET' => 'read',
  288. 'GETBIT' => 'read',
  289. 'BITCOUNT' => 'read',
  290. 'BITPOS' => 'read',
  291. 'BITOP' => 'write',
  292. 'MGET' => 'read',
  293. 'SET' => 'write',
  294. 'GETRANGE' => 'read',
  295. 'GETSET' => 'write',
  296. 'INCR' => 'write',
  297. 'INCRBY' => 'write',
  298. 'INCRBYFLOAT' => 'write',
  299. 'SETBIT' => 'write',
  300. 'SETEX' => 'write',
  301. 'MSET' => 'write',
  302. 'MSETNX' => 'write',
  303. 'SETNX' => 'write',
  304. 'SETRANGE' => 'write',
  305. 'STRLEN' => 'read',
  306. 'SUBSTR' => 'read',
  307. 'BITFIELD' => 'variable',
  308. /* commands operating on lists */
  309. 'LINSERT' => 'write',
  310. 'LINDEX' => 'read',
  311. 'LLEN' => 'read',
  312. 'LPOP' => 'write',
  313. 'RPOP' => 'write',
  314. 'BLPOP' => 'write',
  315. 'BRPOP' => 'write',
  316. 'LPUSH' => 'write',
  317. 'LPUSHX' => 'write',
  318. 'RPUSH' => 'write',
  319. 'RPUSHX' => 'write',
  320. 'LRANGE' => 'read',
  321. 'LREM' => 'write',
  322. 'LSET' => 'write',
  323. 'LTRIM' => 'write',
  324. /* commands operating on sets */
  325. 'SADD' => 'write',
  326. 'SCARD' => 'read',
  327. 'SISMEMBER' => 'read',
  328. 'SMEMBERS' => 'read',
  329. 'SSCAN' => 'read',
  330. 'SRANDMEMBER' => 'read',
  331. 'SPOP' => 'write',
  332. 'SREM' => 'write',
  333. 'SINTER' => 'read',
  334. 'SUNION' => 'read',
  335. 'SDIFF' => 'read',
  336. /* commands operating on sorted sets */
  337. 'ZADD' => 'write',
  338. 'ZCARD' => 'read',
  339. 'ZCOUNT' => 'read',
  340. 'ZINCRBY' => 'write',
  341. 'ZRANGE' => 'read',
  342. 'ZRANGEBYSCORE' => 'read',
  343. 'ZRANK' => 'read',
  344. 'ZREM' => 'write',
  345. 'ZREMRANGEBYRANK' => 'write',
  346. 'ZREMRANGEBYSCORE' => 'write',
  347. 'ZREVRANGE' => 'read',
  348. 'ZREVRANGEBYSCORE' => 'read',
  349. 'ZREVRANK' => 'read',
  350. 'ZSCORE' => 'read',
  351. 'ZSCAN' => 'read',
  352. 'ZLEXCOUNT' => 'read',
  353. 'ZRANGEBYLEX' => 'read',
  354. 'ZREMRANGEBYLEX' => 'write',
  355. 'ZREVRANGEBYLEX' => 'read',
  356. /* commands operating on hashes */
  357. 'HDEL' => 'write',
  358. 'HEXISTS' => 'read',
  359. 'HGET' => 'read',
  360. 'HGETALL' => 'read',
  361. 'HMGET' => 'read',
  362. 'HINCRBY' => 'write',
  363. 'HINCRBYFLOAT' => 'write',
  364. 'HKEYS' => 'read',
  365. 'HLEN' => 'read',
  366. 'HSET' => 'write',
  367. 'HSETNX' => 'write',
  368. 'HVALS' => 'read',
  369. 'HSCAN' => 'read',
  370. 'HSTRLEN' => 'read',
  371. /* commands operating on HyperLogLog */
  372. 'PFADD' => 'write',
  373. 'PFMERGE' => 'write',
  374. 'PFCOUNT' => 'read',
  375. /* scripting */
  376. 'EVAL' => 'write',
  377. 'EVALSHA' => 'write',
  378. /* commands performing geospatial operations */
  379. 'GEOADD' => 'write',
  380. 'GEOHASH' => 'read',
  381. 'GEOPOS' => 'read',
  382. 'GEODIST' => 'read',
  383. );
  384. if (isset($type)) {
  385. $commands = array_filter($commands, function ($expectedType) use ($type) {
  386. return $expectedType === $type;
  387. });
  388. }
  389. return array_keys($commands);
  390. }
  391. }