Moved to core-java-function module

Added concurrent tests
This commit is contained in:
maenolis 2023-11-17 22:44:42 +02:00
parent 2ed78462c8
commit 62c3d6bf32
8 changed files with 110 additions and 65 deletions

View File

@ -1,26 +0,0 @@
package com.baeldung.lazylambda;
import java.util.function.Supplier;
import org.junit.Test;
import org.mockito.Mockito;
public class LazyLambdaSupplierUnitTest {
@Test
public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() {
@SuppressWarnings("unchecked") Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
Mockito.when(mockedExpensiveFunction.get())
.thenReturn("expensive call");
LazyLambdaSupplier<String> testee = new LazyLambdaSupplier<>(mockedExpensiveFunction);
Mockito.verify(mockedExpensiveFunction, Mockito.never())
.get();
testee.getData();
testee.getData();
testee.getData();
testee.getData();
Mockito.verify(mockedExpensiveFunction, Mockito.times(1))
.get();
}
}

View File

@ -1,26 +0,0 @@
package com.baeldung.lazylambda;
import java.util.function.Supplier;
import org.junit.Test;
import org.mockito.Mockito;
public class LazyLambdaThreadSafeSupplierUnitTest {
@Test
public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() {
@SuppressWarnings("unchecked") Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
Mockito.when(mockedExpensiveFunction.get())
.thenReturn("expensive call");
LazyLambdaThreadSafeSupplier<String> testee = new LazyLambdaThreadSafeSupplier<>(mockedExpensiveFunction);
Mockito.verify(mockedExpensiveFunction, Mockito.never())
.get();
testee.getData();
testee.getData();
testee.getData();
testee.getData();
Mockito.verify(mockedExpensiveFunction, Mockito.times(1))
.get();
}
}

View File

@ -4,7 +4,7 @@ import java.util.function.Supplier;
public class LambdaSupplier<T> {
private final Supplier<T> expensiveData;
protected final Supplier<T> expensiveData;
public LambdaSupplier(Supplier<T> expensiveData) {
this.expensiveData = expensiveData;

View File

@ -2,16 +2,15 @@ package com.baeldung.lazylambda;
import java.util.function.Supplier;
public class LazyLambdaSupplier<T> {
private final Supplier<T> expensiveData;
public class LazyLambdaSupplier<T> extends LambdaSupplier<T> {
private T data;
public LazyLambdaSupplier(Supplier<T> expensiveData) {
this.expensiveData = expensiveData;
super(expensiveData);
}
@Override
public T getData() {
if (data != null) {
return data;

View File

@ -3,14 +3,12 @@ package com.baeldung.lazylambda;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
public class LazyLambdaThreadSafeSupplier<T> {
private final Supplier<T> expensiveData;
public class LazyLambdaThreadSafeSupplier<T> extends LambdaSupplier<T> {
private final AtomicReference<T> data;
public LazyLambdaThreadSafeSupplier(Supplier<T> expensiveData) {
this.expensiveData = expensiveData;
super(expensiveData);
data = new AtomicReference<>();
}
@ -22,7 +20,6 @@ public class LazyLambdaThreadSafeSupplier<T> {
}
}
}
return data.get();
}

View File

@ -17,9 +17,7 @@ public class LambdaSupplierUnitTest {
.get();
testee.getData();
testee.getData();
testee.getData();
testee.getData();
Mockito.verify(mockedExpensiveFunction, Mockito.times(4))
Mockito.verify(mockedExpensiveFunction, Mockito.times(2))
.get();
}

View File

@ -0,0 +1,51 @@
package com.baeldung.lazylambda;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
public class LazyLambdaSupplierUnitTest {
@Test
public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() {
@SuppressWarnings("unchecked") Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
Mockito.when(mockedExpensiveFunction.get())
.thenReturn("expensive call");
LazyLambdaSupplier<String> testee = new LazyLambdaSupplier<>(mockedExpensiveFunction);
Mockito.verify(mockedExpensiveFunction, Mockito.never())
.get();
testee.getData();
testee.getData();
Mockito.verify(mockedExpensiveFunction, Mockito.times(1))
.get();
}
@Test
public void whenCalledMultipleTimesConcurrently_thenShouldBeCalledMultipleTimes() throws InterruptedException {
@SuppressWarnings("unchecked") Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
Mockito.when(mockedExpensiveFunction.get())
.thenAnswer((Answer<String>) invocation -> {
Thread.sleep(1000L);
return "Late response!";
});
LazyLambdaSupplier<String> testee = new LazyLambdaSupplier<>(mockedExpensiveFunction);
Mockito.verify(mockedExpensiveFunction, Mockito.never())
.get();
ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.invokeAll(List.of(testee::getData, testee::getData));
executorService.shutdown();
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
Mockito.verify(mockedExpensiveFunction, Mockito.times(2))
.get();
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.lazylambda;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
public class LazyLambdaThreadSafeSupplierUnitTest {
@Test
public void whenCalledMultipleTimes_thenShouldBeCalledOnlyOnce() {
@SuppressWarnings("unchecked") Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
Mockito.when(mockedExpensiveFunction.get())
.thenReturn("expensive call");
LazyLambdaThreadSafeSupplier<String> testee = new LazyLambdaThreadSafeSupplier<>(mockedExpensiveFunction);
Mockito.verify(mockedExpensiveFunction, Mockito.never())
.get();
testee.getData();
testee.getData();
Mockito.verify(mockedExpensiveFunction, Mockito.times(1))
.get();
}
@Test
public void whenCalledMultipleTimesConcurrently_thenShouldBeCalledOnlyOnce() throws InterruptedException {
@SuppressWarnings("unchecked") Supplier<String> mockedExpensiveFunction = Mockito.mock(Supplier.class);
Mockito.when(mockedExpensiveFunction.get())
.thenAnswer((Answer<String>) invocation -> {
Thread.sleep(1000L);
return "Late response!";
});
LazyLambdaThreadSafeSupplier<String> testee = new LazyLambdaThreadSafeSupplier<>(mockedExpensiveFunction);
Mockito.verify(mockedExpensiveFunction, Mockito.never())
.get();
ExecutorService executorService = Executors.newFixedThreadPool(4);
executorService.invokeAll(List.of(testee::getData, testee::getData));
executorService.shutdown();
if (!executorService.awaitTermination(5, TimeUnit.SECONDS)) {
executorService.shutdownNow();
}
Mockito.verify(mockedExpensiveFunction, Mockito.times(1))
.get();
}
}