Search in sources :

Example 1 with BoltProtocolV1

use of org.neo4j.bolt.v1.transport.BoltProtocolV1 in project neo4j by neo4j.

the class BoltKernelExtension method newVersions.

private Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> newVersions(LogService logging, WorkerFactory workerFactory) {
    Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> availableVersions = new HashMap<>();
    availableVersions.put((long) BoltProtocolV1.VERSION, (channel, isEncrypted) -> {
        BoltConnectionDescriptor descriptor = new BoltConnectionDescriptor(channel.remoteAddress(), channel.localAddress());
        BoltWorker worker = workerFactory.newWorker(descriptor, channel::close);
        return new BoltProtocolV1(worker, channel, logging);
    });
    return availableVersions;
}
Also used : BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) HashMap(java.util.HashMap) BiFunction(java.util.function.BiFunction) BoltWorker(org.neo4j.bolt.v1.runtime.BoltWorker) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1)

Example 2 with BoltProtocolV1

use of org.neo4j.bolt.v1.transport.BoltProtocolV1 in project neo4j by neo4j.

the class FragmentedMessageDeliveryTest method testPermutation.

private void testPermutation(byte[] unfragmented, ByteBuf[] fragments) throws Exception {
    // Given
    BoltStateMachine machine = mock(BoltStateMachine.class);
    Channel ch = mock(Channel.class);
    when(ch.alloc()).thenReturn(UnpooledByteBufAllocator.DEFAULT);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    when(ctx.channel()).thenReturn(ch);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), ch, NullLogService.getInstance());
    // When data arrives split up according to the current permutation
    for (ByteBuf fragment : fragments) {
        fragment.readerIndex(0).retain();
        protocol.handle(ctx, fragment);
    }
    // Then the session should've received the specified messages, and the protocol should be in a nice clean state
    try {
        verify(machine).run(eq("Mjölnir"), anyMapOf(String.class, Object.class), any(BoltResponseHandler.class));
    } catch (AssertionError e) {
        throw new AssertionError("Failed to handle fragmented delivery.\n" + "Messages: " + Arrays.toString(messages) + "\n" + "Chunk size: " + chunkSize + "\n" + "Serialized data delivered in fragments: " + describeFragments(fragments) + "\n" + "Unfragmented data: " + HexPrinter.hex(unfragmented) + "\n", e);
    } finally {
        // To avoid buffer leak errors
        protocol.close();
    }
}
Also used : SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) RecordingByteChannel(org.neo4j.bolt.v1.messaging.RecordingByteChannel) Channel(io.netty.channel.Channel) BoltResponseHandler(org.neo4j.bolt.v1.runtime.BoltResponseHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) ByteBuf(io.netty.buffer.ByteBuf)

Example 3 with BoltProtocolV1

use of org.neo4j.bolt.v1.transport.BoltProtocolV1 in project neo4j by neo4j.

the class SocketTransportHandlerTest method protocolChooser.

private ProtocolChooser protocolChooser(final BoltStateMachine machine) {
    Map<Long, BiFunction<Channel, Boolean, BoltProtocol>> availableVersions = new HashMap<>();
    availableVersions.put((long) BoltProtocolV1.VERSION, (channel, isSecure) -> new BoltProtocolV1(new SynchronousBoltWorker(machine), channel, NullLogService.getInstance()));
    return new ProtocolChooser(availableVersions, false, true);
}
Also used : SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) HashMap(java.util.HashMap) BiFunction(java.util.function.BiFunction) BoltProtocolV1(org.neo4j.bolt.v1.transport.BoltProtocolV1) ProtocolChooser(org.neo4j.bolt.transport.ProtocolChooser)

Example 4 with BoltProtocolV1

use of org.neo4j.bolt.v1.transport.BoltProtocolV1 in project neo4j by neo4j.

the class BoltProtocolV1Test method shouldNotTalkToChannelDirectlyOnFatalError.

@Test
public void shouldNotTalkToChannelDirectlyOnFatalError() throws Throwable {
    // Given
    Channel outputChannel = newChannelMock();
    BoltStateMachine machine = mock(BoltStateMachine.class);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), outputChannel, NullLogService.getInstance());
    verify(outputChannel).alloc();
    // And given inbound data that'll explode when the protocol tries to interpret it
    ByteBuf bomb = mock(ByteBuf.class);
    doThrow(IOException.class).when(bomb).readableBytes();
    // When
    protocol.handle(mock(ChannelHandlerContext.class), bomb);
    // Then the protocol should not mess with the channel (because it runs on the IO thread, and only the worker thread should produce writes)
    verifyNoMoreInteractions(outputChannel);
    // But instead make sure the state machine is shut down
    verify(machine).close();
}
Also used : SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 5 with BoltProtocolV1

use of org.neo4j.bolt.v1.transport.BoltProtocolV1 in project neo4j by neo4j.

the class BoltProtocolV1Test method closesInputAndOutput.

@Test
public void closesInputAndOutput() {
    Channel outputChannel = mock(Channel.class);
    ByteBufAllocator allocator = mock(ByteBufAllocator.class);
    ByteBuf buffer = mock(ByteBuf.class);
    when(outputChannel.alloc()).thenReturn(allocator);
    when(allocator.buffer(anyInt(), anyInt())).thenReturn(buffer);
    BoltStateMachine machine = mock(BoltStateMachine.class);
    BoltProtocolV1 protocol = new BoltProtocolV1(new SynchronousBoltWorker(machine), outputChannel, NullLogService.getInstance());
    protocol.close();
    verify(machine).close();
    verify(buffer).release();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) SynchronousBoltWorker(org.neo4j.bolt.v1.runtime.SynchronousBoltWorker) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) Channel(io.netty.channel.Channel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

SynchronousBoltWorker (org.neo4j.bolt.v1.runtime.SynchronousBoltWorker)5 ByteBuf (io.netty.buffer.ByteBuf)4 Channel (io.netty.channel.Channel)3 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)3 Test (org.junit.Test)3 BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)3 BoltProtocolV1 (org.neo4j.bolt.v1.transport.BoltProtocolV1)3 HashMap (java.util.HashMap)2 BiFunction (java.util.function.BiFunction)2 BoltWorker (org.neo4j.bolt.v1.runtime.BoltWorker)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 ProtocolChooser (org.neo4j.bolt.transport.ProtocolChooser)1 RecordingByteChannel (org.neo4j.bolt.v1.messaging.RecordingByteChannel)1 BoltConnectionDescriptor (org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor)1 BoltResponseHandler (org.neo4j.bolt.v1.runtime.BoltResponseHandler)1 SimpleLogService (org.neo4j.kernel.impl.logging.SimpleLogService)1 AssertableLogProvider (org.neo4j.logging.AssertableLogProvider)1