Search in sources :

Example 1 with BoltProtocol

use of org.neo4j.bolt.BoltProtocol in project neo4j by neo4j.

the class AbstractBoltProtocolTest method shouldInstallChannelHandlersInCorrectOrder.

@Test
void shouldInstallChannelHandlersInCorrectOrder() throws Throwable {
    // Given
    BoltChannel boltChannel = newTestBoltChannel(channel);
    BoltConnectionFactory connectionFactory = mock(BoltConnectionFactory.class);
    var memoryTracker = mock(MemoryTracker.class);
    when(connectionFactory.newConnection(eq(boltChannel), any(), any())).thenReturn(mock(BoltConnection.class));
    BoltProtocol boltProtocol = new TestAbstractBoltProtocol(boltChannel, connectionFactory, mock(BoltStateMachineFactory.class), Config.defaults(), NullLogService.getInstance(), mock(TransportThrottleGroup.class), mock(ChannelProtector.class), memoryTracker);
    // When
    boltProtocol.install();
    Iterator<Map.Entry<String, ChannelHandler>> handlers = channel.pipeline().iterator();
    assertThat(handlers.next().getValue()).isInstanceOf(ChunkDecoder.class);
    assertThat(handlers.next().getValue()).isInstanceOf(MessageAccumulator.class);
    assertThat(handlers.next().getValue()).isInstanceOf(MessageDecoder.class);
    assertThat(handlers.next().getValue()).isInstanceOf(HouseKeeper.class);
    assertFalse(handlers.hasNext());
}
Also used : ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltConnectionFactory(org.neo4j.bolt.runtime.BoltConnectionFactory) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) BoltStateMachineFactory(org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) Test(org.junit.jupiter.api.Test)

Example 2 with BoltProtocol

use of org.neo4j.bolt.BoltProtocol in project neo4j by neo4j.

the class FragmentedMessageDeliveryTest method testPermutation.

private void testPermutation(byte[] unfragmented, ByteBuf[] fragments) throws Exception {
    // Given
    channel = new EmbeddedChannel();
    BoltChannel boltChannel = newTestBoltChannel(channel);
    BoltStateMachine machine = mock(BoltStateMachine.class);
    SynchronousBoltConnection boltConnection = new SynchronousBoltConnection(machine);
    NullLogService logging = NullLogService.getInstance();
    var bookmarksParser = mock(BookmarksParser.class);
    var memoryTracker = mock(MemoryTracker.class);
    BoltProtocol boltProtocol = new BoltProtocolV4(boltChannel, (ch, s, messageWriter) -> boltConnection, (v, ch, hints, mem) -> machine, Config.defaults(), bookmarksParser, logging, mock(TransportThrottleGroup.class), mock(ChannelProtector.class), memoryTracker);
    boltProtocol.install();
    // When data arrives split up according to the current permutation
    for (ByteBuf fragment : fragments) {
        channel.writeInbound(fragment.readerIndex(0).retain());
    }
    // Then the session should've received the specified messages, and the protocol should be in a nice clean state
    try {
        RequestMessage run = new RunMessage("Mjölnir", EMPTY_MAP);
        verify(machine).process(eq(run), 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);
    }
}
Also used : SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) BoltProtocolV4(org.neo4j.bolt.v4.BoltProtocolV4) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) NullLogService(org.neo4j.logging.internal.NullLogService) BoltResponseHandler(org.neo4j.bolt.runtime.BoltResponseHandler) ByteBuf(io.netty.buffer.ByteBuf) RunMessage(org.neo4j.bolt.v4.messaging.RunMessage) ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) RequestMessage(org.neo4j.bolt.messaging.RequestMessage) TransportThrottleGroup(org.neo4j.bolt.transport.TransportThrottleGroup)

Example 3 with BoltProtocol

use of org.neo4j.bolt.BoltProtocol in project neo4j by neo4j.

the class DefaultBoltProtocolFactoryTest method shouldCreateBoltProtocol.

