Skip to content

Cleanup test server output#5559

Merged
osipxd merged 4 commits intomainfrom
osipxd/cleanup-test-server
Apr 30, 2026
Merged

Cleanup test server output#5559
osipxd merged 4 commits intomainfrom
osipxd/cleanup-test-server

Conversation

@osipxd
Copy link
Copy Markdown
Member

@osipxd osipxd commented Apr 28, 2026

Subsystem
Test Infrastructure

Motivation
Test server prints a lot of exceptions in logs making it expensive for AI tools to analyze the output.

Solution

  • Add StatusPages plugin only when flag ktorbuild.testServer.verbose is true
  • Do not print CancellationExceptions

@osipxd osipxd self-assigned this Apr 28, 2026
@osipxd osipxd changed the title Cleanup test server Cleanup test server output Apr 28, 2026
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 28, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Refactors test-server lifecycle to use external CoroutineScope and SupervisorJob, adds a verbose: Boolean toggle to Application.tests(verbose) controlling installation of StatusPages, removes CloseableGroup, updates TCP server to accept an external scope, adjusts a WebSocket test to respond 500 explicitly, and documents the ktorbuild.testServer.verbose Gradle property.

Changes

Cohort / File(s) Summary
Lifecycle & Service
ktor-test-server/src/main/kotlin/test/server/TestServer.kt, ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
Replaced Closeable-based lifecycle with CoroutineScope-driven startup/shutdown. startServer now takes scope: CoroutineScope, verbose: Boolean. TestServerService gains Parameters with verbose: Property<Boolean>, creates a SupervisorJob-backed scope, starts server via startServer(scope, verbose), and cancels/joins scope on shutdown.
Application & TCP server
ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt, ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt
Application.tests()Application.tests(verbose: Boolean) so StatusPages (stacktrace-printing 500 handler) is installed only when verbose. TestTcpServer no longer implements CoroutineScope/Closeable; it accepts an external CoroutineScope and refactors bind/accept into suspend functions, avoiding GlobalScope.
Tests & removal
ktor-test-server/src/main/kotlin/test/server/tests/WebSockets.kt, (deleted) ktor-test-server/src/main/kotlin/test/server/tests/CloseableGroup.kt
WebSockets test route /500 now responds with HttpStatusCode.InternalServerError instead of throwing; CloseableGroup utility deleted.
Docs & build config
ktor-test-server/README.md, ktor-test-server/settings.gradle.kts
Added README section documenting -Pktorbuild.testServer.verbose=true. Updated version catalog source for ktorLibs (version catalog reference bumped).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~30 minutes

Possibly related PRs

Suggested labels

👍 ship!

