public static BoltChannel newTestBoltChannel( String id ) { return new BoltChannel( id, "bolt", new EmbeddedChannel() ); }
public SynchronousBoltConnection( BoltStateMachine machine ) { this.channel = new EmbeddedChannel(); this.output = new ChunkedOutput( this.channel, TransportThrottleGroup.NO_THROTTLE ); this.machine = machine; }
@Before public void setup() { channel = new EmbeddedChannel(); log = mock( Log.class ); connection = mock( BoltConnection.class ); when( connection.id() ).thenReturn( channel.id().asLongText() ); when( connection.channel() ).thenReturn( channel ); }
@Test void shouldManageChannelsInChannelInitializer() { NetworkConnectionTracker connectionTracker = mock( NetworkConnectionTracker.class ); SocketTransport socketTransport = newSocketTransport( connectionTracker, NO_THROTTLE ); EmbeddedChannel channel = new EmbeddedChannel( socketTransport.channelInitializer() ); ArgumentCaptor<TrackedNetworkConnection> captor = ArgumentCaptor.forClass( TrackedNetworkConnection.class ); verify( connectionTracker ).add( captor.capture() ); verify( connectionTracker, never() ).remove( any() ); channel.close(); verify( connectionTracker ).remove( captor.getValue() ); }
@Test public void shouldNotPropagateExceptionCaught() throws Exception { ChannelInboundHandler next = mock( ChannelInboundHandler.class ); BoltConnection connection = mock( BoltConnection.class ); channel = new EmbeddedChannel( new HouseKeeper( connection, NullLog.getInstance() ), next ); channel.pipeline().fireExceptionCaught( new RuntimeException( "some exception" ) ); verify( next, never() ).exceptionCaught( any(), any() ); }
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(); }
@Test void shouldManageThrottlersInChannelInitializer() { TransportThrottleGroup throttleGroup = mock( TransportThrottleGroup.class ); SocketTransport socketTransport = newSocketTransport( NetworkConnectionTracker.NO_OP, throttleGroup ); EmbeddedChannel channel = new EmbeddedChannel( socketTransport.channelInitializer() ); verify( throttleGroup ).install( channel ); verify( throttleGroup, never() ).uninstall( channel ); channel.close(); verify( throttleGroup ).uninstall( channel ); }
@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 shouldNotPropagateChannelInactive() throws Exception { ChannelInboundHandler next = mock( ChannelInboundHandler.class ); BoltConnection connection = mock( BoltConnection.class ); channel = new EmbeddedChannel( new HouseKeeper( connection, NullLog.getInstance() ), next ); channel.pipeline().fireChannelInactive(); verify( next, never() ).channelInactive( 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 shouldStopConnectionOnExceptionCaught() { BoltConnection connection = mock( BoltConnection.class ); channel = new EmbeddedChannel( new HouseKeeper( connection, NullLog.getInstance() ) ); channel.pipeline().fireExceptionCaught( new RuntimeException( "some exception" ) ); verify( connection ).stop(); }
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() ); }
@Test void shouldInstallTransportSelectionHandler() { SocketTransport socketTransport = newSocketTransport( NetworkConnectionTracker.NO_OP, NO_THROTTLE ); EmbeddedChannel channel = new EmbeddedChannel( socketTransport.channelInitializer() ); TransportSelectionHandler handler = channel.pipeline().get( TransportSelectionHandler.class ); assertNotNull( handler ); }
@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 shouldStopConnectionOnChannelInactive() { BoltConnection connection = mock( BoltConnection.class ); channel = new EmbeddedChannel( new HouseKeeper( connection, NullLog.getInstance() ) ); channel.pipeline().fireChannelInactive(); verify( connection ).stop(); }
@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 shouldLogExceptionOnExceptionCaught() { AssertableLogProvider logProvider = new AssertableLogProvider(); BoltConnection connection = mock( BoltConnection.class ); channel = new EmbeddedChannel( new HouseKeeper( connection, logProvider.getLog( HouseKeeper.class ) ) ); RuntimeException exception = new RuntimeException( "some exception" ); channel.pipeline().fireExceptionCaught( exception ); verify( connection ).stop(); logProvider.assertExactly( inLog( HouseKeeper.class ).error( startsWith( "Fatal error occurred when handling a client connection" ), equalTo( exception ) ) ); }