Search in sources :

Example 31 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method shouldListRestrictedTransaction.

//@Test
public void shouldListRestrictedTransaction() {
    final DoubleLatch doubleLatch = new DoubleLatch(2);
    ClassWithProcedures.setTestLatch(new ClassWithProcedures.LatchedRunnables(doubleLatch, () -> {
    }, () -> {
    }));
    new Thread(() -> assertEmpty(writeSubject, "CALL test.waitForLatch()")).start();
    doubleLatch.startAndWaitForAllToStart();
    try {
        assertSuccess(adminSubject, "CALL dbms.listTransactions()", r -> assertKeyIsMap(r, "username", "activeTransactions", map("adminSubject", "1", "writeSubject", "1")));
    } finally {
        doubleLatch.finishAndWaitForAllToFinish();
    }
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch)

Example 32 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method queryWaitingForLocksShouldBeKilledBeforeLocksAreReleased.

//---------- terminate query -----------
/*
     * User starts query1 that takes a lock and runs for a long time.
     * User starts query2 that needs to wait for that lock.
     * query2 is blocked waiting for lock to be released.
     * Admin terminates query2.
     * query2 is immediately terminated, even though locks have not been released.
     */
@Test
public void queryWaitingForLocksShouldBeKilledBeforeLocksAreReleased() throws Throwable {
    assertEmpty(adminSubject, "CREATE (:MyNode {prop: 2})");
    // create new latch
    ClassWithProcedures.doubleLatch = new DoubleLatch(2);
    // start never-ending query
    String query1 = "MATCH (n:MyNode) SET n.prop = 5 WITH * CALL test.neverEnding() RETURN 1";
    ThreadedTransaction<S> tx1 = new ThreadedTransaction<>(neo, new DoubleLatch());
    tx1.executeEarly(threading, writeSubject, KernelTransaction.Type.explicit, query1);
    // wait for query1 to be stuck in procedure with its write lock
    ClassWithProcedures.doubleLatch.startAndWaitForAllToStart();
    // start query2
    ThreadedTransaction<S> tx2 = new ThreadedTransaction<>(neo, new DoubleLatch());
    String query2 = "MATCH (n:MyNode) SET n.prop = 10 RETURN 1";
    tx2.executeEarly(threading, writeSubject, KernelTransaction.Type.explicit, query2);
    assertQueryIsRunning(query2);
    // get the query id of query2 and kill it
    assertSuccess(adminSubject, "CALL dbms.listQueries() YIELD query, queryId " + "WITH query, queryId WHERE query = '" + query2 + "'" + "CALL dbms.killQuery(queryId) YIELD queryId AS killedId " + "RETURN 1", itr -> assertThat(itr.hasNext(), equalTo(true)));
    tx2.closeAndAssertQueryKilled();
    // allow query1 to exit procedure and finish
    ClassWithProcedures.doubleLatch.finish();
    tx1.closeAndAssertSuccess();
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 33 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method executeTwoQueriesAndKillTheFirst.

private void executeTwoQueriesAndKillTheFirst(S executor1, S executor2, S killer) throws Throwable {
    DoubleLatch latch = new DoubleLatch(3);
    ThreadedTransaction<S> tx1 = new ThreadedTransaction<>(neo, latch);
    ThreadedTransaction<S> tx2 = new ThreadedTransaction<>(neo, latch);
    String q1 = tx1.execute(threading, executor1, "UNWIND [1,2,3] AS x RETURN x");
    tx2.execute(threading, executor2, "UNWIND [4,5,6] AS y RETURN y");
    latch.startAndWaitForAllToStart();
    String id1 = extractQueryId(q1);
    assertSuccess(killer, "CALL dbms.killQuery('" + id1 + "') YIELD username " + "RETURN count(username) AS count, username", r -> {
        List<Map<String, Object>> actual = r.stream().collect(toList());
        @SuppressWarnings("unchecked") Matcher<Map<String, Object>> mapMatcher = allOf((Matcher) hasEntry(equalTo("count"), anyOf(equalTo(1), equalTo(1L))), (Matcher) hasEntry(equalTo("username"), equalTo("readSubject")));
        assertThat(actual, matchesOneToOneInAnyOrder(mapMatcher));
    });
    latch.finishAndWaitForAllToFinish();
    tx1.closeAndAssertTransactionTermination();
    tx2.closeAndAssertSuccess();
    assertEmpty(adminSubject, "CALL dbms.listQueries() YIELD query WITH * WHERE NOT query CONTAINS 'listQueries' RETURN *");
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) MapUtil.stringMap(org.neo4j.helpers.collection.MapUtil.stringMap) Collections.emptyMap(java.util.Collections.emptyMap)

Example 34 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method shouldTerminateQueriesEvenIfUsingPeriodicCommit.

@SuppressWarnings("unchecked")
@Test
public void shouldTerminateQueriesEvenIfUsingPeriodicCommit() throws Throwable {
    for (int batchSize = 8; batchSize <= 11; batchSize++) {
        // 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, batchSize, 50 - batchSize);
        server.start();
        int localPort = getLocalPort(server);
        // 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 writeQueryId = extractQueryId(writeQuery);
            assertSuccess(adminSubject, "CALL dbms.killQuery('" + writeQueryId + "') YIELD username " + "RETURN count(username) AS count, username", r -> {
                List<Map<String, Object>> actual = r.stream().collect(toList());
                Matcher<Map<String, Object>> mapMatcher = allOf((Matcher) hasEntry(equalTo("count"), anyOf(equalTo(1), equalTo(1L))), (Matcher) hasEntry(equalTo("username"), equalTo("writeSubject")));
                assertThat(actual, matchesOneToOneInAnyOrder(mapMatcher));
            });
        } finally {
            // When
            barrier.release();
            latch.finishAndWaitForAllToFinish();
            // Then
            write.closeAndAssertTransactionTermination();
            // stop server after assertion to avoid other kind of failures due to races (e.g., already closed
            // lock clients )
            server.stop();
        }
    }
}
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) 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 35 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method shouldTerminateSelfTransactionsExceptTerminationTransaction.

