-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
clientprovider_test.go
129 lines (118 loc) · 3.71 KB
/
clientprovider_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
package g8
import (
"sync"
"testing"
"time"
"github.com/TwiN/gocache/v2"
)
var (
getClientByTokenFunc = func(token string) *Client {
if token == "valid-token" {
return &Client{Token: token}
}
return nil
}
)
func TestClientProvider_GetClientByToken(t *testing.T) {
provider := NewClientProvider(getClientByTokenFunc)
if client := provider.GetClientByToken("valid-token"); client == nil {
t.Error("should've returned a client")
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("should've returned nil")
}
}
func TestClientProvider_WithCache(t *testing.T) {
provider := NewClientProvider(getClientByTokenFunc).WithCache(gocache.NoExpiration, 10000)
if provider.cache.(*gocache.Cache).Count() != 0 {
t.Error("expected cache to be empty")
}
if client := provider.GetClientByToken("valid-token"); client == nil {
t.Error("expected client, got nil")
}
if provider.cache.(*gocache.Cache).Count() != 1 {
t.Error("expected cache size to be 1")
}
if client := provider.GetClientByToken("valid-token"); client == nil {
t.Error("expected client, got nil")
}
if provider.cache.(*gocache.Cache).Count() != 1 {
t.Error("expected cache size to be 1")
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("expected nil, got", client)
}
if provider.cache.(*gocache.Cache).Count() != 2 {
t.Error("expected cache size to be 2")
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("expected nil, got", client)
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("should've returned nil (cached)")
}
}
func TestClientProvider_WithCacheAndExpiration(t *testing.T) {
provider := NewClientProvider(getClientByTokenFunc).WithCache(10*time.Millisecond, 10)
provider.GetClientByToken("token")
if provider.cache.(*gocache.Cache).Count() != 1 {
t.Error("expected cache size to be 1")
}
if provider.cache.(*gocache.Cache).Stats().ExpiredKeys != 0 {
t.Error("expected cache statistics to report 0 expired key")
}
time.Sleep(15 * time.Millisecond)
provider.GetClientByToken("token")
if provider.cache.(*gocache.Cache).Stats().ExpiredKeys != 1 {
t.Error("expected cache statistics to report 1 expired key")
}
}
type customCache struct {
entries map[string]any
sync.Mutex
}
func (c *customCache) Get(key string) (value any, exists bool) {
c.Lock()
v, exists := c.entries[key]
c.Unlock()
return v, exists
}
func (c *customCache) Set(key string, value any) {
c.Lock()
if c.entries == nil {
c.entries = make(map[string]any)
}
c.entries[key] = value
c.Unlock()
}
var _ Cache = (*customCache)(nil)
func TestClientProvider_WithCustomCache(t *testing.T) {
provider := NewClientProvider(getClientByTokenFunc).WithCustomCache(&customCache{})
if len(provider.cache.(*customCache).entries) != 0 {
t.Error("expected cache to be empty")
}
if client := provider.GetClientByToken("valid-token"); client == nil {
t.Error("expected client, got nil")
}
if len(provider.cache.(*customCache).entries) != 1 {
t.Error("expected cache size to be 1")
}
if client := provider.GetClientByToken("valid-token"); client == nil {
t.Error("expected client, got nil")
}
if len(provider.cache.(*customCache).entries) != 1 {
t.Error("expected cache size to be 1")
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("expected nil, got", client)
}
if len(provider.cache.(*customCache).entries) != 2 {
t.Error("expected cache size to be 2")
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("expected nil, got", client)
}
if client := provider.GetClientByToken("invalid-token"); client != nil {
t.Error("should've returned nil (cached)")
}
}