Skip to content

Commit

Permalink
Fix dead-lock while waiting for command output while in test
Browse files Browse the repository at this point in the history
  • Loading branch information
mumoshu committed Sep 23, 2020
1 parent 88d8194 commit e0c7683
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"golang.org/x/sync/errgroup"

"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -257,17 +259,35 @@ func TestExamples(t *testing.T) {
errWrite.Close()
}()

outBuf := new(bytes.Buffer)
if _, err := outBuf.ReadFrom(outRead); err != nil {
t.Fatalf("unexpected error: %v", err)
}
out := outBuf.String()
var out, errOut string

eg := &errgroup.Group{}

eg.Go(func() error {
outBuf := new(bytes.Buffer)
if _, err := outBuf.ReadFrom(outRead); err != nil {
return fmt.Errorf("reading stdout: %w", err)
}

out = outBuf.String()

return nil
})

eg.Go(func() error {
errBuf := new(bytes.Buffer)
if _, err := errBuf.ReadFrom(errRead); err != nil {
return fmt.Errorf("reading stderr: %w", err)
}

errOut = errBuf.String()

return nil
})

errBuf := new(bytes.Buffer)
if _, err := errBuf.ReadFrom(errRead); err != nil {
if err := eg.Wait(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
errOut := errBuf.String()

if tc.expectErr != "" {
if err == nil {
Expand Down

0 comments on commit e0c7683

Please sign in to comment.