-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcache_expiry_test.go
More file actions
155 lines (129 loc) · 3.5 KB
/
cache_expiry_test.go
File metadata and controls
155 lines (129 loc) · 3.5 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
155
package oauth
import (
"fmt"
"testing"
"time"
)
func TestTokenCacheExpiry(t *testing.T) {
cache := &TokenCache{
cache: make(map[string]*CachedToken),
}
user := &User{
Username: "testuser",
Email: "test@example.com",
Subject: "123",
}
// Add a token with very short expiry
tokenHash := "test-token-hash"
expiresAt := time.Now().Add(50 * time.Millisecond)
cache.setCachedToken(tokenHash, user, expiresAt)
// Should be cached immediately
cached, exists := cache.getCachedToken(tokenHash)
if !exists {
t.Error("Token should be cached immediately")
}
if cached.User.Username != "testuser" {
t.Errorf("Cached user username = %s, want testuser", cached.User.Username)
}
// Wait for expiry
time.Sleep(100 * time.Millisecond)
// Should be expired now
cached, exists = cache.getCachedToken(tokenHash)
if exists {
t.Error("Token should be expired after expiry time")
}
if cached != nil {
t.Error("Expired token should return nil for cached entry")
}
}
func TestTokenCacheMultipleEntriesExpiry(t *testing.T) {
cache := &TokenCache{
cache: make(map[string]*CachedToken),
}
user := &User{
Username: "testuser",
Email: "test@example.com",
Subject: "123",
}
// Add multiple tokens with short expiry
for i := 0; i < 10; i++ {
tokenHash := fmt.Sprintf("test-token-hash-%d", i)
expiresAt := time.Now().Add(50 * time.Millisecond)
cache.setCachedToken(tokenHash, user, expiresAt)
}
// Wait for expiry
time.Sleep(100 * time.Millisecond)
// All should be expired
for i := 0; i < 10; i++ {
tokenHash := fmt.Sprintf("test-token-hash-%d", i)
_, exists := cache.getCachedToken(tokenHash)
if exists {
t.Errorf("Token %s should be expired", tokenHash)
}
}
}
func TestTokenCacheNoExpiry(t *testing.T) {
cache := &TokenCache{
cache: make(map[string]*CachedToken),
}
user := &User{
Username: "testuser",
Email: "test@example.com",
Subject: "123",
}
// Add a token with long expiry
tokenHash := "test-token-hash"
expiresAt := time.Now().Add(1 * time.Hour)
cache.setCachedToken(tokenHash, user, expiresAt)
// Wait a bit but not past expiry
time.Sleep(10 * time.Millisecond)
// Should still be cached
cached, exists := cache.getCachedToken(tokenHash)
if !exists {
t.Error("Token should still be cached")
}
if cached.User.Username != "testuser" {
t.Errorf("Cached user username = %s, want testuser", cached.User.Username)
}
}
func TestTokenCacheOverwrite(t *testing.T) {
cache := &TokenCache{
cache: make(map[string]*CachedToken),
}
user1 := &User{
Username: "user1",
Email: "user1@example.com",
Subject: "1",
}
user2 := &User{
Username: "user2",
Email: "user2@example.com",
Subject: "2",
}
// Add first user
tokenHash := "test-token-hash"
expiresAt := time.Now().Add(1 * time.Hour)
cache.setCachedToken(tokenHash, user1, expiresAt)
// Verify first user
cached, exists := cache.getCachedToken(tokenHash)
if !exists || cached.User.Username != "user1" {
t.Error("First user should be cached")
}
// Overwrite with second user
cache.setCachedToken(tokenHash, user2, expiresAt)
// Verify second user
cached, exists = cache.getCachedToken(tokenHash)
if !exists || cached.User.Username != "user2" {
t.Error("Second user should be cached after overwrite")
}
}
func TestTokenCacheEmpty(t *testing.T) {
cache := &TokenCache{
cache: make(map[string]*CachedToken),
}
// Non-existent token should return not exists
_, exists := cache.getCachedToken("non-existent")
if exists {
t.Error("Non-existent token should not exist in cache")
}
}