use of javaslang.collection.Vector in project javaslang by javaslang.
the class LazyTest method shouldBeConsistentFromMultipleThreads.
@Test
@SuppressWarnings({ "StatementWithEmptyBody", "rawtypes" })
public void shouldBeConsistentFromMultipleThreads() throws Exception {
for (int i = 0; i < 100; i++) {
final AtomicBoolean canProceed = new AtomicBoolean(false);
final Vector<CompletableFuture<Void>> futures = Vector.range(0, 10).map(j -> {
final AtomicBoolean isEvaluated = new AtomicBoolean(false);
final Integer expected = ((j % 2) == 1) ? null : j;
Lazy<Integer> lazy = Lazy.of(() -> {
assertThat(isEvaluated.getAndSet(true)).isFalse();
return expected;
});
return Tuple.of(lazy, expected);
}).flatMap(t -> range(0, 5).map(j -> runAsync(() -> {
while (!canProceed.get()) {
}
assertThat(t._1.get()).isEqualTo(t._2);
})));
final CompletableFuture all = CompletableFuture.allOf(futures.toJavaList().toArray(new CompletableFuture<?>[0]));
canProceed.set(true);
all.join();
}
}
Aggregations