-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnamespace_test.go
157 lines (131 loc) · 4.22 KB
/
namespace_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
package whatnot
import (
"path"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/sync/errgroup"
)
var manager *NameSpaceManager
func TestNamespaces(t *testing.T) {
t.Run("Register a Namespace", registerNewNamespace)
t.Run("Populate Namespace randomly", populateNamespaceWithRandomFiles)
}
func registerNewNamespace(t *testing.T) {
const testNameSpace = "globaltest"
t.Log("Creating Namespace Manager")
manager, err := NewNamespaceManager()
if !assert.Nil(t, err, "NewNamespaceManager returned error") {
return
}
gns := NewNamespace(testNameSpace)
err = manager.RegisterNamespace(gns)
if !assert.Nil(t, err, "RegisterNamespace returned error") {
return
}
fns, err := manager.FetchNamespace(testNameSpace)
if !assert.Nil(t, err, "FetchNamespace returned error") {
return
}
assert.Equal(t, gns.name, fns.name, "namespace identifiers did not match")
}
var SimpleElementNames = []rune("abcdefghijklmnopqrstuvwxyz01234567890-_")
var ComplexElementNames = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890!@#$%^&*()-_+= ;.,<>'\"[]{}() ")
// randomize path depth - will massive reduce overall path generation
// and produced a 'jagged' namespace
var randomLayout = false
const linearCreate = false // generate path elements as they are named, or do as concurrent batch
var maxTestElementNameSize = 32
var maxTestPathDepth = 5 // careful changing this one, since its the primary exponent
var maxTestPathWidth = 5
// how many trailing 'files' to randomly generate - this can be quite high since it is not an exponent
var maxTestPathEnds = 7
var created []string
func populateNamespaceWithRandomFiles(t *testing.T) {
gns := createTestNamespace(t)
err := gns.GenerateRandomPaths("/", 0)
assert.Nil(t, err, "randomized pathelement creation failed")
var i int
var p string
if linearCreate == false {
t.Logf("concurrently creating %d absolute paths", len(created))
eg := errgroup.Group{}
for i, p = range created { // over 8192 goroutines and the race condition tester dies
e := p
eg.Go(func() error {
return gns.RegisterAbsolutePath(PathString(e).ToAbsolutePath())
})
}
err := eg.Wait()
if !assert.Nil(t, err, "concurrent creation of path elements did not succeed") {
t.Error(err.Error())
}
}
t.Logf("check for successful creation of %d absolute paths", len(created))
for i, p = range created {
elem := gns.FetchAbsolutePath(PathString(p))
if !assert.NotNil(t, elem, "created element was not returned") {
t.Errorf("lost registered path element: %s after %d matches", p, i-1)
return
}
}
t.Logf("tested %d random path elements successfully", i)
}
func (ns *Namespace) GenerateRandomPaths(root string, startdepth int) (err error) {
numfiles := maxTestPathEnds
if randomLayout {
numfiles = randid.Intn(numfiles) + 1
}
for i := 0; i < numfiles; i++ {
if err = ns.createRandomPathElement(root); err != nil {
return err
}
}
if startdepth+1 <= maxTestPathDepth {
numdirs := maxTestPathWidth
if randomLayout {
numdirs = randid.Intn(numdirs) + 1
}
for i := 0; i < numdirs; i++ {
if randomLayout {
if randid.Intn(2) != 0 { // randomize created or not
if err = ns.randomAbsolutePath(root, startdepth+1); err != nil {
return err
}
}
} else {
if err = ns.randomAbsolutePath(root, startdepth+1); err != nil {
return err
}
}
}
}
return err
}
func (ns *Namespace) randomPathName(length int, alphabet []rune) string {
b := make([]rune, length)
for i := range b {
b[i] = alphabet[randid.Intn(len(alphabet))]
}
return string(b)
}
func (ns *Namespace) randomAbsolutePath(root string, depth int) (err error) {
if depth > maxTestPathDepth {
return
}
n := randid.Intn(maxTestElementNameSize-4) + 4
name := ns.randomPathName(n, SimpleElementNames)
root = path.Join(root, name)
return ns.GenerateRandomPaths(root, depth)
}
func (ns *Namespace) createRandomPathElement(root string) error {
name := ns.randomPathName(randid.Intn(8), SimpleElementNames)
filepath := path.Join(root, name)
if len(created) < 1000 { // limit it to 1000 to accomodate the race condition detector
created = append(created, filepath)
}
if linearCreate {
return ns.RegisterAbsolutePath(PathString(filepath).ToAbsolutePath())
} else {
return nil
}
}