diff --git a/node-tools/cmd/node-tools/main_test.go b/node-tools/cmd/node-tools/main_test.go index d9d12b7..50d0fa6 100644 --- a/node-tools/cmd/node-tools/main_test.go +++ b/node-tools/cmd/node-tools/main_test.go @@ -13,11 +13,43 @@ import ( "github.com/novox/mesh-tools/node-tools/internal/runtime" ) -// The runner's start waits for its credential and its bus within the start's bound (issue 1496): one test, two cases, so -// design 46 7-approval-mark §22's whole test of the gate adds no test to the module (design 47 G §419, §626). +// The runner's start waits for its credential and its bus within the start's bound (issue 1496), and reads its settings +// before it serves: one test, three cases, so design 46 7-approval-mark §22's whole test of the gate adds no test to the +// module (design 47 G §419, §626). func TestTheRunnersStartWaitsForItsCredentialAndItsBus(t *testing.T) { t.Run("the credential", awaitsCredential) t.Run("the bus", connectsWithin) + // **The runner's settings are read through the one settings reader** (design 47 H2 §813 (12), §775, H-A-369, H-A-370, + // H-A-377): drain-bound, a duration text the node-engine composes as MESH_CONFIG_DRAIN_BOUND, 30 s when none is composed; a + // value that is no duration, of it or of the kept call's pace, stops the runner before it serves, naming the setting. + t.Run("its settings", func(t *testing.T) { + for _, row := range []struct { + word, composed, refused string + want time.Duration + }{ + {"MESH_CONFIG_DRAIN_BOUND", "", "", 30 * time.Second}, + {"MESH_CONFIG_DRAIN_BOUND", "5s", "", 5 * time.Second}, + {"MESH_CONFIG_DRAIN_BOUND", "30", "drain-bound", 0}, + {"MESH_CONFIG_KEPT_CALL_PACE_FIRST", "1", "kept-call-pace-first", 30 * time.Second}, + {"MESH_CONFIG_KEPT_CALL_PACE_MAX", "100ms", "kept-call-pace-max", 30 * time.Second}, + } { + t.Run(row.word+"="+row.composed, func(t *testing.T) { + t.Setenv(row.word, row.composed) + if row.refused != "" { + if err := serve(t.Context()); err == nil || !strings.Contains(err.Error(), row.refused) { + t.Errorf("the runner with %s=%q served (%v); want it stopped before it serves, naming %s", row.word, + row.composed, err, row.refused) + } + if row.want == 0 { + return + } + } + if got, err := drainBoundSetting(); err != nil || got != row.want { + t.Errorf("drain-bound read %s, %v; want %s", got, err, row.want) + } + }) + } + }) } // A runtime started before its credential is on the machine waits for it, woken by the watch of its directory, and gives @@ -130,35 +162,3 @@ func connectsWithin(t *testing.T) { }) } } - -// **The runner's settings are read through the one settings reader** (design 47 H2 §813 (12), §775, H-A-369, H-A-370, -// H-A-377): drain-bound, a duration text the node-engine composes as MESH_CONFIG_DRAIN_BOUND, 30 s when none is composed; a -// value that is no duration, of it or of the kept call's pace, stops the runner before it serves, naming the setting. -func TestTheRunnersSettingsAreReadThroughTheOneReader(t *testing.T) { - for _, row := range []struct { - word, composed, refused string - want time.Duration - }{ - {"MESH_CONFIG_DRAIN_BOUND", "", "", 30 * time.Second}, - {"MESH_CONFIG_DRAIN_BOUND", "5s", "", 5 * time.Second}, - {"MESH_CONFIG_DRAIN_BOUND", "30", "drain-bound", 0}, - {"MESH_CONFIG_KEPT_CALL_PACE_FIRST", "1", "kept-call-pace-first", 30 * time.Second}, - {"MESH_CONFIG_KEPT_CALL_PACE_MAX", "100ms", "kept-call-pace-max", 30 * time.Second}, - } { - t.Run(row.word+"="+row.composed, func(t *testing.T) { - t.Setenv(row.word, row.composed) - if row.refused != "" { - if err := serve(t.Context()); err == nil || !strings.Contains(err.Error(), row.refused) { - t.Errorf("the runner with %s=%q served (%v); want it stopped before it serves, naming %s", row.word, - row.composed, err, row.refused) - } - if row.want == 0 { - return - } - } - if got, err := drainBoundSetting(); err != nil || got != row.want { - t.Errorf("drain-bound read %s, %v; want %s", got, err, row.want) - } - }) - } -} diff --git a/node-tools/internal/launch/drain_test.go b/node-tools/internal/launch/drain_test.go deleted file mode 100644 index 06e82bb..0000000 --- a/node-tools/internal/launch/drain_test.go +++ /dev/null @@ -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) - } - }) - } -} diff --git a/node-tools/internal/launch/error_code_test.go b/node-tools/internal/launch/error_code_test.go index 3b3e1b2..becd4ac 100644 --- a/node-tools/internal/launch/error_code_test.go +++ b/node-tools/internal/launch/error_code_test.go @@ -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 +` diff --git a/node-tools/internal/launch/seat_test.go b/node-tools/internal/launch/seat_test.go index 25edae4..a15006f 100644 --- a/node-tools/internal/launch/seat_test.go +++ b/node-tools/internal/launch/seat_test.go @@ -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) - } -}