Search in sources :

Example 11 with BoltStateMachine

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

the class BoltConnectionIT method shouldCloseConnectionAckFailureBeforeInit.

@Test
public void shouldCloseConnectionAckFailureBeforeInit() throws Throwable {
    // Given
    BoltStateMachine machine = env.newMachine(CONNECTION_DESCRIPTOR);
    // when
    BoltResponseRecorder recorder = new BoltResponseRecorder();
    verifyKillsConnection(() -> machine.ackFailure(recorder));
    // then
    assertThat(recorder.nextResponse(), failedWithStatus(Status.Request.Invalid));
}
Also used : BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) Test(org.junit.Test)

Example 12 with BoltStateMachine

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

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

the class SocketTransportHandlerTest method shouldInitializeProtocolOnFirstMessage.

@Test
public void shouldInitializeProtocolOnFirstMessage() throws Exception {
    BoltStateMachine machine = mock(BoltStateMachine.class);
    ProtocolChooser chooser = protocolChooser(machine);
    ChannelHandlerContext context = channelHandlerContextMock();
    SocketTransportHandler handler = new SocketTransportHandler(chooser, NullLogProvider.getInstance());
    handler.channelRead(context, handshake());
    BoltProtocol protocol1 = chooser.chosenProtocol();
    handler.channelRead(context, handshake());
    BoltProtocol protocol2 = chooser.chosenProtocol();
    assertSame(protocol1, protocol2);
}
Also used : BoltProtocol(org.neo4j.bolt.transport.BoltProtocol) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) SocketTransportHandler(org.neo4j.bolt.transport.SocketTransportHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ProtocolChooser(org.neo4j.bolt.transport.ProtocolChooser) Test(org.junit.Test)

Example 14 with BoltStateMachine

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

the class BoltConnectionIT method shouldSupportUsingPeriodicCommitInSession.

@Test
public void shouldSupportUsingPeriodicCommitInSession() throws Exception {
    // Given
    BoltStateMachine machine = env.newMachine(CONNECTION_DESCRIPTOR);
    machine.init(USER_AGENT, emptyMap(), null);
    Map<String, Object> params = new HashMap<>();
    params.put("csvFileUrl", createLocalIrisData(machine));
    long txIdBeforeQuery = env.lastClosedTxId();
    long batch = 40;
    // When
    Object[] result = runAndPull(machine, "USING PERIODIC COMMIT " + batch + "\n" + "LOAD CSV WITH HEADERS FROM {csvFileUrl} AS l\n" + "MATCH (c:Class {name: l.class_name})\n" + "CREATE (s:Sample {sepal_length: l.sepal_length, sepal_width: l.sepal_width, petal_length: l.petal_length, petal_width: l.petal_width})\n" + "CREATE (c)<-[:HAS_CLASS]-(s)\n" + "RETURN count(*) AS c", params);
    // Then
    assertThat(result.length, equalTo(1));
    Record record = (Record) result[0];
    Object[] fields = record.fields();
    assertThat(fields.length, equalTo(1));
    assertThat(fields[0], equalTo(150L));
    /*
         * 7 tokens have been created for
         * 'Sample' label
         * 'HAS_CLASS' relationship type
         * 'name', 'sepal_length', 'sepal_width', 'petal_length', and 'petal_width' property keys
         *
         * Note that the token id for the label 'Class' has been created in `createLocalIrisData(...)` so it shouldn't1
         * be counted again here
         */
    long tokensCommits = 7;
    long commits = (IRIS_DATA.split("\n").length - 1) / batch;
    long txId = env.lastClosedTxId();
    assertEquals(tokensCommits + commits + txIdBeforeQuery, txId);
}
Also used : BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) HashMap(java.util.HashMap) Record(org.neo4j.bolt.v1.runtime.spi.Record) Test(org.junit.Test)

Example 15 with BoltStateMachine

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

the class BoltConnectionIT method shouldHandleImplicitCommitFailure.

@Test
public void shouldHandleImplicitCommitFailure() throws Throwable {
    // Given
    BoltStateMachine machine = env.newMachine(CONNECTION_DESCRIPTOR);
    machine.init(USER_AGENT, emptyMap(), null);
    machine.run("CREATE (n:Victim)-[:REL]->()", EMPTY_PARAMS, nullResponseHandler());
    machine.discardAll(nullResponseHandler());
    // When I perform an action that will fail on commit
    BoltResponseRecorder recorder = new BoltResponseRecorder();
    machine.run("MATCH (n:Victim) DELETE n", EMPTY_PARAMS, recorder);
    // Then the statement running should have succeeded
    assertThat(recorder.nextResponse(), succeeded());
    recorder.reset();
    machine.discardAll(recorder);
    // But the stop should have failed, since it implicitly triggers commit and thus triggers a failure
    assertThat(recorder.nextResponse(), failedWithStatus(Status.Schema.ConstraintValidationFailed));
//assertThat( discarding.next(), failedWith( Status.Schema.ConstraintValidationFailed ) );
}
Also used : BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) Test(org.junit.Test)

Aggregations

BoltStateMachine (org.neo4j.bolt.v1.runtime.BoltStateMachine)49 Test (org.junit.Test)43 BoltResponseRecorder (org.neo4j.bolt.testing.BoltResponseRecorder)31 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 HashMap (java.util.HashMap)4 SocketTransportHandler (org.neo4j.bolt.transport.SocketTransportHandler)4 BoltConnectionFatality (org.neo4j.bolt.v1.runtime.BoltConnectionFatality)4 SynchronousBoltWorker (org.neo4j.bolt.v1.runtime.SynchronousBoltWorker)4 ByteBuf (io.netty.buffer.ByteBuf)3 Channel (io.netty.channel.Channel)3 InetSocketAddress (java.net.InetSocketAddress)3 BaseMatcher (org.hamcrest.BaseMatcher)3 Description (org.hamcrest.Description)3 BoltConnectionDescriptor (org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor)3 Record (org.neo4j.bolt.v1.runtime.spi.Record)3 ProtocolChooser (org.neo4j.bolt.transport.ProtocolChooser)2 BoltResponseHandler (org.neo4j.bolt.v1.runtime.BoltResponseHandler)2 StatementProcessor (org.neo4j.bolt.v1.runtime.StatementProcessor)2 BoltResult (org.neo4j.bolt.v1.runtime.spi.BoltResult)2 BoltProtocolV1 (org.neo4j.bolt.v1.transport.BoltProtocolV1)2