From df32d508fbca03e074861dcd248d3b573f285672 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 07:56:05 -0600 Subject: [PATCH] test(mcpx): run elicit deadline tests in a testing/synctest bubble (#2639) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pilot testing/synctest on the elicit component, the in-process piece whose tests touch no sockets and no subprocesses (the bubble rule). - TestAnAnswerCanComeFromAnotherProcess: synctest.Wait replaces the 50ms sleep-as-scheduler-hint; the waiter's 10s Await deadline and the 5s fallback timer are now virtual time, so an unwoken waiter fails in microseconds instead of after ten wall-clock seconds. - TestTouchNeverPassesTheMaxAgeCeiling and TestAnAbandonedQuestionStillExpires: their 600ms/450ms sleeps stay, but are virtual time — advancing the fake clock is exactly what those tests assert (ceiling clamp, ttl expiry), and they no longer cost wall time. No production change: test-only edit, no clock seam needed. --- pkgs/mcpx/internal/elicit/elicit_test.go | 131 +++++++++++++---------- 1 file changed, 75 insertions(+), 56 deletions(-) diff --git a/pkgs/mcpx/internal/elicit/elicit_test.go b/pkgs/mcpx/internal/elicit/elicit_test.go index 31dea4e9dd..e564045656 100644 --- a/pkgs/mcpx/internal/elicit/elicit_test.go +++ b/pkgs/mcpx/internal/elicit/elicit_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "strings" "testing" + "testing/synctest" "time" "github.com/developing-today/code/pkgs/mcpx/internal/elicit" @@ -36,36 +37,45 @@ func ask(t *testing.T, b *elicit.Broker, r elicit.Request) elicit.Request { func TestAnAnswerCanComeFromAnotherProcess(t *testing.T) { // The whole point: the asker and the answerer need not be the same // process, so CI can raise a question a person answers later. - b := open(t) - r := ask(t, b, elicit.Request{Session: "s1"}) + // + // testing/synctest pilot (#2639): the waiter's Await deadline and the + // fallback timer are virtual time, so a waiter that is never woken + // fails in microseconds rather than after ten wall-clock seconds, and + // synctest.Wait replaces the old sleep-as-scheduler-hint: it returns + // exactly when the waiter goroutine is blocked in Await, whatever the + // machine's speed. + synctest.Test(t, func(t *testing.T) { + b := open(t) + r := ask(t, b, elicit.Request{Session: "s1"}) - done := make(chan elicit.Answer, 1) - go func() { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - a, err := b.Await(ctx, r.ID) - if err != nil { - t.Error(err) - } - done <- a - }() + done := make(chan elicit.Answer, 1) + go func() { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + a, err := b.Await(ctx, r.ID) + if err != nil { + t.Error(err) + } + done <- a + }() - time.Sleep(50 * time.Millisecond) - if err := b.Respond(elicit.Answer{ - ID: r.ID, Action: elicit.Accept, - Content: json.RawMessage(`{"repo":"me/thing"}`), By: "cli", - }); err != nil { - t.Fatal(err) - } + synctest.Wait() + if err := b.Respond(elicit.Answer{ + ID: r.ID, Action: elicit.Accept, + Content: json.RawMessage(`{"repo":"me/thing"}`), By: "cli", + }); err != nil { + t.Fatal(err) + } - select { - case a := <-done: - if a.Action != elicit.Accept || !strings.Contains(string(a.Content), "me/thing") { - t.Errorf("got %+v", a) + select { + case a := <-done: + if a.Action != elicit.Accept || !strings.Contains(string(a.Content), "me/thing") { + t.Errorf("got %+v", a) + } + case <-time.After(5 * time.Second): + t.Fatal("the waiter was never woken") } - case <-time.After(5 * time.Second): - t.Fatal("the waiter was never woken") - } + }) } func TestAnExpiredQuestionBecomesCancelNotDecline(t *testing.T) { @@ -253,25 +263,30 @@ func TestItemReadRenewsButCollectionReadDoesNot(t *testing.T) { func TestTouchNeverPassesTheMaxAgeCeiling(t *testing.T) { // Renewal is bounded: however often a question is read, it dies at // created + maxAge. - b := open(t) - b.Configure(2*time.Second, 2500*time.Millisecond) - created := time.Now().Add(-time.Second) - r := ask(t, b, elicit.Request{Created: created}) - ceiling := created.Add(2500 * time.Millisecond) - if !r.ExpiresAt.Truncate(time.Millisecond).Equal(created.Add(2 * time.Second).Truncate(time.Millisecond)) { - t.Fatalf("initial deadline = %v, want created+ttl (below ceiling)", r.ExpiresAt) - } - time.Sleep(600 * time.Millisecond) - got, ok, err := b.Get(r.ID) - if err != nil || !ok { - t.Fatalf("get: ok=%v err=%v", ok, err) - } - if got.ExpiresAt.After(ceiling) { - t.Errorf("touch pushed the deadline past the ceiling: %v > %v", got.ExpiresAt, ceiling) - } - if got.ExpiresAt.Before(ceiling.Add(-100 * time.Millisecond)) { - t.Errorf("deadline = %v, want ~ceiling %v", got.ExpiresAt, ceiling) - } + // synctest pilot (#2639): the 600ms below is virtual time — it moves + // the fake clock, which is the subject of the assertion (a read 600ms + // after creation must clamp at the ceiling), and costs no wall time. + synctest.Test(t, func(t *testing.T) { + b := open(t) + b.Configure(2*time.Second, 2500*time.Millisecond) + created := time.Now().Add(-time.Second) + r := ask(t, b, elicit.Request{Created: created}) + ceiling := created.Add(2500 * time.Millisecond) + if !r.ExpiresAt.Truncate(time.Millisecond).Equal(created.Add(2 * time.Second).Truncate(time.Millisecond)) { + t.Fatalf("initial deadline = %v, want created+ttl (below ceiling)", r.ExpiresAt) + } + time.Sleep(600 * time.Millisecond) + got, ok, err := b.Get(r.ID) + if err != nil || !ok { + t.Fatalf("get: ok=%v err=%v", ok, err) + } + if got.ExpiresAt.After(ceiling) { + t.Errorf("touch pushed the deadline past the ceiling: %v > %v", got.ExpiresAt, ceiling) + } + if got.ExpiresAt.Before(ceiling.Add(-100 * time.Millisecond)) { + t.Errorf("deadline = %v, want ~ceiling %v", got.ExpiresAt, ceiling) + } + }) } func TestTouchNeverShortensAnExplicitExpiry(t *testing.T) { @@ -294,15 +309,19 @@ func TestTouchNeverShortensAnExplicitExpiry(t *testing.T) { func TestAnAbandonedQuestionStillExpires(t *testing.T) { // Touch-on-read changes nothing for a question nobody reads: it // expires on the idle schedule, exactly as before. - b := open(t) - b.Configure(300*time.Millisecond, time.Hour) - r := ask(t, b, elicit.Request{}) - time.Sleep(450 * time.Millisecond) - pending, err := b.Pending(elicit.Filter{}) - if err != nil { - t.Fatal(err) - } - if len(pending) != 0 { - t.Errorf("abandoned question %s still pending after its ttl", r.ID) - } + // synctest pilot (#2639): the 450ms below is virtual time, deliberately + // moving the fake clock past the 300ms ttl; the test costs no wall time. + synctest.Test(t, func(t *testing.T) { + b := open(t) + b.Configure(300*time.Millisecond, time.Hour) + r := ask(t, b, elicit.Request{}) + time.Sleep(450 * time.Millisecond) + pending, err := b.Pending(elicit.Filter{}) + if err != nil { + t.Fatal(err) + } + if len(pending) != 0 { + t.Errorf("abandoned question %s still pending after its ttl", r.ID) + } + }) }