MySQLPacketCodecEngine implements DatabasePacketCodecEngine<MySQLPacket> { @Override public void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out, final int readableBytes) { int payloadLength = in.markReaderIndex().readMediumLE(); int realPacketLength = payloadLength + MySQLPacket.PAYLOAD_LENGTH + MySQLPacket.SEQUENCE_LENGTH; if (readableBytes < realPacketLength) { in.resetReaderIndex(); return; } out.add(in.readRetainedSlice(payloadLength + MySQLPacket.SEQUENCE_LENGTH)); }  @Override boolean isValidHeader(final int readableBytes); @Override void decode(final ChannelHandlerContext context, final ByteBuf in, final List<Object> out, final int readableBytes); @Override void encode(final ChannelHandlerContext context, final MySQLPacket message, final ByteBuf out); @Override MySQLPacketPayload createPacketPayload(final ByteBuf message);  }
@Test public void assertDecode() { when(byteBuf.markReaderIndex()).thenReturn(byteBuf); when(byteBuf.readMediumLE()).thenReturn(50); when(byteBuf.readRetainedSlice(51)).thenReturn(byteBuf); List<Object> out = new LinkedList<>(); new MySQLPacketCodecEngine().decode(context, byteBuf, out, 54); assertThat(out.size(), is(1)); }
@Test public void assertDecodeWithEmptyPacket() { when(byteBuf.markReaderIndex()).thenReturn(byteBuf); when(byteBuf.readMediumLE()).thenReturn(0); List<Object> out = new LinkedList<>(); new MySQLPacketCodecEngine().decode(context, byteBuf, out, 4); assertThat(out.size(), is(1)); }
@Test public void assertDecodeWithStickyPacket() { when(byteBuf.markReaderIndex()).thenReturn(byteBuf); when(byteBuf.readMediumLE()).thenReturn(50); List<Object> out = new LinkedList<>(); new MySQLPacketCodecEngine().decode(context, byteBuf, out, 40); assertTrue(out.isEmpty()); }