libgocryptfs/tests/matrix/concurrency_test.go
Jakob Unterwurzacher a55b3cc15a tests/matrix: fix data race in TestConcurrentReadWrite
Fixes https://github.com/golang/go/issues/54715

Output was:

$ go test ./tests/matrix -run TestConcurrentReadWrite -race
test_helpers: warning: testParentDir "/tmp/gocryptfs-test-parent-1026" does not reside on ext4, we will miss failures caused by ino reuse
PASS
PASS
==================
WARNING: DATA RACE
Write at 0x00c00038a0e0 by goroutine 63:
  runtime.racewriterange()
      <autogenerated>:1 +0x29
  internal/poll.(*FD).Pread()
      /usr/local/go/src/internal/poll/fd_unix.go:193 +0x169
  os.(*File).pread()
      /usr/local/go/src/os/file_posix.go:40 +0x335
  os.(*File).ReadAt()
      /usr/local/go/src/os/file.go:136 +0x2de
  github.com/rfjakob/gocryptfs/v2/tests/matrix.TestConcurrentReadWrite.func1()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/matrix/concurrency_test.go:40 +0x14b

Previous write at 0x00c00038a0e0 by goroutine 61:
  runtime.racewriterange()
      <autogenerated>:1 +0x29
  internal/poll.(*FD).Pread()
      /usr/local/go/src/internal/poll/fd_unix.go:193 +0x169
  os.(*File).pread()
      /usr/local/go/src/os/file_posix.go:40 +0x335
  os.(*File).ReadAt()
      /usr/local/go/src/os/file.go:136 +0x2de
  github.com/rfjakob/gocryptfs/v2/tests/matrix.TestConcurrentReadWrite.func1()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/matrix/concurrency_test.go:40 +0x14b

Goroutine 63 (running) created at:
  github.com/rfjakob/gocryptfs/v2/tests/matrix.TestConcurrentReadWrite()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/matrix/concurrency_test.go:34 +0x31d
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1446 +0x216
  testing.(*T).Run.func1()
      /usr/local/go/src/testing/testing.go:1493 +0x47

Goroutine 61 (running) created at:
  github.com/rfjakob/gocryptfs/v2/tests/matrix.TestConcurrentReadWrite()
      /home/jakob/go/src/github.com/rfjakob/gocryptfs/tests/matrix/concurrency_test.go:34 +0x31d
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:1446 +0x216
  testing.(*T).Run.func1()
      /usr/local/go/src/testing/testing.go:1493 +0x47
==================
--- FAIL: TestConcurrentReadWrite (0.03s)
    testing.go:1319: race detected during execution of test
FAIL
TestMain: matrix[2] = matrix.testcaseMatrix{plaintextnames:false, openssl:"false", aessiv:false, raw64:false, extraArgs:[]string(nil)} failed
FAIL	github.com/rfjakob/gocryptfs/v2/tests/matrix	0.170s
FAIL
2022-08-28 20:31:27 +02:00

188 lines
3.7 KiB
Go

package matrix
import (
"bytes"
"io"
"log"
"os"
"sync"
"syscall"
"testing"
"github.com/rfjakob/gocryptfs/v2/tests/test_helpers"
)
// https://github.com/rfjakob/gocryptfs/issues/363
//
// Note: this test calls log.Fatal() instead of t.Fatal() because apparently,
// calling t.Fatal() from a goroutine hangs the test.
func TestConcurrentReadWrite(t *testing.T) {
var wg sync.WaitGroup
fn := test_helpers.DefaultPlainDir + "/TestConcurrentReadWrite"
if f, err := os.Create(fn); err != nil {
t.Fatal(err)
} else {
f.Close()
}
content := []byte("1234567890")
threads := 10
loops := 30
for i := 0; i < threads; i++ {
// Reader thread
wg.Add(1)
go func() {
buf := make([]byte, 100)
fRd, err := os.Open(fn)
if err != nil {
log.Fatal(err)
}
for j := 0; j < loops; j++ {
n, err := fRd.ReadAt(buf, 0)
if err != nil && err != io.EOF {
log.Fatal(err)
}
if n != 0 && n != 10 {
log.Fatalf("strange read length: %d", n)
}
}
fRd.Close()
wg.Done()
}()
// Writer thread
wg.Add(1)
go func() {
fWr, err := os.OpenFile(fn, os.O_RDWR, 0700)
if err != nil {
log.Fatal(err)
}
for j := 0; j < loops; j++ {
err = fWr.Truncate(0)
if err != nil {
log.Fatal(err)
}
_, err = fWr.WriteAt(content, 0)
if err != nil {
log.Fatal(err)
}
}
fWr.Close()
wg.Done()
}()
}
wg.Wait()
}
// https://github.com/rfjakob/gocryptfs/issues/363
//
// Note: this test calls log.Fatal() instead of t.Fatal() because apparently,
// calling t.Fatal() from a goroutine hangs the test.
func TestConcurrentReadCreate(t *testing.T) {
fn := test_helpers.DefaultPlainDir + "/TestConcurrentReadCreate"
content := []byte("1234567890")
loops := 100
var wg sync.WaitGroup
// "Create()" thread
wg.Add(1)
go func() {
for i := 0; i < loops; i++ {
f, err := os.Create(fn)
if err != nil {
log.Fatal(err)
}
_, err = f.Write(content)
if err != nil {
log.Fatal(err)
}
f.Close()
syscall.Unlink(fn)
}
wg.Done()
}()
// "Reader" thread
wg.Add(1)
go func() {
buf0 := make([]byte, 100)
for i := 0; i < loops; i++ {
f, err := os.Open(fn)
if err != nil {
i++
continue
}
n, err := f.Read(buf0)
f.Close()
if err == io.EOF {
i++
continue
}
if err != nil {
log.Fatal(err)
}
buf := buf0[:n]
if !bytes.Equal(buf, content) {
// Calling t.Fatal() from a goroutine hangs the test so we use log.Fatal
log.Fatalf("%s: content mismatch: have=%q want=%q", t.Name(), string(buf), string(content))
}
}
wg.Done()
}()
wg.Wait()
}
// TestInoReuse tries to uncover problems when a file gets replaced by
// a directory with the same inode number (and vice versa).
//
// So far, it only has triggered warnings like this
//
// go-fuse: warning: Inode.Path: inode i4201033 is orphaned, replacing segment with ".go-fuse.5577006791947779410/deleted"
//
// but none of the "blocked waiting for FORGET".
func TestInoReuse(t *testing.T) {
var wg sync.WaitGroup
fn := test_helpers.DefaultPlainDir + "/" + t.Name()
wg.Add(1)
go func() {
for i := 0; i < 1000; i++ {
fd, err := syscall.Open(fn, syscall.O_CREAT|syscall.O_WRONLY|syscall.O_TRUNC, 0600)
if err == syscall.EISDIR {
continue
}
if err != nil {
t.Error(err)
break
}
var st syscall.Stat_t
syscall.Fstat(fd, &st)
if i%2 == 0 {
syscall.Close(fd)
syscall.Unlink(fn)
} else {
syscall.Unlink(fn)
syscall.Close(fd)
}
}
wg.Done()
}()
wg.Add(1)
go func() {
for i := 0; i < 1000; i++ {
err := syscall.Mkdir(fn, 0700)
if err == syscall.EEXIST {
continue
}
if err != nil {
t.Error(err)
break
}
var st syscall.Stat_t
syscall.Stat(fn, &st)
syscall.Rmdir(fn)
}
wg.Done()
}()
wg.Wait()
}