use of org.neo4j.ha.FinishTx in project neo4j by neo4j.
the class IndexOperationsIT method put_if_absent_works_across_instances.
@Test
public void put_if_absent_works_across_instances() throws Exception {
// GIVEN
// -- two instances, each begin a transaction
String key = "key2", value = "value2";
HighlyAvailableGraphDatabase db1 = cluster.getMaster(), db2 = cluster.getAnySlave();
long node = createNode(db1, key, value, false);
cluster.sync();
OtherThreadExecutor<HighlyAvailableGraphDatabase> w1 = new OtherThreadExecutor<>("w1", db1);
OtherThreadExecutor<HighlyAvailableGraphDatabase> w2 = new OtherThreadExecutor<>("w2", db2);
Transaction tx1 = w1.execute(new BeginTx());
Transaction tx2 = w2.execute(new BeginTx());
// WHEN
// -- second instance does putIfAbsent --> null
assertNull(w2.execute(new PutIfAbsent(node, key, value)));
// -- get a future to first instance putIfAbsent. Wait for it to go and await the lock
Future<Node> w1Future = w1.executeDontWait(new PutIfAbsent(node, key, value));
w1.waitUntilWaiting();
// -- second instance completes tx
w2.execute(new FinishTx(tx2, true));
tx2.success();
tx2.close();
// THEN
// -- first instance can complete the future with a non-null result
assertNotNull(w1Future.get());
w1.execute(new FinishTx(tx1, true));
// -- assert the index has got one entry and both instances have the same data
assertNodeAndIndexingExists(db1, node, key, value);
assertNodeAndIndexingExists(db2, node, key, value);
cluster.sync();
assertNodeAndIndexingExists(cluster.getAnySlave(db1, db2), node, key, value);
w2.close();
w1.close();
}
Aggregations