Search in sources :

Example 1 with JudgeResult

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

the class BulkStringEofJudgerTest method testEofMarkSmall.

@Test
public void testEofMarkSmall() {
    byte[] eofmark = randomString(RedisClientProtocol.RUN_ID_LENGTH).getBytes();
    BulkStringEofMarkJudger judger = new BulkStringEofMarkJudger(eofmark);
    for (int i = 0; i < (1 << 20); i++) {
        JudgeResult result = judger.end(Unpooled.wrappedBuffer(randomString(1).getBytes()));
        Assert.assertFalse(result.isEnd());
        Assert.assertEquals(1, result.getReadLen());
    }
    for (int i = 0; i < eofmark.length; i++) {
        JudgeResult result = judger.end(Unpooled.wrappedBuffer(new byte[] { eofmark[i] }));
        Assert.assertEquals(1, result.getReadLen());
        if (i < eofmark.length - 1) {
            Assert.assertFalse(result.isEnd());
        } else {
            Assert.assertTrue(result.isEnd());
        }
    }
}
Also used : BulkStringEofMarkJudger(com.ctrip.xpipe.redis.core.protocal.protocal.AbstractBulkStringEoFJudger.BulkStringEofMarkJudger) JudgeResult(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest) Test(org.junit.Test)

Example 2 with JudgeResult

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

the class BulkStringEofJudgerTest method testLen.

@Test
public void testLen() {
    int expectedLen = 1 << 20;
    BulkStringLengthEofJudger judger = new BulkStringLengthEofJudger(expectedLen);
    for (int i = 0; i < expectedLen - 1; i++) {
        JudgeResult result = judger.end(Unpooled.wrappedBuffer("a".getBytes()));
        Assert.assertFalse(result.isEnd());
        Assert.assertEquals(1, result.getReadLen());
    }
    JudgeResult result = judger.end(Unpooled.wrappedBuffer("a".getBytes()));
    Assert.assertTrue(result.isEnd());
    Assert.assertEquals(1, result.getReadLen());
    result = judger.end(Unpooled.wrappedBuffer("a".getBytes()));
    Assert.assertTrue(result.isEnd());
    Assert.assertEquals(0, result.getReadLen());
    try {
        result = judger.end(Unpooled.wrappedBuffer("a".getBytes()));
        Assert.fail();
    } catch (Exception e) {
    }
}
Also used : JudgeResult(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult) BulkStringLengthEofJudger(com.ctrip.xpipe.redis.core.protocal.protocal.AbstractBulkStringEoFJudger.BulkStringLengthEofJudger) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest) Test(org.junit.Test)

Example 3 with JudgeResult

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

the class BulkStringParser method read.

@Override
public RedisClientProtocol<InOutPayload> read(ByteBuf byteBuf) {
    switch(bulkStringState) {
        case READING_EOF_MARK:
            eofJudger = readEOfMark(byteBuf);
            if (eofJudger == null) {
                return null;
            }
            logger.debug("[read]{}", eofJudger);
            if (bulkStringParserListener != null) {
                bulkStringParserListener.onEofType(eofJudger.getEofType());
            }
            bulkStringState = BULK_STRING_STATE.READING_CONTENT;
            payload.startInput();
        case READING_CONTENT:
            int readerIndex = byteBuf.readerIndex();
            JudgeResult result = eofJudger.end(byteBuf.slice());
            int length = 0;
            try {
                length = payload.in(byteBuf.slice(readerIndex, result.getReadLen()));
                if (length != result.getReadLen()) {
                    throw new IllegalStateException(String.format("expected readLen:%d, but real:%d", result.getReadLen(), length));
                }
            } catch (IOException e) {
                logger.error("[read][exception]" + payload, e);
                throw new RedisRuntimeException("[write to payload exception]" + payload, e);
            }
            byteBuf.readerIndex(readerIndex + length);
            if (result.isEnd()) {
                int truncate = eofJudger.truncate();
                try {
                    if (truncate > 0) {
                        payload.endInputTruncate(truncate);
                    } else {
                        payload.endInput();
                    }
                } catch (IOException e) {
                    throw new RedisRuntimeException("[write to payload truncate exception]" + payload, e);
                }
                bulkStringState = BULK_STRING_STATE.READING_CR;
            } else {
                break;
            }
        case READING_CR:
            if (byteBuf.readableBytes() == 0) {
                return new BulkStringParser(payload);
            }
            byte data1 = byteBuf.getByte(byteBuf.readerIndex());
            if (data1 == '\r') {
                byteBuf.readByte();
                bulkStringState = BULK_STRING_STATE.READING_LF;
            } else {
                return new BulkStringParser(payload);
            }
        case READING_LF:
            if (byteBuf.readableBytes() == 0) {
                return null;
            }
            data1 = byteBuf.getByte(byteBuf.readerIndex());
            if (data1 == '\n') {
                byteBuf.readByte();
                bulkStringState = BULK_STRING_STATE.END;
            }
            return new BulkStringParser(payload);
        case END:
            return new BulkStringParser(payload);
        default:
            break;
    }
    return null;
}
Also used : IOException(java.io.IOException) JudgeResult(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult) RedisRuntimeException(com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)

Example 4 with JudgeResult

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

the class BulkStringEofJudgerTest method testEofMarkBig.

@Test
public void testEofMarkBig() {
    byte[] eofmark = randomString(RedisClientProtocol.RUN_ID_LENGTH).getBytes();
    BulkStringEofMarkJudger judger = new BulkStringEofMarkJudger(eofmark);
    String data = randomString();
    JudgeResult result = judger.end(Unpooled.wrappedBuffer(data.getBytes()));
    Assert.assertFalse(result.isEnd());
    Assert.assertEquals(data.length(), result.getReadLen());
    String real = randomString() + new String(eofmark);
    result = judger.end(Unpooled.wrappedBuffer(real.getBytes()));
    Assert.assertTrue(result.isEnd());
    Assert.assertEquals(real.length(), result.getReadLen());
}
Also used : BulkStringEofMarkJudger(com.ctrip.xpipe.redis.core.protocal.protocal.AbstractBulkStringEoFJudger.BulkStringEofMarkJudger) JudgeResult(com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult) AbstractRedisTest(com.ctrip.xpipe.redis.core.AbstractRedisTest) Test(org.junit.Test)

Aggregations

JudgeResult (com.ctrip.xpipe.redis.core.protocal.protocal.BulkStringEofJudger.JudgeResult)4 AbstractRedisTest (com.ctrip.xpipe.redis.core.AbstractRedisTest)3 Test (org.junit.Test)3 BulkStringEofMarkJudger (com.ctrip.xpipe.redis.core.protocal.protocal.AbstractBulkStringEoFJudger.BulkStringEofMarkJudger)2 RedisRuntimeException (com.ctrip.xpipe.redis.core.exception.RedisRuntimeException)1 BulkStringLengthEofJudger (com.ctrip.xpipe.redis.core.protocal.protocal.AbstractBulkStringEoFJudger.BulkStringLengthEofJudger)1 IOException (java.io.IOException)1