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