Repository navigation
test(mcpx): pin inlineDaemon's cancel-during-poll paths (#323) #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "errors" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
|
|
||
|
|
@@ -48,6 +49,125 @@ func TestInlineDaemonAlreadyCancelledContext(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| // neverAnswers replaces inlineDaemon's Ping with one that never reports | ||
| // the daemon up, so inlineDaemon stays in its poll loop until the context | ||
| // or the deadline ends the wait. The empty-config daemon answers the | ||
| // first real Ping, which is why the #430 tests never entered the loop | ||
| // (#323). | ||
| func neverAnswers(t *testing.T) { | ||
| t.Helper() | ||
| orig := inlineDaemonPing | ||
| inlineDaemonPing = func(*Client, context.Context) bool { return false } | ||
| t.Cleanup(func() { inlineDaemonPing = orig }) | ||
| } | ||
|
|
||
| // The deadline-exit test below reuses errAfterContext from | ||
| // ensuredaemon_test.go: it reports context.Canceled from Err once its | ||
| // moment passes, but its Done channel never fires — a cancellation only | ||
| // a post-loop Err check can catch (the #328 shape, here for #323). | ||
|
|
||
| // inlineTempDirs lists the mcpx-inline-* directories currently in the | ||
| // temp dir, so a test can tell whether inlineDaemon removed the private | ||
| // directory of the daemon it started (its stop path) or leaked it. | ||
| func inlineTempDirs(t *testing.T) map[string]bool { | ||
| t.Helper() | ||
| matches, err := filepath.Glob(filepath.Join(os.TempDir(), "mcpx-inline-*")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| out := make(map[string]bool, len(matches)) | ||
| for _, m := range matches { | ||
| out[m] = true | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| // Regression for #323: cancelling while inlineDaemon is still polling for | ||
| // its in-process daemon to answer must return context.Canceled promptly | ||
| // and stop the daemon, not burn the whole inline start timeout (5s by | ||
| // default) and report "the in-process daemon did not answer". Before this | ||
| // test, replacing the poll's select on ctx.Done() with a plain sleep left | ||
| // the suite green — neither #430 test ever entered the poll wait. | ||
| func TestInlineDaemonCancelledWhileWaiting(t *testing.T) { | ||
| neverAnswers(t) | ||
| a := inlineDaemonApp(t) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This test inherits Useful? React with 👍 / 👎. |
||
| before := inlineTempDirs(t) | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| go func() { | ||
| time.Sleep(200 * time.Millisecond) | ||
| cancel() | ||
| }() | ||
| start := time.Now() | ||
| c, err := a.inlineDaemon(ctx) | ||
| if c != nil { | ||
| t.Fatalf("inlineDaemon(cancelled while waiting) returned a client %v, want none", c) | ||
| } | ||
| if !errors.Is(err, context.Canceled) { | ||
| t.Fatalf("inlineDaemon(cancelled while waiting) = %v, want context.Canceled", err) | ||
| } | ||
| if elapsed := time.Since(start); elapsed > 2*time.Second { | ||
| t.Fatalf("inlineDaemon took %v after cancellation, want a prompt return well under the 5s start timeout", elapsed) | ||
| } | ||
| for dir := range inlineTempDirs(t) { | ||
| if !before[dir] { | ||
| _ = os.RemoveAll(dir) | ||
| t.Fatalf("inlineDaemon left its in-process daemon's directory %s behind after cancellation", dir) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Regression for #323, the poll loop's other exit: a cancellation that | ||
| // only becomes visible as the loop leaves on its own deadline — its Done | ||
| // channel never fires, so the in-loop select cannot catch it — must still | ||
| // report context.Canceled via the post-loop check, not the generic | ||
| // "did not answer" timeout error. Removing that check leaves this test | ||
| // red while the mid-poll test above stays green. | ||
| func TestInlineDaemonCancelVisibleOnlyAtDeadline(t *testing.T) { | ||
| neverAnswers(t) | ||
| a := inlineDaemonApp(t) | ||
| if err := a.Settings().SetRuntime("daemon.inlineStartTimeout", "800ms"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| ctx := errAfterContext{Context: context.Background(), at: time.Now().Add(150 * time.Millisecond)} | ||
| c, err := a.inlineDaemon(ctx) | ||
| if c != nil { | ||
| t.Fatalf("inlineDaemon(cancel visible only at deadline) returned a client %v, want none", c) | ||
| } | ||
| if !errors.Is(err, context.Canceled) { | ||
| t.Fatalf("inlineDaemon(cancel visible only at deadline) = %v, want context.Canceled", err) | ||
| } | ||
| } | ||
|
|
||
| // The genuine timeout path, also untested before #323: with a live | ||
| // context and a daemon that never answers, inlineDaemon gives up at the | ||
| // inline start timeout with its "did not answer" error (and stops the | ||
| // daemon), rather than reporting a spurious cancellation. | ||
| func TestInlineDaemonNeverAnswersStillTimesOut(t *testing.T) { | ||
| neverAnswers(t) | ||
| a := inlineDaemonApp(t) | ||
| if err := a.Settings().SetRuntime("daemon.inlineStartTimeout", "800ms"); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| before := inlineTempDirs(t) | ||
| start := time.Now() | ||
| c, err := a.inlineDaemon(context.Background()) | ||
| if c != nil { | ||
| t.Fatalf("inlineDaemon(never answers) returned a client %v, want none", c) | ||
| } | ||
| if err == nil || !strings.Contains(err.Error(), "the in-process daemon did not answer") { | ||
| t.Fatalf("inlineDaemon(never answers) = %v, want the did-not-answer timeout error", err) | ||
| } | ||
| if elapsed := time.Since(start); elapsed < 700*time.Millisecond { | ||
| t.Fatalf("inlineDaemon gave up after %v, before the 800ms start timeout", elapsed) | ||
| } | ||
| for dir := range inlineTempDirs(t) { | ||
| if !before[dir] { | ||
| _ = os.RemoveAll(dir) | ||
| t.Fatalf("inlineDaemon left its in-process daemon's directory %s behind after the timeout", dir) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The uncancelled path is unchanged: the in-process daemon starts and the | ||
| // returned client can ping it. | ||
| func TestInlineDaemonStartsWithLiveContext(t *testing.T) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This scans the process-wide temporary directory rather than identifying the directory created by this invocation. If another
mcpxprocess or test binary starts an inline daemon between the before/after snapshots, the subsequent loops classify its directory as a leak, delete it withos.RemoveAll, and fail the test even though this daemon cleaned up correctly. Capture the tested daemon's exact directory or inject a test-specific temp parent instead of scanning and deleting every newmcpx-inline-*match.Useful? React with 👍 / 👎.