Suggested reviewers

  • bjhham
  • e5l
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Cleanup test server output' is concise and clearly describes the main objective of the PR—reducing excessive logging and exception output from the test server.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed The pull request description follows the required template with all essential sections (Subsystem, Motivation, Solution) clearly provided and well-articulated.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch osipxd/cleanup-test-server

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 96e7252c1d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ktor-test-server/src/main/kotlin/test/server/TestServer.kt Outdated
Comment thread ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@ktor-test-server/src/main/kotlin/test/server/TestServerService.kt`:
- Around line 20-23: Wrap the start-up call in the init block in a try/catch so
any exception during startServer(scope) cancels the service scope before
propagating; specifically, in the init {} surrounding startServer(scope) catch
Throwable, call scope.cancel("startup aborted", it) (or scope.cancel()) to stop
any children launched by startServer, then rethrow the exception (do not
swallow), so close() isn't relied upon to run; reference init,
startServer(scope), scope.cancel(...), and close() when making the change.

In `@ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt`:
- Around line 30-34: The catch block in TestTcpServer.kt currently calls
ensureActive() but still logs/prints any CancellationException; update the
handler so that if the caught throwable is a CancellationException it is
rethrown (or rethrown after calling ensureActive()) before any
println/cause.printStackTrace/logging occurs; apply the same change to the
second catch site (the block around lines 40-43) so CancellationException is
never logged but propagated immediately.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7fa47237-a3f9-44f9-8bc5-63aa52d465c7

📥 Commits

Reviewing files that changed from the base of the PR and between 807c696 and 96e7252.

📒 Files selected for processing (6)
  • ktor-test-server/build.gradle.kts
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
  • ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt
  • ktor-test-server/src/main/kotlin/test/server/tests/CloseableGroup.kt
💤 Files with no reviewable changes (3)
  • ktor-test-server/build.gradle.kts
  • ktor-test-server/src/main/kotlin/test/server/tests/CloseableGroup.kt
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt

Comment thread ktor-test-server/src/main/kotlin/test/server/TestServer.kt Outdated
Comment thread ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
Comment thread ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt
@osipxd osipxd force-pushed the osipxd/cleanup-test-server branch from fae7166 to 2ce48fe Compare April 28, 2026 12:00
@osipxd osipxd marked this pull request as draft April 28, 2026 12:00
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

♻️ Duplicate comments (1)
ktor-test-server/src/main/kotlin/test/server/TestServer.kt (1)

36-47: ⚠️ Potential issue | 🟠 Major

Still misses cleanup when startup fails.

If any it.start() throws before Line 41 registers the shutdown coroutine, the servers that already started stay alive and keep their ports bound for the rest of the build.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ktor-test-server/src/main/kotlin/test/server/TestServer.kt` around lines 36 -
47, The startup sequence currently starts servers inside runBlocking via
servers.map { async { it.start() } } and only afterwards launches the shutdown
coroutine (CoroutineName("server-stopper")), so if any it.start() throws the
already-started servers remain running; to fix, register the shutdown coroutine
(the awaitCancellation finally block that calls it.stop(...)) before initiating
starts and/or wrap the start logic in a try/catch that on any exception iterates
the servers that have already started and calls it.stop(gracePeriodMillis = 0,
timeoutMillis = 0); ensure you reference the existing awaitCancellation shutdown
coroutine, the it.start() calls, and the it.stop(...) calls so the cleanup runs
on startup failure.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@ktor-test-server/README.md`:
- Around line 11-14: Update the fenced code block in README.md's Example section
to include a language tag (use bash) so the block reads ```bash followed by the
command "./gradlew :ktor-client-core:jvmTest -Pktorbuild.testServer.verbose" and
ending with ```; this will satisfy markdownlint and enable shell syntax
highlighting for the example.

---

Duplicate comments:
In `@ktor-test-server/src/main/kotlin/test/server/TestServer.kt`:
- Around line 36-47: The startup sequence currently starts servers inside
runBlocking via servers.map { async { it.start() } } and only afterwards
launches the shutdown coroutine (CoroutineName("server-stopper")), so if any
it.start() throws the already-started servers remain running; to fix, register
the shutdown coroutine (the awaitCancellation finally block that calls
it.stop(...)) before initiating starts and/or wrap the start logic in a
try/catch that on any exception iterates the servers that have already started
and calls it.stop(gracePeriodMillis = 0, timeoutMillis = 0); ensure you
reference the existing awaitCancellation shutdown coroutine, the it.start()
calls, and the it.stop(...) calls so the cleanup runs on startup failure.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 419fe0be-1048-4268-88d8-5ed87b8064ac

📥 Commits

Reviewing files that changed from the base of the PR and between fae7166 and 2ce48fe.

📒 Files selected for processing (5)
  • ktor-test-server/README.md
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
  • ktor-test-server/src/main/kotlin/test/server/tests/WebSockets.kt
🚧 Files skipped from review as they are similar to previous changes (2)
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServerService.kt

Comment thread ktor-test-server/README.md
@osipxd osipxd force-pushed the osipxd/cleanup-test-server branch 2 times, most recently from 370b636 to 784f10c Compare April 28, 2026 12:16
@osipxd osipxd marked this pull request as ready for review April 28, 2026 14:37
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 784f10c97f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

♻️ Duplicate comments (1)
ktor-test-server/README.md (1)

12-14: ⚠️ Potential issue | 🟡 Minor

Add the missing bash fence tag.

This still trips MD040 and loses shell highlighting. This was already called out on the previous revision.

Suggested fix
-```
+```bash
 ./gradlew :ktor-client-core:jvmTest -Pktorbuild.testServer.verbose
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ktor-test-server/README.md` around lines 12 - 14, The fenced code block in
README.md is missing a language tag which causes MD040 and loses shell
highlighting; update the triple-backtick fence surrounding the Gradle command
("./gradlew :ktor-client-core:jvmTest -Pktorbuild.testServer.verbose") to use a
bash tag (change ``` to ```bash) so the snippet is syntax-highlighted and MD040
is resolved.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@ktor-test-server/README.md`:
- Around line 12-14: The README's Gradle example is missing the value assignment
for the project property; update the command that shows running the jvmTest for
:ktor-client-core to pass the boolean value by changing the property flag to
include "=true" (i.e., use -Pktorbuild.testServer.verbose=true) so the Gradle
property is actually set when demonstrating the test run command.

In `@ktor-test-server/src/main/kotlin/test/server/TestServer.kt`:
- Around line 36-41: The finally block inside
scope.launch(CoroutineName("server-stopper")) currently calls servers.forEach {
it.stop(gracePeriodMillis = 0, timeoutMillis = 0) } which will abort remaining
stops if one throws; update that shutdown loop to stop each server independently
by wrapping each it.stop(...) in its own try/catch so a failure on one server is
caught, logged (include the server identity and exception), and the loop
continues to attempt to stop the rest (you can use a for loop or forEach with an
inner try/catch); ensure the awaitCancellation() and
CoroutineName("server-stopper") logic remains unchanged.

In `@ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt`:
- Around line 28-37: In ServerSocket.serve(), don't endlessly retry after a
non-cancellation failure from accept(): in the catch block for Throwable
(excluding CancellationException) log the error as you do but then decide to
stop serving for fatal errors instead of continue; specifically detect fatal
conditions (e.g., socket closed/SocketException or the ServerSocket isClosed)
and break/return from the loop or close the ServerSocket to stop the loop,
otherwise for transient errors you may continue; update the catch in
ServerSocket.serve() around accept() to break/return on fatal errors rather than
always continue.

---

Duplicate comments:
In `@ktor-test-server/README.md`:
- Around line 12-14: The fenced code block in README.md is missing a language
tag which causes MD040 and loses shell highlighting; update the triple-backtick
fence surrounding the Gradle command ("./gradlew :ktor-client-core:jvmTest
-Pktorbuild.testServer.verbose") to use a bash tag (change ``` to ```bash) so
the snippet is syntax-highlighted and MD040 is resolved.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 930be4a3-2fc4-47a4-9b7c-926390954533

📥 Commits

Reviewing files that changed from the base of the PR and between 2ce48fe and 784f10c.

📒 Files selected for processing (7)
  • ktor-test-server/README.md
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
  • ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt
  • ktor-test-server/src/main/kotlin/test/server/tests/CloseableGroup.kt
  • ktor-test-server/src/main/kotlin/test/server/tests/WebSockets.kt
💤 Files with no reviewable changes (1)
  • ktor-test-server/src/main/kotlin/test/server/tests/CloseableGroup.kt
✅ Files skipped from review due to trivial changes (1)
  • ktor-test-server/src/main/kotlin/test/server/tests/WebSockets.kt
🚧 Files skipped from review as they are similar to previous changes (1)
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt

Comment thread ktor-test-server/README.md Outdated
Comment thread ktor-test-server/src/main/kotlin/test/server/TestServer.kt
Comment thread ktor-test-server/src/main/kotlin/test/server/TestTcpServer.kt
@osipxd osipxd force-pushed the osipxd/cleanup-test-server branch from 784f10c to ebe9e8e Compare April 29, 2026 06:01
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt`:
- Around line 29-31: The exception<Throwable> StatusPages handler currently
prints every throwable (exception<Throwable> { call, cause -> ... }) including
CancellationException; update the handler to detect
kotlin.coroutines.cancellation.CancellationException (or cause is
CancellationException) and skip calling cause.printStackTrace() for that case,
only printing the stacktrace for non-cancellation throwables while keeping the
existing call.respondText(...) behavior unchanged for both paths.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cd31ec45-d49f-41a0-b320-85d1e23b9153

