-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
204 lines (148 loc) · 4.84 KB
/
cache_test.go
File metadata and controls
204 lines (148 loc) · 4.84 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
package tempuscache
import (
"sync"
"testing"
"time"
)
/*
cache_test.go provides comprehensive validation of TempusCache.
================================================================================
TESTING OBJECTIVES
================================================================================
This test suite verifies:
1. Functional Correctness
- Ensures Set(), Get(), Delete() behave deterministically.
- Confirms LRU updates do not break key retrieval.
2. Expiration Semantics
- Validates TTL-based expiration accuracy.
- Ensures expired keys are never returned.
- Confirms TTL == 0 results in non-expiring entries.
3. Concurrency Safety
- Stress-tests concurrent read/write access.
- Validates correct usage of sync.RWMutex.
- Ensures absence of race conditions and runtime panics.
4. Metrics Accuracy
- Verifies hit/miss statistics tracking.
================================================================================
CONCURRENCY VALIDATION
================================================================================
Concurrency tests are designed to simulate realistic parallel workloads.
Execution correctness must hold regardless of:
- True parallel execution (multi-core CPUs)
- Goroutine interleaving (single-core scheduling)
For full safety verification, tests should be executed with:
go test -race
The Go race detector ensures no data races occur
under concurrent access patterns.
================================================================================
ENGINEERING PHILOSOPHY
================================================================================
The goal of this suite is not only correctness,
but reliability under contention — a core requirement
for production-grade caching systems.
*/
func TestSetAndGet(t *testing.T) {
cache := New()
cache.Set("a", "b", 5*time.Second)
val, found := cache.Get("a")
if !found {
t.Fatal("expected key to be found")
}
if val != "b" {
t.Fatalf("expected 'b', got %v", val)
}
}
func TestExpiration(t *testing.T) {
cache := New()
cache.Set("a", "b", 1*time.Millisecond)
time.Sleep(2 * time.Millisecond)
_, found := cache.Get("a")
if found {
t.Fatal("expected key to be expired")
}
}
func TestNoExpiration(t *testing.T) {
cache := New()
cache.Set("a", "b", 0)
time.Sleep(2 * time.Millisecond)
val, found := cache.Get("a")
if !found || val != "b" {
t.Fatal("expected key to persist without TTL")
}
}
func TestDelete(t *testing.T) {
cache := New()
cache.Set("a", "b", 5*time.Second)
cache.Delete("a")
_, found := cache.Get("a")
if found {
t.Fatal("expected key to be deleted")
}
}
/*
TestConcurrentAccess performs a concurrency stress validation.
================================================================================
PURPOSE
================================================================================
This test ensures:
- Thread safety under simultaneous Set() and Get() operations.
- No "concurrent map writes" runtime panic.
- Correct synchronization via sync.RWMutex.
- Stability under write-read contention.
================================================================================
EXECUTION MODEL
================================================================================
- 100 goroutines are spawned.
- Each goroutine performs:
1. A write operation (Set)
2. A read operation (Get)
A sync.WaitGroup coordinates completion to ensure
all goroutines finish before the test exits.
================================================================================
WHY THIS MATTERS
================================================================================
If locking were implemented incorrectly,
this test would likely:
- Trigger race detector warnings
- Cause runtime panic
- Produce inconsistent state
Passing this test under `go test -race`
provides strong confidence in concurrency correctness.
*/
func TestConcurrentAccess(t *testing.T) {
cache := New()
var wg sync.WaitGroup //WaitGroup waits for collection of goroutines to finish.
for i := 0; i < 100; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
cache.Set("key", i, 5*time.Second)
cache.Get("key")
}(i)
}
wg.Wait() // ENsures all goroutines cpmpletes before test exits.
}
/*
TestStatsTracking verifies accuracy of runtime metrics.
It ensures:
- Cache hits increment correctly on successful retrieval.
- Cache misses increment correctly on failed lookup.
- Stats() returns a consistent snapshot under read lock.
Accurate statistics are critical for:
- Performance monitoring
- Observability
- Production diagnostics
*/
func TestStatsTracking(t *testing.T) {
cache := New()
cache.Set("a", 1, 0)
cache.Get("a") // hit
cache.Get("b") // miss
stats := cache.Stats()
if stats.Hits != 1 {
t.Fatalf("expected 1 hit, got %d", stats.Hits)
}
if stats.Misses != 1 {
t.Fatalf("expected 1 miss, got %d", stats.Misses)
}
}