8000 fix: Improve debuggability of ptytest cleanup · coder/coder@2a527c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a527c9

Browse files
committed
fix: Improve debuggability of ptytest cleanup
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 2a527c9

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

pty/ptytest/ptytest.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,17 @@ 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+
time.Sleep(5 * time.Second)
56+
select {
57+
case <-ctx.Done():
58+
fatalf(t, name, "cleanup", "log pipe did not close in time")
59+
case <-logDone: // Guard against logging after test.
60+
}
5361
})
5462

5563
// Write to log and output buffer.
@@ -62,9 +70,16 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
6270
_ = out.closeErr(err)
6371
}()
6472
t.Cleanup(func() {
73+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
74+
defer cancel()
75+
6576
_ = out.Close()
6677
_ = ptty.Close()
67-
<-copyDone
78+
select {
79+
case <-ctx.Done():
80+
fatalf(t, name, "cleanup", "copy did not close in time")
81+
case <-copyDone:
82+
}
6883
})
6984

7085
tpty := &PTY{
@@ -164,20 +179,30 @@ func (p *PTY) WriteLine(str string) {
164179

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

168193
// Match regular logger timestamp format, we seem to be logging in
169194
// 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...))
195+
t.Logf("%s: %s: %s", time.Now().UTC().Format("2006-01-02 15:04:05.000"), name, fmt.Sprintf(format, args...))
171196
}
172197

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

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

180-
require.FailNowf(p.t, reason, format, args...)
205+
require.FailNowf(t, reason, format, args...)
181206
}
182207

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

0 commit comments

Comments
 (0)
0