tests: fix data race in TestDirIVRace

Ironically, the test for DirIV races had a data race itself
by writing to a bool without taking a lock.

Found by running "./test.bash -race":

WARNING: DATA RACE
Write at 0x00c00001dea5 by goroutine 22:
  github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func1()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:39 +0x38
  github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:73 +0x65c
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:865 +0x163

Previous read at 0x00c00001dea5 by goroutine 23:
  github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace.func2()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:51 +0x8b

Goroutine 22 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:916 +0x699
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:1157 +0xa8
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:865 +0x163
  testing.runTests()
      /usr/local/go/src/testing/testing.go:1155 +0x523
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:1072 +0x2eb
  github.com/rfjakob/gocryptfs/tests/defaults.TestMain()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/main_test.go:21 +0xe1
  main.main()
      _testmain.go:76 +0x222

Goroutine 23 (running) created at:
  github.com/rfjakob/gocryptfs/tests/defaults.TestDirIVRace()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/defaults/diriv_test.go:43 +0x48d
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:865 +0x163
==================
--- FAIL: TestDirIVRace (0.00s)
    testing.go:809: race detected during execution of test
FAIL
This commit is contained in:
Jakob Unterwurzacher 2019-03-03 14:09:33 +01:00
parent cf27037f20
commit cd7a686211
1 changed files with 4 additions and 3 deletions

View File

@ -4,6 +4,7 @@ import (
"io/ioutil"
"os"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
@ -35,8 +36,8 @@ func TestDirIVRace(t *testing.T) {
t.Fatal(err)
}
stop := false
defer func() { stop = true }()
var stop int32
defer func() { atomic.StoreInt32(&stop, 1) }()
var wg sync.WaitGroup
wg.Add(1)
@ -48,7 +49,7 @@ func TestDirIVRace(t *testing.T) {
if err2 == nil {
fd.Close()
}
if stop {
if atomic.LoadInt32(&stop) != 0 {
return
}
}