RoleCommandHandler extends AbstractCommandHandler { @Override protected void doHandle(String[] args, RedisClient redisClient) { RedisKeeperServer redisKeeperServer = redisClient.getRedisKeeperServer(); ReplicationStore replicationStore = redisKeeperServer.getReplicationStore(); RedisMaster redisMaster = redisKeeperServer.getRedisMaster(); Endpoint masterEndPoint = null; if(redisMaster != null){ masterEndPoint = redisMaster.masterEndPoint(); } Object [] result = new Object[5]; result[0] = redisKeeperServer.role().toString(); result[1] = masterEndPoint == null ? "0.0.0.0": masterEndPoint.getHost(); result[2] = masterEndPoint == null ? "0": masterEndPoint.getPort(); result[3] = redisMaster == null ? MASTER_STATE.REDIS_REPL_NONE.getDesc(): redisMaster.getMasterState().getDesc(); result[4] = replicationStore == null ? -1L: replicationStore.getEndOffset(); redisClient.sendMessage(ParserManager.parse(result)); }  @Override String[] getCommands();  }
@Test public void test(){ final AtomicReference<ByteBuf> result = new AtomicReference<>(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { result.set((ByteBuf) invocation.getArguments()[0]); return null; } }).when(redisClient).sendMessage(any(ByteBuf.class)); handler.doHandle(new String[0], redisClient); sleep(10); String real = ByteBufUtils.readToString(result.get()); String expected = String.format("*5\r\n+%s\r\n+%s\r\n:%d\r\n+%s\r\n:%d\r\n", SERVER_ROLE.KEEPER.toString(), host, port, masterState.getDesc(), masterOffset); Assert.assertEquals(expected, real); Object []reverse = new ArrayParser().read(Unpooled.wrappedBuffer(real.getBytes())).getPayload(); SlaveRole slaveRole = new SlaveRole(reverse); Assert.assertEquals(SERVER_ROLE.KEEPER, slaveRole.getServerRole()); Assert.assertEquals(host, slaveRole.getMasterHost()); Assert.assertEquals(port, slaveRole.getMasterPort()); Assert.assertEquals(masterState, slaveRole.getMasterState()); Assert.assertEquals(masterOffset, slaveRole.getMasterOffset()); }