-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxn_test.go
300 lines (268 loc) · 9.05 KB
/
txn_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package fox
import (
"errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tigerwill90/fox/internal/iterutil"
"net/http"
"testing"
)
func TestTxn_Truncate(t *testing.T) {
cases := []struct {
name string
routes []struct {
method string
path string
}
}{
{
name: "common verb node should have a root and no children",
routes: []struct {
method string
path string
}{
{method: http.MethodGet, path: "/foo/bar"},
{method: http.MethodGet, path: "/foo"},
{method: http.MethodPost, path: "/foo/bar"},
{method: http.MethodPost, path: "/foo"},
{method: http.MethodPut, path: "/foo/bar"},
{method: http.MethodPut, path: "/foo"},
{method: http.MethodDelete, path: "/foo/bar"},
{method: http.MethodDelete, path: "/foo"},
},
},
{
name: "not common verb should be removed entirely",
routes: []struct {
method string
path string
}{
{method: http.MethodTrace, path: "/foo/bar"},
{method: http.MethodTrace, path: "/foo"},
{method: http.MethodPost, path: "/foo/bar"},
{method: http.MethodPost, path: "/foo"},
{method: http.MethodPut, path: "/foo/bar"},
{method: http.MethodPut, path: "/foo"},
{method: http.MethodOptions, path: "/foo/bar"},
{method: http.MethodOptions, path: "/foo"},
},
},
{
name: "custom verb should be removed entirely",
routes: []struct {
method string
path string
}{
{method: http.MethodGet, path: "/foo/bar"},
{method: http.MethodGet, path: "/foo"},
{method: http.MethodPost, path: "/foo/bar"},
{method: http.MethodPost, path: "/foo"},
{method: http.MethodPut, path: "/foo/bar"},
{method: http.MethodPut, path: "/foo"},
{method: "BOULOU", path: "/foo/bar"},
{method: "BOULOU", path: "/foo"},
{method: http.MethodDelete, path: "/foo/bar"},
{method: http.MethodDelete, path: "/foo"},
{method: "ANY", path: "/foo/bar"},
{method: "ANY", path: "/foo"},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
f := New()
for _, rte := range tc.routes {
require.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
txn := f.Txn(true)
defer txn.Abort()
methods := make([]string, 0)
for _, rte := range tc.routes {
methods = append(methods, rte.method)
}
if assert.NoError(t, txn.Truncate(methods...)) {
txn.Commit()
}
tree := f.getRoot()
for _, method := range methods {
idx := tree.root.methodIndex(method)
if isRemovable(method) {
assert.Equal(t, idx, -1)
} else {
assert.Len(t, tree.root[idx].children, 0)
}
}
assert.Len(t, tree.root, len(commonVerbs))
})
}
}
func TestTxn_TruncateAll(t *testing.T) {
f := New()
require.NoError(t, onlyError(f.Handle(http.MethodGet, "/foo/bar", emptyHandler)))
require.NoError(t, onlyError(f.Handle(http.MethodPost, "/foo/bar", emptyHandler)))
require.NoError(t, onlyError(f.Handle(http.MethodDelete, "/foo/bar", emptyHandler)))
require.NoError(t, onlyError(f.Handle(http.MethodPut, "/foo/bar", emptyHandler)))
require.NoError(t, onlyError(f.Handle(http.MethodConnect, "/foo/bar", emptyHandler)))
require.NoError(t, onlyError(f.Handle(http.MethodTrace, "/foo/bar", emptyHandler)))
require.NoError(t, onlyError(f.Handle("BOULOU", "/foo/bar", emptyHandler)))
txn := f.Txn(true)
defer txn.Abort()
if assert.NoError(t, txn.Truncate()) {
txn.Commit()
}
tree := f.getRoot()
assert.Len(t, tree.root, len(commonVerbs))
for _, n := range tree.root {
assert.Len(t, n.children, 0)
}
}
func TestTxn_Isolation(t *testing.T) {
t.Run("txn iterator does not observe update once created", func(t *testing.T) {
f := New()
_ = f.Updates(func(txn *Txn) error {
assert.NoError(t, onlyError(txn.Handle(http.MethodGet, "/ab", emptyHandler)))
assert.NoError(t, onlyError(txn.Handle(http.MethodGet, "/ab/cd", emptyHandler)))
assert.NoError(t, onlyError(txn.Handle(http.MethodGet, "/ab/cd/ef", emptyHandler)))
iter := iterutil.Map(iterutil.Right(txn.Iter().All()), func(route *Route) string {
return route.Pattern()
})
patterns := make([]string, 0, 3)
for pattern := range iter {
patterns = append(patterns, pattern)
_ = onlyError(txn.Handle(http.MethodGet, "/ab/cd/ef/gh", emptyHandler))
_ = onlyError(txn.Handle(http.MethodGet, "/ab/cd/ef/gh/ij", emptyHandler))
_ = onlyError(txn.Handle(http.MethodGet, "/ab/cd/e", emptyHandler))
_ = onlyError(txn.Handle(http.MethodGet, "/ax", emptyHandler))
}
assert.Equal(t, []string{"/ab", "/ab/cd", "/ab/cd/ef"}, patterns)
patterns = make([]string, 0, 3)
for pattern := range iter {
patterns = append(patterns, pattern)
}
assert.Equal(t, []string{"/ab", "/ab/cd", "/ab/cd/ef"}, patterns)
return nil
})
})
t.Run("txn snapshot does not observe further write", func(t *testing.T) {
f := New()
_ = f.Updates(func(txn *Txn) error {
for _, rte := range staticRoutes {
assert.NoError(t, onlyError(txn.Handle(rte.method, rte.path, emptyHandler)))
}
snapshot := txn.Snapshot()
for _, rte := range githubAPI {
assert.NoError(t, onlyError(txn.Handle(rte.method, rte.path, emptyHandler)))
}
assert.False(t, snapshot.Has(http.MethodGet, "/repos/{owner}/{repo}/comments"))
assert.False(t, snapshot.Has(http.MethodGet, "/legacy/issues/search/{owner}/{repository}/{state}/{keyword}"))
assert.True(t, txn.Has(http.MethodGet, "/repos/{owner}/{repo}/comments"))
assert.True(t, txn.Has(http.MethodGet, "/legacy/issues/search/{owner}/{repository}/{state}/{keyword}"))
return nil
})
})
t.Run("read only transaction are isolated from write", func(t *testing.T) {
f := New()
for _, rte := range staticRoutes {
assert.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
want := 0
_ = f.View(func(txn *Txn) error {
want = iterutil.Len2(txn.Iter().All())
for _, rte := range githubAPI {
assert.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
assert.Equal(t, want, iterutil.Len2(txn.Iter().All()))
assert.False(t, txn.Has(http.MethodGet, "/repos/{owner}/{repo}/comments"))
assert.False(t, txn.Has(http.MethodGet, "/legacy/issues/search/{owner}/{repository}/{state}/{keyword}"))
assert.True(t, f.Has(http.MethodGet, "/legacy/issues/search/{owner}/{repository}/{state}/{keyword}"))
return nil
})
assert.Equal(t, want+len(githubAPI), iterutil.Len2(f.Iter().All()))
})
t.Run("read only transaction can run uncoordinated", func(t *testing.T) {
f := New()
for _, rte := range staticRoutes {
assert.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
txn1 := f.Txn(false)
defer txn1.Abort()
for _, rte := range githubAPI {
assert.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
txn2 := f.Txn(false)
defer txn2.Abort()
assert.Equal(t, len(staticRoutes), iterutil.Len2(txn1.Iter().All()))
assert.Equal(t, len(staticRoutes)+len(githubAPI), iterutil.Len2(txn2.Iter().All()))
})
t.Run("aborted transaction does not write anything", func(t *testing.T) {
f := New()
for _, rte := range staticRoutes {
assert.NoError(t, onlyError(f.Handle(rte.method, rte.path, emptyHandler)))
}
want := errors.New("aborted")
err := f.Updates(func(txn *Txn) error {
for _, rte := range githubAPI {
assert.NoError(t, onlyError(txn.Handle(rte.method, rte.path, emptyHandler)))
}
assert.Equal(t, len(githubAPI)+len(staticRoutes), iterutil.Len2(txn.Iter().All()))
return want
})
assert.Equal(t, err, want)
assert.Equal(t, len(staticRoutes), iterutil.Len2(f.Iter().All()))
})
t.Run("track registered route", func(t *testing.T) {
f := New()
require.NoError(t, f.Updates(func(txn *Txn) error {
for _, rte := range staticRoutes {
if _, err := txn.Handle(rte.method, "example.com"+rte.path, emptyHandler); err != nil {
return err
}
}
assert.Equal(t, len(staticRoutes), txn.Len())
for _, rte := range staticRoutes {
if err := txn.Delete(rte.method, "example.com"+rte.path); err != nil {
return err
}
}
assert.Zero(t, txn.Len())
return nil
}))
})
}
func TestTxn_WriteOnReadTransaction(t *testing.T) {
f := New()
txn := f.Txn(false)
defer txn.Abort()
assert.ErrorIs(t, onlyError(txn.Handle(http.MethodGet, "/foo", emptyHandler)), ErrReadOnlyTxn)
assert.ErrorIs(t, onlyError(txn.Update(http.MethodGet, "/foo", emptyHandler)), ErrReadOnlyTxn)
assert.ErrorIs(t, txn.Delete(http.MethodGet, "/foo"), ErrReadOnlyTxn)
assert.ErrorIs(t, txn.Truncate(), ErrReadOnlyTxn)
txn.Commit()
}
func TestTxn_WriteOrReadAfterFinalized(t *testing.T) {
f := New()
txn := f.Txn(true)
txn.Abort()
assert.Panics(t, func() {
_, _ = txn.Handle(http.MethodGet, "/foo", emptyHandler)
})
assert.Panics(t, func() {
_, _ = txn.Update(http.MethodGet, "/foo", emptyHandler)
})
assert.Panics(t, func() {
_ = txn.Delete(http.MethodGet, "/foo")
})
assert.Panics(t, func() {
txn.Has(http.MethodGet, "/foo")
})
assert.Panics(t, func() {
txn.Reverse(http.MethodGet, "host", "/foo")
})
assert.Panics(t, func() {
txn.Lookup(nil, nil)
})
assert.NotPanics(t, func() {
txn.Commit()
txn.Abort()
})
}