Skip to content

Commit 8c7c91f

Browse files
committed
fix(tests): make event correlation timestamps deterministic
Replace Thread.sleep(100) with explicit timestamp increments in EventCorrelatorTest. Duplicate events preserve their input timestamps, so advancing those timestamps is sufficient to verify compression. Aggregated events, however, generate their own timestamps using OffsetDateTime.now(). Consecutive calls can return identical values depending on system clock resolution, causing intermittent CI failures on Windows. Following the approach used by the Go client, introduce an injectable timestamp source in EventAggregator using Supplier<OffsetDateTime>. Add a corresponding EventCorrelator constructor to pass the supplier through, while preserving the existing constructors and production behaviour with OffsetDateTime::now. Use an incrementing timestamp supplier in the test to control aggregation timestamps independently of the system clock. This preserves the existing timestamp assertions, removes the timing dependency, and avoids changes to EventLogger.
1 parent 0ec504c commit 8c7c91f

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎extended/src/main/java/io/kubernetes/client/extended/event/legacy/EventAggregator.java‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.util.Objects;
2323
import java.util.Set;
2424
import java.util.function.Function;
25+
import java.util.function.Supplier;
26+
2527
import org.apache.commons.lang3.tuple.MutablePair;
2628

2729
public class EventAggregator {
@@ -30,10 +32,19 @@ public class EventAggregator {
3032
public static final java.time.Duration DEFAULT_EVENT_AGGREGATE_CACHE_EXPIRATION =
3133
java.time.Duration.ofSeconds(600);
3234

35+
public EventAggregator(
36+
int maxLRUCacheEntries,
37+
Function<CoreV1Event, MutablePair<String, String>> keyFunc,
38+
Function<CoreV1Event, String> messageFunc) {
39+
this(maxLRUCacheEntries, keyFunc, messageFunc, OffsetDateTime::now);
40+
}
41+
3342
public EventAggregator(
3443
int maxLRUCacheEntries,
3544
Function<CoreV1Event, MutablePair<String, String>> keyFunc,
36-
Function<CoreV1Event, String> messageFunc) {
45+
Function<CoreV1Event, String> messageFunc,
46+
Supplier<OffsetDateTime> now) {
47+
this.now = now;
3748
this.keyFunc = keyFunc;
3849
this.messageFunc = messageFunc;
3950
this.maxEvents = DEFAULT_MAX_EVENT_LOCAL_KEYS;
@@ -47,11 +58,12 @@ public EventAggregator(
4758
private Cache<String, AggregatedRecord> spammingCache;
4859
private Function<CoreV1Event, MutablePair<String, String>> keyFunc;
4960
private Function<CoreV1Event, String> messageFunc;
61+
private final Supplier<OffsetDateTime> now;
5062

5163
private final int maxEvents;
5264

5365
public synchronized MutablePair<CoreV1Event, String> aggregate(CoreV1Event event) {
54-
OffsetDateTime now = OffsetDateTime.now();
66+
OffsetDateTime now = this.now.get();
5567

5668
MutablePair<String, String> aggregatedKeys = keyFunc.apply(event);
5769
String aggregatedKey = aggregatedKeys.getLeft();

‎extended/src/main/java/io/kubernetes/client/extended/event/legacy/EventCorrelator.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
import io.kubernetes.client.custom.V1Patch;
1616
import io.kubernetes.client.openapi.models.CoreV1Event;
17+
18+
import java.time.OffsetDateTime;
1719
import java.util.Optional;
1820
import java.util.function.Predicate;
21+
import java.util.function.Supplier;
22+
1923
import org.apache.commons.lang3.tuple.MutablePair;
2024

2125
public class EventCorrelator {
@@ -25,14 +29,19 @@ public class EventCorrelator {
2529
protected EventLogger logger;
2630

2731
public EventCorrelator() {
32+
this(OffsetDateTime::now);
33+
}
34+
35+
public EventCorrelator(Supplier<OffsetDateTime> now) {
2836
final int maxLRUCacheEntries = 4096;
2937

3038
this.filter = new EventSpamFilter(maxLRUCacheEntries, EventUtils::getSpamKey)::filter;
3139
this.aggregator =
3240
new EventAggregator(
3341
maxLRUCacheEntries,
3442
EventUtils::getAggregatedAndLocalKeyByReason,
35-
EventUtils::getAggregatedMessageByReason);
43+
EventUtils::getAggregatedMessageByReason,
44+
now);
3645
this.logger = new EventLogger(maxLRUCacheEntries, EventUtils::getEventKey);
3746
}
3847

‎extended/src/test/java/io/kubernetes/client/extended/event/EventCorrelatorTest.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import java.util.Collection;
3333
import java.util.List;
3434
import java.util.Optional;
35+
import java.util.concurrent.atomic.AtomicReference;
36+
3537
import org.apache.commons.lang3.tuple.MutablePair;
3638
import org.junit.jupiter.params.ParameterizedTest;
3739
import org.junit.jupiter.params.provider.MethodSource;
@@ -143,18 +145,19 @@ void eventCorrelate(
143145
CoreV1Event expectedEvent,
144146
Boolean expectedSkip)
145147
throws Exception {
146-
EventCorrelator correlator = new EventCorrelator();
148+
AtomicReference<OffsetDateTime> time =
149+
new AtomicReference<>(OffsetDateTime.now());
150+
EventCorrelator correlator = new EventCorrelator(() -> time.getAndUpdate(t -> t.plusSeconds(1)));
151+
OffsetDateTime now = OffsetDateTime.now();
147152
for (CoreV1Event event : previousEvents) {
148-
OffsetDateTime now = OffsetDateTime.now();
149153
event.setFirstTimestamp(now);
150154
event.setLastTimestamp(now);
151155
Optional<MutablePair<CoreV1Event, V1Patch>> result = correlator.correlate(event);
152156
if (result.isEmpty()) {
153157
correlator.updateState(event);
154158
}
155159
}
156-
Thread.sleep(100);
157-
OffsetDateTime now = OffsetDateTime.now();
160+
now = now.plusSeconds(1);
158161
newEvent.setFirstTimestamp(now);
159162
newEvent.setLastTimestamp(now);
160163
Optional<MutablePair<CoreV1Event, V1Patch>> result = correlator.correlate(newEvent);

0 commit comments

Comments
 (0)