|
3 | 3 | import com.baeldung.infinispan.ConfigurationTest; |
4 | 4 | import org.junit.Test; |
5 | 5 |
|
| 6 | +import java.util.concurrent.Callable; |
| 7 | +import java.util.function.Consumer; |
| 8 | + |
6 | 9 | import static org.assertj.core.api.Java6Assertions.assertThat; |
7 | 10 |
|
8 | 11 | public class HelloWorldServiceUnitTest extends ConfigurationTest { |
9 | 12 |
|
10 | 13 | @Test |
11 | 14 | public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { |
12 | | - long milis = System.currentTimeMillis(); |
13 | | - helloWorldService.findSimpleHelloWorld(); |
14 | | - long executionTime = System.currentTimeMillis() - milis; |
15 | | - |
16 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
17 | | - |
18 | | - milis = System.currentTimeMillis(); |
19 | | - helloWorldService.findSimpleHelloWorld(); |
20 | | - executionTime = System.currentTimeMillis() - milis; |
| 15 | + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) |
| 16 | + .isGreaterThanOrEqualTo(1000); |
21 | 17 |
|
22 | | - assertThat(executionTime).isLessThan(100); |
| 18 | + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) |
| 19 | + .isLessThan(100); |
23 | 20 | } |
24 | 21 |
|
25 | 22 | @Test |
26 | 23 | public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { |
27 | | - long milis = System.currentTimeMillis(); |
28 | | - helloWorldService.findExpiringHelloWorld(); |
29 | | - long executionTime = System.currentTimeMillis() - milis; |
| 24 | + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) |
| 25 | + .isGreaterThanOrEqualTo(1000); |
30 | 26 |
|
31 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
32 | | - |
33 | | - milis = System.currentTimeMillis(); |
34 | | - helloWorldService.findExpiringHelloWorld(); |
35 | | - executionTime = System.currentTimeMillis() - milis; |
36 | | - |
37 | | - assertThat(executionTime).isLessThan(100); |
| 27 | + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) |
| 28 | + .isLessThan(100); |
38 | 29 | } |
39 | 30 |
|
40 | 31 | @Test |
41 | 32 | public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() |
42 | | - throws InterruptedException { |
43 | | - |
44 | | - long milis = System.currentTimeMillis(); |
45 | | - helloWorldService.findSimpleHelloWorldInExpiringCache(); |
46 | | - long executionTime = System.currentTimeMillis() - milis; |
| 33 | + throws InterruptedException { |
47 | 34 |
|
48 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 35 | + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) |
| 36 | + .isGreaterThanOrEqualTo(1000); |
49 | 37 |
|
50 | 38 | Thread.sleep(1100); |
51 | 39 |
|
52 | | - milis = System.currentTimeMillis(); |
53 | | - helloWorldService.findSimpleHelloWorldInExpiringCache(); |
54 | | - executionTime = System.currentTimeMillis() - milis; |
55 | | - |
56 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 40 | + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) |
| 41 | + .isGreaterThanOrEqualTo(1000); |
57 | 42 | } |
58 | 43 |
|
59 | 44 | @Test |
60 | | - public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() |
61 | | - throws InterruptedException { |
62 | | - |
63 | | - long milis = System.currentTimeMillis(); |
64 | | - helloWorldService.findEvictingHelloWorld("key 1"); |
65 | | - long executionTime = System.currentTimeMillis() - milis; |
66 | | - |
67 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 45 | + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { |
68 | 46 |
|
69 | | - milis = System.currentTimeMillis(); |
70 | | - helloWorldService.findEvictingHelloWorld("key 2"); |
71 | | - executionTime = System.currentTimeMillis() - milis; |
| 47 | + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) |
| 48 | + .isGreaterThanOrEqualTo(1000); |
72 | 49 |
|
73 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 50 | + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))) |
| 51 | + .isGreaterThanOrEqualTo(1000); |
74 | 52 |
|
75 | | - milis = System.currentTimeMillis(); |
76 | | - helloWorldService.findEvictingHelloWorld("key 1"); |
77 | | - executionTime = System.currentTimeMillis() - milis; |
78 | | - |
79 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 53 | + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) |
| 54 | + .isGreaterThanOrEqualTo(1000); |
80 | 55 | } |
81 | 56 |
|
82 | 57 | @Test |
83 | | - public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() |
84 | | - throws InterruptedException { |
85 | | - |
86 | | - long milis = System.currentTimeMillis(); |
87 | | - helloWorldService.findPassivatingHelloWorld("key 1"); |
88 | | - long executionTime = System.currentTimeMillis() - milis; |
89 | | - |
90 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 58 | + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { |
91 | 59 |
|
92 | | - milis = System.currentTimeMillis(); |
93 | | - helloWorldService.findPassivatingHelloWorld("key 2"); |
94 | | - executionTime = System.currentTimeMillis() - milis; |
| 60 | + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) |
| 61 | + .isGreaterThanOrEqualTo(1000); |
95 | 62 |
|
96 | | - assertThat(executionTime).isGreaterThanOrEqualTo(1000); |
| 63 | + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))) |
| 64 | + .isGreaterThanOrEqualTo(1000); |
97 | 65 |
|
98 | | - milis = System.currentTimeMillis(); |
99 | | - helloWorldService.findPassivatingHelloWorld("key 1"); |
100 | | - executionTime = System.currentTimeMillis() - milis; |
| 66 | + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) |
| 67 | + .isLessThan(100); |
101 | 68 |
|
102 | | - assertThat(executionTime).isLessThan(100); |
103 | 69 | } |
104 | 70 |
|
105 | 71 | } |
0 commit comments