Search in sources :

Example 11 with DoubleLatch

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

the class ProcedureInteractionTestBase method shouldTerminateTransactionsForUser.

// --------------------- helpers -----------------------
void shouldTerminateTransactionsForUser(S subject, String procedure) throws Throwable {
    DoubleLatch latch = new DoubleLatch(2);
    ThreadedTransaction<S> userThread = new ThreadedTransaction<>(neo, latch);
    userThread.executeCreateNode(threading(), subject);
    latch.startAndWaitForAllToStart();
    assertEmpty(adminSubject, "CALL " + format(procedure, neo.nameOf(subject)));
    Map<String, Long> transactionsByUser = countTransactionsByUsername();
    assertThat(transactionsByUser.get(neo.nameOf(subject)), equalTo(null));
    latch.finishAndWaitForAllToFinish();
    userThread.closeAndAssertTransactionTermination();
    assertEmpty(adminSubject, "MATCH (n:Test) RETURN n.name AS name");
}
Also used : DoubleLatch(org.neo4j.test.DoubleLatch) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Example 12 with DoubleLatch

use of org.neo4j.test.DoubleLatch 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 13 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method shouldTerminateLongRunningProcedureThatChecksTheGuardRegularlyIfKilled.

//---------- procedure guard -----------
@Test
public void shouldTerminateLongRunningProcedureThatChecksTheGuardRegularlyIfKilled() throws Throwable {
    final DoubleLatch latch = new DoubleLatch(2);
    ClassWithProcedures.volatileLatch = latch;
    String loopQuery = "CALL test.loop";
    new Thread(() -> assertFail(readSubject, loopQuery, "Explicitly terminated by the user.")).start();
    latch.startAndWaitForAllToStart();
    try {
        String loopId = extractQueryId(loopQuery);
        assertSuccess(adminSubject, "CALL dbms.killQuery('" + loopId + "') 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("readSubject")));
            assertThat(actual, matchesOneToOneInAnyOrder(mapMatcher));
        });
    } finally {
        latch.finishAndWaitForAllToFinish();
    }
    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) Test(org.junit.Test)

Example 14 with DoubleLatch

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

the class BuiltInProceduresInteractionTestBase method shouldFailToTerminateOtherUsersQuery.

@Test
public void shouldFailToTerminateOtherUsersQuery() throws Throwable {
    DoubleLatch latch = new DoubleLatch(3, true);
    ThreadedTransaction<S> read = new ThreadedTransaction<>(neo, latch);
    ThreadedTransaction<S> write = new ThreadedTransaction<>(neo, latch);
    String q1 = read.execute(threading, readSubject, "UNWIND [1,2,3] AS x RETURN x");
    write.execute(threading, writeSubject, "UNWIND [4,5,6] AS y RETURN y");
    latch.startAndWaitForAllToStart();
    try {
        String id1 = extractQueryId(q1);
        assertFail(writeSubject, "CALL dbms.killQuery('" + id1 + "') YIELD username RETURN *", PERMISSION_DENIED);
        latch.finishAndWaitForAllToFinish();
        read.closeAndAssertSuccess();
        write.closeAndAssertSuccess();
    } catch (Throwable t) {
        latch.finishAndWaitForAllToFinish();
        throw t;
    }
    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) Test(org.junit.Test)

Example 15 with DoubleLatch

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

the class EmbeddedBuiltInProceduresInteractionTest method shouldNotKillQueryIfNotAuthenticated.

@Test
public void shouldNotKillQueryIfNotAuthenticated() throws Throwable {
    EnterpriseSecurityContext authy = createFakeAnonymousEnterpriseSecurityContext();
    GraphDatabaseFacade graph = neo.getLocalGraph();
    DoubleLatch latch = new DoubleLatch(2);
    ThreadedTransaction<EnterpriseSecurityContext> read = new ThreadedTransaction<>(neo, latch);
    String query = read.execute(threading, authy, "UNWIND [1,2,3] AS x RETURN x");
    latch.startAndWaitForAllToStart();
    String id = extractQueryId(query);
    try (InternalTransaction tx = graph.beginTransaction(KernelTransaction.Type.explicit, AnonymousContext.none())) {
        graph.execute(tx, "CALL dbms.killQuery('" + id + "')", Collections.emptyMap());
        throw new AssertionError("Expected exception to be thrown");
    } catch (QueryExecutionException e) {
        assertThat(e.getMessage(), containsString(PERMISSION_DENIED));
    }
    latch.finishAndWaitForAllToFinish();
    read.closeAndAssertSuccess();
}
Also used : EnterpriseSecurityContext(org.neo4j.kernel.enterprise.api.security.EnterpriseSecurityContext) QueryExecutionException(org.neo4j.graphdb.QueryExecutionException) DoubleLatch(org.neo4j.test.DoubleLatch) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) GraphDatabaseFacade(org.neo4j.kernel.impl.factory.GraphDatabaseFacade) InternalTransaction(org.neo4j.kernel.impl.coreapi.InternalTransaction) Test(org.junit.Test)

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