-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
227 lines (186 loc) · 6.69 KB
/
cache_test.go
File metadata and controls
227 lines (186 loc) · 6.69 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package traverse
import (
"fmt"
"testing"
)
// TestNoOpCache tests the NoOpCache implementation.
func TestNoOpCache(t *testing.T) {
cache := &NoOpCache{}
// Get should always return nil, false
if metadata, found := cache.Get("any-key"); found || metadata != nil {
t.Errorf("NoOpCache.Get() should return (nil, false), got (%v, %v)", metadata, found)
}
// Set should be no-op
testMetadata := &Metadata{}
cache.Set("key", testMetadata)
// Get should still return nil, false
if metadata, found := cache.Get("key"); found || metadata != nil {
t.Errorf("NoOpCache.Get() after Set() should still return (nil, false), got (%v, %v)", metadata, found)
}
// Clear should be no-op
cache.Clear()
}
// TestMemoryCache tests the MemoryCache implementation.
func TestMemoryCache(t *testing.T) {
cache := NewMemoryCache()
// Initially empty
if metadata, found := cache.Get("key1"); found || metadata != nil {
t.Errorf("MemoryCache.Get() on empty cache should return (nil, false), got (%v, %v)", metadata, found)
}
// Set and Get
testMetadata := &Metadata{
EntityTypes: []EntityType{
{
Name: "TestEntity",
Properties: []Property{
{Name: "ID", Type: "Edm.String"},
},
},
},
}
cache.Set("key1", testMetadata)
if metadata, found := cache.Get("key1"); !found || metadata == nil {
t.Errorf("MemoryCache.Get() after Set() should return (metadata, true), got (nil, %v)", found)
} else if metadata.EntityTypes[0].Name != "TestEntity" {
t.Errorf("MemoryCache.Get() returned different metadata, got Name=%s", metadata.EntityTypes[0].Name)
}
// Multiple entries
testMetadata2 := &Metadata{EntityTypes: []EntityType{{Name: "AnotherEntity"}}}
cache.Set("key2", testMetadata2)
if metadata, found := cache.Get("key2"); !found || metadata.EntityTypes[0].Name != "AnotherEntity" {
t.Errorf("MemoryCache.Get(key2) failed, expected AnotherEntity")
}
// Original key should still be there
if metadata, found := cache.Get("key1"); !found || metadata.EntityTypes[0].Name != "TestEntity" {
t.Errorf("MemoryCache.Get(key1) after Set(key2) should still return TestEntity")
}
// Overwrite
testMetadata3 := &Metadata{EntityTypes: []EntityType{{Name: "OverwrittenEntity"}}}
cache.Set("key1", testMetadata3)
if metadata, found := cache.Get("key1"); !found || metadata.EntityTypes[0].Name != "OverwrittenEntity" {
t.Errorf("MemoryCache.Get(key1) after overwrite should return OverwrittenEntity")
}
// Clear
cache.Clear()
if metadata, found := cache.Get("key1"); found || metadata != nil {
t.Errorf("MemoryCache.Get() after Clear() should return (nil, false), got (%v, %v)", metadata, found)
}
if metadata, found := cache.Get("key2"); found || metadata != nil {
t.Errorf("MemoryCache.Get() after Clear() should return (nil, false), got (%v, %v)", metadata, found)
}
}
// TestMemoryCacheConcurrency tests that MemoryCache is safe for concurrent use.
func TestMemoryCacheConcurrency(t *testing.T) {
cache := NewMemoryCache()
// Concurrent writes
done := make(chan bool, 10)
for i := 0; i < 10; i++ {
go func(idx int) {
metadata := &Metadata{EntityTypes: []EntityType{{Name: fmt.Sprintf("Entity%d", idx)}}}
cache.Set("key", metadata)
done <- true
}(i)
}
for i := 0; i < 10; i++ {
<-done
}
// Should have last value
if metadata, found := cache.Get("key"); !found || metadata == nil {
t.Errorf("MemoryCache.Get() after concurrent writes should return a value")
}
// Concurrent reads and writes
for i := 0; i < 10; i++ {
go func() {
cache.Get("key")
done <- true
}()
go func() {
cache.Set("key", &Metadata{})
done <- true
}()
}
for i := 0; i < 20; i++ {
<-done
}
t.Logf("Concurrency test passed")
}
// TestClientWithMemoryCache tests Client integration with MemoryCache.
func TestClientWithMemoryCache(t *testing.T) {
cache := NewMemoryCache()
// Create client with cache
client, err := New(
WithBaseURL("http://example.com"),
WithMetadataCache(cache),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
// Verify cache is set
if client.metadataCache == nil {
t.Fatalf("Client.metadataCache is nil")
}
if _, ok := client.metadataCache.(*MemoryCache); !ok {
t.Fatalf("Client.metadataCache is not MemoryCache")
}
// Verify metadata caching interface
testMetadata := &Metadata{EntityTypes: []EntityType{{Name: "TestEntity"}}}
client.metadataCache.Set("test-key", testMetadata)
if cached, found := client.metadataCache.Get("test-key"); !found {
t.Fatalf("Metadata cache lookup failed")
} else if cached.EntityTypes[0].Name != "TestEntity" {
t.Fatalf("Cached metadata is incorrect")
}
}
// TestClientWithNoOpCacheDefault tests that Client uses NoOpCache by default.
func TestClientWithNoOpCacheDefault(t *testing.T) {
client, err := New(
WithBaseURL("http://example.com"),
)
if err != nil {
t.Fatalf("Failed to create client: %v", err)
}
// Verify default cache is NoOpCache
if _, ok := client.metadataCache.(*NoOpCache); !ok {
t.Fatalf("Default Client.metadataCache should be NoOpCache, got %T", client.metadataCache)
}
// Verify it's truly a no-op
testMetadata := &Metadata{EntityTypes: []EntityType{{Name: "TestEntity"}}}
client.metadataCache.Set("test-key", testMetadata)
if cached, found := client.metadataCache.Get("test-key"); found {
t.Fatalf("NoOpCache should not cache, but found entry: %v", cached)
}
}
// TestInMemoryResponseCache_GetMissReturnsNil verifies that Get returns
// (nil, false) when the key has never been stored.
func TestInMemoryResponseCache_GetMissReturnsNil(t *testing.T) {
c := NewInMemoryResponseCache().(*inMemoryResponseCache)
entry, ok := c.Get("missing")
if ok || entry != nil {
t.Errorf("Get(missing) = (%v, %v), want (nil, false)", entry, ok)
}
}
// TestInMemoryResponseCache_SetAndGet verifies a stored entry is retrievable.
func TestInMemoryResponseCache_SetAndGet(t *testing.T) {
c := NewInMemoryResponseCache().(*inMemoryResponseCache)
want := &ResponseCacheEntry{Body: []byte(`{"value":[]}`), ETag: `"abc123"`}
c.Set("key1", want, 0)
got, ok := c.Get("key1")
if !ok {
t.Fatal("Get after Set returned false")
}
if string(got.Body) != string(want.Body) || got.ETag != want.ETag {
t.Errorf("Get returned %+v, want %+v", got, want)
}
}
// TestInMemoryResponseCache_CorruptedEntryReturnsNil verifies that the
// comma-ok type assertion in Get handles a corrupted sync.Map entry gracefully
// instead of panicking.
func TestInMemoryResponseCache_CorruptedEntryReturnsNil(t *testing.T) {
c := NewInMemoryResponseCache().(*inMemoryResponseCache)
// Manually store a value of the wrong type to simulate corruption.
c.m.Store("bad-key", "not-a-*ResponseCacheEntry")
entry, ok := c.Get("bad-key")
if ok || entry != nil {
t.Errorf("Get with wrong type = (%v, %v), want (nil, false)", entry, ok)
}
}