MessageFramer { public void flush() { if (buffer != null && buffer.readableBytes() > 0) { commitToSink(false, true); } } MessageFramer(Sink sink, WritableBufferAllocator bufferAllocator); void writePayload(InputStream message); void flush(); boolean isClosed(); void close(); void dispose();  }
@Test public void simpleUnknownLengthPayload() { writeUnknownLength(framer, new byte[]{3, 14}); framer.flush(); verify(sink).deliverFrame(toWriteBuffer(new byte[] {0, 0, 0, 0, 2}), false, false); verify(sink).deliverFrame(toWriteBuffer(new byte[] {3, 14}), false, true); assertEquals(2, allocator.allocCount); verifyNoMoreInteractions(sink); }
@Test public void emptyUnknownLengthPayloadYieldsFrame() throws Exception { writeUnknownLength(framer, new byte[0]); verifyZeroInteractions(sink); framer.flush(); verify(sink).deliverFrame(toWriteBuffer(new byte[] {0, 0, 0, 0, 0}), false, true); assertEquals(1, allocator.allocCount); }
@Test public void largerFrameSizeUnknownLength() throws Exception { allocator = new BytesWritableBufferAllocator(500, 500); framer = new MessageFramer(sink, allocator); writeUnknownLength(framer, new byte[1000]); framer.flush(); verify(sink, times(2)).deliverFrame(frameCaptor.capture(), eq(false), eq(false)); verify(sink).deliverFrame(frameCaptor.capture(), eq(false), eq(true)); assertEquals(5, frameCaptor.getAllValues().get(0).readableBytes()); byte[] data = new byte[5]; data[3] = 3; data[4] = (byte) 232; assertEquals(toWriteBuffer(data), frameCaptor.getAllValues().get(0)); assertEquals(500, frameCaptor.getAllValues().get(1).readableBytes()); assertEquals(500, frameCaptor.getAllValues().get(2).readableBytes()); verifyNoMoreInteractions(sink); assertEquals(3, allocator.allocCount); }