ReplicationStrategyTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 \PHPUnit_Framework_TestCase as StandardTestCase;
  12. use Predis\Command\CommandInterface;
  13. use Predis\Profile\ServerProfile;
  14. /**
  15. *
  16. */
  17. class ReplicationStrategyTest extends StandardTestCase
  18. {
  19. /**
  20. * @group disconnected
  21. */
  22. public function testReadCommands()
  23. {
  24. $profile = ServerProfile::getDevelopment();
  25. $strategy = new ReplicationStrategy();
  26. foreach ($this->getExpectedCommands('read') as $commandId) {
  27. $command = $profile->createCommand($commandId);
  28. $this->assertTrue($strategy->isReadOperation($command));
  29. }
  30. }
  31. /**
  32. * @group disconnected
  33. */
  34. public function testWriteCommands()
  35. {
  36. $profile = ServerProfile::getDevelopment();
  37. $strategy = new ReplicationStrategy();
  38. foreach ($this->getExpectedCommands('write') as $commandId) {
  39. $command = $profile->createCommand($commandId);
  40. $this->assertFalse($strategy->isReadOperation($command), $commandId);
  41. }
  42. }
  43. /**
  44. * @group disconnected
  45. */
  46. public function testDisallowedCommands()
  47. {
  48. $profile = ServerProfile::getDevelopment();
  49. $strategy = new ReplicationStrategy();
  50. foreach ($this->getExpectedCommands('disallowed') as $commandId) {
  51. $command = $profile->createCommand($commandId);
  52. $this->assertTrue($strategy->isDisallowedOperation($command), $commandId);
  53. }
  54. }
  55. /**
  56. * @group disconnected
  57. */
  58. public function testSortCommand()
  59. {
  60. $profile = ServerProfile::getDevelopment();
  61. $strategy = new ReplicationStrategy();
  62. $cmdReadSort = $profile->createCommand('SORT', array('key:list'));
  63. $this->assertTrue($strategy->isReadOperation($cmdReadSort), 'SORT [read-only]');
  64. $cmdWriteSort = $profile->createCommand('SORT', array('key:list', array('store' => 'key:stored')));
  65. $this->assertFalse($strategy->isReadOperation($cmdWriteSort), 'SORT [write with STORE]');
  66. }
  67. /**
  68. * @group disconnected
  69. * @expectedException Predis\NotSupportedException
  70. * @expectedExceptionMessage The command INFO is not allowed in replication mode
  71. */
  72. public function testUsingDisallowedCommandThrowsException()
  73. {
  74. $profile = ServerProfile::getDevelopment();
  75. $strategy = new ReplicationStrategy();
  76. $command = $profile->createCommand('INFO');
  77. $strategy->isReadOperation($command);
  78. }
  79. /**
  80. * @group disconnected
  81. */
  82. public function testDefaultIsWriteOperation()
  83. {
  84. $strategy = new ReplicationStrategy();
  85. $command = $this->getMock('Predis\Command\CommandInterface');
  86. $command->expects($this->any())
  87. ->method('getId')
  88. ->will($this->returnValue('CMDTEST'));
  89. $this->assertFalse($strategy->isReadOperation($command));
  90. }
  91. /**
  92. * @group disconnected
  93. */
  94. public function testCanSetCommandAsReadOperation()
  95. {
  96. $strategy = new ReplicationStrategy();
  97. $command = $this->getMock('Predis\Command\CommandInterface');
  98. $command->expects($this->any())
  99. ->method('getId')
  100. ->will($this->returnValue('CMDTEST'));
  101. $strategy->setCommandReadOnly('CMDTEST', true);
  102. $this->assertTrue($strategy->isReadOperation($command));
  103. }
  104. /**
  105. * @group disconnected
  106. */
  107. public function testCanSetCommandAsWriteOperation()
  108. {
  109. $strategy = new ReplicationStrategy();
  110. $command = $this->getMock('Predis\Command\CommandInterface');
  111. $command->expects($this->any())
  112. ->method('getId')
  113. ->will($this->returnValue('CMDTEST'));
  114. $strategy->setCommandReadOnly('CMDTEST', false);
  115. $this->assertFalse($strategy->isReadOperation($command));
  116. $strategy->setCommandReadOnly('GET', false);
  117. $this->assertFalse($strategy->isReadOperation($command));
  118. }
  119. /**
  120. * @group disconnected
  121. */
  122. public function testCanUseCallableToCheckCommand()
  123. {
  124. $strategy = new ReplicationStrategy();
  125. $profile = ServerProfile::getDevelopment();
  126. $strategy->setCommandReadOnly('SET', function ($command) {
  127. return $command->getArgument(1) === true;
  128. });
  129. $command = $profile->createCommand('SET', array('trigger', false));
  130. $this->assertFalse($strategy->isReadOperation($command));
  131. $command = $profile->createCommand('SET', array('trigger', true));
  132. $this->assertTrue($strategy->isReadOperation($command));
  133. }
  134. /**
  135. * @group disconnected
  136. */
  137. public function testSetLuaScriptAsReadOperation()
  138. {
  139. $strategy = new ReplicationStrategy();
  140. $profile = ServerProfile::getDevelopment();
  141. $writeScript = 'redis.call("set", "foo", "bar")';
  142. $readScript = 'return true';
  143. $strategy->setScriptReadOnly($readScript, true);
  144. $cmdEval = $profile->createCommand('EVAL', array($writeScript));
  145. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($writeScript)));
  146. $this->assertFalse($strategy->isReadOperation($cmdEval));
  147. $this->assertFalse($strategy->isReadOperation($cmdEvalSHA));
  148. $cmdEval = $profile->createCommand('EVAL', array($readScript));
  149. $cmdEvalSHA = $profile->createCommand('EVALSHA', array(sha1($readScript)));
  150. $this->assertTrue($strategy->isReadOperation($cmdEval));
  151. $this->assertTrue($strategy->isReadOperation($cmdEvalSHA));
  152. }
  153. /**
  154. * @group disconnected
  155. */
  156. public function testSetLuaScriptAsReadOperationWorksWithScriptedCommand()
  157. {
  158. $strategy = new ReplicationStrategy();
  159. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript'));
  160. $command->expects($this->any())
  161. ->method('getScript')
  162. ->will($this->returnValue($script = 'return true'));
  163. $strategy->setScriptReadOnly($script, function ($command) {
  164. return $command->getArgument(2) === true;
  165. });
  166. $command->setArguments(array(false));
  167. $this->assertFalse($strategy->isReadOperation($command));
  168. $command->setArguments(array(true));
  169. $this->assertTrue($strategy->isReadOperation($command));
  170. }
  171. /**
  172. * @group disconnected
  173. */
  174. public function testSetLuaScriptAsReadOperationWorksWithScriptedCommandAndCallableCheck()
  175. {
  176. $strategy = new ReplicationStrategy();
  177. $command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript'));
  178. $command->expects($this->any())
  179. ->method('getScript')
  180. ->will($this->returnValue($script = 'return true'));
  181. $command->setArguments(array('trigger', false));
  182. $strategy->setScriptReadOnly($script, true);
  183. $this->assertTrue($strategy->isReadOperation($command));
  184. }
  185. // ******************************************************************** //
  186. // ---- HELPER METHODS ------------------------------------------------ //
  187. // ******************************************************************** //
  188. /**
  189. * Returns the list of expected supported commands.
  190. *
  191. * @param string $type Optional type of command (based on its keys)
  192. * @return array
  193. */
  194. protected function getExpectedCommands($type = null)
  195. {
  196. $commands = array(
  197. /* commands operating on the connection */
  198. 'EXISTS' => 'read',
  199. 'AUTH' => 'read',
  200. 'SELECT' => 'read',
  201. 'ECHO' => 'read',
  202. 'QUIT' => 'read',
  203. 'OBJECT' => 'read',
  204. 'BITCOUNT' => 'read',
  205. 'TIME' => 'read',
  206. 'SHUTDOWN' => 'disallowed',
  207. 'INFO' => 'disallowed',
  208. 'DBSIZE' => 'disallowed',
  209. 'LASTSAVE' => 'disallowed',
  210. 'CONFIG' => 'disallowed',
  211. 'MONITOR' => 'disallowed',
  212. 'SLAVEOF' => 'disallowed',
  213. 'SAVE' => 'disallowed',
  214. 'BGSAVE' => 'disallowed',
  215. 'BGREWRITEAOF' => 'disallowed',
  216. 'SLOWLOG' => 'disallowed',
  217. /* commands operating on the key space */
  218. 'EXISTS' => 'read',
  219. 'DEL' => 'write',
  220. 'TYPE' => 'read',
  221. 'EXPIRE' => 'write',
  222. 'EXPIREAT' => 'write',
  223. 'PERSIST' => 'write',
  224. 'PEXPIRE' => 'write',
  225. 'PEXPIREAT' => 'write',
  226. 'TTL' => 'read',
  227. 'PTTL' => 'write',
  228. 'SORT' => 'variable',
  229. 'KEYS' => 'read',
  230. 'RANDOMKEY' => 'read',
  231. /* commands operating on string values */
  232. 'APPEND' => 'write',
  233. 'DECR' => 'write',
  234. 'DECRBY' => 'write',
  235. 'GET' => 'read',
  236. 'GETBIT' => 'read',
  237. 'MGET' => 'read',
  238. 'SET' => 'write',
  239. 'GETRANGE' => 'read',
  240. 'GETSET' => 'write',
  241. 'INCR' => 'write',
  242. 'INCRBY' => 'write',
  243. 'SETBIT' => 'write',
  244. 'SETEX' => 'write',
  245. 'MSET' => 'write',
  246. 'MSETNX' => 'write',
  247. 'SETNX' => 'write',
  248. 'SETRANGE' => 'write',
  249. 'STRLEN' => 'read',
  250. 'SUBSTR' => 'read',
  251. /* commands operating on lists */
  252. 'LINSERT' => 'write',
  253. 'LINDEX' => 'read',
  254. 'LLEN' => 'read',
  255. 'LPOP' => 'write',
  256. 'RPOP' => 'write',
  257. 'BLPOP' => 'write',
  258. 'BRPOP' => 'write',
  259. 'LPUSH' => 'write',
  260. 'LPUSHX' => 'write',
  261. 'RPUSH' => 'write',
  262. 'RPUSHX' => 'write',
  263. 'LRANGE' => 'read',
  264. 'LREM' => 'write',
  265. 'LSET' => 'write',
  266. 'LTRIM' => 'write',
  267. /* commands operating on sets */
  268. 'SADD' => 'write',
  269. 'SCARD' => 'read',
  270. 'SISMEMBER' => 'read',
  271. 'SMEMBERS' => 'read',
  272. 'SPOP' => 'write',
  273. 'SRANDMEMBER' => 'read',
  274. 'SREM' => 'write',
  275. 'SINTER' => 'read',
  276. 'SUNION' => 'read',
  277. 'SDIFF' => 'read',
  278. /* commands operating on sorted sets */
  279. 'ZADD' => 'write',
  280. 'ZCARD' => 'read',
  281. 'ZCOUNT' => 'read',
  282. 'ZINCRBY' => 'write',
  283. 'ZRANGE' => 'read',
  284. 'ZRANGEBYSCORE' => 'read',
  285. 'ZRANK' => 'read',
  286. 'ZREM' => 'write',
  287. 'ZREMRANGEBYRANK' => 'write',
  288. 'ZREMRANGEBYSCORE' => 'write',
  289. 'ZREVRANGE' => 'read',
  290. 'ZREVRANGEBYSCORE' => 'read',
  291. 'ZREVRANK' => 'read',
  292. 'ZSCORE' => 'read',
  293. /* commands operating on hashes */
  294. 'HDEL' => 'write',
  295. 'HEXISTS' => 'read',
  296. 'HGET' => 'read',
  297. 'HGETALL' => 'read',
  298. 'HMGET' => 'read',
  299. 'HINCRBY' => 'write',
  300. 'HINCRBYFLOAT' => 'write',
  301. 'HKEYS' => 'read',
  302. 'HLEN' => 'read',
  303. 'HSET' => 'write',
  304. 'HSETNX' => 'write',
  305. 'HVALS' => 'read',
  306. /* scripting */
  307. 'EVAL' => 'write',
  308. 'EVALSHA' => 'write',
  309. );
  310. if (isset($type)) {
  311. $commands = array_filter($commands, function ($expectedType) use ($type) {
  312. return $expectedType === $type;
  313. });
  314. }
  315. return array_keys($commands);
  316. }
  317. }