Search in sources :

Example 1 with ArrayParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser in project x-pipe by ctripcorp.

the class RoleCommandHandlerTest method test.

@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);
    // reverse
    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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayParser(com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser) ByteBuf(io.netty.buffer.ByteBuf) SlaveRole(com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test) AbstractRedisKeeperTest(com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)

Example 2 with ArrayParser

use of com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser in project x-pipe by ctripcorp.

the class DefaultRedisClient method readCommands.

@Override
public String[] readCommands(ByteBuf byteBuf) {
    while (true) {
        switch(commandState) {
            case READ_SIGN:
                if (!hasDataRead(byteBuf)) {
                    return null;
                }
                int readIndex = byteBuf.readerIndex();
                byte sign = byteBuf.getByte(readIndex);
                if (sign == RedisClientProtocol.ASTERISK_BYTE) {
                    redisClientProtocol = new ArrayParser();
                } else if (sign == '\n') {
                    byteBuf.readByte();
                    return new String[] { "\n" };
                } else {
                    redisClientProtocol = new SimpleStringParser();
                }
                commandState = COMMAND_STATE.READ_COMMANDS;
            case READ_COMMANDS:
                RedisClientProtocol<?> resultParser = redisClientProtocol.read(byteBuf);
                if (resultParser == null) {
                    return null;
                }
                Object result = resultParser.getPayload();
                if (result == null) {
                    return new String[0];
                }
                commandState = COMMAND_STATE.READ_SIGN;
                String[] ret = null;
                if (result instanceof String) {
                    ret = handleString((String) result);
                } else if (result instanceof Object[]) {
                    ret = handleArray((Object[]) result);
                } else {
                    throw new IllegalStateException("unkonw result array:" + result);
                }
                return ret;
            default:
                throw new IllegalStateException("unkonwn state:" + commandState);
        }
    }
}
Also used : SimpleStringParser(com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser) ArrayParser(com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser)

Aggregations

ArrayParser (com.ctrip.xpipe.redis.core.protocal.protocal.ArrayParser)2 SlaveRole (com.ctrip.xpipe.redis.core.protocal.pojo.SlaveRole)1 SimpleStringParser (com.ctrip.xpipe.redis.core.protocal.protocal.SimpleStringParser)1 AbstractRedisKeeperTest (com.ctrip.xpipe.redis.keeper.AbstractRedisKeeperTest)1 ByteBuf (io.netty.buffer.ByteBuf)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1