|
50 | 50 | public class TestCacheOvershoot { |
51 | 51 | @InjectSoftAssertions protected SoftAssertions soft; |
52 | 52 |
|
| 53 | + /** This simulates the production setup. */ |
53 | 54 | @RepeatedTest(3) // consider the first repetition as a warmup (C1/C2) |
54 | 55 | public void testCacheOvershootDirectEviction() throws Exception { |
55 | | - testCacheOvershoot(Runnable::run); |
| 56 | + testCacheOvershoot(Runnable::run, true); |
56 | 57 | } |
57 | 58 |
|
| 59 | + /** This test <em>illustrates</em> delayed eviction, leading to more heap usage than admitted. */ |
58 | 60 | @RepeatedTest(3) // consider the first repetition as a warmup (C1/C2) |
59 | 61 | public void testCacheOvershootDelayedEviction() throws Exception { |
60 | 62 | // Production uses Runnable::run, but that lets this test sometimes run way too |
61 | 63 | // long, so we introduce some delay to simulate the case that eviction cannot keep up. |
62 | | - testCacheOvershoot(t -> delayedExecutor(2, TimeUnit.MILLISECONDS).execute(t)); |
| 64 | + testCacheOvershoot(t -> delayedExecutor(2, TimeUnit.MILLISECONDS).execute(t), false); |
63 | 65 | } |
64 | 66 |
|
65 | | - private void testCacheOvershoot(Executor evictionExecutor) throws Exception { |
| 67 | + private void testCacheOvershoot(Executor evictionExecutor, boolean direct) throws Exception { |
66 | 68 | var meterRegistry = new SimpleMeterRegistry(); |
67 | 69 |
|
68 | 70 | var config = |
@@ -119,20 +121,41 @@ private void testCacheOvershoot(Executor evictionExecutor) throws Exception { |
119 | 121 | }); |
120 | 122 | } |
121 | 123 |
|
122 | | - for (int i = 0; i < 50 && !seenOvershoot; i++) { |
| 124 | + for (int i = 0; i < 50; i++) { |
123 | 125 | Thread.sleep(10); |
124 | 126 | var w = cache.currentWeightReported(); |
125 | | - if (w > maxWeight) { |
| 127 | + if (w > admitWeight) { |
126 | 128 | seenOvershoot = true; |
127 | 129 | } |
128 | 130 | } |
129 | 131 |
|
130 | 132 | stop.set(true); |
131 | 133 | } |
132 | 134 |
|
133 | | - soft.assertThat(cache.currentWeightReported()).isLessThanOrEqualTo(admitWeight); |
134 | | - soft.assertThat(cache.rejections()).isEqualTo(0L); |
135 | | - soft.assertThat(meterRejectedWeight.totalAmount()).isEqualTo(0d); |
136 | | - soft.assertThat(seenOvershoot).isFalse(); |
| 135 | + // The read of `Eviction` properties is a plain volatile read since Caffeine version 3.2.3 |
| 136 | + // and trigger cleanups asynchronously. Before 3.2.3, cleanups happened synchronously. |
| 137 | + // This change breaks the initially present assertions of this test, but not the |
| 138 | + // functionality of the production code. |
| 139 | + // See https://github.com/ben-manes/caffeine/issues/1897 |
| 140 | + if (direct) { |
| 141 | + soft.assertThat(cache.currentWeightReported()).isLessThanOrEqualTo(admitWeight); |
| 142 | + |
| 143 | + // We will (with a good probability) see no rejections with direct eviction. But it can still |
| 144 | + // happen, |
| 145 | + // so we cannot have the following two assertions. Rejections are expected. |
| 146 | + // soft.assertThat(cache.rejections()).isEqualTo(0L); |
| 147 | + // soft.assertThat(meterRejectedWeight.totalAmount()).isEqualTo(0d); |
| 148 | + soft.assertThat(seenOvershoot).isFalse(); |
| 149 | + } else { |
| 150 | + // Note: we cannot ensure that the current weight is limited to the admitted weight with |
| 151 | + // asynchronous eviction. |
| 152 | + |
| 153 | + // We will (with an extremely high probability) see the current weight exceed the admitted |
| 154 | + // weight with |
| 155 | + // delayed eviction. |
| 156 | + soft.assertThat(cache.rejections()).isGreaterThan(0L); |
| 157 | + soft.assertThat(meterRejectedWeight.totalAmount()).isGreaterThan(0d); |
| 158 | + soft.assertThat(seenOvershoot).isTrue(); |
| 159 | + } |
137 | 160 | } |
138 | 161 | } |
0 commit comments