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