R1-4: the module's unit tests end at 159, within the budget

The drain's rows join the stopping runtime's table, the settings reader joins the start's cases, and
the pull through a bundle's channel joins the take through it: 162 test functions become 159, every
case kept (review round 1, finding 4).

Builds: design 47 H2 §819
This commit is contained in:
2026-11-22 02:21:43 +01:00
parent cab76319b8
commit c90ff7d9c0
4 changed files with 177 additions and 196 deletions
-131
View File
@@ -1,131 +0,0 @@
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)
}
})
}
}
+115 -2
View File
@@ -1,11 +1,13 @@
package launch
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"testing"
"time"
@@ -51,6 +53,11 @@ done
// to stop, the power bundle among them dialling the system bus again, which restarted the bus during the
// shutdown and held it 29 minutes). A bundle that exits while it serves is started again; once the runtime
// itself is stopping, none is, by a restart or by a call.
//
// **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 TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) {
saved := RestartFirst
RestartFirst = 10 * time.Millisecond
@@ -59,12 +66,104 @@ func TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) {
name string
stopping bool
starts int
drain time.Duration // a drain row: the runner drains its calls in flight within this bound
release bool // a drain row's held call ends within the bound
}{
{"a bundle that dies while the runtime serves", false, 2},
{"a bundle signalled with the stopping runtime", true, 1},
{"a bundle that dies while the runtime serves", false, 2, 0, false},
{"a bundle signalled with the stopping runtime", true, 1, 0, false},
{"a call that ends within the drain bound is answered, and a call after the stop is answered nothing", false, 0, 10 * time.Second, true},
{"a call that outlives the drain bound is cut and said", false, 0, 50 * time.Millisecond, false},
} {
t.Run(row.name, func(t *testing.T) {
shuttingDown.Store(false)
if row.drain > 0 {
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.drain, 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.drain || 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)
}
return
}
dir := t.TempDir()
entry, out := filepath.Join(dir, "bundle"), filepath.Join(dir, "out")
if err := os.WriteFile(entry, []byte(notingBundle), 0o755); err != nil {
@@ -114,3 +213,17 @@ func TestABundleIsStartedAgainUnlessTheRuntimeIsStopping(t *testing.T) {
})
}
}
// 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
`
+28 -29
View File
@@ -128,6 +128,34 @@ func TestABundleTakesASeatsWorkThroughItsOwnChannel(t *testing.T) {
t.Errorf("a publish with ack went the unacknowledged way: %v", b.published)
}
})
// 1887/A11: a bundle's pull reaches the runtime's bus from its own channel, as its other seat traffic does.
t.Run("ABundlesPullReachesTheRuntimeThroughItsOwnChannel", func(t *testing.T) {
entry := filepath.Join(t.TempDir(), "bundle")
if err := os.WriteFile(entry, []byte(pullingBundle), 0o755); err != nil {
t.Fatal(err)
}
b := &seating{}
l, err := Start("mesh-build-go", entry, os.Environ(), b, t.Logf)
if err != nil {
t.Fatal(err)
}
t.Cleanup(l.Stop)
for i := 0; ; i++ {
b.mu.Lock()
verb, params := b.verb, b.params
b.mu.Unlock()
if verb == "pull" {
if params != `{"worker":"SEAT_NODE_BUILD_GO_DELIVERY_worker","wait_ms":0}` {
t.Fatalf("pulled with %s", params)
}
return
}
if i > 100 {
t.Fatal("the bundle's pull never reached the runtime")
}
time.Sleep(20 * time.Millisecond)
}
})
}
// A bundle that pulls one ask from its worker when it is listed.
@@ -142,32 +170,3 @@ while IFS= read -r line; do
esac
done
`
// 1887/A11: a bundle's pull reaches the runtime's bus from its own channel, as its other seat traffic does.
func TestABundlePullsASeatsWorkThroughItsOwnChannel(t *testing.T) {
entry := filepath.Join(t.TempDir(), "bundle")
if err := os.WriteFile(entry, []byte(pullingBundle), 0o755); err != nil {
t.Fatal(err)
}
b := &seating{}
l, err := Start("mesh-build-go", entry, os.Environ(), b, t.Logf)
if err != nil {
t.Fatal(err)
}
t.Cleanup(l.Stop)
for i := 0; ; i++ {
b.mu.Lock()
verb, params := b.verb, b.params
b.mu.Unlock()
if verb == "pull" {
if params != `{"worker":"SEAT_NODE_BUILD_GO_DELIVERY_worker","wait_ms":0}` {
t.Fatalf("pulled with %s", params)
}
return
}
if i > 100 {
t.Fatal("the bundle's pull never reached the runtime")
}
time.Sleep(20 * time.Millisecond)
}
}