-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_test.go
More file actions
129 lines (111 loc) · 2.95 KB
/
session_test.go
File metadata and controls
129 lines (111 loc) · 2.95 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
package jmap
import (
"context"
"errors"
"sync"
"testing"
"time"
)
func TestNewDefaultSessionCache(t *testing.T) {
cache := NewDefaultSessionCache(5 * time.Minute)
if cache == nil {
t.Fatal("returned nil")
}
}
func TestDefaultSessionCacheGet(t *testing.T) {
t.Run("fetches on first call", func(t *testing.T) {
cache := NewDefaultSessionCache(time.Minute)
calls := 0
getter := func(_ context.Context) (Session, error) {
calls++
return Session{isSet: true, Username: "user@example.com"}, nil
}
sess, err := cache.Get(context.Background(), getter)
if err != nil {
t.Fatalf("Get() error: %v", err)
}
if sess.Username != "user@example.com" {
t.Errorf("Username = %q, want %q", sess.Username, "user@example.com")
}
if calls != 1 {
t.Errorf("getter called %d times, want 1", calls)
}
})
t.Run("returns cached on second call", func(t *testing.T) {
cache := NewDefaultSessionCache(time.Minute)
calls := 0
getter := func(_ context.Context) (Session, error) {
calls++
return Session{isSet: true, Username: "user@example.com"}, nil
}
cache.Get(context.Background(), getter)
cache.Get(context.Background(), getter)
if calls != 1 {
t.Errorf("getter called %d times, want 1", calls)
}
})
t.Run("getter error propagates", func(t *testing.T) {
cache := NewDefaultSessionCache(time.Minute)
getter := func(_ context.Context) (Session, error) {
return Session{}, errors.New("network error")
}
_, err := cache.Get(context.Background(), getter)
if err == nil {
t.Fatal("expected error")
}
})
}
func TestDefaultSessionCacheInvalidate(t *testing.T) {
cache := NewDefaultSessionCache(time.Minute)
calls := 0
getter := func(_ context.Context) (Session, error) {
calls++
return Session{isSet: true}, nil
}
cache.Get(context.Background(), getter)
cache.Invalidate(context.Background())
cache.Get(context.Background(), getter)
if calls != 2 {
t.Errorf("getter called %d times after invalidate, want 2", calls)
}
}
func TestDefaultSessionCacheTTLExpiry(t *testing.T) {
cache := NewDefaultSessionCache(10 * time.Millisecond)
calls := 0
getter := func(_ context.Context) (Session, error) {
calls++
return Session{isSet: true}, nil
}
cache.Get(context.Background(), getter)
time.Sleep(20 * time.Millisecond)
cache.Get(context.Background(), getter)
if calls != 2 {
t.Errorf("getter called %d times, want 2 (TTL expired)", calls)
}
}
func TestDefaultSessionCacheConcurrent(t *testing.T) {
cache := NewDefaultSessionCache(time.Minute)
var mu sync.Mutex
calls := 0
getter := func(_ context.Context) (Session, error) {
mu.Lock()
calls++
mu.Unlock()
return Session{isSet: true}, nil
}
var wg sync.WaitGroup
for range 20 {
wg.Add(1)
go func() {
defer wg.Done()
_, err := cache.Get(context.Background(), getter)
if err != nil {
t.Errorf("Get() error: %v", err)
}
}()
}
wg.Wait()
if calls > 5 {
t.Errorf("getter called %d times, expected much fewer with caching", calls)
}
}