Search in sources :

Example 1 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class TransactionIT method shouldTerminateQueriesEvenIfUsingPeriodicCommit.

@Test
public void shouldTerminateQueriesEvenIfUsingPeriodicCommit() throws Exception {
    // Spawns a throttled HTTP server, runs a PERIODIC COMMIT that fetches data from this server,
    // and checks that the query able to be terminated
    // We start with 3, because that is how many actors we have -
    // 1. the http server
    // 2. the running query
    // 3. the one terminating 2
    final DoubleLatch latch = new DoubleLatch(3, true);
    // This is used to block the http server between the first and second batch
    final Barrier.Control barrier = new Barrier.Control();
    // Serve CSV via local web server, let Jetty find a random port for us
    Server server = createHttpServer(latch, barrier, 20, 30);
    server.start();
    int localPort = getLocalPort(server);
    final BoltStateMachine[] machine = { null };
    Thread thread = new Thread() {

        @Override
        public void run() {
            try (BoltStateMachine stateMachine = env.newMachine(new BoltConnectionDescriptor(new InetSocketAddress("<testClient>", 56789), new InetSocketAddress("<writeServer>", 7468)))) {
                machine[0] = stateMachine;
                stateMachine.init(USER_AGENT, emptyMap(), null);
                String query = format("USING PERIODIC COMMIT 10 LOAD CSV FROM 'http://localhost:%d' AS line " + "CREATE (n:A {id: line[0], square: line[1]}) " + "WITH count(*) as number " + "CREATE (n:ShouldNotExist)", localPort);
                try {
                    latch.start();
                    stateMachine.run(query, emptyMap(), nullResponseHandler());
                    stateMachine.pullAll(nullResponseHandler());
                } finally {
                    latch.finish();
                }
            } catch (BoltConnectionFatality connectionFatality) {
                throw new RuntimeException(connectionFatality);
            }
        }
    };
    thread.setName("query runner");
    thread.start();
    // We block this thread here, waiting for the http server to spin up and the running query to get started
    latch.startAndWaitForAllToStart();
    Thread.sleep(1000);
    // This is the call that RESETs the Bolt connection and will terminate the running query
    machine[0].reset(nullResponseHandler());
    barrier.release();
    // We block again here, waiting for the running query to have been terminated, and for the server to have
    // wrapped up and finished streaming http results
    latch.finishAndWaitForAllToFinish();
    // And now we check that the last node did not get created
    try (Transaction ignored = env.graph().beginTx()) {
        assertFalse("Query was not terminated in time - nodes were created!", env.graph().findNodes(Label.label("ShouldNotExist")).hasNext());
    }
}
Also used : Server(org.eclipse.jetty.server.Server) InetSocketAddress(java.net.InetSocketAddress) BoltConnectionFatality(org.neo4j.bolt.v1.runtime.BoltConnectionFatality) Barrier(org.neo4j.test.Barrier) BoltConnectionDescriptor(org.neo4j.bolt.v1.runtime.BoltConnectionDescriptor) Transaction(org.neo4j.graphdb.Transaction) BoltStateMachine(org.neo4j.bolt.v1.runtime.BoltStateMachine) DoubleLatch(org.neo4j.test.DoubleLatch) Test(org.junit.Test)

Example 2 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class NodeCountsTest method shouldNotSeeNodeCountsOfOtherTransaction.

