-
Notifications
You must be signed in to change notification settings - Fork 0
/
authcache_test.go
105 lines (90 loc) · 2.11 KB
/
authcache_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
package httpclient
import (
"fmt"
"math/rand"
"net/url"
"sort"
"testing"
"time"
)
var _ = fmt.Println
var ordered = AuthPaths{
{Path: "/1/2/3/4/5/", Auth: "a"},
{Path: "/1/2/3/4/5", Auth: "b"},
{Path: "/1/2/3/4", Auth: "c"},
{Path: "/1/2/3", Auth: "d"},
{Path: "/1/2", Auth: "e"},
{Path: "/1", Auth: "f"},
{Path: "/", Auth: "g"},
}
func TestAuthPathsSort(t *testing.T) {
set := unorderedAuthPaths()
sort.Sort(set)
for i, v := range set {
if v.Path != ordered[i].Path {
t.Errorf("%d: expected %v, got %v\n", i, ordered[i], v)
}
}
}
type authPathMatchTest struct {
Path string
Test string
Expect bool
Explain string
}
var authPathMatchTests = []authPathMatchTest{
{"/1/2/3/4/5/", "/1/2/3/4/5.1", false, "Test path base is /1/2/3/4/, not /1/2/3/4/5/"},
{"/1/2/3/4/5/", "/1/2/3/4/5/1.1", true, "Test path base is /1/2/3/4/5/"},
{"/1/2/3", "/1/2/3/4", true, "Test path extends /1/2/3"},
}
func TestAuthPathMatches(t *testing.T) {
for i, v := range authPathMatchTests {
ap := AuthPath{Path: v.Path}
if v.Expect != ap.Matches(v.Test) {
t.Errorf("%d: %s matches %s returned %v, expected %v (%s)",
i, v.Test, v.Path, !v.Expect, v.Expect, v.Explain)
}
}
}
func BenchmarkAuthPathsSort(b *testing.B) {
for i := 0; i < b.N; i++ {
b.StopTimer()
set := unorderedAuthPaths()
b.StartTimer()
sort.Sort(set)
}
}
func BenchmarkAuthPathsSet(b *testing.B) {
ops := 10
uris := make([]*url.URL, ops)
auth := make([]string, ops)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
n := len(ordered)
for i := 0; i < ops; i++ {
j := r.Intn(n)
uri, err := url.Parse(fmt.Sprintf("http://example.com%s", ordered[j].Path))
if err != nil {
b.Error(err)
}
uris[i] = uri
auth[i] = ordered[j].Auth
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
cache := NewAuthCache()
for j := 0; j < ops; j++ {
cache.Set(uris[j], auth[j])
}
}
}
func unorderedAuthPaths() AuthPaths {
set := make(AuthPaths, len(ordered))
copy(set, ordered)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
n := len(set)
for i := 0; i < n; i++ {
j := r.Intn(n)
set[i], set[j] = set[j], set[i]
}
return set
}