Search in sources :

Example 26 with BoltStateMachine

use of org.neo4j.bolt.runtime.statemachine.BoltStateMachine in project neo4j by neo4j.

the class BoltRequestMessageReaderV41Test method verifyBoltV3MessageIsReadAsBoltV4Message.

private static void verifyBoltV3MessageIsReadAsBoltV4Message(TransactionInitiatingMessage messageV3, TransactionInitiatingMessage messageV4) throws Exception {
    Neo4jPack neo4jPack = newNeo4jPack();
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    BoltRequestMessageReader reader = requestMessageReader(stateMachine);
    PackedInputArray input = new PackedInputArray(encode(neo4jPack, messageV3));
    Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker(input);
    reader.read(unpacker);
    verify(stateMachine).process(eq(messageV4), any());
    assertThat(messageV3.meta()).isEqualTo(messageV4.meta());
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltRequestMessageReader(org.neo4j.bolt.messaging.BoltRequestMessageReader) PackedInputArray(org.neo4j.bolt.packstream.PackedInputArray) BoltProtocolV41ComponentFactory.newNeo4jPack(org.neo4j.bolt.v41.BoltProtocolV41ComponentFactory.newNeo4jPack) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack)

Example 27 with BoltStateMachine

use of org.neo4j.bolt.runtime.statemachine.BoltStateMachine in project neo4j by neo4j.

the class BoltRequestMessageReaderV43Test method verifyBoltV3MessageIsReadAsBoltV4Message.

private static void verifyBoltV3MessageIsReadAsBoltV4Message(TransactionInitiatingMessage messageV3, TransactionInitiatingMessage messageV4) throws Exception {
    Neo4jPack neo4jPack = newNeo4jPack();
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    BoltRequestMessageReader reader = requestMessageReader(stateMachine);
    PackedInputArray input = new PackedInputArray(encode(neo4jPack, messageV3));
    Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker(input);
    reader.read(unpacker);
    verify(stateMachine).process(eq(messageV4), any());
    assertThat(messageV3.meta()).isEqualTo(messageV4.meta());
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltRequestMessageReader(org.neo4j.bolt.messaging.BoltRequestMessageReader) PackedInputArray(org.neo4j.bolt.packstream.PackedInputArray) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack) BoltProtocolV43ComponentFactory.newNeo4jPack(org.neo4j.bolt.v43.BoltProtocolV43ComponentFactory.newNeo4jPack)

Example 28 with BoltStateMachine

use of org.neo4j.bolt.runtime.statemachine.BoltStateMachine 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 29 with BoltStateMachine

use of org.neo4j.bolt.runtime.statemachine.BoltStateMachine 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 30 with BoltStateMachine

use of org.neo4j.bolt.runtime.statemachine.BoltStateMachine in project neo4j by neo4j.

the class BoltRequestMessageReaderV4Test method testMessageDecoding.

private static void testMessageDecoding(RequestMessage message) throws Exception {
    Neo4jPack neo4jPack = newNeo4jPack();
    BoltStateMachine stateMachine = mock(BoltStateMachine.class);
    BoltRequestMessageReader reader = requestMessageReader(stateMachine);
    PackedInputArray input = new PackedInputArray(encode(neo4jPack, message));
    Neo4jPack.Unpacker unpacker = neo4jPack.newUnpacker(input);
    reader.read(unpacker);
    verify(stateMachine).process(eq(message), any());
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) BoltRequestMessageReader(org.neo4j.bolt.messaging.BoltRequestMessageReader) PackedInputArray(org.neo4j.bolt.packstream.PackedInputArray) BoltProtocolV4ComponentFactory.newNeo4jPack(org.neo4j.bolt.v4.BoltProtocolV4ComponentFactory.newNeo4jPack) Neo4jPack(org.neo4j.bolt.packstream.Neo4jPack)

Aggregations

BoltStateMachine (org.neo4j.bolt.runtime.statemachine.BoltStateMachine)61 Test (org.junit.jupiter.api.Test)40 BoltResponseHandler (org.neo4j.bolt.runtime.BoltResponseHandler)11 BoltResponseRecorder (org.neo4j.bolt.testing.BoltResponseRecorder)10 FailedState (org.neo4j.bolt.v4.runtime.FailedState)10 BoltRequestMessageReader (org.neo4j.bolt.messaging.BoltRequestMessageReader)8 Neo4jPack (org.neo4j.bolt.packstream.Neo4jPack)8 PackedInputArray (org.neo4j.bolt.packstream.PackedInputArray)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 Neo4jError (org.neo4j.bolt.runtime.Neo4jError)7 SynchronousBoltConnection (org.neo4j.bolt.runtime.SynchronousBoltConnection)7 BoltProtocolVersion (org.neo4j.bolt.BoltProtocolVersion)6 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 BoltChannel (org.neo4j.bolt.BoltChannel)5 ChannelProtector (org.neo4j.bolt.transport.pipeline.ChannelProtector)4 BoltConnection (org.neo4j.bolt.runtime.BoltConnection)3 BoltConnectionAuthFatality (org.neo4j.bolt.runtime.BoltConnectionAuthFatality)3 BoltStateMachineSPI (org.neo4j.bolt.runtime.statemachine.BoltStateMachineSPI)3 StatementOutcome (org.neo4j.bolt.runtime.statemachine.impl.TransactionStateMachine.StatementOutcome)3 BoltStateMachineV4 (org.neo4j.bolt.v4.BoltStateMachineV4)3