Files
mesh-sdk/go/bus_waits_for_serve_test.go
T
jschoubben 5fa64be894 Revert the revert of G's mesh-sdk merge (#191): G's sdk stands again over reader release 3
Main reverted G's half merge (#189, reverted by 8afc3eb in #191) and then took the reader release's S1-S5, which
copied G's code. G's form holds where they differ (process_bus.go, seats.go, state.go and their tests); the
release's kept names stay so main's callers keep building: SeatViewPrefix beside SeatReadPrefix, and QueueInvoke's
empty strings on not-ok (with its test TestQueueInvoke). The tree differs from G's head f30dee54 only by those and
the release's two tests (objects cap option, seat states and stream kinds).

Builds: design 47 G (the milestone's sdk part, as at f30dee54)

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-13 12:49:56 +01:00

82 lines
2.7 KiB
Go

package stdio
import (
"io"
"testing"
"time"
)
// A bus function called before Serve waits for Serve and proceeds (issue 1966): a module that starts a goroutine
// before stdio.Serve and calls the bus there is no longer refused. It is refused only when the process never serves.
// The test drives the wait itself: serveWait is the test's own timer channel, no row sleeps on a clock.
func subBusFunctionsWaitForServe(t *testing.T) {
const refusal = "before Serve: a bundle reaches the bus through the runtime that launched it"
for _, tc := range []struct {
name string
serve string // "first": before the call; "during": while the call waits; "never"
wantOK bool
}{
{"a call before Serve completes after Serve", "during", true},
{"a call whose wait ends before Serve is refused", "never", false},
{"a call after Serve is as before", "first", true},
} {
t.Run(tc.name, func(t *testing.T) {
servesAlone(t)
expire := make(chan time.Time)
oldAfter := serveTimer
serveTimer = func() (<-chan time.Time, func()) { return expire, func() {} }
t.Cleanup(func() { serveTimer = oldAfter })
inR, inW := io.Pipe()
outR, outW := io.Pipe()
stopped := make(chan struct{})
serve := func() {
go func() { _ = ServeOn("m", nil, inR, outW); close(stopped) }()
}
t.Cleanup(func() { _ = inW.Close(); go func() { _, _ = io.Copy(io.Discard, outR) }() })
type result struct {
c *channel
err error
}
done := make(chan result, 1)
if tc.serve == "first" {
serve()
<-Served()
c, err := live()
done <- result{c, err}
} else {
go func() { c, err := live(); done <- result{c, err} }()
select {
case r := <-done:
t.Fatalf("live() returned before Serve and before its wait ended: %v", r.err)
default:
}
if tc.serve == "during" {
serve()
} else {
expire <- time.Time{}
}
}
r := <-done
if tc.wantOK != (r.err == nil && r.c != nil) {
t.Fatalf("live() = %v, %v; want ok=%v", r.c, r.err, tc.wantOK)
}
if !tc.wantOK && r.err.Error() != refusal {
t.Fatalf("refusal words changed: %v", r.err)
}
if tc.serve != "never" {
_ = inW.Close()
<-stopped
}
})
}
}
// TestBusFunctionsWaitForServe runs the tests of this package that were merged to keep the module within its test budget (design 47 G §419), each as a subtest.
func TestBusFunctionsWaitForServe(t *testing.T) {
t.Run("BusFunctionsWaitForServe", subBusFunctionsWaitForServe)
t.Run("ACapabilityWordIsReadOnceAndNamedWhenNotStated", subACapabilityWordIsReadOnceAndNamedWhenNotStated)
t.Run("ABundleAsksAnAcknowledgedPublishAndASubscriptionForNewMessages", subABundleAsksAnAcknowledgedPublishAndASubscriptionForNewMessages)
}