-
Notifications
You must be signed in to change notification settings - Fork 6
/
dirent_test.go
165 lines (150 loc) · 3.76 KB
/
dirent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//go:build darwin || aix || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
package fastwalk_test
import (
"io/fs"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"github.com/charlievieth/fastwalk"
)
func TestDirent(t *testing.T) {
tempdir := t.TempDir()
fileName := filepath.Join(tempdir, "file.txt")
if err := writeFile(fileName, "file.txt", 0644); err != nil {
t.Fatal(err)
}
linkName := filepath.Join(tempdir, "link.link")
if err := symlink(t, filepath.Base(fileName), linkName); err != nil {
t.Fatal(err)
}
// Use fastwalk.Walk to create the dir entries
getDirEnts := func(t *testing.T) (linkEnt, fileEnt fs.DirEntry) {
err := fastwalk.Walk(nil, tempdir, func(path string, d fs.DirEntry, err error) error {
switch path {
case linkName:
linkEnt = d
case fileName:
fileEnt = d
}
return nil
})
if err != nil {
t.Fatal(err)
}
if fileEnt == nil || linkEnt == nil {
t.Fatal("error walking directory")
}
return linkEnt, fileEnt
}
t.Run("Lstat", func(t *testing.T) {
linkEnt, _ := getDirEnts(t)
want, err := os.Lstat(linkName)
if err != nil {
t.Fatal(err)
}
got, err := linkEnt.Info()
if err != nil {
t.Fatal(err)
}
if !os.SameFile(want, got) {
t.Errorf("lstat mismatch\n got:\n%s\nwant:\n%s",
fastwalk.FormatFileInfo(got), fastwalk.FormatFileInfo(want))
}
})
t.Run("Stat", func(t *testing.T) {
_, fileEnt := getDirEnts(t)
want, err := os.Stat(fileName)
if err != nil {
t.Fatal(err)
}
got, err := fastwalk.StatDirEntry(linkName, fileEnt)
if err != nil {
t.Fatal(err)
}
if !os.SameFile(want, got) {
t.Errorf("lstat mismatch\n got:\n%s\nwant:\n%s",
fastwalk.FormatFileInfo(got), fastwalk.FormatFileInfo(want))
}
fi, err := fileEnt.Info()
if err != nil {
t.Fatal(err)
}
if fi != got {
t.Error("failed to return or cache FileInfo")
}
de := fileEnt.(fastwalk.DirEntry)
fi, err = de.Stat()
if err != nil {
t.Fatal(err)
}
if fi != got {
t.Error("failed to use cached Info result for non-symlink")
}
})
t.Run("Parallel", func(t *testing.T) {
testParallel := func(t *testing.T, de fs.DirEntry, fn func() (fs.FileInfo, error)) {
numCPU := runtime.NumCPU()
infos := make([][]fs.FileInfo, numCPU)
for i := range infos {
infos[i] = make([]fs.FileInfo, 100)
}
// Start all the goroutines at the same time to
// maximise the chance of a race
start := make(chan struct{})
var wg, ready sync.WaitGroup
ready.Add(numCPU)
wg.Add(numCPU)
for i := 0; i < numCPU; i++ {
go func(fis []fs.FileInfo, de fs.DirEntry) {
defer wg.Done()
ready.Done()
<-start
for i := range fis {
fis[i], _ = de.Info()
}
}(infos[i], de)
}
ready.Wait()
close(start) // start all goroutines at once
wg.Wait()
first := infos[0][0]
if first == nil {
t.Fatal("failed to stat file:", de.Name())
}
for _, fis := range infos {
for _, fi := range fis {
if fi != first {
t.Errorf("Expected the same fs.FileInfo to always "+
"be returned got: %#v want: %#v", fi, first)
}
}
}
}
t.Run("File", func(t *testing.T) {
t.Run("Stat", func(t *testing.T) {
_, fileEnt := getDirEnts(t)
de := fileEnt.(fastwalk.DirEntry)
testParallel(t, de, de.Stat)
})
t.Run("Lstat", func(t *testing.T) {
_, fileEnt := getDirEnts(t)
de := fileEnt.(fastwalk.DirEntry)
testParallel(t, de, de.Info)
})
})
t.Run("Link", func(t *testing.T) {
t.Run("Stat", func(t *testing.T) {
linkEnt, _ := getDirEnts(t)
de := linkEnt.(fastwalk.DirEntry)
testParallel(t, de, de.Stat)
})
t.Run("Lstat", func(t *testing.T) {
linkEnt, _ := getDirEnts(t)
de := linkEnt.(fastwalk.DirEntry)
testParallel(t, de, de.Info)
})
})
})
}