📥 Commits

Reviewing files that changed from the base of the PR and between 784f10c and ebe9e8e.

📒 Files selected for processing (5)
  • ktor-test-server/README.md
  • ktor-test-server/settings.gradle.kts
  • ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServer.kt
  • ktor-test-server/src/main/kotlin/test/server/TestServerService.kt
✅ Files skipped from review due to trivial changes (2)
  • ktor-test-server/settings.gradle.kts
  • ktor-test-server/README.md
🚧 Files skipped from review as they are similar to previous changes (1)
  • ktor-test-server/src/main/kotlin/test/server/TestServer.kt

Comment thread ktor-test-server/src/main/kotlin/test/server/ClientTestServer.kt
@osipxd osipxd force-pushed the osipxd/cleanup-test-server branch from ebe9e8e to 7bc47c0 Compare April 29, 2026 07:35
@osipxd osipxd enabled auto-merge (squash) April 29, 2026 13:18
Copy link
Copy Markdown
Contributor

@bjhham bjhham left a comment

Choose a reason for hiding this comment

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

Good call, looks good 👍

@osipxd osipxd merged commit 86fd3cb into main Apr 30, 2026
17 checks passed
@osipxd osipxd deleted the osipxd/cleanup-test-server branch April 30, 2026 08:35
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.

2 participants