From 9ecdeaed0fc0c08f16492c305581dc414d6bd874 Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Mon, 19 Oct 2026 01:04:56 -0500 Subject: [PATCH] test(mcpx): make the orphaned-session test name its own failure mode (#290) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TestTerminateOrphanedSessionEndsTheUpstreamSession flaked in the Nix build with '0 DELETE(s)', and the test discarded everything that could say why: the mock counted only DELETEs for live sessions (a DELETE for a forgotten session is answered 404 and counted as nothing) and the test never looked at the upstream.session.orphaned events that carry TerminateOrphanedSession's failure reason. - sessionUpstream now records every DELETE attempt and its session id, like the daemon package's orphanUpstream already does. - The test captures terminated and orphaned events together (new lifecycleCapture helper; the existing helper's hook holds one kind) and its failure message reports attempts, session ids and the orphaned reasons. The three failure modes now read differently: no DELETE arrived (attempts=0), the DELETE named a session the upstream had forgotten (attempts=1, terminated still fires — 404 is success by design), or the DELETE errored (orphaned event with the reason). Verified with a sabotage run (upstream forgets the session before termination). Test-only change; full internal/pool suite green, -count=200 and -race runs of the target test green locally (the flake itself is Nix-sandbox only and does not reproduce in a dev environment). --- .../internal/pool/upstream_session_test.go | 53 ++++++++++++++++--- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/pkgs/mcpx/internal/pool/upstream_session_test.go b/pkgs/mcpx/internal/pool/upstream_session_test.go index 9198307bf1..7c8262797b 100644 --- a/pkgs/mcpx/internal/pool/upstream_session_test.go +++ b/pkgs/mcpx/internal/pool/upstream_session_test.go @@ -22,7 +22,12 @@ type sessionUpstream struct { live map[string]bool issued int deletes int - served []string + // deleteAttempts and deleted record every DELETE that arrives, including + // ones answered 404, so a failure can tell "no DELETE arrived" apart from + // "a DELETE arrived for a session the upstream no longer knew" (#290). + deleteAttempts int + deleted []string + served []string } func newSessionUpstream(t *testing.T) *sessionUpstream { @@ -45,6 +50,8 @@ func (u *sessionUpstream) serve(w http.ResponseWriter, r *http.Request) { sid := r.Header.Get("Mcp-Session-Id") switch r.Method { case http.MethodDelete: + u.deleteAttempts++ + u.deleted = append(u.deleted, sid) if u.live[sid] { delete(u.live, sid) u.deletes++ @@ -165,10 +172,38 @@ func TestAnUpstreamSessionSurvivesATakeover(t *testing.T) { } } +// lifecycleCapture records pool lifecycle events of several kinds at once. +// lifecycleEvents installs a single-kind hook, and TerminateOrphanedSession's +// failure path (#290) is only visible in the kind the test is not asserting +// on, so this test needs both the terminated and the orphaned events. +func lifecycleCapture(t *testing.T, kinds ...string) func(string) []map[string]any { + t.Helper() + want := make(map[string]bool, len(kinds)) + for _, k := range kinds { + want[k] = true + } + var mu sync.Mutex + got := map[string][]map[string]any{} + prev := pool.Lifecycle + pool.Lifecycle = func(event string, attrs map[string]any) { + if want[event] { + mu.Lock() + got[event] = append(got[event], attrs) + mu.Unlock() + } + } + t.Cleanup(func() { pool.Lifecycle = prev }) + return func(kind string) []map[string]any { + mu.Lock() + defer mu.Unlock() + return append([]map[string]any(nil), got[kind]...) + } +} + // A handed session nobody adopted is ended upstream by // TerminateOrphanedSession, once the predecessor has committed. func TestTerminateOrphanedSessionEndsTheUpstreamSession(t *testing.T) { - terminated := lifecycleEvents(t, "upstream.session.terminated") + events := lifecycleCapture(t, "upstream.session.terminated", "upstream.session.orphaned") up := newSessionUpstream(t) old := upstreamPool(t, up.URL) pingThrough(t, old, "k") @@ -182,11 +217,17 @@ func TestTerminateOrphanedSessionEndsTheUpstreamSession(t *testing.T) { up.mu.Lock() defer up.mu.Unlock() if up.deletes != 1 { - t.Fatalf("the orphaned session saw %d DELETE(s), want exactly 1", up.deletes) + // #290: say why termination did not land — the orphaned events carry + // the reason TerminateOrphanedSession reported, and the mock's + // attempt log separates "no DELETE arrived" from "the DELETE named + // a session the upstream no longer knew". + t.Fatalf("the orphaned session saw %d DELETE(s), want exactly 1: %d DELETE attempt(s) for sessions %v; orphaned events = %v", + up.deletes, up.deleteAttempts, up.deleted, events("upstream.session.orphaned")) } - events := terminated() - if len(events) != 1 || events[0]["server"] != "remote" || events[0]["session"] != "sess-1" { - t.Fatalf("terminated events = %v, want one naming remote/sess-1", events) + terminated := events("upstream.session.terminated") + if len(terminated) != 1 || terminated[0]["server"] != "remote" || terminated[0]["session"] != "sess-1" { + t.Fatalf("terminated events = %v, want one naming remote/sess-1; orphaned events = %v", + terminated, events("upstream.session.orphaned")) } }