use of org.neo4j.internal.counts.CountsChanges.ABSENT in project neo4j by neo4j.
the class CountsChangesTest method shouldUpdateConcurrently.
@Test
void shouldUpdateConcurrently() {
// given
CountsChanges changes = new CountsChanges();
InMemoryCountsStore store = new InMemoryCountsStore();
int numStoredCounts = random.nextInt(10, 100);
for (int i = 0; i < numStoredCounts; i++) {
CountsKey key;
do {
key = randomKey(random.random());
} while (store.counts.containsKey(key));
long count = random.nextLong(100);
store.store(key, count);
}
// when
Race race = new Race();
List<Map<CountsKey, MutableLong>> allThreadChanges = new CopyOnWriteArrayList<>();
race.addContestants(4, r -> () -> {
Random threadRandom = new Random(random.seed() + r);
Map<CountsKey, MutableLong> threadChanges = new HashMap<>();
for (int i = 0; i < 1_0000; i++) {
CountsKey key = randomKey(threadRandom);
long delta = threadRandom.nextInt(12) - 2;
changes.add(key, delta, store);
threadChanges.computeIfAbsent(key, k -> new MutableLong()).add(delta);
}
allThreadChanges.add(threadChanges);
});
race.goUnchecked();
// then
Map<CountsKey, MutableLong> expectedCounts = new HashMap<>();
store.counts.forEach((key, count) -> expectedCounts.put(key, new MutableLong(count.longValue())));
for (Map<CountsKey, MutableLong> threadChanges : allThreadChanges) {
threadChanges.forEach((key, delta) -> expectedCounts.computeIfAbsent(key, k -> new MutableLong()).add(delta.longValue()));
}
expectedCounts.forEach((key, expectedCount) -> {
long expectedCountFromChanges = changes.containsChange(key) ? expectedCount.longValue() : ABSENT;
assertThat(changes.get(key)).as(key.toString()).isEqualTo(expectedCountFromChanges);
});
}
Aggregations