Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
test: replace sleeps in StdioBridge tests with bounded waits
Antonio Ojead562ef7e17e0
diff --git a/internal/node/stdio_bridge_test.go b/internal/node/stdio_bridge_test.goindex e29cbbed..06312625 100644--- a/internal/node/stdio_bridge_test.go+++ b/internal/node/stdio_bridge_test.go@@ -42,23 +42,59 @@ type nopWriteCloser struct{ io.Writer }func (nopWriteCloser) Close() error { return nil }+// waitFor polls cond until it holds; fails the test after 2s.+func waitFor(t *testing.T, what string, cond func() bool) {+ t.Helper()+ deadline := time.Now().Add(2 * time.Second)+ for !cond() {+ if time.Now().After(deadline) {+ t.Fatalf("timed out waiting for %s", what)+ }+ time.Sleep(time.Millisecond)+ }+}++// writeSignalRecorder is a ResponseRecorder that reports each body Write on+// wrote, so a test can tell the handler has emitted a line without reading+// rec.Body while the handler goroutine is still writing to it.+type writeSignalRecorder struct {+ *httptest.ResponseRecorder+ wrote chan struct{}+}++func (r *writeSignalRecorder) Write(p []byte) (int, error) {+ n, err := r.ResponseRecorder.Write(p)+ select {+ case r.wrote <- struct{}{}:+ default:+ }+ return n, err+}+func TestStdioBridge_ServeHTTP_GETStreamsBroadcastLines(t *testing.T) {b, stdoutWriter, _ := newPipeBridge()defer func() { _ = stdoutWriter.Close() }()ctx, cancel := context.WithCancel(context.Background())req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)- rec := httptest.NewRecorder()+ rec := &writeSignalRecorder{ResponseRecorder: httptest.NewRecorder(), wrote: make(chan struct{}, 1)}done := make(chan struct{})go func() {b.ServeHTTP(rec, req)close(done)}()- // Give the handler a moment to register as a client before writing.- time.Sleep(50 * time.Millisecond)+ waitFor(t, "SSE client registration", func() bool {+ b.mu.Lock()+ defer b.mu.Unlock()+ return len(b.clients) > 0+ })_, _ = stdoutWriter.Write([]byte("hello\n"))- time.Sleep(50 * time.Millisecond)+ select {+ case <-rec.wrote:+ case <-time.After(2 * time.Second):+ t.Fatal("timed out waiting for the SSE line to be written")+ }cancel()select {@@ -103,7 +139,11 @@ func TestStdioBridge_ServeHTTP_POSTCallWaitsForMatchingReply(t *testing.T) {close(done)}()- time.Sleep(50 * time.Millisecond)+ waitFor(t, "call registration", func() bool {+ b.mu.Lock()+ defer b.mu.Unlock()+ return len(b.calls) > 0+ })reply := `{"jsonrpc":"2.0","id":1,"result":{}}`_, _ = stdoutWriter.Write([]byte(reply + "\n"))