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:
@@ -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
|
||||
`
|
||||
|
||||
Reference in New Issue
Block a user