@Override protected void decode(ChannelHandlerContext ctx, WireCommand command, List<Object> out) throws Exception { Object result = processCommand(command); if (result != null) { out.add(result); } }
private void read(ByteBuf in, List<Object> results) throws Exception { ByteBuf segmented = (ByteBuf) lengthDecoder.decode(null, in); while (segmented != null) { int before = results.size(); WireCommand command = CommandDecoder.parseCommand(segmented); if (appendDecoder.acceptInboundMessage(command)) { Request request = appendDecoder.processCommand(command); if (request != null) { results.add(request); } } else { results.add(command); } assertTrue(results.size() == before || results.size() == before + 1); segmented.release(); segmented = (ByteBuf) lengthDecoder.decode(null, in); } }
private ArrayList<Object> setupAppend(String testStream, UUID writerId, ByteBuf fakeNetwork) throws Exception { SetupAppend setupAppend = new SetupAppend(1, writerId, testStream, ""); encoder.encode(null, setupAppend, fakeNetwork); ArrayList<Object> received = new ArrayList<>(); WireCommand command = CommandDecoder.parseCommand(fakeNetwork); assertTrue(appendDecoder.acceptInboundMessage(command)); assertEquals(setupAppend, appendDecoder.processCommand(command)); return received; }
@Test public void testVerySmallBlockSize() throws Exception { @Cleanup("release") ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer(); byte[] content = new byte[100]; Arrays.fill(content, (byte) 1); Event event = new Event(Unpooled.wrappedBuffer(content)); Append msg = new Append("segment", writerId, 1, event); CommandEncoder commandEncoder = new CommandEncoder(new FixedBatchSizeTracker(3)); SetupAppend setupAppend = new SetupAppend(1, writerId, "segment", ""); commandEncoder.encode(null, setupAppend, fakeNetwork); appendDecoder.processCommand(setupAppend); ArrayList<Object> received = new ArrayList<>(); commandEncoder.encode(null, msg, fakeNetwork); read(fakeNetwork, received); assertEquals(2, received.size()); Append readAppend = (Append) received.get(1); assertEquals(msg.data.readableBytes(), readAppend.data.readableBytes()); assertEquals(content.length + TYPE_PLUS_LENGTH_SIZE, readAppend.data.readableBytes()); }