@Test
public void shouldNotSeeNodeCountsOfOtherTransaction() throws Exception {
    // given
    GraphDatabaseService graphDb = db.getGraphDatabaseAPI();
    final Barrier.Control barrier = new Barrier.Control();
    long before = numberOfNodes();
    Future<Long> done = threading.execute(new NamedFunction<GraphDatabaseService, Long>("create-nodes") {

        @Override
        public Long apply(GraphDatabaseService graphDb) {
            try (Transaction tx = graphDb.beginTx()) {
                graphDb.createNode();
                graphDb.createNode();
                barrier.reached();
                long whatThisThreadSees = countsForNode();
                tx.success();
                return whatThisThreadSees;
            }
        }
    }, graphDb);
    barrier.await();
    // when
    long during = numberOfNodes();
    barrier.release();
    long whatOtherThreadSees = done.get();
    long after = numberOfNodes();
    // then
    assertEquals(0, before);
    assertEquals(0, during);
    assertEquals(after, whatOtherThreadSees);
    assertEquals(2, after);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Barrier(org.neo4j.test.Barrier) Test(org.junit.Test)

Example 3 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class RelationshipCountsTest method shouldNotCountRelationshipsDeletedInOtherTransaction.

@Test
public void shouldNotCountRelationshipsDeletedInOtherTransaction() throws Exception {
    // given
    GraphDatabaseService graphDb = db.getGraphDatabaseAPI();
    final Relationship rel;
    try (Transaction tx = graphDb.beginTx()) {
        Node node = graphDb.createNode();
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        rel = node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        tx.success();
    }
    final Barrier.Control barrier = new Barrier.Control();
    long before = numberOfRelationships();
    Future<Long> tx = threading.execute(new NamedFunction<GraphDatabaseService, Long>("create-relationships") {

        @Override
        public Long apply(GraphDatabaseService graphDb) {
            try (Transaction tx = graphDb.beginTx()) {
                rel.delete();
                long whatThisThreadSees = countsForRelationship(null, null, null);
                barrier.reached();
                tx.success();
                return whatThisThreadSees;
            }
        }
    }, graphDb);
    barrier.await();
    // when
    long during = numberOfRelationships();
    barrier.release();
    long whatOtherThreadSees = tx.get();
    long after = numberOfRelationships();
    // then
    assertEquals(3, before);
    assertEquals(3, during);
    assertEquals(2, after);
    assertEquals(after, whatOtherThreadSees);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) Barrier(org.neo4j.test.Barrier) Test(org.junit.Test)

Example 4 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class BuiltInProceduresInteractionTestBase method shouldListQueriesEvenIfUsingPeriodicCommit.

@SuppressWarnings("unchecked")
@Test
public void shouldListQueriesEvenIfUsingPeriodicCommit() throws Throwable {
    for (int i = 8; i <= 11; i++) {
        // Spawns a throttled HTTP server, runs a PERIODIC COMMIT that fetches data from this server,
        // and checks that the query is visible when using listQueries()
        // Given
        final DoubleLatch latch = new DoubleLatch(3, true);
        final Barrier.Control barrier = new Barrier.Control();
        // Serve CSV via local web server, let Jetty find a random port for us
        Server server = createHttpServer(latch, barrier, i, 50 - i);
        server.start();
        int localPort = getLocalPort(server);
        OffsetDateTime startTime = OffsetDateTime.now();
        // When
        ThreadedTransaction<S> write = new ThreadedTransaction<>(neo, latch);
        try {
            String writeQuery = write.executeEarly(threading, writeSubject, KernelTransaction.Type.implicit, format("USING PERIODIC COMMIT 10 LOAD CSV FROM 'http://localhost:%d' AS line ", localPort) + "CREATE (n:A {id: line[0], square: line[1]}) " + "RETURN count(*)");
            latch.startAndWaitForAllToStart();
            // Then
            String query = "CALL dbms.listQueries()";
            assertSuccess(adminSubject, query, r -> {
                Set<Map<String, Object>> maps = r.stream().collect(Collectors.toSet());
                Matcher<Map<String, Object>> thisMatcher = listedQuery(startTime, "adminSubject", query);
                Matcher<Map<String, Object>> writeMatcher = listedQuery(startTime, "writeSubject", writeQuery);
                assertThat(maps, hasItem(thisMatcher));
                assertThat(maps, hasItem(writeMatcher));
            });
        } finally {
            // When
            barrier.release();
            latch.finishAndWaitForAllToFinish();
            server.stop();
            // Then
            write.closeAndAssertSuccess();
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) TransactionIT.createHttpServer(org.neo4j.bolt.v1.runtime.integration.TransactionIT.createHttpServer) Barrier(org.neo4j.test.Barrier) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) OffsetDateTime(java.time.OffsetDateTime) DoubleLatch(org.neo4j.test.DoubleLatch) Map(java.util.Map) MapUtil.stringMap(org.neo4j.helpers.collection.MapUtil.stringMap) Collections.emptyMap(java.util.Collections.emptyMap) Test(org.junit.Test)

Example 5 with Barrier

use of org.neo4j.test.Barrier in project neo4j by neo4j.

the class DeferringLocksIT method shouldNotFreakOutIfTwoTransactionsDecideToEachAddTheSameProperty.

@Test(timeout = TEST_TIMEOUT)
public void shouldNotFreakOutIfTwoTransactionsDecideToEachAddTheSameProperty() throws Exception {
    // GIVEN
    final Barrier.Control barrier = new Barrier.Control();
    final Node node;
    try (Transaction tx = db.beginTx()) {
        node = db.createNode();
        tx.success();
    }
    // WHEN
    t2.execute(new WorkerCommand<Void, Void>() {

        @Override
        public Void doWork(Void state) throws Exception {
            try (Transaction tx = db.beginTx()) {
                node.setProperty(PROPERTY_KEY, VALUE_1);
                tx.success();
                barrier.reached();
            }
            return null;
        }
    });
    try (Transaction tx = db.beginTx()) {
        barrier.await();
        node.setProperty(PROPERTY_KEY, VALUE_2);
        tx.success();
        barrier.release();
    }
    try (Transaction tx = db.beginTx()) {
        assertEquals(1, count(node.getPropertyKeys()));
        tx.success();
    }
}
Also used : Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Barrier(org.neo4j.test.Barrier) TransactionFailureException(org.neo4j.graphdb.TransactionFailureException) NotFoundException(org.neo4j.graphdb.NotFoundException) InvalidRecordException(org.neo4j.kernel.impl.store.InvalidRecordException) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)15 Barrier (org.neo4j.test.Barrier)15 Transaction (org.neo4j.graphdb.Transaction)7 Node (org.neo4j.graphdb.Node)6 ExecutionException (java.util.concurrent.ExecutionException)3 Server (org.eclipse.jetty.server.Server)3 NotFoundException (org.neo4j.graphdb.NotFoundException)3 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)3 InvalidRecordException (org.neo4j.kernel.impl.store.InvalidRecordException)3 File (java.io.File)2 IOException (java.io.IOException)2 Collections.emptyMap (java.util.Collections.emptyMap)2 Map (java.util.Map)2 Future (java.util.concurrent.Future)2 MILLISECONDS (java.util.concurrent.TimeUnit.MILLISECONDS)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertSame (org.junit.Assert.assertSame)2 Assert.fail (org.junit.Assert.fail)2 Rule (org.junit.Rule)2