CredisClusterTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. require_once dirname(__FILE__).'/../Client.php';
  3. require_once dirname(__FILE__).'/../Cluster.php';
  4. require_once dirname(__FILE__).'/CredisTestCommon.php';
  5. class CredisClusterTest extends CredisTestCommon
  6. {
  7. /** @var Credis_Cluster */
  8. protected $cluster;
  9. protected function setUp()
  10. {
  11. parent::setUp();
  12. $clients = array_slice($this->redisConfig,0,4);
  13. $this->cluster = new Credis_Cluster($clients,2,$this->useStandalone);
  14. }
  15. protected function tearDown()
  16. {
  17. if($this->cluster) {
  18. $this->cluster->flushAll();
  19. foreach($this->cluster->clients() as $client){
  20. if($client->isConnected()) {
  21. $client->close();
  22. }
  23. }
  24. $this->cluster = NULL;
  25. }
  26. }
  27. public function testKeyHashing()
  28. {
  29. $this->tearDown();
  30. $this->cluster = new Credis_Cluster(array_slice($this->redisConfig, 0, 3), 2, $this->useStandalone);
  31. $keys = array();
  32. $lines = explode("\n", file_get_contents("keys.test"));
  33. foreach ($lines as $line) {
  34. $pair = explode(':', trim($line));
  35. if (count($pair) >= 2) {
  36. $keys[$pair[0]] = $pair[1];
  37. }
  38. }
  39. foreach ($keys as $key => $value) {
  40. $this->assertTrue($this->cluster->set($key, $value));
  41. }
  42. $this->cluster = new Credis_Cluster(array_slice($this->redisConfig, 0, 4), 2, true, $this->useStandalone);
  43. $hits = 0;
  44. foreach ($keys as $key => $value) {
  45. if ($this->cluster->all('get',$key)) {
  46. $hits++;
  47. }
  48. }
  49. $this->assertEquals(count($keys),$hits);
  50. }
  51. public function testAlias()
  52. {
  53. $slicedConfig = array_slice($this->redisConfig, 0, 4);
  54. foreach($slicedConfig as $config) {
  55. $this->assertEquals($config['port'],$this->cluster->client($config['alias'])->getPort());
  56. }
  57. foreach($slicedConfig as $offset => $config) {
  58. $this->assertEquals($config['port'],$this->cluster->client($offset)->getPort());
  59. }
  60. $alias = "non-existent-alias";
  61. $this->setExpectedExceptionShim('CredisException',"Client $alias does not exist.");
  62. $this->cluster->client($alias);
  63. }
  64. public function testMasterSlave()
  65. {
  66. $this->tearDown();
  67. $this->cluster = new Credis_Cluster(array($this->redisConfig[0],$this->redisConfig[6]), 2, $this->useStandalone);
  68. $this->assertTrue($this->cluster->client('master')->set('key','value'));
  69. $this->waitForSlaveReplication();
  70. $this->assertEquals('value',$this->cluster->client('slave')->get('key'));
  71. $this->assertEquals('value',$this->cluster->get('key'));
  72. try
  73. {
  74. $this->cluster->client('slave')->set('key2', 'value');
  75. $this->fail('Writing to readonly slave');
  76. }
  77. catch(CredisException $e)
  78. {
  79. }
  80. $this->tearDown();
  81. $writeOnlyConfig = $this->redisConfig[0];
  82. $writeOnlyConfig['write_only'] = true;
  83. $this->cluster = new Credis_Cluster(array($writeOnlyConfig,$this->redisConfig[6]), 2, $this->useStandalone);
  84. $this->assertTrue($this->cluster->client('master')->set('key','value'));
  85. $this->waitForSlaveReplication();
  86. $this->assertEquals('value',$this->cluster->client('slave')->get('key'));
  87. $this->assertEquals('value',$this->cluster->get('key'));
  88. $this->setExpectedExceptionShim('CredisException');
  89. $this->assertFalse($this->cluster->client('slave')->set('key2','value'));
  90. }
  91. public function testMasterWithoutSlavesAndWriteOnlyFlag()
  92. {
  93. $this->tearDown();
  94. $writeOnlyConfig = $this->redisConfig[0];
  95. $writeOnlyConfig['write_only'] = true;
  96. $this->cluster = new Credis_Cluster(array($writeOnlyConfig),2,$this->useStandalone);
  97. $this->assertTrue($this->cluster->set('key','value'));
  98. $this->assertEquals('value',$this->cluster->get('key'));
  99. }
  100. public function testDontHashForCodeCoverage()
  101. {
  102. $this->assertInternalType('array',$this->cluster->info());
  103. }
  104. public function testByHash()
  105. {
  106. $this->cluster->set('key','value');
  107. $this->assertEquals(6379,$this->cluster->byHash('key')->getPort());
  108. }
  109. public function testRwsplit()
  110. {
  111. $readOnlyCommands = array(
  112. 'EXISTS',
  113. 'TYPE',
  114. 'KEYS',
  115. 'SCAN',
  116. 'RANDOMKEY',
  117. 'TTL',
  118. 'GET',
  119. 'MGET',
  120. 'SUBSTR',
  121. 'STRLEN',
  122. 'GETRANGE',
  123. 'GETBIT',
  124. 'LLEN',
  125. 'LRANGE',
  126. 'LINDEX',
  127. 'SCARD',
  128. 'SISMEMBER',
  129. 'SINTER',
  130. 'SUNION',
  131. 'SDIFF',
  132. 'SMEMBERS',
  133. 'SSCAN',
  134. 'SRANDMEMBER',
  135. 'ZRANGE',
  136. 'ZREVRANGE',
  137. 'ZRANGEBYSCORE',
  138. 'ZREVRANGEBYSCORE',
  139. 'ZCARD',
  140. 'ZSCORE',
  141. 'ZCOUNT',
  142. 'ZRANK',
  143. 'ZREVRANK',
  144. 'ZSCAN',
  145. 'HGET',
  146. 'HMGET',
  147. 'HEXISTS',
  148. 'HLEN',
  149. 'HKEYS',
  150. 'HVALS',
  151. 'HGETALL',
  152. 'HSCAN',
  153. 'PING',
  154. 'AUTH',
  155. 'SELECT',
  156. 'ECHO',
  157. 'QUIT',
  158. 'OBJECT',
  159. 'BITCOUNT',
  160. 'TIME',
  161. 'SORT'
  162. );
  163. foreach($readOnlyCommands as $command){
  164. $this->assertTrue($this->cluster->isReadOnlyCommand($command));
  165. }
  166. $this->assertFalse($this->cluster->isReadOnlyCommand("SET"));
  167. $this->assertFalse($this->cluster->isReadOnlyCommand("HDEL"));
  168. $this->assertFalse($this->cluster->isReadOnlyCommand("RPUSH"));
  169. $this->assertFalse($this->cluster->isReadOnlyCommand("SMOVE"));
  170. $this->assertFalse($this->cluster->isReadOnlyCommand("ZADD"));
  171. }
  172. public function testCredisClientInstancesInConstructor()
  173. {
  174. $this->tearDown();
  175. $two = new Credis_Client($this->redisConfig[1]['host'], $this->redisConfig[1]['port']);
  176. $three = new Credis_Client($this->redisConfig[2]['host'], $this->redisConfig[2]['port']);
  177. $four = new Credis_Client($this->redisConfig[3]['host'], $this->redisConfig[3]['port']);
  178. $this->cluster = new Credis_Cluster(array($two,$three,$four),2,$this->useStandalone);
  179. $this->assertTrue($this->cluster->set('key','value'));
  180. $this->assertEquals('value',$this->cluster->get('key'));
  181. $this->setExpectedExceptionShim('CredisException','Server should either be an array or an instance of Credis_Client');
  182. new Credis_Cluster(array(new stdClass()),2,$this->useStandalone);
  183. }
  184. public function testSetMasterClient()
  185. {
  186. $this->tearDown();
  187. $master = new Credis_Client($this->redisConfig[0]['host'], $this->redisConfig[0]['port']);
  188. $slave = new Credis_Client($this->redisConfig[6]['host'], $this->redisConfig[6]['port']);
  189. $this->cluster = new Credis_Cluster(array($slave),2,$this->useStandalone);
  190. $this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient($master));
  191. $this->assertCount(2,$this->cluster->clients());
  192. $this->assertEquals($this->redisConfig[6]['port'], $this->cluster->client(0)->getPort());
  193. $this->assertEquals($this->redisConfig[0]['port'], $this->cluster->client('master')->getPort());
  194. $this->cluster = new Credis_Cluster(array($this->redisConfig[0]), 2, $this->useStandalone);
  195. $this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient(new Credis_Client($this->redisConfig[1]['host'], $this->redisConfig[1]['port'])));
  196. $this->assertEquals($this->redisConfig[0]['port'], $this->cluster->client('master')->getPort());
  197. $this->cluster = new Credis_Cluster(array($slave),2,$this->useStandalone);
  198. $this->assertInstanceOf('Credis_Cluster',$this->cluster->setMasterClient($master,true));
  199. $this->assertCount(1,$this->cluster->clients());
  200. }
  201. }