From 3656ffd69bd3874548276048890904868c8bdcdd Mon Sep 17 00:00:00 2001 From: Michael Broll Date: Tue, 6 Sep 2016 17:15:19 -0700 Subject: [PATCH 1/2] write test to simulate parallel execution of diff() --- assert/assertions_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/assert/assertions_test.go b/assert/assertions_test.go index 12d8151..df17f89 100644 --- a/assert/assertions_test.go +++ b/assert/assertions_test.go @@ -1125,6 +1125,40 @@ func TestDiffEmptyCases(t *testing.T) { Equal(t, "", diff([]int{1}, []bool{true})) } +// Ensure there are no data races +func TestDiffRace(t *testing.T) { + t.Parallel() + + expected := map[string]string{ + "a": "A", + "b": "B", + "c": "C", + } + + actual := map[string]string{ + "d": "D", + "e": "E", + "f": "F", + } + + // run diffs in parallel simulating tests with t.Parallel() + numRoutines := 10 + rChans := make([]chan string, numRoutines) + for idx := range rChans { + rChans[idx] = make(chan string) + go func(ch chan string) { + defer close(ch) + ch <- diff(expected, actual) + }(rChans[idx]) + } + + for _, ch := range rChans { + for msg := range ch { + NotZero(t, msg) // dummy assert + } + } +} + type mockTestingT struct { } From f6ca157360594a3ae0f05b39400c07f658e31507 Mon Sep 17 00:00:00 2001 From: Michael Broll Date: Tue, 6 Sep 2016 17:16:12 -0700 Subject: [PATCH 2/2] assert: set spew config members in init() to prevent data race detection --- assert/assertions.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assert/assertions.go b/assert/assertions.go index 348d5f1..dee1bc7 100644 --- a/assert/assertions.go +++ b/assert/assertions.go @@ -18,6 +18,10 @@ import ( "github.com/pmezard/go-difflib/difflib" ) +func init() { + spew.Config.SortKeys = true +} + // TestingT is an interface wrapper around *testing.T type TestingT interface { Errorf(format string, args ...interface{}) @@ -989,7 +993,6 @@ func diff(expected interface{}, actual interface{}) string { return "" } - spew.Config.SortKeys = true e := spew.Sdump(expected) a := spew.Sdump(actual)