Skip to content

fix(tests): Make EventCorrelatorTest timestamps deterministic - #4927

Open
flrnd wants to merge 1 commit into
kubernetes-client:masterfrom
flrnd:fix-remove-thread-sleep-from-event-correlator-test
Open

flrnd wants to merge 1 commit into
kubernetes-client:masterfrom
flrnd:fix-remove-thread-sleep-from-event-correlator-test

Conversation

@flrnd

@flrnd flrnd commented Sep 29, 2026 •

Copy link
Copy Markdown

Notes

Avoid relying on Thread.sleep(100) to produce distinct timestamps.

The sleep was removed because the test only requires timestamp ordering, not elapsed time. However, the Windows Bazel build showed that OffsetDateTime.now() can return identical timestamps in consecutive calls depending on the environment clock resolution.

Advance the timestamp explicitly instead, keeping the test deterministic across different platforms.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: flrnd / name: flrnd (fd34a69)

@kubernetes-prow

Copy link
Copy Markdown
Contributor

Welcome @flrnd!

It looks like this is your first PR to kubernetes-client/java 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-client/java has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@kubernetes-prow kubernetes-prow Bot added cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Sep 29, 2026
@brendandburns

Copy link
Copy Markdown
Contributor

/lgtm
/approve

@kubernetes-prow kubernetes-prow Bot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 30, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, flrnd

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubernetes-prow kubernetes-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Sep 30, 2026
@flrnd

flrnd commented Sep 30, 2026 •

Copy link
Copy Markdown
Author

The same failure occurs on Java 11, 17, and 21 in the Windows Bazel build.
In Java 21, two parameterized cases fail because firstTimestamp and lastTimestamp are equal after removing the Thread.sleep(100). It seems that OffsetDateTime.now() calls can produce the same timestamp in this environment.

In this case, I now wonder if it is worth finding a deterministic way to generate the timestamps, or if leaving the sleep in place makes sense.

@flrnd

flrnd commented Sep 30, 2026

Copy link
Copy Markdown
Author

Another alternative to Thread.sleep(100) would be to explicitly advance the timestamp used by the test:

@@ -144,8 +138,8 @@ class EventCorrelatorTest {
       Boolean expectedSkip)
       throws Exception {
     EventCorrelator correlator = new EventCorrelator();
+    OffsetDateTime now = OffsetDateTime.now();
     for (CoreV1Event event : previousEvents) {
-      OffsetDateTime now = OffsetDateTime.now();
       event.setFirstTimestamp(now);
       event.setLastTimestamp(now);
       Optional<MutablePair<CoreV1Event, V1Patch>> result = correlator.correlate(event);
@@ -153,7 +147,7 @@ class EventCorrelatorTest {
         correlator.updateState(event);
       }
     }
-    OffsetDateTime now = OffsetDateTime.now();
+    now = now.plusNanos(10);
     newEvent.setFirstTimestamp(now);
     newEvent.setLastTimestamp(now);

@brendandburns

Copy link
Copy Markdown
Contributor

Feel free to try that alternate fix and update this PR.

@kubernetes-prow kubernetes-prow Bot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Oct 6, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

New changes are detected. LGTM label has been removed.

@flrnd flrnd changed the title fix(tests): remove unnecessary Thread.sleep from EventCorrelatorTest fix(tests): Make EventCorrelatorTest timestamps deterministic Oct 6, 2026
Avoid relying on Thread.sleep(100) to produce distinct timestamps.

The sleep was removed because the test only requires timestamp ordering,
not elapsed time. However, the Windows Bazel build showed that
OffsetDateTime.now() can return identical timestamps in consecutive calls
depending on the environment clock resolution.

Advance the timestamp explicitly instead, keeping the test deterministic
across different platforms.
@flrnd
flrnd force-pushed the fix-remove-thread-sleep-from-event-correlator-test branch from bd1ce4a to 9f9dcc7 Compare October 6, 2026 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants