[lazy] add test to verify re-entrant behaviour of value()
This commit is contained in:
parent
512563c077
commit
d1adfd46d2
1 changed files with 35 additions and 0 deletions
|
@ -1,15 +1,21 @@
|
||||||
package net.kemitix.mon.lazy;
|
package net.kemitix.mon.lazy;
|
||||||
|
|
||||||
import org.assertj.core.api.WithAssertions;
|
import org.assertj.core.api.WithAssertions;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.Timeout;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.*;
|
||||||
import java.util.concurrent.atomic.AtomicBoolean;
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class LazySupplierTest implements WithAssertions {
|
public class LazySupplierTest implements WithAssertions {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public final Timeout timeout = Timeout.seconds(1);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenCreateLazyThenSupplierIsNotCalled() {
|
public void whenCreateLazyThenSupplierIsNotCalled() {
|
||||||
//given
|
//given
|
||||||
|
@ -138,4 +144,33 @@ public class LazySupplierTest implements WithAssertions {
|
||||||
assertThat(value).isEqualTo(uuid.toString());
|
assertThat(value).isEqualTo(uuid.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenLazyValueCalledOnTwoThreadsThenSupplierIsOnlyCalledOnce() throws ExecutionException, InterruptedException {
|
||||||
|
//given
|
||||||
|
final AtomicInteger supplierCalledCounter = new AtomicInteger(0);
|
||||||
|
final CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
final UUID uuid = UUID.randomUUID();
|
||||||
|
final Supplier<UUID> supplier = () -> {
|
||||||
|
supplierCalledCounter.incrementAndGet();
|
||||||
|
for (int i = 0; i < 10000; i++) {
|
||||||
|
// hum
|
||||||
|
};
|
||||||
|
return uuid;
|
||||||
|
};
|
||||||
|
final Lazy<UUID> lazy = Lazy.of(supplier);
|
||||||
|
final Callable<UUID> callable = () -> {
|
||||||
|
latch.await();
|
||||||
|
return lazy.value();
|
||||||
|
};
|
||||||
|
final ExecutorService executorService = Executors.newFixedThreadPool(2);
|
||||||
|
//when
|
||||||
|
final Future<UUID> call1 = executorService.submit(callable);
|
||||||
|
final Future<UUID> call2 = executorService.submit(callable);
|
||||||
|
latch.countDown();
|
||||||
|
//then
|
||||||
|
assertThat(call1.get()).isEqualTo(uuid);
|
||||||
|
assertThat(call2.get()).isEqualTo(uuid);
|
||||||
|
assertThat(supplierCalledCounter).hasValue(1);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue