ReplicationStrategyTest.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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
  184. ->expects($this->any())
  185. ->method('getId')
  186. ->will($this->returnValue('CMDTEST'));
  187. $this->assertFalse($strategy->isReadOperation($command));
  188. }
  189. /**
  190. * @group disconnected
  191. */
  192. public function testCanSetCommandAsReadOperation()
  193. {
  194. $strategy = new ReplicationStrategy();
  195. $command = $this->getMock('Predis\Command\CommandInterface');
  196. $command
  197. ->expects($this->any())
  198. ->method('getId')
  199. ->will($this->returnValue('CMDTEST'));
  200. $strategy->setCommandReadOnly('CMDTEST', true);
  201. $this->assertTrue($strategy->isReadOperation($command));
  202. }
  203. /**
  204. * @group disconnected
  205. */
  206. public function testCanSetCommandAsWriteOperation()
  207. {
  208. $strategy = new ReplicationStrategy();
  209. $command = $this->getMock('Predis\Command\CommandInterface');
  210. $command
  211. ->expects($this->any())
  212. ->method('getId')
  213. ->will($this->returnValue('CMDTEST'));
  214. $strategy->setCommandReadOnly('CMDTEST', false);
  215. $this->assertFalse($strategy->isReadOperation($command));
  216. $strategy->setCommandReadOnly('GET', false);
  217. $this->assertFalse($strategy->isReadOperation($command));
  218. }
  219. /**
  220. * @group disconnected
  221. */
  222. public function testCanUseCallableToCheckCommand()
  223. {
  224. $commands = $this->getCommandFactory();
  225. $strategy = new ReplicationStrategy();
  226. $strategy->setCommandReadOnly('SET', function ($command) {
  227. return $command->getArgument(1) === true;
  228. });
  229. $command = $commands->createCommand('SET', array('trigger', false));
  230. $this->assertFalse($strategy->isReadOperation($command));
  231. $command = $commands->createCommand('SET', array('trigger', true));
  232. $this->assertTrue($strategy->isReadOperation($command));
  233. }
  234. /**
  235. * @group disconnected
  236. */
  237. public function testSetLuaScriptAsReadOperation()
  238. {
  239. $commands = $this->getCommandFactory();
  240. $strategy = new ReplicationStrategy();
  241. $writeScript = 'redis.call("set", "foo", "bar")';
  242. $readScript = 'return true';
  243. $strategy->setScriptReadOnly($readScript, true);
  244. $cmdEval = $commands->createCommand('EVAL', array($writeScript));
  245. $cmdEvalSHA = $commands->createCommand('EVALSHA', array(sha1($writeScript)));
  246. $this->assertFalse($strategy->isReadOperation($cmdEval));
  247. $this->assertFalse($strategy->isReadOperation($cmdEvalSHA));
  248. $cmdEval = $commands->createCommand('EVAL', array($readScript));
  249. $cmdEvalSHA = $commands->createCommand('EVALSHA', array(sha1($readScript)));
  250. $this->assertTrue($strategy->isReadOperation($cmdEval));
  251. $this->assertTrue($strategy->isReadOperation($cmdEvalSHA));
  252. }
  253. /**
  254. * @group disconnected
  255. */
  256. public function testSetLuaScriptAsReadOperationWorksWithScriptCommand()
  257. {
  258. $strategy = new ReplicationStrategy();
  259. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  260. $command
  261. ->expects($this->any())
  262. ->method('getScript')
  263. ->will($this->returnValue($script = 'return true'));
  264. $strategy->setScriptReadOnly($script, function ($command) {
  265. return $command->getArgument(2) === true;
  266. });
  267. $command->setArguments(array(false));
  268. $this->assertFalse($strategy->isReadOperation($command));
  269. $command->setArguments(array(true));
  270. $this->assertTrue($strategy->isReadOperation($command));
  271. }
  272. /**
  273. * @group disconnected
  274. */
  275. public function testSetLuaScriptAsReadOperationWorksWithScriptCommandAndCallableCheck()
  276. {
  277. $strategy = new ReplicationStrategy();
  278. $command = $this->getMock('Predis\Command\ScriptCommand', array('getScript'));
  279. $command
  280. ->expects($this->any())
  281. ->method('getScript')
  282. ->will($this->returnValue($script = 'return true'));
  283. $command->setArguments(array('trigger', false));
  284. $strategy->setScriptReadOnly($script, true);
  285. $this->assertTrue($strategy->isReadOperation($command));
  286. }
  287. // ******************************************************************** //
  288. // ---- HELPER METHODS ------------------------------------------------ //
  289. // ******************************************************************** //
  290. /**
  291. * Returns the list of expected supported commands.
  292. *
  293. * @param string $type Optional type of command (based on its keys)
  294. *
  295. * @return array
  296. */
  297. protected function getExpectedCommands($type = null)
  298. {
  299. $commands = array(
  300. /* commands operating on the connection */
  301. 'AUTH' => 'read',
  302. 'SELECT' => 'read',
  303. 'ECHO' => 'read',
  304. 'QUIT' => 'read',
  305. 'OBJECT' => 'read',
  306. 'TIME' => 'read',
  307. 'SHUTDOWN' => 'disallowed',
  308. 'INFO' => 'disallowed',
  309. 'DBSIZE' => 'disallowed',
  310. 'LASTSAVE' => 'disallowed',
  311. 'CONFIG' => 'disallowed',
  312. 'MONITOR' => 'disallowed',
  313. 'SLAVEOF' => 'disallowed',
  314. 'SAVE' => 'disallowed',
  315. 'BGSAVE' => 'disallowed',
  316. 'BGREWRITEAOF' => 'disallowed',
  317. 'SLOWLOG' => 'disallowed',
  318. /* commands operating on the key space */
  319. 'EXISTS' => 'read',
  320. 'DEL' => 'write',
  321. 'TYPE' => 'read',
  322. 'EXPIRE' => 'write',
  323. 'EXPIREAT' => 'write',
  324. 'PERSIST' => 'write',
  325. 'PEXPIRE' => 'write',
  326. 'PEXPIREAT' => 'write',
  327. 'TTL' => 'read',
  328. 'PTTL' => 'write',
  329. 'SORT' => 'variable',
  330. 'KEYS' => 'read',
  331. 'SCAN' => 'read',
  332. 'RANDOMKEY' => 'read',
  333. /* commands operating on string values */
  334. 'APPEND' => 'write',
  335. 'DECR' => 'write',
  336. 'DECRBY' => 'write',
  337. 'GET' => 'read',
  338. 'GETBIT' => 'read',
  339. 'BITCOUNT' => 'read',
  340. 'BITPOS' => 'read',
  341. 'BITOP' => 'write',
  342. 'MGET' => 'read',
  343. 'SET' => 'write',
  344. 'GETRANGE' => 'read',
  345. 'GETSET' => 'write',
  346. 'INCR' => 'write',
  347. 'INCRBY' => 'write',
  348. 'INCRBYFLOAT' => 'write',
  349. 'SETBIT' => 'write',
  350. 'SETEX' => 'write',
  351. 'MSET' => 'write',
  352. 'MSETNX' => 'write',
  353. 'SETNX' => 'write',
  354. 'SETRANGE' => 'write',
  355. 'STRLEN' => 'read',
  356. 'SUBSTR' => 'read',
  357. 'BITFIELD' => 'variable',
  358. /* commands operating on lists */
  359. 'LINSERT' => 'write',
  360. 'LINDEX' => 'read',
  361. 'LLEN' => 'read',
  362. 'LPOP' => 'write',
  363. 'RPOP' => 'write',
  364. 'BLPOP' => 'write',
  365. 'BRPOP' => 'write',
  366. 'LPUSH' => 'write',
  367. 'LPUSHX' => 'write',
  368. 'RPUSH' => 'write',
  369. 'RPUSHX' => 'write',
  370. 'LRANGE' => 'read',
  371. 'LREM' => 'write',
  372. 'LSET' => 'write',
  373. 'LTRIM' => 'write',
  374. /* commands operating on sets */
  375. 'SADD' => 'write',
  376. 'SCARD' => 'read',
  377. 'SISMEMBER' => 'read',
  378. 'SMEMBERS' => 'read',
  379. 'SSCAN' => 'read',
  380. 'SRANDMEMBER' => 'read',
  381. 'SPOP' => 'write',
  382. 'SREM' => 'write',
  383. 'SINTER' => 'read',
  384. 'SUNION' => 'read',
  385. 'SDIFF' => 'read',
  386. /* commands operating on sorted sets */
  387. 'ZADD' => 'write',
  388. 'ZCARD' => 'read',
  389. 'ZCOUNT' => 'read',
  390. 'ZINCRBY' => 'write',
  391. 'ZRANGE' => 'read',
  392. 'ZRANGEBYSCORE' => 'read',
  393. 'ZRANK' => 'read',
  394. 'ZREM' => 'write',
  395. 'ZREMRANGEBYRANK' => 'write',
  396. 'ZREMRANGEBYSCORE' => 'write',
  397. 'ZREVRANGE' => 'read',
  398. 'ZREVRANGEBYSCORE' => 'read',
  399. 'ZREVRANK' => 'read',
  400. 'ZSCORE' => 'read',
  401. 'ZSCAN' => 'read',
  402. 'ZLEXCOUNT' => 'read',
  403. 'ZRANGEBYLEX' => 'read',
  404. 'ZREMRANGEBYLEX' => 'write',
  405. 'ZREVRANGEBYLEX' => 'read',
  406. /* commands operating on hashes */
  407. 'HDEL' => 'write',
  408. 'HEXISTS' => 'read',
  409. 'HGET' => 'read',
  410. 'HGETALL' => 'read',
  411. 'HMGET' => 'read',
  412. 'HINCRBY' => 'write',
  413. 'HINCRBYFLOAT' => 'write',
  414. 'HKEYS' => 'read',
  415. 'HLEN' => 'read',
  416. 'HSET' => 'write',
  417. 'HSETNX' => 'write',
  418. 'HVALS' => 'read',
  419. 'HSCAN' => 'read',
  420. 'HSTRLEN' => 'read',
  421. /* commands operating on HyperLogLog */
  422. 'PFADD' => 'write',
  423. 'PFMERGE' => 'write',
  424. 'PFCOUNT' => 'read',
  425. /* scripting */
  426. 'EVAL' => 'write',
  427. 'EVALSHA' => 'write',
  428. /* commands performing geospatial operations */
  429. 'GEOADD' => 'write',
  430. 'GEOHASH' => 'read',
  431. 'GEOPOS' => 'read',
  432. 'GEODIST' => 'read',
  433. 'GEORADIUS' => 'variable',
  434. 'GEORADIUSBYMEMBER' => 'variable',
  435. );
  436. if (isset($type)) {
  437. $commands = array_filter($commands, function ($expectedType) use ($type) {
  438. return $expectedType === $type;
  439. });
  440. }
  441. return array_keys($commands);
  442. }
  443. }