ReplicationStrategyTest.php 16 KB

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