8
8
"os/exec"
9
9
"strings"
10
10
"testing"
11
+ "time"
11
12
12
13
"github.com/hinshun/vt10x"
13
14
"github.com/stretchr/testify/assert"
@@ -60,7 +61,7 @@ func Test_Start_copy(t *testing.T) {
60
61
func Test_Start_trucation (t * testing.T ) {
61
62
t .Parallel ()
62
63
63
- ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitLong )
64
+ ctx , cancel := context .WithTimeout (context .Background (), testutil .WaitSuperLong )
64
65
defer cancel ()
65
66
66
67
pc , cmd , err := pty .Start (exec .CommandContext (ctx , cmdCount , argCount ... ))
@@ -71,24 +72,28 @@ func Test_Start_trucation(t *testing.T) {
71
72
defer close (readDone )
72
73
// avoid buffered IO so that we can precisely control how many bytes to read.
73
74
n := 1
74
- for n < countEnd - 25 {
75
+ for n <= countEnd {
75
76
want := fmt .Sprintf ("%d" , n )
76
77
err := readUntil (ctx , t , want , pc .OutputReader ())
77
78
assert .NoError (t , err , "want: %s" , want )
78
79
if err != nil {
79
80
return
80
81
}
81
82
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
+ }
82
90
}
91
+ // ensure we still get to EOF
92
+ endB := & bytes.Buffer {}
93
+ _ , err := io .Copy (endB , pc .OutputReader ())
94
+ assert .NoError (t , err )
83
95
}()
84
96
85
- select {
86
- case <- readDone :
87
- // OK!
88
- case <- ctx .Done ():
89
- t .Error ("read timed out" )
90
- }
91
-
92
97
cmdDone := make (chan error )
93
98
go func () {
94
99
cmdDone <- cmd .Wait ()
@@ -101,27 +106,6 @@ func Test_Start_trucation(t *testing.T) {
101
106
t .Error ("cmd.Wait() timed out" )
102
107
}
103
108
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
-
125
109
select {
126
110
case <- readDone :
127
111
// OK!
0 commit comments