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