|
2 | 2 | // Use of this source code is governed by a BSD-style
|
3 | 3 | // license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -// Package renameio writes files atomically by renaming temporary files. |
6 |
| - |
7 |
| -//+build !nacl,!plan9,!windows,!js |
| 5 | +//+build !plan9 |
8 | 6 |
|
9 | 7 | package renameio
|
10 | 8 |
|
11 | 9 | import (
|
| 10 | + "encoding/binary" |
| 11 | + "errors" |
12 | 12 | "io/ioutil"
|
| 13 | + "math/rand" |
13 | 14 | "os"
|
14 | 15 | "path/filepath"
|
| 16 | + "runtime" |
| 17 | + "sync" |
| 18 | + "sync/atomic" |
15 | 19 | "syscall"
|
16 | 20 | "testing"
|
| 21 | + "time" |
17 | 22 | )
|
18 | 23 |
|
19 |
| -func TestWriteFileModeAppliesUmask(t *testing.T) { |
| 24 | +func TestConcurrentReadsAndWrites(t *testing.T) { |
20 | 25 | dir, err := ioutil.TempDir("", "renameio")
|
21 | 26 | if err != nil {
|
22 |
| - t.Fatalf("Failed to create temporary directory: %v", err) |
| 27 | + t.Fatal(err) |
23 | 28 | }
|
| 29 | + defer os.RemoveAll(dir) |
| 30 | + path := filepath.Join(dir, "blob.bin") |
24 | 31 |
|
25 |
| - const mode = 0644 |
26 |
| - const umask = 0007 |
27 |
| - defer syscall.Umask(syscall.Umask(umask)) |
| 32 | + const chunkWords = 8 << 10 |
| 33 | + buf := make([]byte, 2*chunkWords*8) |
| 34 | + for i := uint64(0); i < 2*chunkWords; i++ { |
| 35 | + binary.LittleEndian.PutUint64(buf[i*8:], i) |
| 36 | + } |
28 | 37 |
|
29 |
| - file := filepath.Join(dir, "testWrite") |
30 |
| - err = WriteFile(file, []byte("go-build"), mode) |
31 |
| - if err != nil { |
32 |
| - t.Fatalf("Failed to write file: %v", err) |
| 38 | + var attempts int64 = 128 |
| 39 | + if !testing.Short() { |
| 40 | + attempts *= 16 |
33 | 41 | }
|
34 |
| - defer os.RemoveAll(dir) |
| 42 | + const parallel = 32 |
35 | 43 |
|
36 |
| - fi, err := os.Stat(file) |
37 |
| - if err != nil { |
38 |
| - t.Fatalf("Stat %q (looking for mode %#o): %s", file, mode, err) |
| 44 | + var sem = make(chan bool, parallel) |
| 45 | + |
| 46 | + var ( |
| 47 | + writeSuccesses, readSuccesses int64 // atomic |
| 48 | + writeErrnoSeen, readErrnoSeen sync.Map |
| 49 | + ) |
| 50 | + |
| 51 | + for n := attempts; n > 0; n-- { |
| 52 | + sem <- true |
| 53 | + go func() { |
| 54 | + defer func() { <-sem }() |
| 55 | + |
| 56 | + time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) |
| 57 | + offset := rand.Intn(chunkWords) |
| 58 | + chunk := buf[offset*8 : (offset+chunkWords)*8] |
| 59 | + if err := WriteFile(path, chunk, 0666); err == nil { |
| 60 | + atomic.AddInt64(&writeSuccesses, 1) |
| 61 | + } else if isEphemeralError(err) { |
| 62 | + var ( |
| 63 | + errno syscall.Errno |
| 64 | + dup bool |
| 65 | + ) |
| 66 | + if errors.As(err, &errno) { |
| 67 | + _, dup = writeErrnoSeen.LoadOrStore(errno, true) |
| 68 | + } |
| 69 | + if !dup { |
| 70 | + t.Logf("ephemeral error: %v", err) |
| 71 | + } |
| 72 | + } else { |
| 73 | + t.Errorf("unexpected error: %v", err) |
| 74 | + } |
| 75 | + |
| 76 | + time.Sleep(time.Duration(rand.Intn(100)) * time.Microsecond) |
| 77 | + data, err := ioutil.ReadFile(path) |
| 78 | + if err == nil { |
| 79 | + atomic.AddInt64(&readSuccesses, 1) |
| 80 | + } else if isEphemeralError(err) { |
| 81 | + var ( |
| 82 | + errno syscall.Errno |
| 83 | + dup bool |
| 84 | + ) |
| 85 | + if errors.As(err, &errno) { |
| 86 | + _, dup = readErrnoSeen.LoadOrStore(errno, true) |
| 87 | + } |
| 88 | + if !dup { |
| 89 | + t.Logf("ephemeral error: %v", err) |
| 90 | + } |
| 91 | + return |
| 92 | + } else { |
| 93 | + t.Errorf("unexpected error: %v", err) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + if len(data) != 8*chunkWords { |
| 98 | + t.Errorf("read %d bytes, but each write is a %d-byte file", len(data), 8*chunkWords) |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + u := binary.LittleEndian.Uint64(data) |
| 103 | + for i := 1; i < chunkWords; i++ { |
| 104 | + next := binary.LittleEndian.Uint64(data[i*8:]) |
| 105 | + if next != u+1 { |
| 106 | + t.Errorf("wrote sequential integers, but read integer out of sequence at offset %d", i) |
| 107 | + return |
| 108 | + } |
| 109 | + u = next |
| 110 | + } |
| 111 | + }() |
| 112 | + } |
| 113 | + |
| 114 | + for n := parallel; n > 0; n-- { |
| 115 | + sem <- true |
| 116 | + } |
| 117 | + |
| 118 | + var minWriteSuccesses int64 = attempts |
| 119 | + if runtime.GOOS == "windows" { |
| 120 | + // Windows produces frequent "Access is denied" errors under heavy rename load. |
| 121 | + // As long as those are the only errors and *some* of the writes succeed, we're happy. |
| 122 | + minWriteSuccesses = attempts / 4 |
| 123 | + } |
| 124 | + |
| 125 | + if writeSuccesses < minWriteSuccesses { |
| 126 | + t.Errorf("%d (of %d) writes succeeded; want ≥ %d", writeSuccesses, attempts, minWriteSuccesses) |
| 127 | + } else { |
| 128 | + t.Logf("%d (of %d) writes succeeded (ok: ≥ %d)", writeSuccesses, attempts, minWriteSuccesses) |
| 129 | + } |
| 130 | + |
| 131 | + var minReadSuccesses int64 = attempts |
| 132 | + if runtime.GOOS == "windows" { |
| 133 | + // Windows produces frequent "Access is denied" errors under heavy rename load. |
| 134 | + // As long as those are the only errors and *some* of the writes succeed, we're happy. |
| 135 | + minReadSuccesses = attempts / 4 |
39 | 136 | }
|
40 | 137 |
|
41 |
| - if fi.Mode()&os.ModePerm != 0640 { |
42 |
| - t.Errorf("Stat %q: mode %#o want %#o", file, fi.Mode()&os.ModePerm, 0640) |
| 138 | + if readSuccesses < minReadSuccesses { |
| 139 | + t.Errorf("%d (of %d) reads succeeded; want ≥ %d", readSuccesses, attempts, minReadSuccesses) |
| 140 | + } else { |
| 141 | + t.Logf("%d (of %d) reads succeeded (ok: ≥ %d)", readSuccesses, attempts, minReadSuccesses) |
43 | 142 | }
|
44 | 143 | }
|
0 commit comments