-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pipinet
committed
Dec 12, 2023
1 parent
7d99819
commit db99c09
Showing
2 changed files
with
43 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
lang/src/test/java/com/qwlabs/lang/RuntimeExpiringMemoizingSupplierTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.qwlabs.lang; | ||
|
||
import com.google.common.base.Supplier; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RuntimeExpiringMemoizingSupplierTest { | ||
@Test | ||
void should_call_once(@Mock RealSupplier delegate) { | ||
RuntimeExpiringMemoizingSupplier<String> supplier = new RuntimeExpiringMemoizingSupplier<>(delegate); | ||
|
||
when(delegate.get()).thenReturn(WithExpiring.of("1", 1, TimeUnit.DAYS)); | ||
|
||
var value1 = supplier.get(); | ||
var value2 = supplier.get(); | ||
|
||
assertSame(value1, value2); | ||
assertThat(value1, is("1")); | ||
|
||
verify(delegate).get(); | ||
} | ||
|
||
static class RealSupplier implements Supplier<WithExpiring<String>> { | ||
@Override | ||
public WithExpiring<String> get() { | ||
return WithExpiring.of("real", 1, TimeUnit.DAYS); | ||
} | ||
} | ||
} |