[test-improver] Improve tests for cmd/serve package#7805
Merged
Conversation
…rage Add two new test cases to serve_test.go that cover previously untested paths in the serveAndWait function: - TestServeAndWait_OnShutdownSignalCalled: verifies the optional onShutdownSignal callback is invoked when context is cancelled - TestServeAndWait_ServeFnError: verifies that an unexpected error returned by serveFn (not http.ErrServerClosed) triggers context cancellation and is propagated to the caller Coverage for serveAndWait: 68.0% -> 84.0% (+16%) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Jun 20, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances unit test coverage for the internal/cmd HTTP server lifecycle helper serveAndWait, focusing on previously untested branches to improve confidence in shutdown behavior and error propagation.
Changes:
- Adds a new test covering the
onShutdownSignalcallback branch during shutdown. - Adds a new test covering the non-
http.ErrServerClosederror path fromserveFn. - Updates the test file to use both
assertandrequirefor clearer test intent.
Show a summary per file
| File | Description |
|---|---|
| internal/cmd/serve_test.go | Adds new unit tests to exercise shutdown callback invocation and unexpected serve error handling in serveAndWait. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 1/1 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Test Improvements:
serve_test.goFile Analyzed
internal/cmd/serve_test.gointernal/cmdImprovements Made
1. Better Testing Patterns
TestFunctionName_Scenarioconventionassert(non-fatal) for signal callback and error value checks,require(fatal) for setup and primary error checks2. Increased Coverage
TestServeAndWait_OnShutdownSignalCalled: exercises theif onShutdownSignal != nil { onShutdownSignal() }branch that was previously always skipped (existing test passednil)TestServeAndWait_ServeFnError: exercises the error propagation path whereserveFnreturns an unexpected error (nothttp.ErrServerClosed), covering thecancel()call in the goroutine and thereturn serveErrbranchserveAndWait68.0%serveAndWait84.0%3. Cleaner & More Stable Tests
TestServeAndWait_ServeFnErroris fully synchronous —serveFnreturns immediately, the goroutine insideserveAndWaitcallscancel(), and the function returns quickly without timing dependenciesTestServeAndWait_OnShutdownSignalCalledfollows the same pattern as the existing graceful shutdown test, reusing the provenrequire.Eventuallyreadiness checkTest Execution
All tests pass:
Why These Changes?
serve_test.gowas selected because it had only one test covering the happy path ofserveAndWait(the core HTTP server lifecycle function), while two important branches had zero coverage:onShutdownSignalcallback — always passed asnilin the existing test, so theif onShutdownSignal != nilbranch was never executedErrServerClosederror fromserveFnshould triggercancel()and be returned to the callerBoth gaps represent real production behavior worth verifying: the shutdown signal callback is used to notify callers when shutdown begins, and robust error propagation from the serve goroutine is critical for graceful error handling.
Generated by Test Improver Workflow
Focuses on better patterns, increased coverage, and more stable tests
Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
index.crates.ioSee Network Configuration for more information.