Skip to content

fix: improve pty and ptytest #5327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Dec 7, 2022
8 changes: 4 additions & 4 deletions pty/pty_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,16 @@ func (p *ptyWindows) Close() error {
return nil
}
p.closed = true
_ = p.outputWrite.Close()
_ = p.outputRead.Close()
_ = p.inputWrite.Close()
_ = p.inputRead.Close()

ret, _, err := procClosePseudoConsole.Call(uintptr(p.console))
if ret < 0 {
return xerrors.Errorf("close pseudo console: %w", err)
}

_ = p.outputWrite.Close()
_ = p.outputRead.Close()
_ = p.inputWrite.Close()
_ = p.inputRead.Close()
return nil
}

Expand Down
9 changes: 8 additions & 1 deletion pty/ptytest/ptytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer cancel()

logf(t, name, "close logw on cleanup")
_ = logw.Close()

logf(t, name, "close logr on cleanup")
_ = logr.Close()

logf(t, name, "logr and logw closed")

select {
case <-ctx.Done():
fatalf(t, name, "cleanup", "log pipe did not close in time")
Expand All @@ -74,6 +80,7 @@ func create(t *testing.T, ptty pty.PTY, name string) *PTY {

// Close pty only so that the copy goroutine can consume the
// remainder of it's buffer and then exit.
logf(t, name, "close pty on cleanup")
err := ptty.Close()
// Pty may already be closed, so don't fail the test, but log
// the error in case it's significant.
Expand Down Expand Up @@ -152,7 +159,7 @@ func (p *PTY) ExpectMatch(str string) string {
p.logf("matched %q = %q", str, buffer.String())
return buffer.String()
case <-timeout.Done():
// Ensure gorouine is cleaned up before test exit.
// Ensure goroutine is cleaned up before test exit.
_ = p.out.closeErr(p.Close())
<-match

Expand Down