1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
22 "honnef.co/go/tools/internal/robustio"
25 func TestConcurrentReadsAndWrites(t *testing.T) {
26 dir, err := ioutil.TempDir("", "renameio")
30 defer os.RemoveAll(dir)
31 path := filepath.Join(dir, "blob.bin")
33 const chunkWords = 8 << 10
34 buf := make([]byte, 2*chunkWords*8)
35 for i := uint64(0); i < 2*chunkWords; i++ {
36 binary.LittleEndian.PutUint64(buf[i*8:], i)
39 var attempts int64 = 128
45 var sem = make(chan bool, parallel)
48 writeSuccesses, readSuccesses int64 // atomic
49 writeErrnoSeen, readErrnoSeen sync.Map
52 for n := attempts; n > 0; n-- {
55 defer func() { <-sem }()
57 time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond)
58 offset := rand.Intn(chunkWords)
59 chunk := buf[offset*8 : (offset+chunkWords)*8]
60 if err := WriteFile(path, chunk, 0666); err == nil {
61 atomic.AddInt64(&writeSuccesses, 1)
62 } else if robustio.IsEphemeralError(err) {
66 if errno, ok := err.(syscall.Errno); ok {
67 _, dup = writeErrnoSeen.LoadOrStore(errno, true)
70 t.Logf("ephemeral error: %v", err)
73 t.Errorf("unexpected error: %v", err)
76 time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond)
77 data, err := ReadFile(path)
79 atomic.AddInt64(&readSuccesses, 1)
80 } else if robustio.IsEphemeralError(err) {
84 if errno, ok := err.(syscall.Errno); ok {
85 _, dup = readErrnoSeen.LoadOrStore(errno, true)
88 t.Logf("ephemeral error: %v", err)
92 t.Errorf("unexpected error: %v", err)
96 if len(data) != 8*chunkWords {
97 t.Errorf("read %d bytes, but each write is a %d-byte file", len(data), 8*chunkWords)
101 u := binary.LittleEndian.Uint64(data)
102 for i := 1; i < chunkWords; i++ {
103 next := binary.LittleEndian.Uint64(data[i*8:])
105 t.Errorf("wrote sequential integers, but read integer out of sequence at offset %d", i)
113 for n := parallel; n > 0; n-- {
117 var minWriteSuccesses int64 = attempts
118 if runtime.GOOS == "windows" {
119 // Windows produces frequent "Access is denied" errors under heavy rename load.
120 // As long as those are the only errors and *some* of the writes succeed, we're happy.
121 minWriteSuccesses = attempts / 4
124 if writeSuccesses < minWriteSuccesses {
125 t.Errorf("%d (of %d) writes succeeded; want ≥ %d", writeSuccesses, attempts, minWriteSuccesses)
127 t.Logf("%d (of %d) writes succeeded (ok: ≥ %d)", writeSuccesses, attempts, minWriteSuccesses)
130 var minReadSuccesses int64 = attempts
132 switch runtime.GOOS {
134 // Windows produces frequent "Access is denied" errors under heavy rename load.
135 // As long as those are the only errors and *some* of the reads succeed, we're happy.
136 minReadSuccesses = attempts / 4
139 // The filesystem on macOS 10.14 occasionally fails with "no such file or
140 // directory" errors. See https://golang.org/issue/33041. The flake rate is
141 // fairly low, so ensure that at least 75% of attempts succeed.
142 minReadSuccesses = attempts - (attempts / 4)
145 if readSuccesses < minReadSuccesses {
146 t.Errorf("%d (of %d) reads succeeded; want ≥ %d", readSuccesses, attempts, minReadSuccesses)
148 t.Logf("%d (of %d) reads succeeded (ok: ≥ %d)", readSuccesses, attempts, minReadSuccesses)