use of org.neo4j.test.Barrier in project neo4j by neo4j.
the class GraphDatabaseServiceTest method givenDatabaseAndStartedTxWhenShutdownThenWaitForTxToFinish.
@Test
public void givenDatabaseAndStartedTxWhenShutdownThenWaitForTxToFinish() throws Exception {
// Given
final GraphDatabaseService db = new TestGraphDatabaseFactory().newImpermanentDatabase();
// When
Barrier.Control barrier = new Barrier.Control();
Future<Object> txFuture = t2.execute(state -> {
try (Transaction tx = db.beginTx()) {
barrier.reached();
db.createNode();
tx.success();
}
return null;
});
// i.e. wait for transaction to start
barrier.await();
// now there's a transaction open, blocked on continueTxSignal
Future<Object> shutdownFuture = t3.execute(state -> {
db.shutdown();
return null;
});
t3.get().waitUntilWaiting(location -> location.isAt(DatabaseAvailability.class, "stop"));
barrier.release();
try {
txFuture.get();
} catch (ExecutionException e) {
// expected
}
shutdownFuture.get();
}
use of org.neo4j.test.Barrier in project neo4j by neo4j.
the class GraphDatabaseServiceTest method givenDatabaseAndStartedTxWhenShutdownAndStartNewTxThenBeginTxTimesOut.
@Test
public void givenDatabaseAndStartedTxWhenShutdownAndStartNewTxThenBeginTxTimesOut() throws Exception {
// Given
GraphDatabaseService db = new TestGraphDatabaseFactory().newImpermanentDatabase();
// When
Barrier.Control barrier = new Barrier.Control();
t2.execute(state -> {
try (Transaction tx = db.beginTx()) {
barrier.reached();
}
return null;
});
barrier.await();
Future<Object> shutdownFuture = t3.execute(state -> {
db.shutdown();
return null;
});
t3.get().waitUntilWaiting(location -> location.isAt(DatabaseAvailability.class, "stop"));
// <-- this triggers t2 to continue its transaction
barrier.release();
shutdownFuture.get();
try {
db.beginTx();
fail("Should fail");
} catch (DatabaseShutdownException e) {
//THEN good
}
}
use of org.neo4j.test.Barrier in project neo4j by neo4j.
the class RelationshipCountsTest method shouldNotCountRelationshipsCreatedInOtherTransaction.
@Test
public void shouldNotCountRelationshipsCreatedInOtherTransaction() throws Exception {
// given
GraphDatabaseService graphDb = db.getGraphDatabaseAPI();
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()) {
Node node = graphDb.createNode();
node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
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(0, before);
assertEquals(0, during);
assertEquals(2, after);
assertEquals(after, whatOtherThreadSees);
}
use of org.neo4j.test.Barrier in project neo4j by neo4j.
the class CountsTrackerTest method shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation.
@Test
public void shouldBeAbleToReadUpToDateValueWhileAnotherThreadIsPerformingRotation() throws Exception {
// given
CountsOracle oracle = someData();
final int firstTransaction = 2, secondTransaction = 3;
try (Lifespan life = new Lifespan()) {
CountsTracker tracker = life.add(newTracker());
oracle.update(tracker, firstTransaction);
tracker.rotate(firstTransaction);
}
// when
final CountsOracle delta = new CountsOracle();
{
CountsOracle.Node n1 = delta.node(1);
// Label 4 has not been used before...
CountsOracle.Node n2 = delta.node(1, 4);
delta.relationship(n1, 1, n2);
// relationshipType 2 has not been used before...
delta.relationship(n2, 2, n1);
}
delta.update(oracle);
try (Lifespan life = new Lifespan()) {
final Barrier.Control barrier = new Barrier.Control();
CountsTracker tracker = life.add(new CountsTracker(resourceManager.logProvider(), resourceManager.fileSystem(), resourceManager.pageCache(), Config.empty(), resourceManager.testPath()) {
@Override
protected boolean include(CountsKey countsKey, ReadableBuffer value) {
barrier.reached();
return super.include(countsKey, value);
}
});
Future<Void> task = threading.execute((ThrowingFunction<CountsTracker, Void, RuntimeException>) t -> {
try {
delta.update(t, secondTransaction);
t.rotate(secondTransaction);
} catch (IOException e) {
throw new AssertionError(e);
}
return null;
}, tracker);
// then
barrier.await();
oracle.verify(tracker);
barrier.release();
task.get();
oracle.verify(tracker);
}
}
use of org.neo4j.test.Barrier in project neo4j by neo4j.
the class StoreCopyCheckPointMutexTest method shouldPropagateStoreCopyActionFailureToOtherStoreCopyRequests.
@Test
public void shouldPropagateStoreCopyActionFailureToOtherStoreCopyRequests() throws Exception {
// GIVEN
Barrier.Control barrier = new Barrier.Control();
IOException controlledFailure = new IOException("My own fault");
AtomicReference<Future<Object>> secondRequest = new AtomicReference<>();
ThrowingAction<IOException> controllableAndFailingAction = new ThrowingAction<IOException>() {
@Override
public void apply() throws IOException {
// Now that we know we're first, start the second request...
secondRequest.set(t3.execute(state -> mutex.storeCopy(ASSERT_NOT_CALLED)));
// ...and wait for it to reach its destination
barrier.awaitUninterruptibly();
try {
// OK, second request has made progress into the request, so we can now produce our failure
throw controlledFailure;
} finally {
barrier.release();
}
}
};
Future<Object> firstRequest = t2.execute(state -> mutex.storeCopy(controllableAndFailingAction));
while (secondRequest.get() == null) {
parkARandomWhile();
}
t3.get().waitUntilWaiting(details -> details.isAt(StoreCopyCheckPointMutex.class, "waitForFirstStoreCopyActionToComplete"));
// WHEN
barrier.reached();
// THEN
try {
firstRequest.get();
} catch (ExecutionException e) {
assertSame(controlledFailure, e.getCause());
}
try {
secondRequest.get().get();
} catch (ExecutionException e) {
Throwable cooperativeActionFailure = e.getCause();
assertThat(cooperativeActionFailure.getMessage(), containsString("Co-operative"));
assertSame(controlledFailure, cooperativeActionFailure.getCause());
}
// WHEN afterwards trying another store-copy
CountingAction action = new CountingAction();
try (Resource lock = mutex.storeCopy(action)) {
// THEN
assertEquals(1, action.count());
}
}
Aggregations