From 7631de20f3fe6df1503de58dcfa91fb4c0ead428 Mon Sep 17 00:00:00 2001 From: Anders Brander Date: Tue, 28 Jan 2020 10:49:20 +0100 Subject: [PATCH 1/2] Fixed TestConcurrency *itself* when testing with the race detector. --- main_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/main_test.go b/main_test.go index bb9b7c2..7091a86 100644 --- a/main_test.go +++ b/main_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "reflect" + "sync" "testing" "time" @@ -327,6 +328,7 @@ keep latest 1 } func TestConcurrency(t *testing.T) { + var wg sync.WaitGroup tmpfile, _ := ioutil.TempFile("/dev/shm", "test.TestReadConfSyntaxError") defer os.Remove(tmpfile.Name()) @@ -337,9 +339,11 @@ func TestConcurrency(t *testing.T) { // This will force clean() to wait for our mainWaitGroup.Done(). mainWaitGroup.Add(1) + wg.Add(1) var cleanErr error go func() { cleanErr = clean(nil, []string{tmpfile.Name()}) + wg.Done() }() // Give some time for the first clean() to acquire the lock. @@ -353,6 +357,8 @@ func TestConcurrency(t *testing.T) { // Let the first clean() exit. mainWaitGroup.Done() + wg.Wait() + if cleanErr != nil { t.Fatalf("clean() returned an error: %s", cleanErr.Error()) } From bd87cfd77198efa64ce094d1464ad0c1b7d0e41c Mon Sep 17 00:00:00 2001 From: Anders Brander Date: Tue, 28 Jan 2020 11:01:04 +0100 Subject: [PATCH 2/2] Use a simple mutex instead of a waitgroup. --- main_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/main_test.go b/main_test.go index 7091a86..34627f5 100644 --- a/main_test.go +++ b/main_test.go @@ -328,7 +328,8 @@ keep latest 1 } func TestConcurrency(t *testing.T) { - var wg sync.WaitGroup + var lock sync.Mutex + tmpfile, _ := ioutil.TempFile("/dev/shm", "test.TestReadConfSyntaxError") defer os.Remove(tmpfile.Name()) @@ -339,11 +340,13 @@ func TestConcurrency(t *testing.T) { // This will force clean() to wait for our mainWaitGroup.Done(). mainWaitGroup.Add(1) - wg.Add(1) + lock.Lock() + var cleanErr error go func() { cleanErr = clean(nil, []string{tmpfile.Name()}) - wg.Done() + + lock.Unlock() }() // Give some time for the first clean() to acquire the lock. @@ -357,7 +360,7 @@ func TestConcurrency(t *testing.T) { // Let the first clean() exit. mainWaitGroup.Done() - wg.Wait() + lock.Lock() if cleanErr != nil { t.Fatalf("clean() returned an error: %s", cleanErr.Error())