Search in sources :

Example 1 with SynchronousBoltConnection

use of org.neo4j.bolt.runtime.SynchronousBoltConnection 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 2 with SynchronousBoltConnection

use of org.neo4j.bolt.runtime.SynchronousBoltConnection in project neo4j by neo4j.

the class BoltRequestMessageV3Test method unpack.

private <T extends RequestMessage> T unpack(RecordingByteChannel channel) throws Exception {
    List<RequestMessage> messages = new ArrayList<>();
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    doAnswer((Answer<Void>) invocationOnMock -> {
        RequestMessage msg = invocationOnMock.getArgument(0);
        messages.add(msg);
        return null;
    }).when(stateMachine).process(any(), any());
    BoltRequestMessageReader reader = new BoltRequestMessageReaderV3(new SynchronousBoltConnection(stateMachine), mock(BoltResponseMessageWriter.class), mock(ChannelProtector.class), NullLogService.getInstance());
    byte[] bytes = channel.getBytes();
    String serialized = HexPrinter.hex(bytes);
    Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker(new PackedInputArray(bytes));
    try {
        reader.read(unpacker);
    } catch (Throwable e) {
        throw new AssertionError("Failed to unpack message, wire data was:\n" + serialized + "[" + bytes.length + "b]", e);
    }
    return (T) messages.get(0);
}
Also used : PackedInputArray(org.neo4j.bolt.packstream.PackedInputArray) ResetMessage(org.neo4j.bolt.v3.messaging.request.ResetMessage) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnyValue(org.neo4j.values.AnyValue) MapUtil.map(org.neo4j.internal.helpers.collection.MapUtil.map) System.lineSeparator(java.lang.System.lineSeparator) NullLogService(org.neo4j.logging.internal.NullLogService) HexPrinter(org.neo4j.common.HexPrinter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) NodeValue(org.neo4j.values.virtual.NodeValue) GOODBYE_MESSAGE(org.neo4j.bolt.v3.messaging.request.GoodbyeMessage.GOODBYE_MESSAGE) BoltRequestMessageWriter(org.neo4j.bolt.messaging.BoltRequestMessageWriter) ArrayList(java.util.ArrayList) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack) Answer(org.mockito.stubbing.Answer) DiscardAllMessage(org.neo4j.bolt.v3.messaging.request.DiscardAllMessage) VirtualValues.map(org.neo4j.values.virtual.VirtualValues.map) RecordingByteChannel(org.neo4j.bolt.messaging.RecordingByteChannel) Mockito.doAnswer(org.mockito.Mockito.doAnswer) MapValue(org.neo4j.values.virtual.MapValue) PullAllMessage(org.neo4j.bolt.v3.messaging.request.PullAllMessage) ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) VirtualValues(org.neo4j.values.virtual.VirtualValues) MessageConditions.serialize(org.neo4j.bolt.testing.MessageConditions.serialize) BoltResponseMessageWriter(org.neo4j.bolt.messaging.BoltResponseMessageWriter) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) ValueUtils(org.neo4j.kernel.impl.util.ValueUtils) RelationshipValue(org.neo4j.values.virtual.RelationshipValue) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltRequestMessageReader(org.neo4j.bolt.messaging.BoltRequestMessageReader) VirtualValues.relationshipValue(org.neo4j.values.virtual.VirtualValues.relationshipValue) Values.stringArray(org.neo4j.values.storable.Values.stringArray) RequestMessage(org.neo4j.bolt.messaging.RequestMessage) BufferedChannelOutput(org.neo4j.bolt.packstream.BufferedChannelOutput) HelloMessage(org.neo4j.bolt.v3.messaging.request.HelloMessage) IOException(java.io.IOException) BeginMessage(org.neo4j.bolt.v3.messaging.request.BeginMessage) Values.stringValue(org.neo4j.values.storable.Values.stringValue) RunMessage(org.neo4j.bolt.v3.messaging.request.RunMessage) Test(org.junit.jupiter.api.Test) VirtualValues.nodeValue(org.neo4j.values.virtual.VirtualValues.nodeValue) List(java.util.List) BoltProtocolV3ComponentFactory.newNeo4jPack(org.neo4j.bolt.v3.BoltProtocolV3ComponentFactory.newNeo4jPack) COMMIT_MESSAGE(org.neo4j.bolt.v3.messaging.request.CommitMessage.COMMIT_MESSAGE) Values.intValue(org.neo4j.values.storable.Values.intValue) ROLLBACK_MESSAGE(org.neo4j.bolt.v3.messaging.request.RollbackMessage.ROLLBACK_MESSAGE) RecordMessage(org.neo4j.bolt.v3.messaging.response.RecordMessage) Mockito.mock(org.mockito.Mockito.mock) BoltResponseMessageWriter(org.neo4j.bolt.messaging.BoltResponseMessageWriter) BoltRequestMessageReader(org.neo4j.bolt.messaging.BoltRequestMessageReader) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) ArrayList(java.util.ArrayList) ChannelProtector(org.neo4j.bolt.transport.pipeline.ChannelProtector) BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) RequestMessage(org.neo4j.bolt.messaging.RequestMessage) PackedInputArray(org.neo4j.bolt.packstream.PackedInputArray) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack) BoltProtocolV3ComponentFactory.newNeo4jPack(org.neo4j.bolt.v3.BoltProtocolV3ComponentFactory.newNeo4jPack)