private void shouldTerminateSelfTransactionsExceptTerminationTransaction(S subject) throws Throwable {
    DoubleLatch latch = new DoubleLatch(2);
    ThreadedTransaction<S> create = new ThreadedTransaction<>(neo, latch);
    create.executeCreateNode(threading, subject);
    latch.startAndWaitForAllToStart();
    String subjectName = neo.nameOf(subject);
    assertSuccess(subject, "CALL dbms.terminateTransactionsForUser( '" + subjectName + "' )", r -> assertKeyIsMap(r, "username", "transactionsTerminated", map(subjectName, "1")));
    latch.finishAndWaitForAllToFinish();
    create.closeAndAssertTransactionTermination();
    assertEmpty(adminSubject, "MATCH (n:Test) RETURN n.name AS name");
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Aggregations

DoubleLatch (org.neo4j.test.DoubleLatch)48 Test (org.junit.Test)37 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)15 Collections.emptyMap (java.util.Collections.emptyMap)9 Map (java.util.Map)9 MapUtil.stringMap (org.neo4j.helpers.collection.MapUtil.stringMap)9 OffsetDateTime (java.time.OffsetDateTime)5 JobScheduler (org.neo4j.kernel.impl.util.JobScheduler)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 NewIndexDescriptor (org.neo4j.kernel.api.schema_new.index.NewIndexDescriptor)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Server (org.eclipse.jetty.server.Server)3 SchemaIndexTestHelper.mockIndexProxy (org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.mockIndexProxy)3 Neo4jJobScheduler (org.neo4j.kernel.impl.util.Neo4jJobScheduler)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.never (org.mockito.Mockito.never)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2