CurrentMasterChooseCommand extends AbstractMasterChooseCommand { @Override public RedisMeta choose() throws Exception { List<RedisMeta> redisMasters = getMasters(allRedises); if (isMasterExactOne(redisMasters)) { RedisMeta peerMaster = redisMasters.get(0); peerMaster.setGid(getRedisGid(peerMaster.getIp(), peerMaster.getPort())); return peerMaster; } else { getLogger().info("[choose] unexpected master size {} for {}, {}, masters: {}", redisMasters.size(), clusterId, shardId, redisMasters); } return null; } CurrentMasterChooseCommand(String clusterId, String shardId, List<RedisMeta> allRedises, ScheduledExecutorService scheduled,
                                      XpipeNettyClientKeyedObjectPool keyedObjectPool, int checkRedisTimeoutSeconds); @Override RedisMeta choose();  }
@Test(expected = IllegalStateException.class) public void testGetGidFail() throws Exception { redis1Master = true; redis2Master = false; supprtGid = false; chooseCommand.choose(); }
@Test public void testMasterTimeout() throws Exception { redis1Master = true; redis2Master = false; redis1.stop(); redis1 = null; RedisMeta redisMeta = chooseCommand.choose(); Assert.assertNull(redisMeta); }
@Test public void testSlaveTimeout() throws Exception { redis1Master = true; redis2Master = false; redis2.stop(); redis2 = null; RedisMeta redisMeta = chooseCommand.choose(); Assert.assertNotNull(redisMeta); Assert.assertEquals(redis1.getPort(), redisMeta.getPort().intValue()); Assert.assertEquals(gid, redisMeta.getGid().intValue()); }
@Test public void testNormalChoose() throws Exception { redis1Master = true; redis2Master = false; RedisMeta redisMeta = chooseCommand.choose(); Assert.assertNotNull(redisMeta); Assert.assertEquals(gid, redisMeta.getGid().intValue()); Assert.assertEquals("127.0.0.1", redisMeta.getIp()); Assert.assertEquals(redis1.getPort(), redisMeta.getPort().intValue()); }
@Test public void testChooseNoMaster() throws Exception { redis1Master = false; redis2Master = false; RedisMeta redisMeta = chooseCommand.choose(); Assert.assertNull(redisMeta); }
@Test public void testChooseMultiMaster() throws Exception { redis1Master = true; redis2Master = true; RedisMeta redisMeta = chooseCommand.choose(); Assert.assertNull(redisMeta); }