Example 3 with SynchronousBoltConnection

use of org.neo4j.bolt.runtime.SynchronousBoltConnection in project neo4j by neo4j.

the class MessageDecoderTest method unpack.

private void unpack(byte[] input) {
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    SynchronousBoltConnection connection = new SynchronousBoltConnection(stateMachine);
    channel = new EmbeddedChannel(newDecoder(connection));
    channel.writeInbound(Unpooled.wrappedBuffer(input));
    channel.finishAndReleaseAll();
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel)

Example 4 with SynchronousBoltConnection

use of org.neo4j.bolt.runtime.SynchronousBoltConnection in project neo4j by neo4j.

the class MessageDecoderTest method shouldDispatchRequestMessage.

@ParameterizedTest
@MethodSource("argumentsProvider")
public void shouldDispatchRequestMessage(Neo4jPack packerUnderTest) throws Exception {
    this.packerUnderTest = packerUnderTest;
    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());
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with SynchronousBoltConnection

use of org.neo4j.bolt.runtime.SynchronousBoltConnection in project neo4j by neo4j.

the class MessageDecoderTest method testUnpackableStructParametersWithKnownType.

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());
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) SynchronousBoltConnection(org.neo4j.bolt.runtime.SynchronousBoltConnection) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) MapValue(org.neo4j.values.virtual.MapValue) RunMessage(org.neo4j.bolt.v3.messaging.request.RunMessage)

Aggregations

SynchronousBoltConnection (org.neo4j.bolt.runtime.SynchronousBoltConnection)7 BoltStateMachine (org.neo4j.bolt.runtime.statemachine.BoltStateMachine)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 ChannelProtector (org.neo4j.bolt.transport.pipeline.ChannelProtector)4 Test (org.junit.jupiter.api.Test)3 BoltResponseHandler (org.neo4j.bolt.runtime.BoltResponseHandler)3 RequestMessage (org.neo4j.bolt.messaging.RequestMessage)2 Unpacker (org.neo4j.bolt.packstream.Neo4jPack.Unpacker)2 BoltConnection (org.neo4j.bolt.runtime.BoltConnection)2 RunMessage (org.neo4j.bolt.v3.messaging.request.RunMessage)2 NullLogService (org.neo4j.logging.internal.NullLogService)2 ByteBuf (io.netty.buffer.ByteBuf)1 IOException (java.io.IOException)1 System.lineSeparator (java.lang.System.lineSeparator)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1