Skip to content

Commit

Permalink
shell: deflake TestSourceFileContext
Browse files Browse the repository at this point in the history
It was cancelling the context after firing the goroutine. If the host
machine is busy enough, or if the user is unlucky enough, it could be
that the fired goroutine finishes before the parent has cancelled the
context. Thus leading to a nil dereference panic as we'd call .Error on
a nil error.

To fix this, remove the goroutine race condition, and stop assuming that
the error will be non-nil.
  • Loading branch information
mvdan committed Jan 19, 2019
1 parent 89c5621 commit 6157fb8
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions shell/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ func TestSourceFileContext(t *testing.T) {
}

ctx, cancel := context.WithCancel(context.Background())
cancel()
errc := make(chan error, 1)
go func() {
_, err := SourceFile(ctx, tf.Name())
errc <- err
}()
cancel()
err = <-errc
want := "context canceled"
if !strings.Contains(err.Error(), want) {
if err == nil || !strings.Contains(err.Error(), want) {
t.Fatalf("error %q does not match %q", err, want)
}
}

0 comments on commit 6157fb8

Please sign in to comment.