-
Notifications
You must be signed in to change notification settings - Fork 30
/
session_test.go
91 lines (83 loc) · 2.29 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
package golf
import (
"testing"
"time"
)
func TestMemorySessionCRUD(t *testing.T) {
cases := []struct {
key, value string
}{
{"foo", "bar"},
{"abc", "123"},
}
mgr := NewMemorySessionManager()
sid, err := mgr.sessionID()
if err != nil {
t.Errorf("Could not generate session ID.")
}
s := MemorySession{sid: sid, data: make(map[string]interface{})}
for _, c := range cases {
s.Set(c.key, c.value)
value, err := s.Get(c.key)
if value != c.value {
t.Errorf("Could not set memory sesseion k-v pair.")
}
err = s.Delete(c.key)
if err != nil {
t.Errorf("Could not delete session key.")
}
_, err = s.Get(c.key)
if err == nil {
t.Errorf("Could not correctly delete session key.")
}
}
}
func TestMemorySessionManager(t *testing.T) {
mgr := NewMemorySessionManager()
s, err := mgr.NewSession()
if err != nil {
t.Errorf("Could not create a new session.")
}
sid := s.SessionID()
newSession, _ := mgr.Session(sid)
if newSession.SessionID() != s.SessionID() {
t.Errorf("Memory session manager could not retrieve a previously generated session.")
}
}
func TestMemorySessionExpire(t *testing.T) {
mgr := NewMemorySessionManager()
sid, _ := mgr.sessionID()
s := MemorySession{sid: sid, data: make(map[string]interface{}), createdAt: time.Now().AddDate(0, 0, -1)}
mgr.sessions[sid] = &s
mgr.GarbageCollection()
_, err := mgr.Session(sid)
if err == nil {
t.Errorf("Could not correctly recycle expired sessions.")
}
}
func TestMemorySessionNotExpire(t *testing.T) {
mgr := NewMemorySessionManager()
sid, _ := mgr.sessionID()
s := MemorySession{sid: sid, data: make(map[string]interface{}), createdAt: time.Now()}
mgr.sessions[sid] = &s
mgr.GarbageCollection()
_, err := mgr.Session(sid)
if err != nil {
t.Errorf("Falsely recycled non-expired sessions.")
}
}
func TestMemorySessionCount(t *testing.T) {
mgr := NewMemorySessionManager()
for i := 0; i < 100; i++ {
sid, _ := mgr.sessionID()
s := MemorySession{sid: sid, data: make(map[string]interface{}), createdAt: time.Now().AddDate(0, 0, -1)}
mgr.sessions[sid] = &s
}
if mgr.Count() != 100 {
t.Errorf("Could not correctly get session count: %v != %v", mgr.Count(), 100)
}
mgr.GarbageCollection()
if mgr.Count() != 0 {
t.Errorf("Could not correctly get session count after GC: %v != %v", mgr.Count(), 0)
}
}