Skip to content

Commit 5e2509a

Browse files
authored
[test] Add tests for cmd.shutdownTracingProviderWithTimeout (#14556)
# Test Coverage Improvement: shutdownTracingProviderWithTimeout ## Function Analyzed - **Package**: `internal/cmd` - **Function**: `shutdownTracingProviderWithTimeout` - **Previous Coverage**: 71.4% - **New Coverage**: 100% - **Complexity**: Low-Medium (timeout context + error branch) ## Why This Function? Overall coverage is already high. This was among the few remaining partially covered non-trivial functions, and its shutdown-error branch (warning callback) was never exercised. ## Tests Added - ✅ SDK provider shutdown against a collector that hangs on export, so the 5s shutdown timeout fires and the `warnf` error path is verified (exactly one warning, expected message) - Skipped under `-short` since it waits for the 5s timeout ## Test Execution ``` --- PASS: TestShutdownTracingProviderWithTimeout (5.00s) tracing.go:95: shutdownTracingProviderWithTimeout 100.0% ``` --- *Generated by Test Coverage Improver* > [!WARNING] > <details> > <summary>Firewall blocked 4 domains</summary> > > The following domains were blocked by the firewall during workflow execution: > > - `example.com` > - `nonexistent.local` > - `slow.example.com` > - `thishostdoesnotexist12345.com` > > To allow these domains, add them to the `network.allowed` list in your workflow frontmatter: > > ```yaml > network: > allowed: > - defaults > - "example.com" > - "nonexistent.local" > - "slow.example.com" > - "thishostdoesnotexist12345.com" > ``` > > See [Network Configuration](https://github.github.com/gh-aw/reference/network/) for more information. > > </details> > Generated by [Test Coverage Improver](https://github.com/github/gh-aw-mcpg/actions/runs/37653372714) · copilot · auto · 60.2 AIC · ⊞ 10.6K · [◷](https://github.com/search?q=repo%3Agithub%2Fgh-aw-mcpg+%22gh-aw-workflow-id%3A+test-coverage-improver%22&type=pullrequests) <!-- gh-aw-agentic-workflow: Test Coverage Improver, engine: copilot, model: auto, id: 37653372714, workflow_id: test-coverage-improver, run: https://github.com/github/gh-aw-mcpg/actions/runs/37653372714 --> <!-- gh-aw-workflow-id: test-coverage-improver --> <!-- gh-aw-workflow-call-id: github/gh-aw-mcpg/test-coverage-improver -->
2 parents 630d13c + fcdb2da commit 5e2509a

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎internal/cmd/tracing_helpers_test.go‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
58
"testing"
69

710
"github.com/spf13/cobra"
@@ -187,6 +190,38 @@ func TestShutdownTracingProviderWithTimeout(t *testing.T) {
187190
})
188191
assert.False(t, warnCalled, "SDK provider with no pending spans should shut down without error")
189192
})
193+
194+
t.Run("warns when shutdown times out flushing to a hanging collector", func(t *testing.T) {
195+
if testing.Short() {
196+
t.Skip("waits for the 5s shutdown timeout")
197+
}
198+
t.Cleanup(func() { otel.SetTracerProvider(noop.NewTracerProvider()) })
199+
200+
release := make(chan struct{})
201+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
202+
select {
203+
case <-release:
204+
case <-r.Context().Done():
205+
}
206+
}))
207+
t.Cleanup(srv.Close)
208+
t.Cleanup(func() { close(release) })
209+
210+
t.Setenv("GH_AW_OTLP_ENDPOINTS", "")
211+
provider, err := tracing.InitProvider(context.Background(), &config.TracingConfig{Endpoint: srv.URL})
212+
require.NoError(t, err)
213+
require.True(t, provider.IsEnabled())
214+
215+
_, span := provider.Tracer().Start(context.Background(), "pending-span")
216+
span.End()
217+
218+
var warnings []string
219+
shutdownTracingProviderWithTimeout(provider, func(format string, args ...any) {
220+
warnings = append(warnings, fmt.Sprintf(format, args...))
221+
})
222+
require.Len(t, warnings, 1, "timed-out flush should surface exactly one warning")
223+
assert.Contains(t, warnings[0], "tracing provider shutdown error")
224+
})
190225
}
191226

192227
func TestSetupCommandTracing(t *testing.T) {

0 commit comments

Comments
 (0)