Search in sources :

Example 31 with BinaryLatch

use of org.neo4j.util.concurrent.BinaryLatch 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

BinaryLatch (org.neo4j.util.concurrent.BinaryLatch)31 Test (org.junit.jupiter.api.Test)21 RepeatedTest (org.junit.jupiter.api.RepeatedTest)5 Transaction (org.neo4j.graphdb.Transaction)5 ExecutorService (java.util.concurrent.ExecutorService)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 Node (org.neo4j.graphdb.Node)4 KernelTransaction (org.neo4j.kernel.api.KernelTransaction)4 AtomicLong (java.util.concurrent.atomic.AtomicLong)3 Timeout (org.junit.jupiter.api.Timeout)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 InternalTransaction (org.neo4j.kernel.impl.coreapi.InternalTransaction)3 Flushable (java.io.Flushable)2 Path (java.nio.file.Path)2 ArrayList (java.util.ArrayList)2 Future (java.util.concurrent.Future)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 RelationshipType (org.neo4j.graphdb.RelationshipType)2