-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_integration_test.go
More file actions
325 lines (269 loc) · 8.03 KB
/
cache_integration_test.go
File metadata and controls
325 lines (269 loc) · 8.03 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package itt
import (
"context"
"testing"
"time"
)
// TestCache_FullAnalysis verifies cache hit/miss behavior for Snapshot.Analyze()
func TestCache_FullAnalysis(t *testing.T) {
cfg := &Builder{
threshold: 1.5,
channelSize: 10,
detectabilityAlpha: 0.05,
}
cfg = cfg.WithCache(1 * time.Minute)
engine, err := cfg.Build()
if err != nil {
t.Fatalf("build failed: %v", err)
}
if err := engine.Start(context.Background()); err != nil {
t.Fatalf("start failed: %v", err)
}
defer engine.Stop()
// Submit events
for i := 0; i < 10; i++ {
engine.AddEvent(Event{
Source: "A",
Target: "B",
Weight: float64(i+1) * 0.1,
Type: "test",
})
}
time.Sleep(50 * time.Millisecond) // allow processing
// First analysis (cache miss)
snap1 := engine.Snapshot()
defer snap1.Close()
start := time.Now()
result1, err := snap1.Analyze()
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
duration1 := time.Since(start)
// Second analysis on same snapshot (cache hit)
start = time.Now()
result2, err := snap1.Analyze()
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
duration2 := time.Since(start)
// Verify results match
if len(result1.Tensions) != len(result2.Tensions) {
t.Fatalf("tension count mismatch: %d vs %d", len(result1.Tensions), len(result2.Tensions))
}
// Cache hit should be significantly faster
if duration2 >= duration1 {
t.Logf("WARNING: cache hit not faster (miss=%v, hit=%v)", duration1, duration2)
}
// Verify cache stats
stats := engine.ResultsCache.Stats()
if stats.Hits < 1 {
t.Fatalf("expected at least 1 cache hit, got %d", stats.Hits)
}
t.Logf("Cache stats: entries=%d, hits=%d, misses=%d, hit_rate=%.2f%%",
stats.Entries, stats.Hits, stats.Misses, stats.HitRate*100)
}
// TestCache_NodeAnalysis verifies cache for AnalyzeNode()
func TestCache_NodeAnalysis(t *testing.T) {
cfg := &Builder{
threshold: 1.5,
channelSize: 10,
detectabilityAlpha: 0.05,
}
cfg = cfg.WithCache(1 * time.Minute)
engine, err := cfg.Build()
if err != nil {
t.Fatalf("build failed: %v", err)
}
if err := engine.Start(context.Background()); err != nil {
t.Fatalf("start failed: %v", err)
}
defer engine.Stop()
// Submit events
engine.AddEvent(Event{Source: "A", Target: "B", Weight: 0.5, Type: "test"})
engine.AddEvent(Event{Source: "B", Target: "C", Weight: 0.5, Type: "test"})
engine.AddEvent(Event{Source: "A", Target: "C", Weight: 0.5, Type: "test"})
time.Sleep(50 * time.Millisecond)
snap := engine.Snapshot()
defer snap.Close()
// First call (miss)
result1, err := snap.AnalyzeNode("A")
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
// Second call (hit)
result2, err := snap.AnalyzeNode("A")
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
// Verify results match
if result1.NodeID != result2.NodeID || result1.Tension != result2.Tension {
t.Fatalf("cached result mismatch")
}
stats := engine.ResultsCache.Stats()
if stats.Hits < 1 {
t.Fatalf("expected at least 1 cache hit, got %d", stats.Hits)
}
}
// TestCache_RegionAnalysis verifies cache for AnalyzeRegion()
func TestCache_RegionAnalysis(t *testing.T) {
cfg := &Builder{
threshold: 1.5,
channelSize: 10,
detectabilityAlpha: 0.05,
}
cfg = cfg.WithCache(1 * time.Minute)
engine, err := cfg.Build()
if err != nil {
t.Fatalf("build failed: %v", err)
}
if err := engine.Start(context.Background()); err != nil {
t.Fatalf("start failed: %v", err)
}
defer engine.Stop()
// Submit events
for _, pair := range [][2]string{{"A", "B"}, {"B", "C"}, {"C", "D"}, {"A", "D"}} {
engine.AddEvent(Event{
Source: pair[0],
Target: pair[1],
Weight: 0.5,
Type: "test",
})
}
time.Sleep(50 * time.Millisecond)
snap := engine.Snapshot()
defer snap.Close()
nodeIDs := []string{"A", "B", "C"}
// First call (miss)
result1, err := snap.AnalyzeRegion(nodeIDs)
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
// Second call (hit)
result2, err := snap.AnalyzeRegion(nodeIDs)
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
// Verify results match
if len(result1.Nodes) != len(result2.Nodes) {
t.Fatalf("node count mismatch: %d vs %d", len(result1.Nodes), len(result2.Nodes))
}
// Test with reordered nodeIDs (should still hit cache due to sorting)
nodeIDsReordered := []string{"C", "A", "B"}
result3, err := snap.AnalyzeRegion(nodeIDsReordered)
if err != nil {
t.Fatalf("analyze failed: %v", err)
}
if len(result3.Nodes) != len(result1.Nodes) {
t.Fatalf("reordered query returned different result")
}
stats := engine.ResultsCache.Stats()
if stats.Hits < 2 {
t.Fatalf("expected at least 2 cache hits, got %d", stats.Hits)
}
}
// TestCache_Invalidation verifies cache invalidation on version removal
func TestCache_Invalidation(t *testing.T) {
cfg := &Builder{
threshold: 1.5,
channelSize: 10,
detectabilityAlpha: 0.05,
}
cfg = cfg.WithCache(1 * time.Minute)
cfg = cfg.GCSnapshotForce(100 * time.Millisecond) // force-close after 100ms
engine, err := cfg.Build()
if err != nil {
t.Fatalf("build failed: %v", err)
}
if err := engine.Start(context.Background()); err != nil {
t.Fatalf("start failed: %v", err)
}
defer engine.Stop()
// Submit event
engine.AddEvent(Event{Source: "A", Target: "B", Weight: 0.5, Type: "test"})
time.Sleep(50 * time.Millisecond)
// Create snapshot and analyze (populates cache)
snap := engine.Snapshot()
versionID := snap.version.ID
snap.Analyze()
snap.Close()
// Verify cache has entry
stats := engine.ResultsCache.Stats()
if stats.Entries < 1 {
t.Fatalf("expected cache entry, got %d", stats.Entries)
}
// Wait for GC to force-close and invalidate
time.Sleep(150 * time.Millisecond)
// Trigger GC manually to ensure it runs
engine.gc.Collect()
// Verify cache invalidated (may take a moment for async cleanup)
time.Sleep(50 * time.Millisecond)
// Check if version was removed (cache should have been invalidated)
// Note: This is a best-effort test since GC timing is non-deterministic
t.Logf("After GC - cache entries: %d, versionID: %d", engine.ResultsCache.Stats().Entries, versionID)
}
// TestCache_Disabled verifies engine works without cache
func TestCache_Disabled(t *testing.T) {
cfg := &Builder{
threshold: 1.5,
channelSize: 10,
detectabilityAlpha: 0.05,
}
// No WithCache() call - cache disabled
engine, err := cfg.Build()
if err != nil {
t.Fatalf("build failed: %v", err)
}
if err := engine.Start(context.Background()); err != nil {
t.Fatalf("start failed: %v", err)
}
defer engine.Stop()
engine.AddEvent(Event{Source: "A", Target: "B", Weight: 0.5, Type: "test"})
time.Sleep(50 * time.Millisecond)
snap := engine.Snapshot()
defer snap.Close()
// Should work without cache
_, err = snap.Analyze()
if err != nil {
t.Fatalf("analyze failed without cache: %v", err)
}
// Verify cache is nil
if engine.ResultsCache != nil {
t.Fatal("expected nil cache when disabled")
}
}
// TestCache_EvictionWorker verifies background eviction
func TestCache_EvictionWorker(t *testing.T) {
cfg := &Builder{
threshold: 1.5,
channelSize: 10,
detectabilityAlpha: 0.05,
}
cfg = cfg.WithCache(50 * time.Millisecond) // very short TTL
engine, err := cfg.Build()
if err != nil {
t.Fatalf("build failed: %v", err)
}
if err := engine.Start(context.Background()); err != nil {
t.Fatalf("start failed: %v", err)
}
defer engine.Stop()
engine.AddEvent(Event{Source: "A", Target: "B", Weight: 0.5, Type: "test"})
time.Sleep(50 * time.Millisecond)
snap := engine.Snapshot()
defer snap.Close()
// Populate cache
snap.Analyze()
stats := engine.ResultsCache.Stats()
if stats.Entries < 1 {
t.Fatalf("expected cache entry, got %d", stats.Entries)
}
// Wait for TTL expiration + eviction worker cycle (10s ticker)
// Note: This test is time-sensitive and may be flaky
time.Sleep(100 * time.Millisecond)
// Manual eviction to test immediately
engine.ResultsCache.EvictExpired()
stats = engine.ResultsCache.Stats()
if stats.Entries > 0 {
t.Logf("Cache still has %d entries (may not have expired yet)", stats.Entries)
}
}