mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 21:20:45 +00:00
CE-938 update memoization to say if it should store null values or not
This commit is contained in:
@ -119,6 +119,57 @@ class MemoizationTest extends BaseTest
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testMayNotStoreNull()
|
||||
{
|
||||
Memoization<String, String> memoization = new Memoization<>();
|
||||
memoization.setMayStoreNullValues(false);
|
||||
|
||||
AtomicInteger callCounter = new AtomicInteger();
|
||||
callCounter.set(0);
|
||||
UnsafeFunction<String, String, Exception> supplier = name ->
|
||||
{
|
||||
callCounter.getAndIncrement();
|
||||
if(name.equals("throw"))
|
||||
{
|
||||
throw (new Exception("You asked me to throw"));
|
||||
}
|
||||
else if(name.equals("null"))
|
||||
{
|
||||
return (null);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (name);
|
||||
}
|
||||
};
|
||||
|
||||
assertThat(memoization.getResult("null", supplier)).isEmpty();
|
||||
assertEquals(1, callCounter.get());
|
||||
|
||||
assertThat(memoization.getResult("null", supplier)).isEmpty();
|
||||
assertEquals(2, callCounter.get()); // should re-run the supplier, incrementing the counter
|
||||
|
||||
assertThat(memoization.getResult("throw", supplier)).isEmpty();
|
||||
assertEquals(3, callCounter.get());
|
||||
|
||||
assertThat(memoization.getResult("throw", supplier)).isEmpty();
|
||||
assertEquals(4, callCounter.get()); // should re-run the supplier, incrementing the counter
|
||||
|
||||
//noinspection AssertBetweenInconvertibleTypes
|
||||
assertThat(memoization.getResult("foo", supplier)).isPresent().get().isEqualTo("foo");
|
||||
assertEquals(5, callCounter.get());
|
||||
|
||||
//noinspection AssertBetweenInconvertibleTypes
|
||||
assertThat(memoization.getResult("foo", supplier)).isPresent().get().isEqualTo("foo");
|
||||
assertEquals(5, callCounter.get()); // should NOT re-run the supplier, NOT incrementing the counter
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user