Debian Codegraph @

sieve_race_test.go

raw
1 // sieve_race_test.go - TOCTOU correctness tests and Phase 1 parallel benchmarks
2 package sieve_test
3
4 import (
5 "fmt"
6 "math/rand"
7 "sync"
8 "testing"
9
10 "github.com/opencoff/go-sieve"
11 )
12
13 // TestTOCTOU_NoDuplicateNodes spawns 100 goroutines all calling Add(sameKey, i)
14 // concurrently, then verifies Len() == 1 and the linked list has exactly 1 node.
15 func TestTOCTOU_NoDuplicateNodes(t *testing.T) {
16 const goroutines = 100
17
18 s := sieve.Must(sieve.New[string, int](goroutines))
19
20 var wg sync.WaitGroup
21 wg.Add(goroutines)
22 for i := 0; i < goroutines; i++ {
23 go func(val int) {
24 defer wg.Done()
25 s.Add("sameKey", val)
26 }(i)
27 }
28 wg.Wait()
29
30 if s.Len() != 1 {
31 t.Fatalf("expected Len()==1 after concurrent Add of same key, got %d", s.Len())
32 }
33
34 // Verify the linked list has exactly 1 node by dumping and counting entries
35 dump := s.Dump()
36 count := 0
37 for _, c := range dump {
38 if c == '\n' {
39 count++
40 }
41 }
42 // Dump format: header line + one line per node + possible trailing newline
43 // With 1 node: header\nnode_line\n => 2 newlines
44 // We just verify Len() == 1 is the definitive check above.
45
46 t.Logf("TOCTOU test passed: Len()=%d, Dump:\n%s", s.Len(), dump)
47 }
48
49 // TestTOCTOU_NoDuplicateNodes_Probe is the same test but using Probe.
50 func TestTOCTOU_NoDuplicateNodes_Probe(t *testing.T) {
51 const goroutines = 100
52
53 s := sieve.Must(sieve.New[string, int](goroutines))
54
55 var wg sync.WaitGroup
56 wg.Add(goroutines)
57 for i := 0; i < goroutines; i++ {
58 go func(val int) {
59 defer wg.Done()
60 s.Probe("sameKey", val)
61 }(i)
62 }
63 wg.Wait()
64
65 if s.Len() != 1 {
66 t.Fatalf("expected Len()==1 after concurrent Probe of same key, got %d", s.Len())
67 }
68 }
69
70 // TestTOCTOU_ManyKeys verifies no orphan nodes under concurrent Add.
71 // Uses a cache large enough for all keys to avoid eviction, isolating
72 // the TOCTOU fix from unrelated stale-pointer races during eviction.
73 func TestTOCTOU_ManyKeys(t *testing.T) {
74 const (
75 keyRange = 256
76 cacheSize = 512 // larger than keyRange, no eviction
77 goroutines = 100
78 opsPerG = 1000
79 )
80
81 s := sieve.Must(sieve.New[int, int](cacheSize))
82
83 var wg sync.WaitGroup
84 wg.Add(goroutines)
85 for g := 0; g < goroutines; g++ {
86 go func() {
87 defer wg.Done()
88 r := rand.New(rand.NewSource(rand.Int63()))
89 for i := 0; i < opsPerG; i++ {
90 key := r.Intn(keyRange)
91 s.Add(key, key)
92 }
93 }()
94 }
95 wg.Wait()
96
97 if s.Len() > keyRange {
98 t.Fatalf("Len()=%d exceeds keyRange %d — orphan nodes from TOCTOU", s.Len(), keyRange)
99 }
100 }
101
102 // BenchmarkGet_Parallel measures concurrent read throughput.
103 func BenchmarkGet_Parallel(b *testing.B) {
104 const cacheSize = 8192
105 s := sieve.Must(sieve.New[int, int](cacheSize))
106
107 // Pre-fill the cache
108 for i := 0; i < cacheSize; i++ {
109 s.Add(i, i)
110 }
111
112 b.ResetTimer()
113 b.RunParallel(func(pb *testing.PB) {
114 r := rand.New(rand.NewSource(rand.Int63()))
115 for pb.Next() {
116 s.Get(r.Intn(cacheSize))
117 }
118 })
119 }
120
121 // BenchmarkAdd_Parallel measures concurrent write throughput.
122 func BenchmarkAdd_Parallel(b *testing.B) {
123 const cacheSize = 8192
124 s := sieve.Must(sieve.New[int, int](cacheSize))
125
126 b.ResetTimer()
127 b.RunParallel(func(pb *testing.PB) {
128 r := rand.New(rand.NewSource(rand.Int63()))
129 for pb.Next() {
130 key := r.Intn(cacheSize * 2)
131 s.Add(key, key)
132 }
133 })
134 }
135
136 // BenchmarkMixed_Parallel measures 60% Get / 30% Add / 10% Delete.
137 func BenchmarkMixed_Parallel(b *testing.B) {
138 const cacheSize = 8192
139 s := sieve.Must(sieve.New[int, int](cacheSize))
140
141 // Pre-fill
142 for i := 0; i < cacheSize; i++ {
143 s.Add(i, i)
144 }
145
146 b.ResetTimer()
147 b.RunParallel(func(pb *testing.PB) {
148 r := rand.New(rand.NewSource(rand.Int63()))
149 for pb.Next() {
150 key := r.Intn(cacheSize * 2)
151 op := r.Intn(10)
152 if op < 6 {
153 s.Get(key)
154 } else if op < 9 {
155 s.Add(key, key)
156 } else {
157 s.Delete(key)
158 }
159 }
160 })
161 }
162
163 // BenchmarkProbe_Parallel measures concurrent Probe (insert-if-absent) throughput.
164 func BenchmarkProbe_Parallel(b *testing.B) {
165 const cacheSize = 8192
166 s := sieve.Must(sieve.New[int, int](cacheSize))
167
168 // Pre-fill half the cache so Probe sees a mix of hits and misses
169 for i := 0; i < cacheSize/2; i++ {
170 s.Add(i, i)
171 }
172
173 b.ResetTimer()
174 b.RunParallel(func(pb *testing.PB) {
175 r := rand.New(rand.NewSource(rand.Int63()))
176 for pb.Next() {
177 key := r.Intn(cacheSize * 2)
178 s.Probe(key, key)
179 }
180 })
181 }
182
183 // BenchmarkDelete_Parallel measures concurrent Delete throughput.
184 func BenchmarkDelete_Parallel(b *testing.B) {
185 const cacheSize = 8192
186 s := sieve.Must(sieve.New[int, int](cacheSize))
187
188 // Pre-fill
189 for i := 0; i < cacheSize; i++ {
190 s.Add(i, i)
191 }
192
193 b.ResetTimer()
194 b.RunParallel(func(pb *testing.PB) {
195 r := rand.New(rand.NewSource(rand.Int63()))
196 for pb.Next() {
197 key := r.Intn(cacheSize * 2)
198 if !s.Delete(key) {
199 // Re-add so future deletes can succeed
200 s.Add(key, key)
201 }
202 }
203 })
204 }
205
206 // BenchmarkAdd_ContentionStorm hammers a small key set from many goroutines.
207 func BenchmarkAdd_ContentionStorm(b *testing.B) {
208 for _, keyRange := range []int{1, 4, 16, 64} {
209 b.Run(fmt.Sprintf("Keys_%d", keyRange), func(b *testing.B) {
210 s := sieve.Must(sieve.New[int, int](1024))
211
212 b.ResetTimer()
213 b.RunParallel(func(pb *testing.PB) {
214 r := rand.New(rand.NewSource(rand.Int63()))
215 for pb.Next() {
216 key := r.Intn(keyRange)
217 s.Add(key, key)
218 }
219 })
220
221 // After benchmark, verify no orphan nodes
222 if s.Len() > keyRange && s.Len() > 1024 {
223 b.Fatalf("Len()=%d exceeds max(keyRange=%d, cap=1024) — orphan nodes", s.Len(), keyRange)
224 }
225 })
226 }
227 }
228
229 // TestTOCTOU_NoDuplicateNodes_ProbeFunc is the same test but using
230 // ProbeFunc. mk runs outside the lock, so several goroutines may
231 // construct a value; exactly one must be stored and every caller must
232 // receive that same stored value.
233 func TestTOCTOU_NoDuplicateNodes_ProbeFunc(t *testing.T) {
234 const goroutines = 100
235
236 s := sieve.Must(sieve.New[string, int](goroutines))
237
238 var wg sync.WaitGroup
239 var mu sync.Mutex
240 seen := make(map[int]int)
241
242 wg.Add(goroutines)
243 for i := 0; i < goroutines; i++ {
244 go func(val int) {
245 defer wg.Done()
246 v, _, _ := s.ProbeFunc("sameKey", func(string) int { return val })
247 mu.Lock()
248 seen[v]++
249 mu.Unlock()
250 }(i)
251 }
252 wg.Wait()
253
254 if s.Len() != 1 {
255 t.Fatalf("expected Len()==1 after concurrent ProbeFunc of same key, got %d", s.Len())
256 }
257 if len(seen) != 1 {
258 t.Fatalf("expected every caller to see the same value, got %d distinct: %v", len(seen), seen)
259 }
260 stored, _ := s.Get("sameKey")
261 for v := range seen {
262 if v != stored {
263 t.Fatalf("callers saw %d but cache holds %d", v, stored)
264 }
265 }
266 }
267
268 // BenchmarkProbeFunc_Parallel measures concurrent ProbeFunc (lazy
269 // insert-if-absent) throughput. Same setup as BenchmarkProbe_Parallel;
270 // the constructor closure captures the key so escape analysis is
271 // exercised under realistic use.
272 func BenchmarkProbeFunc_Parallel(b *testing.B) {
273 const cacheSize = 8192
274 s := sieve.Must(sieve.New[int, int](cacheSize))
275
276 // Pre-fill half the cache so ProbeFunc sees a mix of hits and misses
277 for i := 0; i < cacheSize/2; i++ {
278 s.Add(i, i)
279 }
280
281 b.ResetTimer()
282 b.RunParallel(func(pb *testing.PB) {
283 r := rand.New(rand.NewSource(rand.Int63()))
284 for pb.Next() {
285 key := r.Intn(cacheSize * 2)
286 s.ProbeFunc(key, func(k int) int { return k + key })
287 }
288 })
289 }