ReplicationStrategyTest.php 15 KB

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