Search in sources :

Example 1 with Vector

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();
    }
}
Also used : Option(javaslang.control.Option) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Serializables.deserialize(javaslang.Serializables.deserialize) List(javaslang.collection.List) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Seq(javaslang.collection.Seq) Serializables.serialize(javaslang.Serializables.serialize) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Iterator.range(javaslang.collection.Iterator.range) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Try(javaslang.control.Try) Objects(java.util.Objects) Iterator(javaslang.collection.Iterator) Spliterator(java.util.Spliterator) Vector(javaslang.collection.Vector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Test(org.junit.Test)

Aggregations

Objects (java.util.Objects)1 Spliterator (java.util.Spliterator)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletableFuture.runAsync (java.util.concurrent.CompletableFuture.runAsync)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Serializables.deserialize (javaslang.Serializables.deserialize)1 Serializables.serialize (javaslang.Serializables.serialize)1 Iterator (javaslang.collection.Iterator)1 Iterator.range (javaslang.collection.Iterator.range)1 List (javaslang.collection.List)1 Seq (javaslang.collection.Seq)1 Vector (javaslang.collection.Vector)1 Option (javaslang.control.Option)1 Try (javaslang.control.Try)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.Test)1