From b76283df0860576b4b98bc62ffd6453128e1baaa Mon Sep 17 00:00:00 2001 From: SmallCoccinelle <89733524+SmallCoccinelle@users.noreply.github.com> Date: Tue, 7 Sep 2021 05:30:26 +0200 Subject: [PATCH] Fix data race in progress_test (#1696) The call to p.ExecuteTask happens in a separate go-routine. It writes, under m.mutex, into the job's details. However, the test goroutine itself reads j.Details which is a read race. Protect the reads in the test by the lock. This makes `package job` pass `go test -race` --- pkg/job/progress_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkg/job/progress_test.go b/pkg/job/progress_test.go index f2dbc0bc9..5bca05ae4 100644 --- a/pkg/job/progress_test.go +++ b/pkg/job/progress_test.go @@ -132,14 +132,18 @@ func TestExecuteTask(t *testing.T) { assert := assert.New(t) + m.mutex.Lock() // ensure task is added to the job details assert.Equal(taskDesciption, j.Details[0]) + m.mutex.Unlock() // allow task to finish close(c) time.Sleep(sleepTime) + m.mutex.Lock() // ensure task is removed from the job details assert.Len(j.Details, 0) + m.mutex.Unlock() }