Skip to content

test(pty/ptytest): fix error message on deadline exceeded #8337

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 1 commit into from
Jul 6, 2023
Merged
Changes from all commits
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
test(pty/ptytest): fix error message on deadline exceeded
  • Loading branch information
mafredri committed Jul 6, 2023
commit 3cbb67bd1e4513c32929321f05574df7d0a256f0
33 changes: 17 additions & 16 deletions pty/ptytest/ptytest.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ func (e *outExpecter) ExpectMatchContext(ctx context.Context, str string) string
e.t.Helper()

var buffer bytes.Buffer
err := e.doMatchWithDeadline(ctx, "ExpectMatchContext", func() error {
err := e.doMatchWithDeadline(ctx, "ExpectMatchContext", func(rd *bufio.Reader) error {
for {
r, _, err := e.runeReader.ReadRune()
r, _, err := rd.ReadRune()
if err != nil {
return err
}
Expand All @@ -186,9 +186,9 @@ func (e *outExpecter) ExpectNoMatchBefore(ctx context.Context, match, before str
e.t.Helper()

var buffer bytes.Buffer
err := e.doMatchWithDeadline(ctx, "ExpectNoMatchBefore", func() error {
err := e.doMatchWithDeadline(ctx, "ExpectNoMatchBefore", func(rd *bufio.Reader) error {
for {
r, _, err := e.runeReader.ReadRune()
r, _, err := rd.ReadRune()
if err != nil {
return err
}
Expand Down Expand Up @@ -218,9 +218,9 @@ func (e *outExpecter) Peek(ctx context.Context, n int) []byte {
e.t.Helper()

var out []byte
err := e.doMatchWithDeadline(ctx, "Peek", func() error {
err := e.doMatchWithDeadline(ctx, "Peek", func(rd *bufio.Reader) error {
var err error
out, err = e.runeReader.Peek(n)
out, err = rd.Peek(n)
return err
})
if err != nil {
Expand All @@ -235,9 +235,9 @@ func (e *outExpecter) ReadRune(ctx context.Context) rune {
e.t.Helper()

var r rune
err := e.doMatchWithDeadline(ctx, "ReadRune", func() error {
err := e.doMatchWithDeadline(ctx, "ReadRune", func(rd *bufio.Reader) error {
var err error
r, _, err = e.runeReader.ReadRune()
r, _, err = rd.ReadRune()
return err
})
if err != nil {
Expand All @@ -252,9 +252,9 @@ func (e *outExpecter) ReadLine(ctx context.Context) string {
e.t.Helper()

var buffer bytes.Buffer
err := e.doMatchWithDeadline(ctx, "ReadLine", func() error {
err := e.doMatchWithDeadline(ctx, "ReadLine", func(rd *bufio.Reader) error {
for {
r, _, err := e.runeReader.ReadRune()
r, _, err := rd.ReadRune()
if err != nil {
return err
}
Expand All @@ -267,14 +267,14 @@ func (e *outExpecter) ReadLine(ctx context.Context) string {

// Unicode code points can be up to 4 bytes, but the
// ones we're looking for are only 1 byte.
b, _ := e.runeReader.Peek(1)
b, _ := rd.Peek(1)
if len(b) == 0 {
return nil
}

r, _ = utf8.DecodeRune(b)
if r == '\n' {
_, _, err = e.runeReader.ReadRune()
_, _, err = rd.ReadRune()
if err != nil {
return err
}
Expand All @@ -297,7 +297,7 @@ func (e *outExpecter) ReadLine(ctx context.Context) string {
return buffer.String()
}

func (e *outExpecter) doMatchWithDeadline(ctx context.Context, name string, fn func() error) error {
func (e *outExpecter) doMatchWithDeadline(ctx context.Context, name string, fn func(*bufio.Reader) error) error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: This is a bonus change to make it more obvious what reader should be used in these functions.

e.t.Helper()

// A timeout is mandatory, caller can decide by passing a context
Expand All @@ -314,14 +314,15 @@ func (e *outExpecter) doMatchWithDeadline(ctx context.Context, name string, fn f
match := make(chan error, 1)
go func() {
defer close(match)
match <- fn()
match <- fn(e.runeReader)
}()
select {
case err := <-match:
return err
case <-ctx.Done():
// Ensure goroutine is cleaned up before test exit.
_ = e.close("match deadline exceeded")
// Ensure goroutine is cleaned up before test exit, do not call
// (*outExpecter).close here to let the caller decide.
_ = e.out.Close()
<-match

return xerrors.Errorf("match deadline exceeded: %w", ctx.Err())
Expand Down