@ParameterizedTest(name = "V{0}.{1}")
@CsvSource({ "3, 0", "4, 0", "4, 1", "4, 2", "4, 3" })
void shouldCreateBoltProtocol(int majorVersion, int minorVersion) throws Throwable {
    EmbeddedChannel channel = new EmbeddedChannel();
    BoltChannel boltChannel = newTestBoltChannel(channel);
    BoltProtocolVersion boltProtocolVersion = new BoltProtocolVersion(majorVersion, minorVersion);
    BoltStateMachineFactory stateMachineFactory = mock(BoltStateMachineFactory.class);
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    var channelProtector = mock(ChannelProtector.class);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    when(stateMachineFactory.newStateMachine(boltProtocolVersion, boltChannel, MapValue.EMPTY, memoryTracker)).thenReturn(stateMachine);
    BoltConnectionFactory connectionFactory = mock(BoltConnectionFactory.class);
    BoltConnection connection = mock(BoltConnection.class);
    when(connectionFactory.newConnection(eq(boltChannel), eq(stateMachine), any())).thenReturn(connection);
    BoltProtocolFactory factory = new DefaultBoltProtocolFactory(connectionFactory, stateMachineFactory, Config.defaults(), NullLogService.getInstance(), new TestDatabaseIdRepository(), CustomBookmarkFormatParser.DEFAULT, mock(TransportThrottleGroup.class), Clocks.fakeClock(), Duration.ZERO);
    BoltProtocol protocol = factory.create(boltProtocolVersion, boltChannel, channelProtector, memoryTracker);
    protocol.install();
    // handler with correct version is created
    assertEquals(boltProtocolVersion, protocol.version());
    // it uses the expected worker
    verify(connectionFactory).newConnection(eq(boltChannel), any(BoltStateMachine.class), any(BoltResponseMessageWriter.class));
    verify(memoryTracker, times(5)).allocateHeap(anyLong());
    // and halts this same worker when closed
    verify(connection, never()).stop();
    channel.close();
    verify(connection).stop();
    channel.finishAndReleaseAll();
}
Also used : BoltResponseMessageWriter(org.neo4j.bolt.messaging.BoltResponseMessageWriter) BoltConnectionFactory(org.neo4j.bolt.runtime.BoltConnectionFactory) BoltStateMachineFactory(org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TestDatabaseIdRepository(org.neo4j.kernel.database.TestDatabaseIdRepository) BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltConnection(org.neo4j.bolt.runtime.BoltConnection) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) BoltChannel(org.neo4j.bolt.BoltChannel) BoltTestUtil.newTestBoltChannel(org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with BoltProtocol

use of org.neo4j.bolt.BoltProtocol in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldChooseFirstAvailableProtocol.

@Test
void shouldChooseFirstAvailableProtocol() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 1 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 0 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(1), channel.readOutbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
    verify(protocol).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with BoltProtocol

use of org.neo4j.bolt.BoltProtocol in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldHandleHandshakeFollowedImmediatelyByMessage.

@Test
void shouldHandleHandshakeFollowedImmediatelyByMessage() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 1 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // this is a message
    new byte[] { 1, 2, 3, 4 });
    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());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
    verify(protocol).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

BoltProtocol (org.neo4j.bolt.BoltProtocol)15 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)12 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 Test (org.junit.jupiter.api.Test)10 BoltProtocolFactory (org.neo4j.bolt.transport.BoltProtocolFactory)10 ByteBuf (io.netty.buffer.ByteBuf)9 BoltProtocolVersion (org.neo4j.bolt.BoltProtocolVersion)5 BoltChannel (org.neo4j.bolt.BoltChannel)4 BoltTestUtil.newTestBoltChannel (org.neo4j.bolt.testing.BoltTestUtil.newTestBoltChannel)4 BoltConnectionFactory (org.neo4j.bolt.runtime.BoltConnectionFactory)3 BoltStateMachineFactory (org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory)3 ChannelProtector (org.neo4j.bolt.transport.pipeline.ChannelProtector)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 BoltConnection (org.neo4j.bolt.runtime.BoltConnection)2 BoltStateMachine (org.neo4j.bolt.runtime.statemachine.BoltStateMachine)2 TestDatabaseIdRepository (org.neo4j.kernel.database.TestDatabaseIdRepository)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1 BoltResponseMessageWriter (org.neo4j.bolt.messaging.BoltResponseMessageWriter)1