Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

test(agent): make OpenClaw EOF test deterministic (MUL-5802) (#6501)

Multica Eve7bc1e3c36a1e
diff --git a/server/pkg/agent/openclaw_stdout_test.go b/server/pkg/agent/openclaw_stdout_test.go
index 1965978238..a12800ec4f 100644
--- a/server/pkg/agent/openclaw_stdout_test.go
+++ b/server/pkg/agent/openclaw_stdout_test.go
@@ -272,32 +272,70 @@ func TestReadOpenclawStdoutDoesNotWaitForIdleGraceAtEOF(t *testing.T) {
}
}
+// stagedOpenclawEOFReader returns an incomplete result first, then waits until
+// the test releases the final bytes. The final Read returns data and io.EOF
+// together, so completion and clean EOF are one deterministic observation
+// rather than two independently scheduled pipe operations.
+type stagedOpenclawEOFReader struct {
+ prefix string
+ suffix string
+ suffixRead chan struct{}
+ releaseSuffix chan struct{}
+ readCount int
+}
+
+func (r *stagedOpenclawEOFReader) Read(p []byte) (int, error) {
+ switch r.readCount {
+ case 0:
+ r.readCount++
+ return copy(p, r.prefix), nil
+ case 1:
+ r.readCount++
+ close(r.suffixRead)
+ <-r.releaseSuffix
+ return copy(p, r.suffix), io.EOF
+ default:
+ return 0, io.EOF
+ }
+}
+
// TestReadOpenclawStdoutWaitsForCompleteResult pins the safety half of the
// shortcut: idle output alone is not enough. Cutting off a partial buffer would
// throw away work the agent has already done, which is worse than the hang this
// change fixes.
func TestReadOpenclawStdoutWaitsForCompleteResult(t *testing.T) {
- pr, pw, err := os.Pipe()
- if err != nil {
- t.Fatalf("pipe: %v", err)
- }
- defer pr.Close()
-
- // A partial blob that cannot parse, followed by silence.
- if _, err := pw.WriteString(`{"payloads":[{"text":"half`); err != nil {
- t.Fatalf("write: %v", err)
- }
+ r := &stagedOpenclawEOFReader{
+ prefix: `{"payloads":[{"text":"half`,
+ suffix: `"}],"meta":{"durationMs":1}}`,
+ suffixRead: make(chan struct{}),
+ releaseSuffix: make(chan struct{}),
+ }
+ released := false
+ t.Cleanup(func() {
+ if !released {
+ close(r.releaseSuffix)
+ }
+ })
type outcome struct {
cutShort bool
buf string
+ err error
}
done := make(chan outcome, 1)
go func() {
- buf, cutShort, _ := readOpenclawStdout(pr, 200*time.Millisecond)
- done <- outcome{cutShort: cutShort, buf: string(buf)}
+ buf, cutShort, err := readOpenclawStdout(r, 200*time.Millisecond)
+ done <- outcome{cutShort: cutShort, buf: string(buf), err: err}
}()
+ select {
+ case <-r.suffixRead:
+ // The incomplete prefix has been consumed and the reader is now
+ // deliberately silent until the test releases the suffix.
+ case <-time.After(5 * time.Second):
+ t.Fatal("readOpenclawStdout did not request the final bytes")
+ }
+
// Well past the idle grace: without the parse guard the reader would have
// returned cutShort by now.
time.Sleep(700 * time.Millisecond)
@@ -309,14 +347,18 @@ func TestReadOpenclawStdoutWaitsForCompleteResult(t *testing.T) {
default:
}
- // Completing the blob and closing gives the reader a clean EOF.
- if _, err := pw.WriteString(`"}],"meta":{"durationMs":1}}`); err != nil {
- t.Fatalf("write: %v", err)
- }
- pw.Close()
+ // Complete the blob and report EOF in the same Read. The old os.Pipe test
+ // performed a write and close separately, allowing the idle ticker to win
+ // after the bytes became parseable but before the read goroutine observed
+ // EOF on a loaded CI runner.
+ released = true
+ close(r.releaseSuffix)
select {
case got := <-done:
+ if got.err != nil {
+ t.Errorf("readOpenclawStdout: %v", got.err)
+ }
if got.cutShort {
t.Error("cutShort = true after a clean EOF, want false")
}