public void nonInterferenceOfSeparateHistories() throws NigoriCryptographyException, IOException,
UnauthorisedException {
failed = false;
Thread[] threads = new Thread[THREADS];
final MigoriDatastore migori = getStore();
final Index index = new Index("Concurrency");
assertTrue("Not registered", migori.register());
try {
final List<Throwable> exceptionList =
Collections.synchronizedList(new LinkedList<Throwable>());
for (int j = 0; j < THREADS; ++j) {
final int startFactor = j;
threads[j] = new Thread() {
@Override
public void run() {
boolean succeeded = false;
try {
RevValue last = migori.put(index, Util.int2bin(0));
for (int i = startFactor * REPEATS; i < startFactor * REPEATS + REPEATS; ++i) {
last = migori.put(index, Util.int2bin(i), last);
}
succeeded = true;
} catch (Throwable e) {
exceptionList.add(e);
} finally {
if (!succeeded && !failed) {
failed = true;
}
}
}
};
}
startThenJoinThreads(threads);
ifFailedPrintFailures(failed, exceptionList);
Collection<RevValue> heads = migori.get(index);
assertEquals(4, heads.size());
int total = 0;
RevValue deleteAt = null;
for (RevValue head : heads) {
deleteAt = head;
int value = Util.bin2int(head.getValue(), 0);
total += value;
}
assertEquals(REPEATS * 10 - 4, total);
assertNotNull(deleteAt);
if (deleteAt != null)
assertTrue(migori.removeIndex(index, deleteAt.getRevision()));
} finally {
assertTrue("Not unregistered", migori.unregister());
}
}