Files
mesh-sdk/go/live_before_serve_test.go
T
jschoubben 240065f5b4 mesh-sdk: a bus function called before Serve waits for Serve instead of refusing (issue 1966)
A module that starts a goroutine before stdio.Serve and calls the bus there was refused and retried on its own tick, a minute later. Refused now only when the process never serves.

Claude-Session: https://claude.ai/code/session_01NcbgfFSnn24M3GcqttoU7i
2026-11-05 22:01:04 +01:00

39 lines
1.5 KiB
Go

package stdio
import (
"os"
"os/exec"
"testing"
"time"
)
// 1171/AC4: before Serve, a bundle's bus functions still refuse exactly as on main (stdio.go:391-398,
// unchanged for bundles). Run in a fresh process: once any other test in this package has called Serve,
// current stays set for the rest of the test binary, so "before Serve" can only be checked honestly in a
// process of its own (the self-exec pattern Go's own testing package uses for os.Exit).
func TestBusFunctionsRefuseBeforeServeInAFreshProcess(t *testing.T) {
const marker = "MESH_SDK_TEST_FRESH_PROCESS"
if os.Getenv(marker) == "1" {
serveWait = 10 * time.Millisecond
if err := Emit("x", map[string]any{}); err == nil ||
err.Error() != "emit before Serve: a bundle emits through the runtime that launched it" {
t.Fatalf("Emit before Serve: %v", err)
}
if _, err := Ask("m.t", nil); err == nil ||
err.Error() != "before Serve: a bundle reaches the bus through the runtime that launched it" {
t.Fatalf("Ask before Serve: %v", err)
}
if err := Subscribe("x", func(Envelope) error { return nil }); err == nil ||
err.Error() != "before Serve: a bundle reaches the bus through the runtime that launched it" {
t.Fatalf("Subscribe before Serve: %v", err)
}
return
}
cmd := exec.Command(os.Args[0], "-test.run=^TestBusFunctionsRefuseBeforeServeInAFreshProcess$", "-test.v")
cmd.Env = append(os.Environ(), marker+"=1")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("a fresh process did not refuse every bus function before Serve: %v\n%s", err, out)
}
}