-
Notifications
You must be signed in to change notification settings - Fork 0
/
memmap_test.go
113 lines (96 loc) · 2.26 KB
/
memmap_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
package memmap
import (
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
)
func TestMemFsMkdirInConcurrency(t *testing.T) {
t.Parallel()
assert := require.New(t)
const dir = "test_dir"
const n = 1000
for i := 0; i < n; i++ {
fs := afero.NewMemMapFs()
c1 := make(chan error, 1)
c2 := make(chan error, 1)
go func() {
c1 <- fs.Mkdir(dir, 0755)
}()
go func() {
c2 <- fs.Mkdir(dir, 0755)
}()
// Only one attempt of creating the directory should succeed.
err1 := <-c1
err2 := <-c2
assert.NotEqualf(err1, err2, "run #%v, more than one success", i)
}
}
func TestListFilesAfterConcurrencyFileWrite(t *testing.T) {
const dir = "test_dir"
const n = 10000
mfs := afero.NewMemMapFs()
assert := require.New(t)
allFilePaths := make([]string, 0, n)
// run concurrency test
var wg sync.WaitGroup
for i := 0; i < n; i++ {
fp := filepath.Join(
dir,
fmt.Sprintf("%02d", n%10),
fmt.Sprintf("%d.txt", i),
)
allFilePaths = append(allFilePaths, fp)
wg.Add(1)
go func() {
defer wg.Done()
wt, err := newFileWriter(mfs, fp)
assert.NoError(err)
defer func() {
assert.NoError(wt.Close())
}()
// write 30 bytes
for j := 0; j < 10; j++ {
_, err := wt.Write([]byte("000"))
assert.NoError(err)
}
}()
}
wg.Wait()
// Test1: find all files by full path access
for _, fp := range allFilePaths {
info, err := mfs.Stat(fp)
assert.NoErrorf(err, "stat file %s", fp)
assert.Equalf(int64(30), info.Size(), "file %s size unmatch", fp)
}
// Test2: find all files by walk
foundFiles := make([]string, 0, n)
wErr := afero.Walk(mfs, dir, func(path string, info fs.FileInfo, err error) error {
assert.NoError(err)
if info.IsDir() {
return nil // skip dir
}
if strings.HasSuffix(info.Name(), ".txt") {
foundFiles = append(foundFiles, path)
}
return nil
})
assert.NoError(wErr, "walk")
assert.Equal(n, len(foundFiles), "missing files")
}
// newFileWriter mkdir and create file for writer
func newFileWriter(vfs afero.Fs, path string) (io.WriteCloser, error) {
if err := vfs.MkdirAll(filepath.Dir(path), 0755); err != nil {
return nil, err
}
wt, err := vfs.Create(path)
if err != nil {
return nil, err
}
return wt, nil
}