Skip to content

Commit e39e885

Browse files
committed
Rework truncation test to not assume OS buffers
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent 2c9c6ef commit e39e885

File tree

1 file changed

+14
-30
lines changed

1 file changed

+14
-30
lines changed

pty/start_test.go

Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os/exec"
99
"strings"
1010
"testing"
11+
"time"
1112

1213
"github.com/hinshun/vt10x"
1314
"github.com/stretchr/testify/assert"
@@ -60,7 +61,7 @@ func Test_Start_copy(t *testing.T) {
6061
func Test_Start_trucation(t *testing.T) {
6162
t.Parallel()
6263

63-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
64+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
6465
defer cancel()
6566

6667
pc, cmd, err := pty.Start(exec.CommandContext(ctx, cmdCount, argCount...))
@@ -71,24 +72,28 @@ func Test_Start_trucation(t *testing.T) {
7172
defer close(readDone)
7273
// avoid buffered IO so that we can precisely control how many bytes to read.
7374
n := 1
74-
for n < countEnd-25 {
75+
for n <= countEnd {
7576
want := fmt.Sprintf("%d", n)
7677
err := readUntil(ctx, t, want, pc.OutputReader())
7778
assert.NoError(t, err, "want: %s", want)
7879
if err != nil {
7980
return
8081
}
8182
n++
83+
if (countEnd - n) < 100 {
84+
// If the OS buffers the output, the process can exit even if
85+
// we're not done reading. We want to slow our reads so that
86+
// if there is a race between reading the data and it being
87+
// truncated, we will lose and fail the test.
88+
time.Sleep(testutil.IntervalFast)
89+
}
8290
}
91+
// ensure we still get to EOF
92+
endB := &bytes.Buffer{}
93+
_, err := io.Copy(endB, pc.OutputReader())
94+
assert.NoError(t, err)
8395
}()
8496

85-
select {
86-
case <-readDone:
87-
// OK!
88-
case <-ctx.Done():
89-
t.Error("read timed out")
90-
}
91-
9297
cmdDone := make(chan error)
9398
go func() {
9499
cmdDone <- cmd.Wait()
@@ -101,27 +106,6 @@ func Test_Start_trucation(t *testing.T) {
101106
t.Error("cmd.Wait() timed out")
102107
}
103108

104-
// do our final 25 reads, to make sure the output wasn't lost
105-
readDone = make(chan struct{})
106-
go func() {
107-
defer close(readDone)
108-
// avoid buffered IO so that we can precisely control how many bytes to read.
109-
n := countEnd - 25
110-
for n <= countEnd {
111-
want := fmt.Sprintf("%d", n)
112-
err := readUntil(ctx, t, want, pc.OutputReader())
113-
assert.NoError(t, err, "want: %s", want)
114-
if err != nil {
115-
return
116-
}
117-
n++
118-
}
119-
// ensure we still get to EOF
120-
endB := &bytes.Buffer{}
121-
_, err := io.Copy(endB, pc.OutputReader())
122-
assert.NoError(t, err)
123-
}()
124-
125109
select {
126110
case <-readDone:
127111
// OK!

0 commit comments

Comments
 (0)