Files
mesh-tools/node-tools/internal/launch/drain_test.go
T
jschoubben 858beb2b63 T2 amendment: a call after the stop began is answered nothing, never refused
A refusal would end the caller's kept call; left unanswered, the kept call paces and meets the new
runner. The ask made once the drain began ends Unanswered, so the bus sends its caller nothing.

Builds: design 47 H2 §813 (12), H-A-369
2026-11-21 23:57:07 +01:00

132 lines
4.5 KiB
Go

package launch
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
)
// holdingBundle serves one tool, hold, which answers only once a line can be read from the fifo $HOLD: a call that lasts as long
// as the test keeps it.
const holdingBundle = `#!/bin/sh
while IFS= read -r line; do
id=$(printf '%s' "$line" | sed -n 's/.*"id":\([0-9]*\)[,}].*/\1/p')
case "$line" in
*'"method":"initialize"'*) printf '{"jsonrpc":"2.0","id":%s,"result":{}}\n' "$id" ;;
*'"method":"tools/list"'*) printf '{"jsonrpc":"2.0","id":%s,"result":{"tools":[{"name":"hold","inputSchema":{}}]}}\n' "$id" ;;
*'"method":"tools/call"'*) read -r go < "$HOLD"
printf '{"jsonrpc":"2.0","id":%s,"result":{"content":[{"type":"text","text":"{\\"held\\":true}"}]}}\n' "$id" ;;
esac
done
`
// **The tool runner drains at a stop** (design 47 H2 §813 (12), H-A-342 (2), H-A-353, H-A-369): once it drains, no new call is
// taken and its caller is answered nothing (Unanswered), never refused; a call
// in flight that ends within the drain bound is answered; one still in flight at the bound is cut, said with its bundle, its verb
// and its age, and left unanswered (Unanswered, so the bus sends its caller nothing: a transport fault).
func TestTheToolRunnerDrainsItsCallsInFlightAtAStop(t *testing.T) {
for _, row := range []struct {
name string
bound time.Duration
release bool
}{
{"a call that ends within the bound is answered, and a call after the stop is answered nothing", 10 * time.Second, true},
{"a call that outlives the bound is cut and said", 50 * time.Millisecond, false},
} {
t.Run(row.name, func(t *testing.T) {
t.Cleanup(undrain)
dir := t.TempDir()
entry, hold := filepath.Join(dir, "bundle"), filepath.Join(dir, "hold")
if err := os.WriteFile(entry, []byte(holdingBundle), 0o755); err != nil {
t.Fatal(err)
}
if err := syscall.Mkfifo(hold, 0o600); err != nil {
t.Fatal(err)
}
l, err := Start("holding", entry, append(os.Environ(), "HOLD="+hold), &subscribing{}, t.Logf)
if err != nil {
t.Fatal(err)
}
t.Cleanup(l.Stop)
run := l.Registrations[0].Tools[0].Run
type answer struct {
raw json.RawMessage
err error
}
first := make(chan answer, 1)
go func() { raw, err := run("", json.RawMessage(`{}`)); first <- answer{raw, err} }()
// The call is in flight once the runner counted it.
for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(time.Millisecond) {
asks.mu.Lock()
n := len(asks.of)
asks.mu.Unlock()
if n == 1 {
break
}
if time.Now().After(deadline) {
t.Fatal("the call was never in flight")
}
}
var mu sync.Mutex
var said []string
drained := make(chan struct{})
began := time.Now()
go func() {
Drain(row.bound, func(f string, a ...any) { mu.Lock(); said = append(said, fmt.Sprintf(f, a...)); mu.Unlock() })
close(drained)
}()
for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(time.Millisecond) {
asks.mu.Lock()
d := asks.draining
asks.mu.Unlock()
if d {
break
}
if time.Now().After(deadline) {
t.Fatal("the drain never began")
}
}
if row.release {
var unanswered interface{ Unanswered() bool }
if _, err := run("", json.RawMessage(`{}`)); !errors.As(err, &unanswered) || !unanswered.Unanswered() {
t.Errorf("a call made after the stop ended %v, want it answered nothing, never refused", err)
}
f, err := os.OpenFile(hold, os.O_WRONLY, 0)
if err != nil {
t.Fatal(err)
}
_, _ = f.WriteString("go\n")
_ = f.Close()
}
got := <-first
<-drained
mu.Lock()
defer mu.Unlock()
if row.release {
if got.err != nil || !strings.Contains(string(got.raw), "held") {
t.Errorf("the call in flight was answered %s, %v; want it finished", got.raw, got.err)
}
if time.Since(began) >= row.bound || len(said) != 0 {
t.Errorf("the drain took %s and said %q; want it ended at the call's end, saying nothing", time.Since(began), said)
}
return
}
var unanswered interface{ Unanswered() bool }
if !errors.As(got.err, &unanswered) || !unanswered.Unanswered() {
t.Errorf("the call past the bound ended %v, want it cut and unanswered", got.err)
}
if len(said) != 1 || !strings.Contains(said[0], "holding") || !strings.Contains(said[0], "hold,") ||
!strings.Contains(said[0], "old") {
t.Errorf("the cut was said %q, want one line naming the bundle, the verb and the call's age", said)
}
})
}
}