Skip to content

Commit f12df7b

Browse files
committed
fix: Close ptytest ttys asynchronously to enable timeout
1 parent ee4f0fc commit f12df7b

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pty/ptytest/ptytest.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,29 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
7272
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
7373
defer cancel()
7474

75-
_ = out.Close()
76-
_ = ptty.Close()
75+
// Close the tty asynchronously to allow the select below to timeout.
76+
errC := make(chan error, 2)
77+
doClose := func(c io.Closer) {
78+
select {
79+
case <-ctx.Done():
80+
case errC <- c.Close():
81+
}
82+
}
83+
go func() {
84+
// Close the tty first so allow out to consume its last bytes.
85+
doClose(ptty)
86+
doClose(out)
87+
}()
88+
89+
More:
7790
select {
7891
case <-ctx.Done():
7992
fatalf(t, name, "cleanup", "copy did not close in time")
93+
case err := <-errC:
94+
if err != nil {
95+
logf(t, name, "copy close error: %v", err)
96+
}
97+
goto More
8098
case <-copyDone:
8199
}
82100
})

0 commit comments

Comments
 (0)