/** * Read the next decompressed {@link ByteBuf} from the {@link EmbeddedChannel} * or {@code null} if one does not exist. * * @param decompressor The channel to read from * @return The next decoded {@link ByteBuf} from the {@link EmbeddedChannel} or {@code null} if one does not exist */ private static ByteBuf nextReadableBuf(EmbeddedChannel decompressor) { for (;;) { final ByteBuf buf = decompressor.readInbound(); if (buf == null) { return null; } if (!buf.isReadable()) { buf.release(); continue; } return buf; } }
private void fetchDecoderOutput(List<Object> out) { for (;;) { ByteBuf buf = decoder.readInbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } out.add(new DefaultHttpContent(buf)); } } }
private byte[] fetchDecoderOutput() { final CompositeByteBuf decoded = Unpooled.compositeBuffer(); for (;;) { final ByteBuf buf = decoder.readInbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } decoded.addComponent(true, buf); } final byte[] ret = ByteBufUtil.getBytes(decoded); decoded.release(); return ret; } }
@Test public void shouldDecodeMultipleConsecutiveMessages() { channel.writeInbound( wrappedBuffer( new byte[]{1, 2, 3} ) ); channel.writeInbound( wrappedBuffer( new byte[0] ) ); channel.writeInbound( wrappedBuffer( new byte[]{4, 5} ) ); channel.writeInbound( wrappedBuffer( new byte[]{6} ) ); channel.writeInbound( wrappedBuffer( new byte[0] ) ); channel.writeInbound( wrappedBuffer( new byte[]{7, 8} ) ); channel.writeInbound( wrappedBuffer( new byte[]{9, 10} ) ); channel.writeInbound( wrappedBuffer( new byte[0] ) ); assertEquals( 3, channel.inboundMessages().size() ); assertByteBufEquals( wrappedBuffer( new byte[]{1, 2, 3} ), channel.readInbound() ); assertByteBufEquals( wrappedBuffer( new byte[]{4, 5, 6} ), channel.readInbound() ); assertByteBufEquals( wrappedBuffer( new byte[]{7, 8, 9, 10} ), channel.readInbound() ); }
@Test public void shouldDecodeMaxSizeChunk() { byte[] message = new byte[0xFFFF]; ByteBuf input = buffer(); input.writeShort( message.length ); input.writeBytes( message ); assertTrue( channel.writeInbound( input ) ); assertTrue( channel.finish() ); assertEquals( 1, channel.inboundMessages().size() ); assertByteBufEquals( wrappedBuffer( message ), channel.readInbound() ); } }
@Test public void shouldDecodeFullChunk() { // whole chunk with header and body arrives at once ByteBuf input = buffer(); input.writeShort( 7 ); input.writeByte( 1 ); input.writeByte( 11 ); input.writeByte( 2 ); input.writeByte( 22 ); input.writeByte( 3 ); input.writeByte( 33 ); input.writeByte( 4 ); // after buffer is written there should be something to read on the other side assertTrue( channel.writeInbound( input ) ); assertTrue( channel.finish() ); // there should only be a single chunk available for reading assertEquals( 1, channel.inboundMessages().size() ); // it should have no size header and expected body assertByteBufEquals( input.slice( 2, 7 ), channel.readInbound() ); }
@Test public void shouldDecodeMessageWithSingleChunk() { assertFalse( channel.writeInbound( wrappedBuffer( new byte[]{1, 2, 3, 4, 5} ) ) ); assertTrue( channel.writeInbound( wrappedBuffer( new byte[0] ) ) ); assertTrue( channel.finish() ); assertEquals( 1, channel.inboundMessages().size() ); assertByteBufEquals( wrappedBuffer( new byte[]{1, 2, 3, 4, 5} ), channel.readInbound() ); }
@Test public void shouldDecodeMessageWithMultipleChunks() { assertFalse( channel.writeInbound( wrappedBuffer( new byte[]{1, 2, 3} ) ) ); assertFalse( channel.writeInbound( wrappedBuffer( new byte[]{4, 5} ) ) ); assertFalse( channel.writeInbound( wrappedBuffer( new byte[]{6, 7, 8} ) ) ); assertTrue( channel.writeInbound( wrappedBuffer( new byte[0] ) ) ); assertTrue( channel.finish() ); assertEquals( 1, channel.inboundMessages().size() ); assertByteBufEquals( wrappedBuffer( new byte[]{1, 2, 3, 4, 5, 6, 7, 8} ), channel.readInbound() ); }
assertByteBufEquals( wrappedBuffer( new byte[]{1, 11, 2, 22, 3, 33, 4, 44, 5} ), channel.readInbound() );
ByteBuf partUncompressedContent = decoder.readInbound(); if (partUncompressedContent == null) { break;
@Test public void shouldDecodeEmptyChunk() { // chunk contains just the size header which is zero ByteBuf input = copyShort( 0 ); assertTrue( channel.writeInbound( input ) ); assertTrue( channel.finish() ); // there should only be a single chunk available for reading assertEquals( 1, channel.inboundMessages().size() ); // it should have no size header and empty body assertByteBufEquals( wrappedBuffer( new byte[0] ), channel.readInbound() ); }
@Test public void shouldHandleHandshakeFollowedImmediatelyByMessage() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17}, // preamble new byte[]{0, 0, 0, 0}, // first choice - no protocol new byte[]{0, 0, 0, 1}, // second choise - protocol 1 new byte[]{0, 0, 0, 0}, // third choice - no protocol new byte[]{0, 0, 0, 0}, // fourth choice - no protocol new byte[]{1, 2, 3, 4} ); // this is a message channel.writeInbound( input ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 1 ), channel.readOutbound() ); assertEquals( 1, channel.inboundMessages().size() ); assertByteBufEquals( Unpooled.wrappedBuffer( new byte[]{1, 2, 3, 4} ), channel.readInbound() ); thrown.expect( NoSuchElementException.class ); channel.pipeline().remove( ProtocolHandshaker.class ); assertTrue( channel.isActive() ); verify( protocol ).install(); }
@Test public void unrecognizedMessagesShouldBeDropped() throws Exception { ByteBuf buf = Unpooled.buffer(); buf.writeInt(1); buf.writeByte(-1); EmbeddedChannel channel = new EmbeddedChannel(new ResponseDecoder(folder.newFolder())); channel.writeInbound(buf); assertNull(channel.readInbound()); }
@Test public void unrecognizedMessagesShouldBeIgnored() throws Exception { StandbyBlobReader reader = mock(StandbyBlobReader.class); EmbeddedChannel channel = new EmbeddedChannel(new GetBlobRequestHandler(reader)); channel.writeInbound("unrecognized"); assertEquals("unrecognized", channel.readInbound()); }
@Test public void unrecognizedMessagesShouldBeIgnored() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new GetHeadRequestHandler(mock(StandbyHeadReader.class))); channel.writeInbound("unrecognized"); assertEquals("unrecognized", channel.readInbound()); }
@Test public void shouldDropInvalidMessages() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new RequestDecoder()); channel.writeInbound("Standby-CMD@clientId:z"); assertNull(channel.readInbound()); }
@Test public void shouldDecodeValidGetHeadRequests() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new RequestDecoder()); channel.writeInbound(Messages.newGetHeadRequest("clientId", false)); GetHeadRequest request = (GetHeadRequest) channel.readInbound(); assertEquals("clientId", request.getClientId()); }
@Test public void shouldDecodeValidGetBlobRequests() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new RequestDecoder()); channel.writeInbound(Messages.newGetBlobRequest("clientId", "blobId", false)); GetBlobRequest request = (GetBlobRequest) channel.readInbound(); assertEquals("clientId", request.getClientId()); assertEquals("blobId", request.getBlobId()); }
@Test public void shouldDecodeValidGetReferencesRequests() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new RequestDecoder()); channel.writeInbound(Messages.newGetReferencesRequest("clientId", "segmentId", false)); GetReferencesRequest request = (GetReferencesRequest) channel.readInbound(); assertEquals("clientId", request.getClientId()); assertEquals("segmentId", request.getSegmentId()); }
@Test public void shouldDecodeValidGetSegmentRequests() throws Exception { EmbeddedChannel channel = new EmbeddedChannel(new RequestDecoder()); channel.writeInbound(Messages.newGetSegmentRequest("clientId", "segmentId", false)); GetSegmentRequest request = (GetSegmentRequest) channel.readInbound(); assertEquals("clientId", request.getClientId()); assertEquals("segmentId", request.getSegmentId()); }