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`
This commit is contained in:
SmallCoccinelle 2021-09-07 05:30:26 +02:00 committed by GitHub
parent 489db34db2
commit b76283df08
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

View File

@ -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()
}