|
17 | 17 | package de.codecentric.boot.admin.server.services; |
18 | 18 |
|
19 | 19 | import java.time.Duration; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | +import java.util.concurrent.CopyOnWriteArrayList; |
20 | 25 | import java.util.function.Function; |
| 26 | +import java.util.stream.Collectors; |
| 27 | +import java.util.stream.IntStream; |
21 | 28 |
|
22 | 29 | import org.junit.jupiter.api.AfterEach; |
23 | 30 | import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.invocation.InvocationOnMock; |
24 | 32 | import reactor.core.publisher.Mono; |
25 | 33 |
|
26 | 34 | import de.codecentric.boot.admin.server.domain.values.InstanceId; |
27 | 35 |
|
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
28 | 37 | import static org.awaitility.Awaitility.await; |
29 | 38 | import static org.mockito.ArgumentMatchers.any; |
30 | 39 | import static org.mockito.Mockito.atLeast; |
31 | 40 | import static org.mockito.Mockito.atLeastOnce; |
| 41 | +import static org.mockito.Mockito.doAnswer; |
32 | 42 | import static org.mockito.Mockito.mock; |
33 | 43 | import static org.mockito.Mockito.never; |
34 | 44 | import static org.mockito.Mockito.verify; |
@@ -106,9 +116,133 @@ void should_check_after_error() { |
106 | 116 | .untilAsserted(() -> verify(this.checkFn, atLeast(2)).apply(InstanceId.of("Test"))); |
107 | 117 | } |
108 | 118 |
|
| 119 | + @Test |
| 120 | + void should_not_overflow_when_checks_timeout_randomly() { |
| 121 | + Duration CHECK_INTERVAL = Duration.ofMillis(500); |
| 122 | + |
| 123 | + @SuppressWarnings("unchecked") |
| 124 | + Function<InstanceId, Mono<Void>> timeoutCheckFn = mock(Function.class); |
| 125 | + |
| 126 | + java.util.concurrent.atomic.AtomicInteger invocationCount = new java.util.concurrent.atomic.AtomicInteger(0); |
| 127 | + doAnswer((invocation) -> { |
| 128 | + if (invocationCount.getAndIncrement() % 2 == 0) { |
| 129 | + // Succeed quickly on even invocations |
| 130 | + return Mono.empty(); |
| 131 | + } |
| 132 | + else { |
| 133 | + // Timeout on odd invocations |
| 134 | + return Mono.just("slow response").delayElement(CHECK_INTERVAL.plus(Duration.ofSeconds(1))).then(); |
| 135 | + } |
| 136 | + }).when(timeoutCheckFn).apply(any()); |
| 137 | + |
| 138 | + IntervalCheck timeoutCheck = new IntervalCheck("overflow-test", timeoutCheckFn, CHECK_INTERVAL, CHECK_INTERVAL, |
| 139 | + Duration.ofSeconds(1)); |
| 140 | + |
| 141 | + List<Throwable> retryErrors = new CopyOnWriteArrayList<>(); |
| 142 | + |
| 143 | + timeoutCheck.setRetryConsumer(retryErrors::add); |
| 144 | + timeoutCheck.markAsChecked(INSTANCE_ID); |
| 145 | + timeoutCheck.start(); |
| 146 | + try { |
| 147 | + await().atMost(Duration.ofSeconds(5)) |
| 148 | + .until(() -> retryErrors.stream() |
| 149 | + .noneMatch((Throwable er) -> "OverflowException".equalsIgnoreCase(er.getClass().getSimpleName()))); |
| 150 | + |
| 151 | + assertThat(retryErrors).noneMatch((Throwable e) -> e.getCause() != null |
| 152 | + && "OverflowException".equalsIgnoreCase(e.getCause().getClass().getSimpleName())); |
| 153 | + } |
| 154 | + finally { |
| 155 | + timeoutCheck.stop(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void should_not_lose_checks_under_backpressure() { |
| 161 | + Duration CHECK_INTERVAL = Duration.ofMillis(100); |
| 162 | + |
| 163 | + @SuppressWarnings("unchecked") |
| 164 | + Function<InstanceId, Mono<Void>> slowCheckFn = mock(Function.class); |
| 165 | + IntervalCheck slowCheck = new IntervalCheck("backpressure-test", slowCheckFn, CHECK_INTERVAL, |
| 166 | + Duration.ofMillis(50), Duration.ofSeconds(1)); |
| 167 | + |
| 168 | + List<Long> checkTimes = new CopyOnWriteArrayList<>(); |
| 169 | + doAnswer((invocation) -> { |
| 170 | + checkTimes.add(System.currentTimeMillis()); |
| 171 | + return Mono.empty(); |
| 172 | + }).when(slowCheckFn).apply(any()); |
| 173 | + |
| 174 | + slowCheck.markAsChecked(INSTANCE_ID); |
| 175 | + slowCheck.start(); |
| 176 | + |
| 177 | + try { |
| 178 | + await().atMost(Duration.ofSeconds(2)).until(() -> checkTimes.size() >= 5); |
| 179 | + // With onBackpressureLatest, we should have processed multiple checks without |
| 180 | + // drops |
| 181 | + assertThat(checkTimes).hasSizeGreaterThanOrEqualTo(5); |
| 182 | + } |
| 183 | + finally { |
| 184 | + slowCheck.stop(); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void should_not_lose_checks_under_backpressure_latest() { |
| 190 | + Duration CHECK_INTERVAL = Duration.ofMillis(100); |
| 191 | + |
| 192 | + @SuppressWarnings("unchecked") |
| 193 | + Function<InstanceId, Mono<Void>> slowCheckFn = mock(Function.class); |
| 194 | + |
| 195 | + IntervalCheck slowCheck = new IntervalCheck("backpressure-test", slowCheckFn, CHECK_INTERVAL, CHECK_INTERVAL, |
| 196 | + Duration.ofSeconds(1)); |
| 197 | + |
| 198 | + // Add multiple instances to increase load and cause drops |
| 199 | + Set<InstanceId> instanceIds = IntStream.range(0, 50) |
| 200 | + .mapToObj((i) -> InstanceId.of("Test" + i)) |
| 201 | + .collect(Collectors.toSet()); |
| 202 | + |
| 203 | + instanceIds.forEach((InstanceId instanceId) -> slowCheck.markAsChecked(instanceId)); |
| 204 | + |
| 205 | + List<Long> checkTimes = new CopyOnWriteArrayList<>(); |
| 206 | + Map<String, List<Long>> checkTimesPerInstance = new ConcurrentHashMap<>(); |
| 207 | + |
| 208 | + java.util.concurrent.atomic.AtomicInteger invocationCount = new java.util.concurrent.atomic.AtomicInteger(0); |
| 209 | + doAnswer((invocation) -> { |
| 210 | + long checkTime = System.currentTimeMillis(); |
| 211 | + String instanceId = instanceIdString(invocation); |
| 212 | + List<Long> checkTimesInstance = checkTimesPerInstance.computeIfAbsent(instanceId, |
| 213 | + (String k) -> new CopyOnWriteArrayList<>()); |
| 214 | + checkTimesInstance.add(checkTime); |
| 215 | + checkTimes.add(checkTime); |
| 216 | + if (invocationCount.getAndIncrement() % 2 == 0) { |
| 217 | + // Sometimes succeed quickly |
| 218 | + return Mono.empty(); |
| 219 | + } |
| 220 | + else { |
| 221 | + // Sometimes slow |
| 222 | + return Mono.delay(CHECK_INTERVAL.plus(Duration.ofMillis(500))).then(); |
| 223 | + } |
| 224 | + }).when(slowCheckFn).apply(any()); |
| 225 | + |
| 226 | + slowCheck.start(); |
| 227 | + |
| 228 | + try { |
| 229 | + await().atMost(Duration.ofSeconds(5)).until(() -> checkTimes.size() >= 500); |
| 230 | + // With onBackpressureLatest, we should process more checks without drops |
| 231 | + instanceIds.forEach((InstanceId instanceId) -> assertThat(checkTimesPerInstance.get(instanceId.getValue())) |
| 232 | + .hasSizeGreaterThanOrEqualTo(10)); |
| 233 | + } |
| 234 | + finally { |
| 235 | + slowCheck.stop(); |
| 236 | + } |
| 237 | + } |
| 238 | + |
109 | 239 | @AfterEach |
110 | 240 | void tearDown() { |
111 | 241 | this.intervalCheck.stop(); |
112 | 242 | } |
113 | 243 |
|
| 244 | + private static String instanceIdString(InvocationOnMock invocation) { |
| 245 | + return invocation.getArguments()[0].toString(); |
| 246 | + } |
| 247 | + |
114 | 248 | } |
0 commit comments