Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(test): Rewrite test to use runReadLoop helper (bypass PTY race) (#60669)

mendral-app[bot]6a9ebc102d4e
diff --git a/tools/phrocs/internal/process/proc_test.go b/tools/phrocs/internal/process/proc_test.go
index ef209c42746a..2c983e8fbc17 100644
--- a/tools/phrocs/internal/process/proc_test.go
+++ b/tools/phrocs/internal/process/proc_test.go
@@ -282,47 +282,30 @@ func TestStop_kills_entire_process_group(t *testing.T) {
func TestReadLoop_batchesOutput(t *testing.T) {
const totalLines = 500
- // Spawn a process that writes 500 lines as fast as possible
- p := NewProcess("batch-test", config.ProcConfig{
- Shell: fmt.Sprintf(`for i in $(seq 1 %d); do echo "line $i"; done`, totalLines),
- }, totalLines+100, "")
+ p := NewProcess("batch-test", config.ProcConfig{}, totalLines+100, "")
- send, msgs, mu := collectMsgs()
- if err := p.Start(send); err != nil {
- t.Skipf("skipping: cannot spawn subprocess: %v", err)
+ // Build input: 500 lines terminated by \r\n (as a PTY would produce).
+ var input strings.Builder
+ for i := 1; i <= totalLines; i++ {
+ fmt.Fprintf(&input, "line %d\r\n", i)
}
- // Wait for the process to finish
- deadline := time.After(10 * time.Second)
- for {
- select {
- case <-deadline:
- t.Fatal("timed out waiting for process to finish")
- default:
- }
- st := p.Status()
- if st == StatusDone || st == StatusCrashed {
- break
- }
- time.Sleep(50 * time.Millisecond)
- }
+ // Use runReadLoopCollect to feed data directly, avoiding the Linux kernel
+ // PTY race (commit 1a48632ffed6) where close() can discard unread data.
+ msgs := runReadLoopCollect(t, p, input.String())
- // Most lines should be buffered. The exact count may vary slightly due
- // to VT screen row trimming and PTY buffering, but should be close.
lines := p.Lines()
- if len(lines) < totalLines/2 {
- t.Fatalf("expected at least %d buffered lines, got %d", totalLines/2, len(lines))
+ if len(lines) < totalLines {
+ t.Fatalf("expected all %d buffered lines, got %d", totalLines, len(lines))
}
- // Count OutputMsg notifications — should be far fewer than totalLines
- mu.Lock()
+ // Count OutputMsg notifications — should be far fewer than totalLines.
var outputMsgCount int
- for _, msg := range *msgs {
+ for _, msg := range msgs {
if _, ok := msg.(OutputMsg); ok {
outputMsgCount++
}
}
- mu.Unlock()
t.Logf("lines=%d, OutputMsg count=%d (%.1fx reduction)",
totalLines, outputMsgCount, float64(totalLines)/float64(outputMsgCount))
@@ -330,8 +313,6 @@ func TestReadLoop_batchesOutput(t *testing.T) {
if outputMsgCount >= totalLines {
t.Errorf("expected batching to reduce OutputMsg count below %d, got %d", totalLines, outputMsgCount)
}
- // With 16ms flush interval and 500 lines written instantly, we expect
- // significant coalescing (typically <20 messages)
if outputMsgCount > totalLines/5 {
t.Errorf("batching not effective enough: %d OutputMsgs for %d lines", outputMsgCount, totalLines)
}
@@ -342,6 +323,12 @@ func TestReadLoop_batchesOutput(t *testing.T) {
// 1a48632ffed6) where data written and immediately followed by close() on the
// child's PTY end can be dropped before the parent's read is scheduled.
func runReadLoop(t *testing.T, p *Process, input string) {
+ t.Helper()
+ runReadLoopCollect(t, p, input)
+}
+
+// runReadLoopCollect is like runReadLoop but returns all messages emitted.
+func runReadLoopCollect(t *testing.T, p *Process, input string) []tea.Msg {
t.Helper()
outChannel := make(chan tea.Msg, 64)
done := make(chan struct{})
@@ -354,6 +341,13 @@ func runReadLoop(t *testing.T, p *Process, input string) {
case <-time.After(2 * time.Second):
t.Fatal("readLoop did not return")
}
+ // Drain remaining messages from the channel.
+ close(outChannel)
+ var msgs []tea.Msg
+ for msg := range outChannel {
+ msgs = append(msgs, msg)
+ }
+ return msgs
}
func TestHasPrompt_partialLine(t *testing.T) {