Skip to content

Commit e9b6398

Browse files
committed
fix: Increase ptytest buffer to resolve flake
From investigating the following run: https://github.com/coder/coder/runs/6156348454?check_suite_focus=true It's believed that the cause is data being discarded due to the buffer filling up. This _might_ fix it, but not entirely sure.
1 parent 5575e3c commit e9b6398

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

agent/agent_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func TestAgent(t *testing.T) {
7676
session.Stdin = ptty.Input()
7777
err = session.Start(command)
7878
require.NoError(t, err)
79+
caret := "$"
80+
if runtime.GOOS == "windows" {
81+
caret = ">"
82+
}
83+
ptty.ExpectMatch(caret)
7984
ptty.WriteLine("echo test")
8085
ptty.ExpectMatch("test")
8186
ptty.WriteLine("exit")

pty/ptytest/ptytest.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package ptytest
33
import (
44
"bufio"
55
"bytes"
6+
"context"
67
"io"
78
"os"
89
"os/exec"
910
"regexp"
1011
"runtime"
1112
"strings"
1213
"testing"
14+
"time"
1315
"unicode/utf8"
1416

1517
"github.com/stretchr/testify/require"
@@ -60,7 +62,7 @@ func create(t *testing.T, ptty pty.PTY) *PTY {
6062
PTY: ptty,
6163

6264
outputWriter: writer,
63-
runeReader: bufio.NewReaderSize(ptty.Output(), utf8.UTFMax),
65+
runeReader: bufio.NewReaderSize(ptty.Output(), 4096),
6466
}
6567
}
6668

@@ -76,6 +78,19 @@ func (p *PTY) ExpectMatch(str string) string {
7678
var buffer bytes.Buffer
7779
multiWriter := io.MultiWriter(&buffer, p.outputWriter)
7880
runeWriter := bufio.NewWriterSize(multiWriter, utf8.UTFMax)
81+
complete, cancelFunc := context.WithCancel(context.Background())
82+
defer cancelFunc()
83+
go func() {
84+
timer := time.NewTimer(10 * time.Second)
85+
defer timer.Stop()
86+
select {
87+
case <-complete.Done():
88+
return
89+
case <-timer.C:
90+
}
91+
_ = p.Close()
92+
p.t.Errorf("match exceeded deadline: %q", str)
93+
}()
7994
for {
8095
var r rune
8196
r, _, err := p.runeReader.ReadRune()

0 commit comments

Comments
 (0)