RedisMasterCheckAction extends AbstractLeaderAwareHealthCheckAction { @Override protected void doTask() { getActionInstance().getRedisSession().role(new RedisSession.RollCallback() { @Override public void role(String role) { notifyListeners(new RedisMasterActionContext(instance, Server.SERVER_ROLE.of(role))); } @Override public void fail(Throwable e) { notifyListeners(new RedisMasterActionContext(instance, Server.SERVER_ROLE.UNKNOWN)); } }); } RedisMasterCheckAction(ScheduledExecutorService scheduled, RedisHealthCheckInstance instance, ExecutorService executors);   }
@Test public void testRedisMaster() throws Exception { result = new Supplier<String>() { @Override public String get() { return ROLE_MASTER; } }; instance.getRedisInstanceInfo().isMaster(false); action.doTask(); waitConditionUntilTimeOut(() -> null != context, 1000); Assert.assertFalse(context.instance().getRedisInstanceInfo().isMaster()); Assert.assertEquals(com.ctrip.xpipe.api.server.Server.SERVER_ROLE.MASTER, context.getResult()); }
@Test public void testRedisSlave() throws Exception { result = new Supplier<String>() { @Override public String get() { return ROLE_SLAVE; } }; instance.getRedisInstanceInfo().isMaster(true); action.doTask(); waitConditionUntilTimeOut(() -> null != context, 1000); Assert.assertTrue(context.instance().getRedisInstanceInfo().isMaster()); Assert.assertEquals(com.ctrip.xpipe.api.server.Server.SERVER_ROLE.SLAVE, context.getResult()); }
@Test public void testRedisHang() throws Exception { instance = newHangedRedisHealthCheckInstance(); instance.getRedisInstanceInfo().isMaster(true); action = new RedisMasterCheckAction(scheduled, instance, executors); action.addListener(listener); action.doTask(); waitConditionUntilTimeOut(() -> null != context, 1000); Assert.assertTrue(context.instance().getRedisInstanceInfo().isMaster()); Assert.assertEquals(com.ctrip.xpipe.api.server.Server.SERVER_ROLE.UNKNOWN, context.getResult()); }