carlos / platform Public

moveprobe: watch the worker's grace on an 8 s build, tests in parallel

TestMoveprobeWorkerOutlivesMain waited out the worker's real 60 s grace.
main.go gains workerGraceOverride, a string set only at link time
(-ldflags -X), which orphanGrace parses in place of workerGrace; a build
that does not set it, which is every build but this test's, keeps the
minute, and the test checks that of its own plain build first. The test
links its own moveprobe with an 8 s grace and holds it to the same
window as before (grace -1 s to +10 s).

Every test has its own fixture and shares only the read-only binPath, so
all of them now run in parallel. workerPIDs matches any moveprobe* build
the tests made, still only for the test's own database.

Package: 93 s to 11 s locally; the floor is the store-cache test's two
real 5 s windows.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 80755cb124e3548e1559e89a32147521e9b15977 parent 3c47ada
2 files changed, +60 −13
  • cmd/moveprobe/main.go +17 −2
  • cmd/moveprobe/main_test.go +43 −11
diff --git a/cmd/moveprobe/main.go b/cmd/moveprobe/main.go
index 9d07ecea8..fd005ee26 100644
--- a/cmd/moveprobe/main.go
+++ b/cmd/moveprobe/main.go
@@ -86,6 +86,20 @@ const (
modeAfterWrite = "after-write"
)
+// workerGraceOverride, set at link time (-ldflags "-X
+// main.workerGraceOverride=8s"), replaces workerGrace. Only the tests set
+// it, to watch the worker leave after seconds rather than a minute; a build
+// that does not set it keeps workerGrace.
+var workerGraceOverride string
+
+// orphanGrace is workerGrace, or the link-time override when one is set.
+func orphanGrace() time.Duration {
+ if d, err := time.ParseDuration(workerGraceOverride); err == nil && d > 0 {
+ return d
+ }
+ return workerGrace
+}
+
func main() {
if len(os.Args) > 1 && os.Args[1] == "worker" {
os.Exit(runWorker(os.Args[2:]))
@@ -708,7 +722,7 @@ func (a *app) checkWorker() error {
}
// runWorker writes a worker row every second. Once its parent has gone it
-// keeps writing for workerGrace and then exits.
+// keeps writing for workerGrace (orphanGrace) and then exits.
func runWorker(args []string) int {
fl := flag.NewFlagSet("moveprobe worker", flag.ContinueOnError)
dbPath := fl.String("db", "", "the instance database")
@@ -737,6 +751,7 @@ func runWorker(args []string) int {
return 1
}
defer db.Close()
+ grace := orphanGrace()
var orphaned time.Time
t := time.NewTicker(workerEvery)
defer t.Stop()
@@ -744,7 +759,7 @@ func runWorker(args []string) int {
if orphaned.IsZero() && os.Getppid() != *parent {
orphaned = time.Now()
}
- if !orphaned.IsZero() && time.Since(orphaned) >= workerGrace {
+ if !orphaned.IsZero() && time.Since(orphaned) >= grace {
return 0
}
report(insertSeq(db, "worker", here))
diff --git a/cmd/moveprobe/main_test.go b/cmd/moveprobe/main_test.go
index 693ecda3c..eabb11d2f 100644
--- a/cmd/moveprobe/main_test.go
+++ b/cmd/moveprobe/main_test.go
@@ -93,6 +93,8 @@ func shortTempDir(t *testing.T) string {
// fixture is one instance's world: its data directory (the flat layout,
// <dir>/h1.db and <dir>/h1/), a test S3 holding its store, and a control
// socket served by the platform's own schedule verbs over a registry.
+// Each test makes its own, and shares nothing else but the read-only
+// binPath, so every test runs in parallel.
type fixture struct {
t *testing.T
sockDir string
@@ -252,10 +254,16 @@ type probe struct {
// exec.Command(bin, "--socket", sock, "--db", dbPath) with the composed env.
// Its stdout and stderr are pipes, as the backend's tee makes them.
func (f *fixture) start(env map[string]string) *probe {
+ f.t.Helper()
+ return f.startBin(binPath, env)
+}
+
+// startBin is start with another build of moveprobe.
+func (f *fixture) startBin(bin string, env map[string]string) *probe {
f.t.Helper()
f.starts++
p := &probe{t: f.t, sock: filepath.Join(f.sockDir, fmt.Sprintf("a%d.sock", f.starts)), out: &lockedBuffer{}, done: make(chan struct{})}
- p.cmd = exec.Command(binPath, "--socket", p.sock, "--db", f.db)
+ p.cmd = exec.Command(bin, "--socket", p.sock, "--db", f.db)
p.cmd.Env = childEnv(env)
p.cmd.Stdout = p.out
p.cmd.Stderr = p.out
@@ -405,8 +413,9 @@ func (p *probe) report() reportDoc {
return r
}
-// workerPIDs lists the pids whose command line starts with the test binary
-// and "worker" and names db.
+// workerPIDs lists the pids whose command line starts with a moveprobe
+// build the tests made (binPath, or a test's own, all named moveprobe*) and
+// "worker", and names db.
func workerPIDs(db string) []int {
var pids []int
ents, _ := os.ReadDir("/proc")
@@ -420,7 +429,7 @@ func workerPIDs(db string) []int {
continue
}
args := strings.Split(strings.TrimRight(string(raw), "\x00"), "\x00")
- if len(args) < 2 || args[0] != binPath || args[1] != "worker" || !strings.Contains(string(raw), db) {
+ if len(args) < 2 || !strings.HasPrefix(filepath.Base(args[0]), "moveprobe") || args[1] != "worker" || !strings.Contains(string(raw), db) {
continue
}
if alive(pid) {
@@ -540,6 +549,7 @@ func (p *probe) fire(tm registry.Timer, token string) (int, string) {
// ---- the launch contract --------------------------------------------------
func TestMoveprobeLaunchContract(t *testing.T) {
+ t.Parallel()
t.Run("the agent's variables and a store endpoint", func(t *testing.T) {
f := newFixture(t)
p := f.start(f.env)
@@ -765,10 +775,23 @@ func (o *oneConn) Addr() net.Addr { return o.c.LocalAddr() }
// ---- the forked worker ------------------------------------------------------
+// The worker's grace is a minute in every build but this test's: it links
+// its own moveprobe with an 8 s grace (workerGraceOverride) and watches
+// that build's worker outlive its main, keep writing, and leave on its own
+// at the grace, as the shipped build does at a minute.
func TestMoveprobeWorkerOutlivesMain(t *testing.T) {
- t.Parallel() // a minute of waiting for the worker's own exit
+ t.Parallel()
+ if workerGrace != 60*time.Second || workerGraceOverride != "" || orphanGrace() != workerGrace {
+ t.Fatalf("a plain build's worker grace is %s (const %s, override %q), want a minute", orphanGrace(), workerGrace, workerGraceOverride)
+ }
+ const grace = 8 * time.Second
+ bin := filepath.Join(t.TempDir(), "moveprobe-grace")
+ build := exec.Command("go", "build", "-ldflags", "-X main.workerGraceOverride="+grace.String(), "-o", bin, ".")
+ if out, err := build.CombinedOutput(); err != nil {
+ t.Fatalf("build moveprobe with an %s grace: %v\n%s", grace, err, out)
+ }
f := newFixture(t)
- p := f.start(f.env)
+ p := f.startBin(bin, f.env)
p.wait("/ready", http.StatusOK)
r := p.report()
if r.WorkerPID <= 0 || !alive(r.WorkerPID) {
@@ -798,22 +821,23 @@ func TestMoveprobeWorkerOutlivesMain(t *testing.T) {
t.Fatalf("the worker stopped writing after the main died: %d then %d rows", n0, n1)
}
- // It exits on its own about a minute after its parent has gone.
- for alive(r.WorkerPID) && time.Since(killed) < 80*time.Second {
- time.Sleep(250 * time.Millisecond)
+ // It exits on its own about its grace after its parent has gone.
+ for alive(r.WorkerPID) && time.Since(killed) < grace+20*time.Second {
+ time.Sleep(50 * time.Millisecond)
}
gone := time.Since(killed)
if alive(r.WorkerPID) {
t.Fatalf("the worker is still alive %s after the main died", gone.Round(time.Second))
}
- if gone < 59*time.Second || gone > 70*time.Second {
- t.Errorf("the worker exited %s after the main died, want about 60s", gone.Round(time.Second))
+ if gone < grace-time.Second || gone > grace+10*time.Second {
+ t.Errorf("the worker exited %s after the main died, want about %s", gone.Round(time.Second), grace)
}
}
// ---- the failure switch ---------------------------------------------------
func TestReadyFailsWhereTold(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
// The installed state: a normal boot in uk, stopped, its worker killed
@@ -900,6 +924,7 @@ func TestReadyFailsWhereTold(t *testing.T) {
// ---- the report -----------------------------------------------------------
func TestReportCountsRowsByRegion(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
uk := f.start(f.env)
uk.wait("/ready", http.StatusOK)
@@ -991,6 +1016,7 @@ func TestReportCountsRowsByRegion(t *testing.T) {
// ---- the instance key -----------------------------------------------------
func TestReadyChecksBothKeys(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
p := f.start(f.env)
p.wait("/ready", http.StatusOK)
@@ -1061,6 +1087,7 @@ func allowInserts(t *testing.T, db, kind string) {
// TestReadyFollowsTheWorkersLastWrite: /ready is 200 only once a worker row
// has committed since this start and the worker's latest write succeeded.
func TestReadyFollowsTheWorkersLastWrite(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
seed := f.start(f.env)
seed.wait("/ready", http.StatusOK)
@@ -1113,6 +1140,7 @@ func TestReadyFollowsTheWorkersLastWrite(t *testing.T) {
}
func TestAfterWriteExitsWhenItsRowFails(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
seed := f.start(f.env)
seed.wait("/ready", http.StatusOK)
@@ -1197,6 +1225,7 @@ func (r *failRows) Next(dest []driver.Value) error {
func init() { sql.Register("moveprobe-fail", failDriver{}) }
func TestReportNeverAnswersPartial(t *testing.T) {
+ t.Parallel()
report := func(t *testing.T, db *sql.DB) (int, string) {
t.Helper()
a := &app{db: db, filesDir: t.TempDir(), region: "uk", mode: modeNormal, storeErr: errors.New("store: not under test")}
@@ -1237,6 +1266,7 @@ func TestReportNeverAnswersPartial(t *testing.T) {
}
func TestArmOnceReusesOneControlConnection(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
p := f.start(f.env)
p.wait("/ready", http.StatusOK)
@@ -1254,6 +1284,7 @@ func TestArmOnceReusesOneControlConnection(t *testing.T) {
// TestReadyFailsWhenTheWorkerStops: a worker that has died, or has sent no
// status for 3 s, fails readiness, and the report says so.
func TestReadyFailsWhenTheWorkerStops(t *testing.T) {
+ t.Parallel()
t.Run("the worker dies after its first good write", func(t *testing.T) {
f := newFixture(t)
p := f.start(f.env)
@@ -1301,6 +1332,7 @@ func TestReadyFailsWhenTheWorkerStops(t *testing.T) {
// store check per 5 s, however many requests arrive, and requests that land
// while a check is running share its result.
func TestReadyCoalescesTheStoreCheck(t *testing.T) {
+ t.Parallel()
f := newFixture(t)
p := f.start(f.env)
p.wait("/ready", http.StatusOK)