Skip to content

fix(tests): fix intermittent connect timeout failure - #5140

Merged
k-wall merged 1 commit into
kroxylicious:mainfrom
AdityaThakur1998:fix/connect-timeout-test-clock-ordering
Oct 19, 2026
Merged

k-wall merged 1 commit into
kroxylicious:mainfrom
AdityaThakur1998:fix/connect-timeout-test-clock-ordering

Conversation

@AdityaThakur1998

Copy link
Copy Markdown
Contributor

Closes #5111.

ResilienceIT#shouldHonourConfiguredUpstreamConnectTimeout was disabled under #5111 after failing intermittently on CI, never locally:

Expecting actual: 1.983740463S
to be greater than or equal to: 2S

This re-enables it and fixes the cause. Test-only change.

Cause

The test timed a window that started after the thing it was measuring.

System.nanoTime() was read in the try body, by which point the AdminClient — created in the try-with-resources header — had already bootstrapped and caused the proxy to dial the blackholed address. So elapsed = CONNECT_TIMEOUT − gap + reconnect, where gap is the time between the dial starting and the clock being read. The gap sat on the wrong side of the inequality: anything that stalled in it made elapsed smaller, and with ~120ms of margin it only had to stall for ~140ms to go negative.

Confirmed two ways. First, by inspection: ServerConnectionStateMachine#initConnection is the only Bootstrap.connect in kroxylicious-runtime, reachable only via connect(), whose three callers in ClientConnectionStateMachine each require an established downstream client channel. DefaultKroxyliciousTester's constructor creates no client, so nothing in proxy startup can dial. Second, by measurement — scraping every kroxylicious_proxy_to_server* series a full second after the tester was constructed and before tester.admin() was called:

series before tester.admin() after
..._connections_total {node_id=bootstrap} absent 1.0
count of proxy_to_server series 0 3

No meters registered at all beforehand. The AdminClient is the only trigger, so a clock read before it strictly precedes the dial.

Fix

Read the clock before anything that can initiate an upstream connect, by moving the Admin creation into a nested try after it:

try (var tester = kroxyliciousTester(config);
        var managementClient = tester.getManagementClient()) {
    var startedAt = System.nanoTime();

    // When
    try (var admin = tester.admin()) {
        ...
    }
}

elapsed > CONNECT_TIMEOUT + reconnect now holds by construction rather than by margin — any pause anywhere increases elapsed instead of eating into it. The nested try is why the When label sits above a resource header; creating the client isn't the action under test, and describeCluster() remains a single statement.

Testing

Same assertions, same constants, same configuration. ResilienceIT passes as a full class, 9 tests, 0 skipped.

elapsed margin over the 2s floor
before 2123–2162ms 123–162ms
after 2326–2361ms 326–361ms

The ~200ms shift is larger than the 1.4ms of construction time the reordering moves. Most likely the gap was dominated by first-use initialisation of the JDK HttpClient inside ManagementClient, which is cheap once warm and expensive on the first construction in a fork — which would also explain why the failure was order-dependent and so only ever appeared on CI. Not worth chasing further, since the fix removes the term whatever its size.

No changelog entry: nothing user-facing changes.

🤖 Assisted by Claude Opus

shouldHonourConfiguredUpstreamConnectTimeout read System.nanoTime()
only after the AdminClient had been constructed in the outer
try-with-resources header. The AdminClient bootstraps on construction,
and that is the sole trigger for the proxy's upstream dial, so the clock
started after the connect timeout window had already begun. The measured
elapsed therefore spanned less than the configured 2s timeout, and under
CI jitter the floor assertion could go negative and fail intermittently.

Read the clock before creating the Admin by moving its construction into
a nested try-with-resources after startedAt, so the measured window fully
contains the connect timeout. Re-enable the test that was temporarily
disabled in kroxylicious#5111.

Relates-to: kroxylicious#5111
Assisted-by: Claude Opus 4.8 <[email protected]>
Signed-off-by: AdityaThakur1998 <[email protected]>
@AdityaThakur1998
AdityaThakur1998 requested a review from a team as a code owner October 19, 2026 11:08
@sonarqubecloud

Copy link
Copy Markdown

@sonarqubecloud

Copy link
Copy Markdown

