ReplicationStrategyTest.php 16 KB

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