Skip to content

Commit bd9a87c

Browse files
knightdnapivovarit
authored andcommitted
Simplify unit test logic (eugenp#3548)
1 parent a48e062 commit bd9a87c

1 file changed

Lines changed: 28 additions & 34 deletions

File tree

guava/src/test/java/org/baeldung/guava/GuavaMemoizerUnitTest.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void givenInteger_whenGetFactorial_thenShouldCalculateFactorial() {
3838
int n = 95;
3939

4040
// when
41-
BigInteger factorial = new Factorial().getFactorial(n);
41+
BigInteger factorial = Factorial.getFactorial(n);
4242

4343
// then
4444
BigInteger expectedFactorial = new BigInteger("10329978488239059262599702099394727095397746340117372869212250571234293987594703124871765375385424468563282236864226607350415360000000000000000000000");
@@ -47,52 +47,46 @@ public void givenInteger_whenGetFactorial_thenShouldCalculateFactorial() {
4747

4848
@Test
4949
public void givenMemoizedSupplier_whenGet_thenSubsequentGetsAreFast() {
50+
// given
5051
Supplier<BigInteger> memoizedSupplier;
5152
memoizedSupplier = Suppliers.memoize(CostlySupplier::generateBigNumber);
5253

53-
Instant start = Instant.now();
54-
BigInteger bigNumber = memoizedSupplier.get();
55-
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
56-
assertThat(bigNumber, is(equalTo(new BigInteger("12345"))));
57-
assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D)));
58-
59-
start = Instant.now();
60-
bigNumber = memoizedSupplier.get().add(BigInteger.ONE);
61-
durationInMs = Duration.between(start, Instant.now()).toMillis();
62-
assertThat(bigNumber, is(equalTo(new BigInteger("12346"))));
63-
assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D)));
64-
65-
start = Instant.now();
66-
bigNumber = memoizedSupplier.get().add(BigInteger.TEN);
67-
durationInMs = Duration.between(start, Instant.now()).toMillis();
68-
assertThat(bigNumber, is(equalTo(new BigInteger("12355"))));
69-
assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D)));
54+
// when
55+
BigInteger expectedValue = new BigInteger("12345");
56+
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
57+
58+
// then
59+
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
60+
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
7061
}
7162

7263
@Test
7364
public void givenMemoizedSupplierWithExpiration_whenGet_thenSubsequentGetsBeforeExpiredAreFast() throws InterruptedException {
65+
// given
7466
Supplier<BigInteger> memoizedSupplier;
7567
memoizedSupplier = Suppliers.memoizeWithExpiration(CostlySupplier::generateBigNumber, 3, TimeUnit.SECONDS);
7668

77-
Instant start = Instant.now();
78-
BigInteger bigNumber = memoizedSupplier.get();
79-
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
80-
assertThat(bigNumber, is(equalTo(new BigInteger("12345"))));
81-
assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D)));
82-
83-
start = Instant.now();
84-
bigNumber = memoizedSupplier.get().add(BigInteger.ONE);
85-
durationInMs = Duration.between(start, Instant.now()).toMillis();
86-
assertThat(bigNumber, is(equalTo(new BigInteger("12346"))));
87-
assertThat(durationInMs.doubleValue(), is(closeTo(0D, 100D)));
69+
// when
70+
BigInteger expectedValue = new BigInteger("12345");
71+
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
8872

73+
// then
74+
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 0D);
75+
// add one more second until memoized Supplier is evicted from memory
8976
TimeUnit.SECONDS.sleep(1);
77+
assertSupplierGetExecutionResultAndDuration(memoizedSupplier, expectedValue, 2000D);
78+
}
79+
80+
private <T> void assertSupplierGetExecutionResultAndDuration(Supplier<T> supplier,
81+
T expectedValue,
82+
double expectedDurationInMs) {
83+
Instant start = Instant.now();
84+
T value = supplier.get();
85+
Long durationInMs = Duration.between(start, Instant.now()).toMillis();
86+
double marginOfErrorInMs = 100D;
9087

91-
start = Instant.now();
92-
bigNumber = memoizedSupplier.get().add(BigInteger.TEN);
93-
durationInMs = Duration.between(start, Instant.now()).toMillis();
94-
assertThat(bigNumber, is(equalTo(new BigInteger("12355"))));
95-
assertThat(durationInMs.doubleValue(), is(closeTo(2000D, 100D)));
88+
assertThat(value, is(equalTo(expectedValue)));
89+
assertThat(durationInMs.doubleValue(), is(closeTo(expectedDurationInMs, marginOfErrorInMs)));
9690
}
9791

9892
}

0 commit comments

Comments
 (0)