S2 ask-once-go: Ask calls once; the kind stopping goes
Builds: design 47 H2 H-A-570 (2) (3) (4), H-A-624 (3)
This commit is contained in:
+20
-26
@@ -2,6 +2,7 @@ package stdio
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
@@ -9,8 +10,6 @@ import (
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.novox.be/novox/mesh-sdk/go/busretry"
|
||||
)
|
||||
|
||||
// A Go bundle serves as the module the runtime names, answers calls concurrently, and emits through
|
||||
@@ -416,27 +415,21 @@ func subABundleAsksAnAcknowledgedPublishAndASubscriptionForNewMessages(t *testin
|
||||
_ = inW.Close()
|
||||
}
|
||||
|
||||
// A call a stopping tool runner did not take reaches the bundle as the typed fault stopping and is asked again after a pause,
|
||||
// at the kept-call pace, until the new runner answers; past Ask's bound the fault is the caller's as such, ErrStopping; a
|
||||
// plain refusal, and a bare timeout, are never asked again (design 47 H2 H-A-542). Nothing sleeps: the pause moves a clock.
|
||||
func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T) {
|
||||
// Ask makes its call once and asks nothing again itself (design 47 H2 H-A-570 (2)): a call met by no responders returns that fault
|
||||
// after one ask, a reply of the withdrawn kind stopping is the runtime's refusal like any other, and at the bound (the setting
|
||||
// ask-bound) the fault is "no answer within <bound>", a call's own deadline.
|
||||
func TestAskCallsOnceAndFaultsAtItsBound(t *testing.T) {
|
||||
servesAlone(t)
|
||||
t.Setenv(ServedAs, "builder")
|
||||
clock := time.Unix(0, 0)
|
||||
var paused []time.Duration
|
||||
wasPause, wasNow := pause, askNow
|
||||
t.Cleanup(func() { pause, askNow = wasPause, wasNow })
|
||||
pause = func(d time.Duration) { paused = append(paused, d); clock = clock.Add(d) }
|
||||
askNow = func() time.Time { return clock }
|
||||
// The bound is the setting ask-bound (H-A-549), here 10 s for its default 60 s.
|
||||
t.Setenv("MESH_CONFIG_ASK_BOUND", "10s")
|
||||
// The bound is the setting ask-bound (H-A-570 (4)), here 50 ms for its default 60 s.
|
||||
t.Setenv("MESH_CONFIG_ASK_BOUND", "50ms")
|
||||
inR, inW := io.Pipe()
|
||||
outR, outW := io.Pipe()
|
||||
go func() {
|
||||
_ = ServeOn("", []Tool{{Name: "publish", Run: func(map[string]any) (any, error) {
|
||||
answer, err := Ask("seat:go-package-registry.publish", map[string]any{})
|
||||
if err != nil {
|
||||
return map[string]any{"stopping": errors.Is(err, ErrStopping), "words": err.Error()}, nil
|
||||
return map[string]any{"deadline": errors.Is(err, context.DeadlineExceeded), "words": err.Error()}, nil
|
||||
}
|
||||
return json.RawMessage(answer), nil
|
||||
}}}, inR, outW)
|
||||
@@ -462,24 +455,28 @@ func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T)
|
||||
send := func(m map[string]any) { b, _ := json.Marshal(m); _, _ = inW.Write(append(b, '\n')) }
|
||||
stopping := map[string]any{"code": -32000, "message": "registry's publish was not taken: the tool runner is stopping",
|
||||
"data": map[string]any{"error-kind": "stopping"}}
|
||||
noResponders := map[string]any{"code": -32000, "message": "nats: no responders available for request"}
|
||||
for i, row := range []struct {
|
||||
name string
|
||||
answers []map[string]any // the runtime's answers to the asks, in order: an error, or a result
|
||||
asks int
|
||||
want string
|
||||
}{
|
||||
{"answered by the new runner after two stoppings", []map[string]any{{"error": stopping}, {"error": stopping}, {"result": "published"}}, 3,
|
||||
`"published"`},
|
||||
{"stopping past the bound", []map[string]any{{"error": stopping}, {"error": stopping}, {"error": stopping}}, 3,
|
||||
`"stopping":true`},
|
||||
{"a plain refusal", []map[string]any{{"error": map[string]any{"code": -32000, "message": "refused"}}}, 1, `"stopping":false`},
|
||||
{"a bare timeout", []map[string]any{{"error": map[string]any{"code": -32000, "message": "timeout"}}}, 1, `"words":"timeout"`},
|
||||
{"answered", []map[string]any{{"result": "published"}}, 1, `"published"`},
|
||||
{"no responders is returned after one ask", []map[string]any{{"error": noResponders}}, 1, `"words":"nats: no responders available for request"`},
|
||||
{"a reply of the withdrawn kind stopping is a refusal", []map[string]any{{"error": stopping}}, 1, `"deadline":false`},
|
||||
{"no answer within the bound", nil, 1, `"words":"no answer within 50ms: context deadline exceeded"`},
|
||||
} {
|
||||
t.Run(row.name, func(t *testing.T) {
|
||||
clock, paused = time.Unix(0, 0), nil
|
||||
t.Cleanup(busretry.Waits(time.Second, 2*time.Second, 4*time.Second))
|
||||
send(map[string]any{"jsonrpc": "2.0", "id": 100 + i, "method": "tools/call", "params": map[string]any{"name": "publish"}})
|
||||
asks := 0
|
||||
if row.answers == nil {
|
||||
m := next()
|
||||
asks++
|
||||
if m["method"] != "mesh/ask" {
|
||||
t.Fatalf("the bundle said %v, want an ask", m)
|
||||
}
|
||||
}
|
||||
for _, a := range row.answers {
|
||||
m := next()
|
||||
if m["method"] != "mesh/ask" {
|
||||
@@ -494,9 +491,6 @@ func TestAStoppingRunnersCallIsAskedAgainUntilTheNewRunnerAnswers(t *testing.T)
|
||||
if r["id"] != float64(100+i) || asks != row.asks || !strings.Contains(string(text), strings.ReplaceAll(row.want, `"`, `\"`)) {
|
||||
t.Errorf("asked %d times, answered %v; want %d asks and %s", asks, r, row.asks, row.want)
|
||||
}
|
||||
if want := row.asks - 1; len(paused) != want || (want >= 2 && paused[1] != 2*paused[0]) {
|
||||
t.Errorf("paused %v, want %d pauses at the kept-call pace", paused, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
_ = inW.Close()
|
||||
|
||||
Reference in New Issue
Block a user