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");
}
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();
}
}
}
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 *");
}
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 *");
}
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();
}
Aggregations