Skip to content

fix: Rewrite ptytest to buffer stdout #3170

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 7 commits into from
Jul 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: Simplify timeout in ExpectMatch
  • Loading branch information
mafredri committed Jul 25, 2022
commit 7b331cd3c4cea992ac811060088e50bd77d8d89f
29 changes: 9 additions & 20 deletions pty/ptytest/ptytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,21 +86,8 @@ type PTY struct {
func (p *PTY) ExpectMatch(str string) string {
p.t.Helper()

complete, cancelFunc := context.WithCancel(context.Background())
defer cancelFunc()

timeout := make(chan error, 1)
go func() {
defer close(timeout)
timer := time.NewTimer(10 * time.Second)
defer timer.Stop()
select {
case <-complete.Done():
return
case <-timer.C:
}
timeout <- xerrors.Errorf("%s match exceeded deadline", time.Now())
}()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

var buffer bytes.Buffer
match := make(chan error, 1)
Expand All @@ -127,14 +114,16 @@ func (p *PTY) ExpectMatch(str string) string {
select {
case err := <-match:
if err != nil {
p.t.Fatalf("read error: %v (wanted %q; got %q)", err, str, buffer.String())
p.t.Fatalf("%s: read error: %v (wanted %q; got %q)", time.Now(), err, str, buffer.String())
return ""
}
p.t.Logf("matched %q = %q", str, buffer.String())
case err := <-timeout:
p.t.Logf("%s: matched %q = %q", time.Now(), str, buffer.String())
return buffer.String()
case <-ctx.Done():
_ = p.out.closeErr(p.Close())
p.t.Fatalf("%s: wanted %q; got %q", err, str, buffer.String())
p.t.Fatalf("%s: match exceeded deadline: wanted %q; got %q", time.Now(), str, buffer.String())
return ""
}
return buffer.String()
}

func (p *PTY) Write(r rune) {
Expand Down