private void decode(ByteBuf in, List<Object> out) { // call retain here as it will call release after its written to the channel decoder.writeInbound(in.retain()); fetchDecoderOutput(out); }
@Override public HttpData decode(HttpData obj) { if (obj instanceof ByteBufHolder) { decoder.writeInbound(((ByteBufHolder) obj).content()); } else { final ByteBuf compressed = Unpooled.wrappedBuffer(obj.array(), obj.offset(), obj.length()); decoder.writeInbound(compressed); } return HttpData.of(fetchDecoderOutput()); }
@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 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() ); }
private void unpack( byte[] input ) throws IOException { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( input ) ); channel.finishAndReleaseAll(); }
@Benchmark public void netty() { GET.setIndex(readerIndex, writeIndex); nettyChannel.writeInbound(GET); ByteBuf result = (ByteBuf) nettyChannel.outboundMessages().poll(); consume(result); } }
@Benchmark public void vertx() { GET.setIndex(readerIndex, writeIndex); vertxChannel.writeInbound(GET); ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll(); consume(result); }
@Test public void shouldDispatchReset() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, ResetMessage.INSTANCE ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).process( eq( ResetMessage.INSTANCE ), any() ); }
@Test public void shouldDispatchDiscardAll() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, DiscardAllMessage.INSTANCE ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).process( eq( DiscardAllMessage.INSTANCE ), any() ); }
@Test public void shouldDispatchPullAll() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, PullAllMessage.INSTANCE ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).process( eq( PullAllMessage.INSTANCE ), any() ); }
@Test public void shouldDispatchAckFailure() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, AckFailureMessage.INSTANCE ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).process( eq( AckFailureMessage.INSTANCE ), any() ); }
@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 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() ); } }
private void testUnpackableStructParametersWithKnownType( Neo4jPack packerForSerialization, AnyValue parameterValue, String expectedMessage ) throws Exception { String statement = "RETURN $x"; MapValue parameters = VirtualValues.map( new String[]{"x"}, new AnyValue[]{parameterValue } ); BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerForSerialization, new RunMessage( statement, parameters ) ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).handleExternalFailure( eq( Neo4jError.from( Status.Statement.TypeError, expectedMessage ) ), any() ); }
@Fork(value = 1, jvmArgsAppend = { "-Dvertx.threadChecks=false", "-Dvertx.disableContextTimings=true", "-Dvertx.disableTCCL=true", "-Dvertx.disableHttpHeadersValidation=true", }) @Benchmark public void vertxOpt() { GET.setIndex(readerIndex, writeIndex); vertxChannel.writeInbound(GET); ByteBuf result = (ByteBuf) vertxChannel.outboundMessages().poll(); consume(result); }
@Test public void shouldCallExternalErrorOnInitWithNullKeys() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); String userAgent = "Test/User Agent 1.0"; Map<String,Object> authToken = MapUtil.map( "scheme", "basic", null, "user", "credentials", "password" ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, new InitMessage( userAgent, authToken ) ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).handleExternalFailure( eq( Neo4jError.from( Status.Request.Invalid, "Value `null` is not supported as key in maps, must be a non-nullable string." ) ), any() ); }
@Test public void shouldDispatchRun() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); String statement = "RETURN 1"; MapValue parameters = ValueUtils.asMapValue( MapUtil.map( "param1", 1, "param2", "2", "param3", true, "param4", 5.0 ) ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, new RunMessage( statement, parameters ) ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).process( eq( new RunMessage( statement, parameters ) ), any() ); }
@Test public void shouldDispatchInit() throws Exception { BoltStateMachine stateMachine = mock( BoltStateMachine.class ); SynchronousBoltConnection connection = new SynchronousBoltConnection( stateMachine ); channel = new EmbeddedChannel( newDecoder( connection ) ); String userAgent = "Test/User Agent 1.0"; Map<String, Object> authToken = MapUtil.map( "scheme", "basic", "principal", "user", "credentials", "password" ); channel.writeInbound( Unpooled.wrappedBuffer( serialize( packerUnderTest, new InitMessage( userAgent, authToken ) ) ) ); channel.finishAndReleaseAll(); verify( stateMachine ).process( refEq( new InitMessage( userAgent, authToken ), "authToken" ), any() ); }
@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 shouldRejectIfHttp() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When FullHttpRequest request = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "http://hello_world:10000" ); request.headers().setInt( HttpHeaderNames.CONTENT_LENGTH, 0 ); channel.writeInbound( request ); // Then assertEquals( 0, channel.outboundMessages().size() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); logProvider.assertExactly( AssertableLogProvider.inLog( ProtocolHandshaker.class ).warn( "Unsupported connection type: 'HTTP'. Bolt protocol only operates over a TCP connection or WebSocket." ) ); }