| 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. |
| 4 |
|
| 5 |
//go:build !plan9 |
| 6 |
|
| 7 |
package renameio |
| 8 |
|
| 9 |
import ( |
| 10 |
"encoding/binary" |
| 11 |
"errors" |
| 12 |
"math/rand" |
| 13 |
"path/filepath" |
| 14 |
"runtime" |
| 15 |
"sync" |
| 16 |
"sync/atomic" |
| 17 |
"syscall" |
| 18 |
"testing" |
| 19 |
"time" |
| 20 |
|
| 21 |
"honnef.co/go/tools/internal/robustio" |
| 22 |
) |
| 23 |
|
| 24 |
func TestConcurrentReadsAndWrites(t *testing.T) { |
| 25 |
dir := t.TempDir() |
| 26 |
path := filepath.Join(dir, "blob.bin") |
| 27 |
|
| 28 |
const chunkWords = 8 << 10 |
| 29 |
buf := make([]byte, 2*chunkWords*8) |
| 30 |
for i := range uint64(2 * chunkWords) { |
| 31 |
binary.LittleEndian.PutUint64(buf[i*8:], i) |
| 32 |
} |
| 33 |
|
| 34 |
var attempts int64 = 128 |
| 35 |
if !testing.Short() { |
| 36 |
attempts *= 16 |
| 37 |
} |
| 38 |
const parallel = 32 |
| 39 |
|
| 40 |
var sem = make(chan bool, parallel) |
| 41 |
|
| 42 |
var ( |
| 43 |
writeSuccesses, readSuccesses int64 // atomic |
| 44 |
writeErrnoSeen, readErrnoSeen sync.Map |
| 45 |
) |
| 46 |
|
| 47 |
for n := attempts; n > 0; n-- { |
| 48 |
sem <- true |
| 49 |
go func() { |
| 50 |
defer func() { <-sem }() |
| 51 |
|
| 52 |
time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) |
| 53 |
offset := rand.Intn(chunkWords) |
| 54 |
chunk := buf[offset*8 : (offset+chunkWords)*8] |
| 55 |
if err := WriteFile(path, chunk, 0666); err == nil { |
| 56 |
atomic.AddInt64(&writeSuccesses, 1) |
| 57 |
} else if robustio.IsEphemeralError(err) { |
| 58 |
var ( |
| 59 |
errno syscall.Errno |
| 60 |
dup bool |
| 61 |
) |
| 62 |
if errors.As(err, &errno) { |
| 63 |
_, dup = writeErrnoSeen.LoadOrStore(errno, true) |
| 64 |
} |
| 65 |
if !dup { |
| 66 |
t.Logf("ephemeral error: %v", err) |
| 67 |
} |
| 68 |
} else { |
| 69 |
t.Errorf("unexpected error: %v", err) |
| 70 |
} |
| 71 |
|
| 72 |
time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) |
| 73 |
data, err := ReadFile(path) |
| 74 |
if err == nil { |
| 75 |
atomic.AddInt64(&readSuccesses, 1) |
| 76 |
} else if robustio.IsEphemeralError(err) { |
| 77 |
var ( |
| 78 |
errno syscall.Errno |
| 79 |
dup bool |
| 80 |
) |
| 81 |
if errors.As(err, &errno) { |
| 82 |
_, dup = readErrnoSeen.LoadOrStore(errno, true) |
| 83 |
} |
| 84 |
if !dup { |
| 85 |
t.Logf("ephemeral error: %v", err) |
| 86 |
} |
| 87 |
return |
| 88 |
} else { |
| 89 |
t.Errorf("unexpected error: %v", err) |
| 90 |
return |
| 91 |
} |
| 92 |
|
| 93 |
if len(data) != 8*chunkWords { |
| 94 |
t.Errorf("read %d bytes, but each write is a %d-byte file", len(data), 8*chunkWords) |
| 95 |
return |
| 96 |
} |
| 97 |
|
| 98 |
u := binary.LittleEndian.Uint64(data) |
| 99 |
for i := 1; i < chunkWords; i++ { |
| 100 |
next := binary.LittleEndian.Uint64(data[i*8:]) |
| 101 |
if next != u+1 { |
| 102 |
t.Errorf("wrote sequential integers, but read integer out of sequence at offset %d", i) |
| 103 |
return |
| 104 |
} |
| 105 |
u = next |
| 106 |
} |
| 107 |
}() |
| 108 |
} |
| 109 |
|
| 110 |
for n := parallel; n > 0; n-- { |
| 111 |
sem <- true |
| 112 |
} |
| 113 |
|
| 114 |
var minWriteSuccesses int64 = attempts |
| 115 |
if runtime.GOOS == "windows" { |
| 116 |
// Windows produces frequent "Access is denied" errors under heavy rename load. |
| 117 |
// As long as those are the only errors and *some* of the writes succeed, we're happy. |
| 118 |
minWriteSuccesses = attempts / 4 |
| 119 |
} |
| 120 |
|
| 121 |
if writeSuccesses < minWriteSuccesses { |
| 122 |
t.Errorf("%d (of %d) writes succeeded; want ≥ %d", writeSuccesses, attempts, minWriteSuccesses) |
| 123 |
} else { |
| 124 |
t.Logf("%d (of %d) writes succeeded (ok: ≥ %d)", writeSuccesses, attempts, minWriteSuccesses) |
| 125 |
} |
| 126 |
|
| 127 |
var minReadSuccesses int64 = attempts |
| 128 |
|
| 129 |
switch runtime.GOOS { |
| 130 |
case "windows": |
| 131 |
// Windows produces frequent "Access is denied" errors under heavy rename load. |
| 132 |
// As long as those are the only errors and *some* of the reads succeed, we're happy. |
| 133 |
minReadSuccesses = attempts / 4 |
| 134 |
|
| 135 |
case "darwin": |
| 136 |
// The filesystem on macOS 10.14 occasionally fails with "no such file or |
| 137 |
// directory" errors. See https://golang.org/issue/33041. The flake rate is |
| 138 |
// fairly low, so ensure that at least 75% of attempts succeed. |
| 139 |
minReadSuccesses = attempts - (attempts / 4) |
| 140 |
} |
| 141 |
|
| 142 |
if readSuccesses < minReadSuccesses { |
| 143 |
t.Errorf("%d (of %d) reads succeeded; want ≥ %d", readSuccesses, attempts, minReadSuccesses) |
| 144 |
} else { |
| 145 |
t.Logf("%d (of %d) reads succeeded (ok: ≥ %d)", readSuccesses, attempts, minReadSuccesses) |
| 146 |
} |
| 147 |
} |