-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsession_test.go
248 lines (213 loc) · 5.3 KB
/
session_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
// MIT License
// Copyright (c) 2022 Leon Ding
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package gws
import (
"net/http"
"reflect"
"sync"
"testing"
"time"
)
// TestNewSession testing session create
func TestNewSession(t *testing.T) {
nowTime := time.Now()
uuid := uuid73()
session := &Session{
session{
id: uuid,
Values: make(Values),
CreateTime: nowTime,
ExpireTime: nowTime.Add(lifeTime),
},
}
tests := []struct {
name string
want *Session
}{
{"successful", session},
//{"fail",NewSession()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := session; !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewSession() = %v, want %v", got, tt.want)
}
})
}
}
// TestSessionExpired testing session data expire checking
func TestSessionExpired(t *testing.T) {
nowTime := time.Now()
uuid := uuid73()
type fields struct {
session session
}
tests := []struct {
name string
fields fields
want bool
}{
{"successful", fields{
session: session{
id: uuid,
Values: make(Values),
CreateTime: nowTime,
ExpireTime: nowTime.Add(lifeTime),
},
}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Session{
session: tt.fields.session,
}
// fix lock concurrent race
// https://deepsource.io/gh/auula/gws/run/5b13c99b-9101-4e4f-8197-acfd730c28a0/go/VET-V0008
if got := s.Expired(); got != tt.want {
t.Errorf("Expired() = %v, want %v", got, tt.want)
}
})
}
}
// TestNewCookie testing cookie
func TestNewCookie(t *testing.T) {
Open(DefaultRAMOptions)
tests := []struct {
name string
want *http.Cookie
}{
{"successful", NewCookie()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewCookie(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewCookie() = %v, want %v", got, tt.want)
}
})
}
}
// TestStoreFactory testing global config & global storage
func TestStoreFactory(t *testing.T) {
store := NewRAM()
StoreFactory(NewOptions(), store)
type args struct {
opt Options
store Storage
}
tests := []struct {
name string
args args
}{
{"successful", args{
NewOptions(),
store,
}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if &tt.args.store == &globalStore {
t.Error("store init fail")
}
})
}
}
// TestRAMStore testing memory storage read write remove
func TestRAMStore(t *testing.T) {
t.Log("init ram store")
nowTime := time.Now()
uuid := uuid73()
Open(DefaultRAMOptions)
t.Log("store write session data")
if globalStore.Write(&Session{
session{
id: uuid,
Values: make(Values),
CreateTime: nowTime,
ExpireTime: nowTime.Add(lifeTime),
},
}) != nil {
t.Error("save session data fail.")
}
var session Session
session.id = uuid
t.Log("store read session data")
if err := globalStore.Read(&session); err != nil {
t.Error(err)
}
t.Log("set and get session data")
if session.ID() != uuid {
t.Error("data synchronization failed")
}
t.Log("store remove session data")
if globalStore.Remove(&session) != nil {
t.Error("data synchronization failed")
}
if err := globalStore.Read(&session); err != ErrSessionNoData {
t.Error(err)
}
}
// TestSessionConcurrent testing session data concurrent safe
func TestSessionConcurrent(t *testing.T) {
session := NewSession()
session.Values["count"] = 0
var (
wg sync.WaitGroup
mux sync.Mutex
)
size := 10000
wg.Add(size)
for i := 0; i < size/2; i++ {
go func() {
mux.Lock()
if v, ok := session.Values["count"].(int); ok {
session.Values["count"] = v + 1
}
wg.Done()
mux.Unlock()
}()
go func() {
mux.Lock()
if v, ok := session.Values["count"].(int); ok {
session.Values["count"] = v + 1
}
wg.Done()
mux.Unlock()
}()
}
wg.Wait()
t.Log(session.Values["count"].(int))
}
func TestSessionInvalidate(t *testing.T) {
globalStore = NewRAM()
nowTime := time.Now()
uuid := uuid73()
session := &Session{
session{
id: uuid,
Values: make(Values),
CreateTime: nowTime,
ExpireTime: nowTime.Add(lifeTime),
},
}
// write session to storage
globalStore.Write(session)
t.Log(globalStore.(*RamStore).store)
// invalidate remove session to storage
Invalidate(session)
t.Log(globalStore.(*RamStore).store)
}