This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreaddir_unix_test.go
142 lines (120 loc) · 2.67 KB
/
readdir_unix_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
//go:build aix || darwin || dragonfly || freebsd || (js && wasm) || linux || netbsd || openbsd || solaris
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
package fs
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
ps "github.com/beyondstorage/go-storage/v4/pairs"
"github.com/beyondstorage/go-storage/v4/types"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func fsReaddir(b *testing.B) {
s, _ := newStorager()
it, err := s.List("/usr/lib")
if err != nil {
b.Error(err)
}
for {
_, err := it.Next()
if err == types.IterateDone {
break
}
}
}
func osReaddir(b *testing.B) {
_, err := ioutil.ReadDir("/usr/lib")
if err != nil {
b.Error(err)
}
}
func TestGetFilesFs(t *testing.T) {
s, _ := newStorager()
it, err := s.List("/usr/lib")
if err != nil {
t.Error(err)
}
for {
o, err := it.Next()
if err == types.IterateDone {
break
}
assert.NotNil(t, o)
}
}
func TestIssue68(t *testing.T) {
// There are fuzzy logic in testIssue68.
// Run it 100 times to make sure everything is ok.
for i := 0; i < 100; i++ {
// We will create upto 1000 files, introduce rand for fuzzing.
numbers := 225 + rand.Intn(800)
t.Run(fmt.Sprintf("list %d files", numbers), func(t *testing.T) {
testIssue68(t, numbers)
})
}
}
// This test case intends to reproduce issue #68.
//
// ref: https://github.com/beyondstorage/go-service-fs/issues/68
func testIssue68(t *testing.T, numbers int) {
tmpDir := t.TempDir()
store, err := newStorager(ps.WithWorkDir(tmpDir))
if err != nil {
t.Errorf("new storager: %v", err)
}
// Create enough files in a dir, the file name must be long enough.
// So that the total size will bigger than 8196.
for i := 0; i < numbers; i++ {
// uuid's max size is 36.
// We use rand here for fuzzing.
filename := uuid.NewString()[:1+rand.Intn(35)]
f, err := os.Create(path.Join(tmpDir, filename))
if err != nil {
t.Error(err)
}
err = f.Close()
if err != nil {
t.Error(err)
}
}
expected := make(map[string]struct{})
fi, err := ioutil.ReadDir(tmpDir)
if err != nil {
t.Error(err)
}
for _, v := range fi {
expected[v.Name()] = struct{}{}
}
actual := make(map[string]struct{})
it, err := store.List("")
if err != nil {
t.Error(err)
}
for {
o, err := it.Next()
if err == types.IterateDone {
break
}
_, exist := actual[o.Path]
if exist {
t.Errorf("file %s exists already", o.Path)
return
}
actual[o.Path] = struct{}{}
}
assert.Equal(t, expected, actual)
}
func BenchmarkGetFilesFs(b *testing.B) {
for i := 0; i < b.N; i++ {
fsReaddir(b)
}
}
func BenchmarkGetFilesOs(b *testing.B) {
for i := 0; i < b.N; i++ {
osReaddir(b)
}
}