Skip to content

Commit 6ed12ad

Browse files
authored
fix: Improve debuggability of ptytest cleanup (coder#5170)
There are sporadic flakes in some tests on Windows related to the use of `ptytest`. Since it's not clear why they fail, this commit adds some more logging and timeouts to the cleanup methods.
1 parent 25da224 commit 6ed12ad

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

pty/ptytest/ptytest.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
4747
logDone := make(chan struct{})
4848
logr, logw := io.Pipe()
4949
t.Cleanup(func() {
50+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
51+
defer cancel()
52+
5053
_ = logw.Close()
5154
_ = logr.Close()
52-
<-logDone // Guard against logging after test.
55+
select {
56+
case <-ctx.Done():
57+
fatalf(t, name, "cleanup", "log pipe did not close in time")
58+
case <-logDone: // Guard against logging after test.
59+
}
5360
})
5461

5562
// Write to log and output buffer.
@@ -62,9 +69,16 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
6269
_ = out.closeErr(err)
6370
}()
6471
t.Cleanup(func() {
72+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
73+
defer cancel()
74+
6575
_ = out.Close()
6676
_ = ptty.Close()
67-
<-copyDone
77+
select {
78+
case <-ctx.Done():
79+
fatalf(t, name, "cleanup", "copy did not close in time")
80+
case <-copyDone:
81+
}
6882
})
6983

7084
tpty := &PTY{
@@ -164,20 +178,30 @@ func (p *PTY) WriteLine(str string) {
164178

165179
func (p *PTY) logf(format string, args ...interface{}) {
166180
p.t.Helper()
181+
logf(p.t, p.name, format, args...)
182+
}
183+
184+
func (p *PTY) fatalf(reason string, format string, args ...interface{}) {
185+
p.t.Helper()
186+
fatalf(p.t, p.name, reason, format, args...)
187+
}
188+
189+
func logf(t *testing.T, name, format string, args ...interface{}) {
190+
t.Helper()
167191

168192
// Match regular logger timestamp format, we seem to be logging in
169193
// UTC in other places as well, so match here.
170-
p.t.Logf("%s: %s: %s", time.Now().UTC().Format("2006-01-02 15:04:05.000"), p.name, fmt.Sprintf(format, args...))
194+
t.Logf("%s: %s: %s", time.Now().UTC().Format("2006-01-02 15:04:05.000"), name, fmt.Sprintf(format, args...))
171195
}
172196

173-
func (p *PTY) fatalf(reason string, format string, args ...interface{}) {
174-
p.t.Helper()
197+
func fatalf(t *testing.T, name, reason, format string, args ...interface{}) {
198+
t.Helper()
175199

176200
// Ensure the message is part of the normal log stream before
177201
// failing the test.
178-
p.logf("%s: %s", reason, fmt.Sprintf(format, args...))
202+
logf(t, name, "%s: %s", reason, fmt.Sprintf(format, args...))
179203

180-
require.FailNowf(p.t, reason, format, args...)
204+
require.FailNowf(t, reason, format, args...)
181205
}
182206

183207
// stdbuf is like a buffered stdout, it buffers writes until read.

0 commit comments

Comments
 (0)