This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathcache_test.go
More file actions
154 lines (134 loc) · 3.21 KB
/
cache_test.go
File metadata and controls
154 lines (134 loc) · 3.21 KB
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
package api_test
import (
"reflect"
"strconv"
"sync"
"testing"
"koding/api"
"koding/api/apitest"
)
func TestSessionCache(t *testing.T) {
var storage apitest.TrxStorage
var auth = apitest.NewFakeAuth()
users := []*api.Session{
{User: &api.User{Username: "user1", Team: "foobar"}},
{User: &api.User{Username: "user2", Team: "foobar"}},
{User: &api.User{Username: "user3", Team: "foobar"}},
{User: &api.User{Username: "user", Team: "team"}},
}
usernames := make(map[string]struct{}, len(users))
for _, user := range users {
usernames[user.User.String()] = struct{}{}
}
cases := []struct {
name string
opts *api.AuthOptions // client
trxs []apitest.Trx // underlying cache operations
}{{
"new user1",
&api.AuthOptions{
User: users[0].User,
},
[]apitest.Trx{
{Type: "get", Session: users[0]},
{Type: "set", Session: users[0]},
},
}, {
"already cached user",
&api.AuthOptions{
User: users[0].User,
},
[]apitest.Trx{
{Type: "get", Session: users[0]},
},
}, {
"invalide session of a cached user",
&api.AuthOptions{
User: users[0].User,
Refresh: true,
},
[]apitest.Trx{
{Type: "delete", Session: users[0]},
{Type: "set", Session: users[0]},
},
}, {
"new user2",
&api.AuthOptions{
User: users[1].User,
},
[]apitest.Trx{
{Type: "get", Session: users[1]},
{Type: "set", Session: users[1]},
},
}, {
"new user3",
&api.AuthOptions{
User: users[2].User,
},
[]apitest.Trx{
{Type: "get", Session: users[2]},
{Type: "set", Session: users[2]},
},
}, {
"new user",
&api.AuthOptions{
User: users[3].User,
},
[]apitest.Trx{
{Type: "get", Session: users[3]},
{Type: "set", Session: users[3]},
},
}}
cache := api.NewCache(auth.Auth)
cache.Storage = &storage
for _, cas := range cases {
t.Run(cas.name, func(t *testing.T) {
trxID := len(storage.Trxs)
_, err := cache.Auth(cas.opts)
if err != nil {
t.Fatalf("Auth()=%s", err)
}
if err := storage.Slice(trxID).Match(cas.trxs); err != nil {
t.Fatalf("Match()=%s", err)
}
})
}
if sessions := storage.Build(); !reflect.DeepEqual(sessions, auth.Sessions) {
t.Fatalf("got %+v, want %+v", sessions, auth.Sessions)
}
if len(auth.Sessions) != len(usernames) {
t.Fatalf("want len(auth)=%d == len(allKeys)=%d", len(auth.Sessions), len(usernames))
}
for username := range usernames {
if _, ok := auth.Sessions[username]; !ok {
t.Fatalf("username %q not found in auth", username)
}
}
}
func TestSessionCacheParallel(t *testing.T) {
var storage apitest.TrxStorage
var auth = apitest.NewFakeAuth()
cache := api.NewCache(auth.Auth)
cache.Storage = &storage
const ConcurrentAuths = 10
var wg sync.WaitGroup
wg.Add(ConcurrentAuths)
for i := 0; i < ConcurrentAuths; i++ {
go func(i int) {
defer wg.Done()
cache.Auth(&api.AuthOptions{
User: &api.User{
Username: "user" + strconv.Itoa(i),
Team: "foobar" + strconv.Itoa(i),
},
})
}(i)
}
wg.Wait()
if sessions := storage.Build(); !reflect.DeepEqual(sessions, auth.Sessions) {
t.Fatalf("got %+v, want %+v", sessions, auth.Sessions)
}
if len(auth.Sessions) != ConcurrentAuths {
t.Fatalf("want len(auth)=%d; got: %d", ConcurrentAuths, len(auth.Sessions))
}
}