Skip to content

Commit

Permalink
Fix a couple race conditions
Browse files Browse the repository at this point in the history
Found with -race
  • Loading branch information
dnephin committed Oct 25, 2020
1 parent 8f9d58b commit 485b2ea
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
16 changes: 11 additions & 5 deletions testjson/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"sort"
"strings"
"sync"
"time"

"github.com/jonboulle/clockwork"
Expand Down Expand Up @@ -261,11 +262,12 @@ func newPackage() *Package {

// Execution of one or more test packages
type Execution struct {
started time.Time
packages map[string]*Package
errors []string
done bool
lastRunID int
started time.Time
packages map[string]*Package
errorsLock sync.RWMutex
errors []string
done bool
lastRunID int
}

func (e *Execution) add(event TestEvent) {
Expand Down Expand Up @@ -462,11 +464,15 @@ func (e *Execution) addError(err string) {
if strings.HasPrefix(err, "# ") {
return
}
e.errorsLock.Lock()
e.errors = append(e.errors, err)
e.errorsLock.Unlock()
}

// Errors returns a list of all the errors.
func (e *Execution) Errors() []string {
e.errorsLock.RLock()
defer e.errorsLock.RUnlock()
return e.errors
}

Expand Down
1 change: 1 addition & 0 deletions testjson/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var expectedExecution = &Execution{
var cmpExecutionShallow = gocmp.Options{
gocmp.AllowUnexported(Execution{}, Package{}),
gocmp.FilterPath(stringPath("started"), opt.TimeWithThreshold(10*time.Second)),
cmpopts.IgnoreFields(Execution{}, "errorsLock"),
cmpopts.EquateEmpty(),
cmpPackageShallow,
}
Expand Down
4 changes: 2 additions & 2 deletions testjson/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ type executionSummary interface {
}

type noOutputSummary struct {
Execution
*Execution
}

func (s *noOutputSummary) OutputLines(_ TestCase) []string {
Expand All @@ -161,7 +161,7 @@ func newExecSummary(execution *Execution, opts Summary) executionSummary {
if opts.Includes(SummarizeOutput) {
return execution
}
return &noOutputSummary{Execution: *execution}
return &noOutputSummary{Execution: execution}
}

func writeTestCaseSummary(out io.Writer, execution executionSummary, conf testCaseFormatConfig) {
Expand Down

0 comments on commit 485b2ea

Please sign in to comment.