Simplify unit test logic (#3548)
This commit is contained in:
parent
a48e062598
commit
bd9a87c137
|
@ -38,7 +38,7 @@ public class GuavaMemoizerUnitTest {
|
||||||
int n = 95;
|
int n = 95;
|
||||||
|
|
||||||
// when
|
// when
|
||||||
BigInteger factorial = new Factorial().getFactorial(n);
|
BigInteger factorial = Factorial.getFactorial(n);
|
||||||
|
|
||||||
// then
|
// then
|
||||||
BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000");
|
BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000");
|
||||||
|
@ -47,52 +47,46 @@ public class GuavaMemoizerUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() {
|
public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() {
|
||||||
|
// given
|
||||||
Supplier<BigInteger> memoizedSupplier;
|
Supplier<BigInteger> memoizedSupplier;
|
||||||
memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber);
|
memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber);
|
||||||
|
|
||||||
Instant start = Instant.now();
|
// when
|
||||||
BigInteger bigNumber = memoizedSupplier.get();
|
BigInteger expectedValue = new BigInteger("12345");
|
||||||
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
|
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
|
||||||
assertThat(bigNumber, is(equalTo(new BigInteger("12345"))));
|
|
||||||
assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D)));
|
|
||||||
|
|
||||||
start = Instant.now();
|
// then
|
||||||
bigNumber = memoizedSupplier.get().add(BigInteger.ONE);
|
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
|
||||||
durationInMs = Duration.between(start, Instant.now()).toMillis();
|
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
|
||||||
assertThat(bigNumber, is(equalTo(new BigInteger("12346"))));
|
|
||||||
assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D)));
|
|
||||||
|
|
||||||
start = Instant.now();
|
|
||||||
bigNumber = memoizedSupplier.get().add(BigInteger.TEN);
|
|
||||||
durationInMs = Duration.between(start, Instant.now()).toMillis();
|
|
||||||
assertThat(bigNumber, is(equalTo(new BigInteger("12355"))));
|
|
||||||
assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException {
|
public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException {
|
||||||
|
// given
|
||||||
Supplier<BigInteger> memoizedSupplier;
|
Supplier<BigInteger> memoizedSupplier;
|
||||||
memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS);
|
memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS);
|
||||||
|
|
||||||
Instant start = Instant.now();
|
// when
|
||||||
BigInteger bigNumber = memoizedSupplier.get();
|
BigInteger expectedValue = new BigInteger("12345");
|
||||||
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
|
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
|
||||||
assertThat(bigNumber, is(equalTo(new BigInteger("12345"))));
|
|
||||||
assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D)));
|
|
||||||
|
|
||||||
start = Instant.now();
|
|
||||||
bigNumber = memoizedSupplier.get().add(BigInteger.ONE);
|
|
||||||
durationInMs = Duration.between(start, Instant.now()).toMillis();
|
|
||||||
assertThat(bigNumber, is(equalTo(new BigInteger("12346"))));
|
|
||||||
assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D)));
|
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
|
||||||
|
// add one more second until memoized Supplier is evicted from memory
|
||||||
TimeUnit.SECONDS.sleep(1);
|
TimeUnit.SECONDS.sleep(1);
|
||||||
|
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
|
||||||
|
}
|
||||||
|
|
||||||
start = Instant.now();
|
private <T> void assertSupplierGetExecutionResultAndDuration(Supplier<T> supplier,
|
||||||
bigNumber = memoizedSupplier.get().add(BigInteger.TEN);
|
T expectedValue,
|
||||||
durationInMs = Duration.between(start, Instant.now()).toMillis();
|
double expectedDurationInMs) {
|
||||||
assertThat(bigNumber, is(equalTo(new BigInteger("12355"))));
|
Instant start = Instant.now();
|
||||||
assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D)));
|
T value = supplier.get();
|
||||||
|
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
|
||||||
|
double marginOfErrorInMs = 100D;
|
||||||
|
|
||||||
|
assertThat(value, is(equalTo(expectedValue)));
|
||||||
|
assertThat(durationInMs.doubleValue(), is(closeTo(expectedDurationInMs, marginOfErrorInMs)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue