Skip to content

test(ktor): wait for a route's failure in real time - #130

Merged
rocketraman merged 1 commit into
masterfrom
rg/ktor-test-real-timeout
Dec 5, 2026
Merged

rocketraman merged 1 commit into
masterfrom
rg/ktor-test-real-timeout

Conversation

@rocketraman

Copy link
Copy Markdown
Owner

Summary

KtorServiceTest's "a route can fail the service's scope, which stops the application" test is flaky.

TestBalloon runs every test in a TestScope by default (TestSession.DefaultConfiguration calls .testScope(true)), so delays and timeouts run on a virtual clock. The test waited for the route's failure with withTimeout(10.seconds) { failure.await() }. That failure is thrown on a real thread (Dispatchers.Default). If the test coroutine had nothing else scheduled when it started waiting, the virtual clock jumped ten seconds ahead and the timeout fired at once. The test passed only when the failure had already arrived, which a slow CI runner can miss.

The wait now runs on Dispatchers.Default, so its timeout is ten real seconds. GrpcServerTest already waits this way.

Testing

  • :boot-server-http-ktor:test passes five times in a row.
  • With a 200 ms sleep added before the route's error("died"), the previous wait fails on both CIO and Netty with the TimeoutCancellationException seen in CI, and the new wait passes.

TestBalloon runs tests in a TestScope, on a virtual clock. The test
waited for the failure, which comes from a real thread, with
withTimeout(10.seconds) on that clock. Once the test had nothing else
scheduled, the virtual clock jumped ahead and the timeout fired at
once, unless the failure had already arrived. That made the test
flaky on a slow runner.

Wait on Dispatchers.Default, so the timeout is 10 real seconds, as
GrpcServerTest does.
@claude

claude Bot commented Dec 5, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@rocketraman
rocketraman merged commit eda2eb0 into master Dec 5, 2026
2 checks passed
@rocketraman
rocketraman deleted the rg/ktor-test-real-timeout branch December 5, 2026 07:58
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.

1 participant