diff --git a/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go b/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go index 9317174daa..44f6933e79 100644 --- a/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go +++ b/seaweed-block/core/host/volume/workspace_first_heartbeat_test.go @@ -2,7 +2,14 @@ package volume import ( "context" + "errors" + "fmt" "net" + "os" + "os/exec" + "path/filepath" + "regexp" + "strconv" "strings" "sync" "testing" @@ -108,39 +115,58 @@ func firstHeartbeatRig(t *testing.T, s *orderServer) (h *Host, started time.Time }() t.Cleanup(func() { cancel() - joinOrFailStop(ch, 10*time.Second, func(msg string) { panic(msg) }) + joinOrFailStop(ch, 10*time.Second) }) return h, started, ch } -// joinOrFailStop waits for done within bound. On timeout it calls failStop, -// which must not return (the rig's panics and stops the test binary): the -// cleanups after it would close the host and the server, and restore shared -// settings, under a lease runtime that is still running. -func joinOrFailStop(done <-chan struct{}, bound time.Duration, failStop func(string)) { +// joinOrFailStop waits for done within bound. On timeout it ends the test +// binary at once (os.Exit, no unwinding): a panic would still run the +// cleanups after it (testing runs the rest when one panics), which close the +// host and the server and restore shared settings under a lease runtime +// that is still running. +func joinOrFailStop(done <-chan struct{}, bound time.Duration) { select { case <-done: case <-time.After(bound): - failStop("the lease runtime did not return within " + bound.String() + " of its cancellation; refusing teardown under a live worker") + fmt.Fprintln(os.Stderr, "the lease runtime did not return within "+bound.String()+" of its cancellation; refusing teardown under a live worker") + os.Exit(3) } } -// The negative path of the rig's join: a worker that never returns reaches -// failStop with the cause, after the bound and not before. +// The rig's join fail-stops before teardown. A child test binary registers a +// teardown cleanup (it writes a marker file) and, after it, the join: with a +// worker that never returns the child exits with status 3 and the marker is +// absent (no teardown ran); with a worker that returned it passes and the +// marker is present. func TestAFailedRuntimeJoinFailStopsBeforeTeardown(t *testing.T) { - never := make(chan struct{}) - start := time.Now() - var cause string - joinOrFailStop(never, 50*time.Millisecond, func(msg string) { cause = msg }) - if !strings.Contains(cause, "refusing teardown under a live worker") { - t.Fatalf("a worker that never returned did not reach failStop: %q", cause) + if marker := os.Getenv("JOIN_FAILSTOP_MARKER"); marker != "" { + t.Cleanup(func() { _ = os.WriteFile(marker, []byte("teardown ran"), 0o644) }) // the later teardown + done := make(chan struct{}) + if os.Getenv("JOIN_FAILSTOP_JOINED") == "true" { + close(done) + } + t.Cleanup(func() { joinOrFailStop(done, 50*time.Millisecond) }) // runs first + return } - if time.Since(start) < 50*time.Millisecond { - t.Fatalf("failStop came before the %s bound", 50*time.Millisecond) + for _, joined := range []bool{false, true} { + marker := filepath.Join(t.TempDir(), "teardown-ran") + cmd := exec.Command(os.Args[0], "-test.run=^"+regexp.QuoteMeta(t.Name())+"$", "-test.count=1") + cmd.Env = append(os.Environ(), "JOIN_FAILSTOP_MARKER="+marker, "JOIN_FAILSTOP_JOINED="+strconv.FormatBool(joined)) + out, err := cmd.CombinedOutput() + _, statErr := os.Stat(marker) + var exit *exec.ExitError + switch { + case joined && (err != nil || statErr != nil): + t.Fatalf("a returned worker: child %v, teardown marker %v\n%s", err, statErr, out) + case !joined && !(errors.As(err, &exit) && exit.ExitCode() == 3): + t.Fatalf("a worker that never returned: child %v, want exit status 3\n%s", err, out) + case !joined && statErr == nil: + t.Fatalf("a worker that never returned: the later teardown ran\n%s", out) + case !joined && !strings.Contains(string(out), "refusing teardown under a live worker"): + t.Fatalf("a worker that never returned: no cause in the output\n%s", out) + } } - done := make(chan struct{}) - close(done) - joinOrFailStop(done, time.Second, func(msg string) { t.Fatalf("a returned worker reached failStop: %s", msg) }) } // Identity check (1) of the v1.1.2 bound: a v1.1 holder's first lease