@AdityaThakur1998 AdityaThakur1998 changed the title fix(tests): read clock before upstream dial in connect timeout test fix(tests): fix intermittent connect timeout failure Oct 19, 2026

@k-wall k-wall left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@k-wall
k-wall added this pull request to the merge queue Oct 19, 2026
Merged via the queue into kroxylicious:main with commit c39a655 Oct 19, 2026
45 checks passed
SamBarker pushed a commit to Divyanshkumar62/kroxylicious that referenced this pull request Oct 28, 2026
…roxylicious#5140)

shouldHonourConfiguredUpstreamConnectTimeout read System.nanoTime()
only after the AdminClient had been constructed in the outer
try-with-resources header. The AdminClient bootstraps on construction,
and that is the sole trigger for the proxy's upstream dial, so the clock
started after the connect timeout window had already begun. The measured
elapsed therefore spanned less than the configured 2s timeout, and under
CI jitter the floor assertion could go negative and fail intermittently.

Read the clock before creating the Admin by moving its construction into
a nested try-with-resources after startedAt, so the measured window fully
contains the connect timeout. Re-enable the test that was temporarily
disabled in kroxylicious#5111.

Relates-to: kroxylicious#5111
Assisted-by: Claude Opus 4.8 <[email protected]>

Signed-off-by: AdityaThakur1998 <[email protected]>
Signed-off-by: Sam Barker <[email protected]>
franvila pushed a commit to franvila/kproxy that referenced this pull request Oct 30, 2026
…roxylicious#5140)

shouldHonourConfiguredUpstreamConnectTimeout read System.nanoTime()
only after the AdminClient had been constructed in the outer
try-with-resources header. The AdminClient bootstraps on construction,
and that is the sole trigger for the proxy's upstream dial, so the clock
started after the connect timeout window had already begun. The measured
elapsed therefore spanned less than the configured 2s timeout, and under
CI jitter the floor assertion could go negative and fail intermittently.

Read the clock before creating the Admin by moving its construction into
a nested try-with-resources after startedAt, so the measured window fully
contains the connect timeout. Re-enable the test that was temporarily
disabled in kroxylicious#5111.

Relates-to: kroxylicious#5111
Assisted-by: Claude Opus 4.8 <[email protected]>

Signed-off-by: AdityaThakur1998 <[email protected]>
Signed-off-by: Francisco Vila <[email protected]>
franvila pushed a commit to franvila/kproxy that referenced this pull request Nov 18, 2026
…roxylicious#5140)

shouldHonourConfiguredUpstreamConnectTimeout read System.nanoTime()
only after the AdminClient had been constructed in the outer
try-with-resources header. The AdminClient bootstraps on construction,
and that is the sole trigger for the proxy's upstream dial, so the clock
started after the connect timeout window had already begun. The measured
elapsed therefore spanned less than the configured 2s timeout, and under
CI jitter the floor assertion could go negative and fail intermittently.

Read the clock before creating the Admin by moving its construction into
a nested try-with-resources after startedAt, so the measured window fully
contains the connect timeout. Re-enable the test that was temporarily
disabled in kroxylicious#5111.

Relates-to: kroxylicious#5111
Assisted-by: Claude Opus 4.8 <[email protected]>

Signed-off-by: AdityaThakur1998 <[email protected]>
Signed-off-by: Francisco Vila <[email protected]>
franvila pushed a commit to franvila/kproxy that referenced this pull request Nov 18, 2026
…roxylicious#5140)

shouldHonourConfiguredUpstreamConnectTimeout read System.nanoTime()
only after the AdminClient had been constructed in the outer
try-with-resources header. The AdminClient bootstraps on construction,
and that is the sole trigger for the proxy's upstream dial, so the clock
started after the connect timeout window had already begun. The measured
elapsed therefore spanned less than the configured 2s timeout, and under
CI jitter the floor assertion could go negative and fail intermittently.

Read the clock before creating the Admin by moving its construction into
a nested try-with-resources after startedAt, so the measured window fully
contains the connect timeout. Re-enable the test that was temporarily
disabled in kroxylicious#5111.

Relates-to: kroxylicious#5111
Assisted-by: Claude Opus 4.8 <[email protected]>

Signed-off-by: AdityaThakur1998 <[email protected]>
Signed-off-by: Francisco Vila <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Non-Deterministic Test shouldHonourConfiguredUpstreamConnectTimeout

2 participants