Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 75 additions & 56 deletions pkgs/mcpx/internal/elicit/elicit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"path/filepath"
"strings"
"testing"
"testing/synctest"
"time"

"github.com/developing-today/code/pkgs/mcpx/internal/elicit"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
})
}
Loading