| 1 | package g8 |
| 2 | |
| 3 | import ( |
| 4 | "sync" |
| 5 | "testing" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/TwiN/gocache/v2" |
| 9 | ) |
| 10 | |
| 11 | var ( |
| 12 | getClientByTokenFunc = func(token string) *Client { |
| 13 | if token == "valid-token" { |
| 14 | return NewClient("valid-token").WithData("client-data") |
| 15 | } |
| 16 | return nil |
| 17 | } |
| 18 | ) |
| 19 | |
| 20 | func TestClientProvider_GetClientByToken(t *testing.T) { |
| 21 | provider := NewClientProvider(getClientByTokenFunc) |
| 22 | if client := provider.GetClientByToken("valid-token"); client == nil { |
| 23 | t.Error("should've returned a client") |
| 24 | } else if client.Data != "client-data" { |
| 25 | t.Error("expected client data to be 'client-data', got", client.Data) |
| 26 | } |
| 27 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 28 | t.Error("should've returned nil") |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | func TestClientProvider_WithCache(t *testing.T) { |
| 33 | provider := NewClientProvider(getClientByTokenFunc).WithCache(gocache.NoExpiration, 10000) |
| 34 | if provider.cache.(*gocache.Cache).Count() != 0 { |
| 35 | t.Error("expected cache to be empty") |
| 36 | } |
| 37 | if client := provider.GetClientByToken("valid-token"); client == nil { |
| 38 | t.Error("expected client, got nil") |
| 39 | } |
| 40 | if provider.cache.(*gocache.Cache).Count() != 1 { |
| 41 | t.Error("expected cache size to be 1") |
| 42 | } |
| 43 | if client := provider.GetClientByToken("valid-token"); client == nil { |
| 44 | t.Error("expected client, got nil") |
| 45 | } |
| 46 | if provider.cache.(*gocache.Cache).Count() != 1 { |
| 47 | t.Error("expected cache size to be 1") |
| 48 | } |
| 49 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 50 | t.Error("expected nil, got", client) |
| 51 | } |
| 52 | if provider.cache.(*gocache.Cache).Count() != 2 { |
| 53 | t.Error("expected cache size to be 2") |
| 54 | } |
| 55 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 56 | t.Error("expected nil, got", client) |
| 57 | } |
| 58 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 59 | t.Error("should've returned nil (cached)") |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func TestClientProvider_WithCacheAndExpiration(t *testing.T) { |
| 64 | provider := NewClientProvider(getClientByTokenFunc).WithCache(10*time.Millisecond, 10) |
| 65 | provider.GetClientByToken("token") |
| 66 | if provider.cache.(*gocache.Cache).Count() != 1 { |
| 67 | t.Error("expected cache size to be 1") |
| 68 | } |
| 69 | if provider.cache.(*gocache.Cache).Stats().ExpiredKeys != 0 { |
| 70 | t.Error("expected cache statistics to report 0 expired key") |
| 71 | } |
| 72 | time.Sleep(15 * time.Millisecond) |
| 73 | provider.GetClientByToken("token") |
| 74 | if provider.cache.(*gocache.Cache).Stats().ExpiredKeys != 1 { |
| 75 | t.Error("expected cache statistics to report 1 expired key") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | type customCache struct { |
| 80 | entries map[string]any |
| 81 | sync.Mutex |
| 82 | } |
| 83 | |
| 84 | func (c *customCache) Get(key string) (value any, exists bool) { |
| 85 | c.Lock() |
| 86 | v, exists := c.entries[key] |
| 87 | c.Unlock() |
| 88 | return v, exists |
| 89 | } |
| 90 | |
| 91 | func (c *customCache) Set(key string, value any) { |
| 92 | c.Lock() |
| 93 | if c.entries == nil { |
| 94 | c.entries = make(map[string]any) |
| 95 | } |
| 96 | c.entries[key] = value |
| 97 | c.Unlock() |
| 98 | } |
| 99 | |
| 100 | var _ Cache = (*customCache)(nil) |
| 101 | |
| 102 | func TestClientProvider_WithCustomCache(t *testing.T) { |
| 103 | provider := NewClientProvider(getClientByTokenFunc).WithCustomCache(&customCache{}) |
| 104 | if len(provider.cache.(*customCache).entries) != 0 { |
| 105 | t.Error("expected cache to be empty") |
| 106 | } |
| 107 | if client := provider.GetClientByToken("valid-token"); client == nil { |
| 108 | t.Error("expected client, got nil") |
| 109 | } |
| 110 | if len(provider.cache.(*customCache).entries) != 1 { |
| 111 | t.Error("expected cache size to be 1") |
| 112 | } |
| 113 | if client := provider.GetClientByToken("valid-token"); client == nil { |
| 114 | t.Error("expected client, got nil") |
| 115 | } |
| 116 | if len(provider.cache.(*customCache).entries) != 1 { |
| 117 | t.Error("expected cache size to be 1") |
| 118 | } |
| 119 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 120 | t.Error("expected nil, got", client) |
| 121 | } |
| 122 | if len(provider.cache.(*customCache).entries) != 2 { |
| 123 | t.Error("expected cache size to be 2") |
| 124 | } |
| 125 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 126 | t.Error("expected nil, got", client) |
| 127 | } |
| 128 | if client := provider.GetClientByToken("invalid-token"); client != nil { |
| 129 | t.Error("should've returned nil (cached)") |
| 130 | } |
| 131 | } |