Skip to content

fix: removing Thread.sleep() from KubernetesReconcilerCreatorTest - #4915

Merged
kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
sarveshkaushal:sarveshkaushal/flaky-test-k8reconciler-clean
Oct 16, 2026
Merged

kubernetes-prow[bot] merged 1 commit into
kubernetes-client:masterfrom
sarveshkaushal:sarveshkaushal/flaky-test-k8reconciler-clean

Conversation

@sarveshkaushal

@sarveshkaushal sarveshkaushal commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Notes

Replaces the fixed Thread.sleep() in KubernetesReconcilerCreatorTest with semaphore based synchronization.

The custom work-queue key function releases a semaphore after adding the generated request to the controller work queue. The test waits for that signal before asserting on the queue.

This is related to issue #1223

Testing

Ran mvn install on the project root.

@kubernetes-prow kubernetes-prow Bot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 18, 2026
@sarveshkaushal

Copy link
Copy Markdown
Contributor Author

/assign @brendandburns

@brendandburns

Copy link
Copy Markdown
Contributor

As with the other ones, please try to minimize the diff for easier reviewing.

@sarveshkaushal

Copy link
Copy Markdown
Contributor Author

@brendandburns - Formatting changes in this are expected as I have enclosed the code in try block. I have fixed the formatting changes in other PRs.

@SpringBootTest(classes = {KubernetesReconcilerCreatorTest.App.class})
class KubernetesReconcilerCreatorTest {

private static final Semaphore REQUEST_ENQUEUED = new Semaphore(0);

@brendandburns brendandburns Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use globals that aren't constants in the test since it makes it impossible to run tests in parallel in the same JVM.

@brendandburns

Copy link
Copy Markdown
Contributor

This change looks ok, except for the global Semaphore part, please switch that to be a test local semaphore instead. Also not sure why you added the shutdown (and thus the try/catch) it's not necessarily bad, but I'm not sure its needed.

@sarveshkaushal
sarveshkaushal force-pushed the sarveshkaushal/flaky-test-k8reconciler-clean branch from 11235db to 669e4ae Compare October 7, 2026 01:47
@sarveshkaushal
sarveshkaushal force-pushed the sarveshkaushal/flaky-test-k8reconciler-clean branch from 669e4ae to 6990ce5 Compare October 7, 2026 04:38
@kubernetes-prow kubernetes-prow Bot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 7, 2026
@sarveshkaushal

Copy link
Copy Markdown
Contributor Author

@brendandburns - Thanks for the feedback! You’re right, the static Semaphore was introducing shared global state, which isn't ideal for parallel test execution.
To fix this cleanly, I’ve pivoted to using Awaitility instead of a Semaphore. This eliminates all static/global state. The PR has been updated with this minimal change. Let me know what you think!

@brendandburns

Copy link
Copy Markdown
Contributor

I think that Awaitility just uses sleep under the hood (I'm not sure how it could do anything other than poll on that condition) so I'm not sure it really fixes this test.

@sarveshkaushal

Copy link
Copy Markdown
Contributor Author

I was debating it with Gemini and here is the reasoning:

Awaitility replaces a blind pause with an intelligent evaluate-and-sleep loop.
Instead of waiting for a time, it waits for a state. If your asynchronous task finishes early, Awaitility catches it immediately and moves to the next test. If the task takes longer than usual because the machine is under load, Awaitility patiently waits up to your maximum timeout limit before failing.

@brendandburns

brendandburns commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Awaitlity is basically just while loop with a bunch of small sleeps. imho, using a semaphore or countdown latch is the right way to do this because it waits exactly the right amount of time.

If you really want to go down this route, just use a while loop, let's not bring in an extra library dependency just for this.

Edited, we already have a dependency on that library, so I guess this is ok, I'd still prefer a synchronization primitive.

@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 Oct 16, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

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

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 Oct 16, 2026
@kubernetes-prow
kubernetes-prow Bot merged commit 9d473c1 into kubernetes-client:master Oct 16, 2026
14 checks passed
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. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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