RoleCommand extends AbstractRedisCommand<Role> { @Override protected Role format(Object payload) { if(payload instanceof Object[]){ Object []arrayPayload = (Object[]) payload; if(arrayPayload.length == 5){ return new SlaveRole(arrayPayload); }else if(arrayPayload.length == 3){ return new MasterRole(arrayPayload); } throw new IllegalStateException("unknown supported payload:" + StringUtil.join(",", arrayPayload)); } throw new IllegalStateException("unknown payload:" + payload); } @VisibleForTesting  RoleCommand(String host, int port, ScheduledExecutorService scheduled);  RoleCommand(String host, int port, boolean log, ScheduledExecutorService scheduled);  RoleCommand(SimpleObjectPool<NettyClient> clientPool, ScheduledExecutorService scheduled);  RoleCommand(SimpleObjectPool<NettyClient> clientPool, int timeoutMilli, boolean log, ScheduledExecutorService scheduled); @Override String getName(); @Override ByteBuf getRequest();  }
@Test public void test() throws Exception{ SlaveRole role = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0L); Server server = startServer(ByteBufUtils.readToString(role.format())); RoleCommand roleCommand = new RoleCommand(LOCAL_HOST, server.getPort(), scheduled); Role real = roleCommand.execute().get(); logger.info("[test]{}", real); Assert.assertEquals(role, real); }
@Test public void testNettyThreadLocalHoldingObjects() throws Exception { SlaveRole role = new SlaveRole(SERVER_ROLE.KEEPER, "localhost", randomPort(), MASTER_STATE.REDIS_REPL_CONNECT, 0L); Server server = startServer(ByteBufUtils.readToString(role.format())); int tasks = 5; CountDownLatch latch = new CountDownLatch(tasks); long before = MemoryPrinter.getFreeMemory(); for(int i = 0; i < tasks; i++) { RoleCommand roleCommand = new RoleCommand(LOCAL_HOST, server.getPort(), scheduled); roleCommand.execute().addListener(new CommandFutureListener<Role>() { @Override public void operationComplete(CommandFuture<Role> commandFuture) throws Exception { latch.countDown(); } }); } latch.await(); long after = MemoryPrinter.getFreeMemory(); logger.info("[delta]{}", after - before); }