Search in sources :

Example 61 with BoltStateMachine

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

the class TransactionIT method shouldReadYourOwnWrites.

@Test
void shouldReadYourOwnWrites() throws Exception {
    var latch = new BinaryLatch();
    String bookmarkPrefix = null;
    try (var machine = newStateMachineAfterAuth()) {
        var recorder = new BoltResponseRecorder();
        machine.process(run("CREATE (n:A {prop:'one'})"), nullResponseHandler());
        machine.process(pullAll(), recorder);
        var bookmark = ((TextValue) recorder.nextResponse().metadata("bookmark")).stringValue();
        bookmarkPrefix = bookmark.split(":")[0];
    }
    var dbVersion = env.lastClosedTxId();
    var thread = new Thread(() -> {
        try (BoltStateMachine machine = newStateMachineAfterAuth()) {
            latch.await();
            var recorder = new BoltResponseRecorder();
            machine.process(run("MATCH (n:A) SET n.prop = 'two'", EMPTY_MAP), nullResponseHandler());
            machine.process(pullAll(), recorder);
        } catch (Throwable connectionFatality) {
            throw new RuntimeException(connectionFatality);
        }
    });
    thread.start();
    var dbVersionAfterWrite = dbVersion + 1;
    try (var machine = newStateMachineAfterAuth()) {
        var recorder = new BoltResponseRecorder();
        latch.release();
        var bookmark = bookmarkPrefix + ":" + dbVersionAfterWrite;
        machine.process(begin(env.databaseIdRepository(), asMapValue(singletonMap("bookmarks", List.of(bookmark)))), recorder);
        machine.process(run("MATCH (n:A) RETURN n.prop"), recorder);
        machine.process(pullAll(), recorder);
        machine.process(commit(), recorder);
        assertThat(recorder.nextResponse()).satisfies(succeeded());
        assertThat(recorder.nextResponse()).satisfies(succeeded());
        assertThat(recorder.nextResponse()).satisfies(succeededWithRecord("two"));
        assertThat(recorder.nextResponse()).satisfies(succeededWithMetadata("bookmark", BOOKMARK_PATTERN));
    }
    thread.join();
}
Also used : BoltStateMachine(org.neo4j.bolt.runtime.statemachine.BoltStateMachine) TextValue(org.neo4j.values.storable.TextValue) BoltResponseRecorder(org.neo4j.bolt.testing.BoltResponseRecorder) BinaryLatch(org.neo4j.util.concurrent.BinaryLatch) Test(org.junit.jupiter.api.Test)

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