-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter_test.go
More file actions
491 lines (421 loc) · 12.7 KB
/
Copy pathwriter_test.go
File metadata and controls
491 lines (421 loc) · 12.7 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
package ring
import (
"sync"
"sync/atomic"
"testing"
"time"
)
// =============================================================================
// WriteWithBackoff Tests
// =============================================================================
// TestWriteWithBackoff tests the backoff write mechanism
func TestWriteWithBackoff(t *testing.T) {
ring, err := NewShardedRing(64, 4) // Small ring: 16 per shard
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
config := WriteConfig{
MaxRetries: 5,
BackoffDuration: 10 * time.Microsecond,
MaxBackoffs: 100, // Give up after 100 backoff cycles
}
// Fill the ring completely (single shard)
for i := 0; i < 16; i++ {
if !ring.Write(0, i) {
t.Fatalf("Initial fill failed at %d", i)
}
}
// Now the shard is full - WriteWithBackoff should fail after max backoffs
// since there's no consumer draining
success := ring.WriteWithBackoff(0, 999, config)
if success {
t.Error("WriteWithBackoff should have failed on full ring with no consumer")
}
// Drain some items
for i := 0; i < 5; i++ {
ring.TryRead()
}
// Now write should succeed
success = ring.WriteWithBackoff(0, 999, config)
if !success {
t.Error("WriteWithBackoff should have succeeded after draining")
}
}
// TestWriteWithBackoffConcurrent tests backoff with concurrent producer and consumer
func TestWriteWithBackoffConcurrent(t *testing.T) {
// Small ring - 128 items
ring, err := NewShardedRing(128, 8)
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
config := WriteConfig{
MaxRetries: 10,
BackoffDuration: 100 * time.Microsecond,
MaxBackoffs: 0, // Unlimited - will eventually succeed
}
numProducers := 4
itemsPerProducer := 1000
totalItems := int64(numProducers * itemsPerProducer)
var producerWg sync.WaitGroup
var consumerWg sync.WaitGroup
var itemsWritten atomic.Int64
var itemsRead atomic.Int64
var producersDone atomic.Bool
// Start consumer
consumerWg.Add(1)
go func() {
defer consumerWg.Done()
for !producersDone.Load() || ring.Len() > 0 {
batch := ring.ReadBatch(50)
itemsRead.Add(int64(len(batch)))
if len(batch) == 0 {
time.Sleep(50 * time.Microsecond)
}
}
}()
// Start producers with backoff
for p := 0; p < numProducers; p++ {
producerWg.Add(1)
go func(producerID int) {
defer producerWg.Done()
for i := 0; i < itemsPerProducer; i++ {
// This will backoff instead of spinning aggressively
ring.WriteWithBackoff(uint64(producerID), i, config)
itemsWritten.Add(1)
}
}(p)
}
// Wait for producers
done := make(chan struct{})
go func() {
producerWg.Wait()
producersDone.Store(true)
consumerWg.Wait()
close(done)
}()
select {
case <-done:
// Success
case <-time.After(10 * time.Second):
t.Fatal("Test timed out")
}
written := itemsWritten.Load()
read := itemsRead.Load()
if written != totalItems {
t.Errorf("Items written = %d, want %d", written, totalItems)
}
if read != written {
t.Errorf("Items read = %d, want %d", read, written)
}
t.Logf("Successfully processed %d items through 128-item ring with backoff", written)
}
// TestDefaultWriteConfig tests the default configuration
func TestDefaultWriteConfig(t *testing.T) {
config := DefaultWriteConfig()
if config.MaxRetries != 10 {
t.Errorf("Default MaxRetries = %d, want 10", config.MaxRetries)
}
if config.BackoffDuration != 100*time.Microsecond {
t.Errorf("Default BackoffDuration = %v, want 100µs", config.BackoffDuration)
}
if config.MaxBackoffs != 0 {
t.Errorf("Default MaxBackoffs = %d, want 0 (unlimited)", config.MaxBackoffs)
}
}
// =============================================================================
// Strategy Tests
// =============================================================================
// TestRetryStrategyString tests the String method of RetryStrategy
func TestRetryStrategyString(t *testing.T) {
tests := []struct {
strategy RetryStrategy
want string
}{
{SleepBackoff, "SleepBackoff"},
{NextShard, "NextShard"},
{RandomShard, "RandomShard"},
{AdaptiveBackoff, "AdaptiveBackoff"},
{SpinThenYield, "SpinThenYield"},
{Hybrid, "Hybrid"},
{AutoAdaptive, "AutoAdaptive"},
{RetryStrategy(99), "Unknown"},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.strategy.String(); got != tt.want {
t.Errorf("RetryStrategy.String() = %v, want %v", got, tt.want)
}
})
}
}
// TestNewWriter tests Writer creation and basic functionality
func TestNewWriter(t *testing.T) {
ring, err := NewShardedRing(1024, 4)
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
config := WriteConfig{
Strategy: NextShard,
MaxRetries: 10,
BackoffDuration: 100 * time.Microsecond,
MaxBackoffs: 100,
}
writer := NewWriter(ring, 0, config)
if writer == nil {
t.Fatal("NewWriter returned nil")
}
// Test basic write
if !writer.Write("test") {
t.Error("Writer.Write failed on empty ring")
}
// Read back
val, ok := ring.TryRead()
if !ok {
t.Error("TryRead failed")
}
if val != "test" {
t.Errorf("Got %v, want 'test'", val)
}
}
// TestWriterStrategies tests that each strategy can write successfully
func TestWriterStrategies(t *testing.T) {
strategies := []RetryStrategy{
SleepBackoff,
NextShard,
RandomShard,
AdaptiveBackoff,
SpinThenYield,
Hybrid,
AutoAdaptive,
}
for _, strategy := range strategies {
t.Run(strategy.String(), func(t *testing.T) {
// Use large ring so single-shard strategies don't fill up
// 400 capacity with 4 shards = 100 per shard
ring, err := NewShardedRing(400, 4)
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
config := WriteConfig{
Strategy: strategy,
MaxRetries: 10,
BackoffDuration: 10 * time.Microsecond,
MaxBackoffs: 100,
MaxBackoffDuration: 1 * time.Millisecond,
BackoffMultiplier: 2.0,
}
writer := NewWriter(ring, 0, config)
// Write 50 items (fits in one shard with room to spare)
numItems := 50
for i := 0; i < numItems; i++ {
if !writer.Write(i) {
t.Errorf("Write failed at index %d", i)
}
}
if ring.Len() != uint64(numItems) {
t.Errorf("Ring length = %d, want %d", ring.Len(), numItems)
}
// Read them all back (don't check order for multi-shard strategies)
readCount := 0
for {
_, ok := ring.TryRead()
if !ok {
break
}
readCount++
}
if readCount != numItems {
t.Errorf("Read %d items, want %d", readCount, numItems)
}
})
}
}
// TestNextShardFallback tests that NextShard strategy falls back to other shards
func TestNextShardFallback(t *testing.T) {
ring, err := NewShardedRing(64, 4) // 16 per shard
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
// Fill shard 0 completely using direct writes
for i := 0; i < 16; i++ {
if !ring.Write(0, i) {
t.Fatalf("Failed to fill shard 0 at index %d", i)
}
}
// Verify shard 0 is full
if ring.Write(0, "overflow") {
t.Error("Shard 0 should be full")
}
// Now use NextShard strategy - it should write to another shard
config := WriteConfig{
Strategy: NextShard,
MaxRetries: 1, // Low retries to quickly move to next shard
BackoffDuration: 10 * time.Microsecond,
MaxBackoffs: 10,
}
writer := NewWriter(ring, 0, config) // Producer 0 would normally use shard 0
// This should succeed by falling back to other shards
if !writer.Write("fallback") {
t.Error("NextShard strategy should have found space in another shard")
}
// Verify total count
if ring.Len() != 17 {
t.Errorf("Ring length = %d, want 17", ring.Len())
}
}
// TestWriterConcurrent tests concurrent writes with different strategies
func TestWriterConcurrent(t *testing.T) {
strategies := []RetryStrategy{
SleepBackoff,
NextShard,
SpinThenYield,
AutoAdaptive,
}
for _, strategy := range strategies {
t.Run(strategy.String(), func(t *testing.T) {
ring, err := NewShardedRing(10000, 8)
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
config := WriteConfig{
Strategy: strategy,
MaxRetries: 10,
BackoffDuration: 10 * time.Microsecond,
MaxBackoffs: 0, // Unlimited
}
numProducers := 4
itemsPerProducer := 500
var wg sync.WaitGroup
var writeFailures atomic.Int64
for p := 0; p < numProducers; p++ {
wg.Add(1)
go func(producerID int) {
defer wg.Done()
writer := NewWriter(ring, uint64(producerID), config)
for i := 0; i < itemsPerProducer; i++ {
if !writer.Write(producerID*10000 + i) {
writeFailures.Add(1)
}
}
}(p)
}
wg.Wait()
expectedItems := uint64(numProducers*itemsPerProducer) - uint64(writeFailures.Load())
actualLen := ring.Len()
if actualLen != expectedItems {
t.Errorf("Len() = %d, want %d (failures: %d)", actualLen, expectedItems, writeFailures.Load())
}
// Read all items
readCount := 0
for {
_, ok := ring.TryRead()
if !ok {
break
}
readCount++
}
if readCount != int(expectedItems) {
t.Errorf("Read %d items, want %d", readCount, expectedItems)
}
})
}
}
// TestWriterReset tests the Reset method
func TestWriterReset(t *testing.T) {
ring, err := NewShardedRing(256, 4)
if err != nil {
t.Fatalf("NewShardedRing failed: %v", err)
}
config := WriteConfig{
Strategy: AdaptiveBackoff,
MaxRetries: 5,
BackoffDuration: 100 * time.Microsecond,
MaxBackoffs: 10,
MaxBackoffDuration: 1 * time.Millisecond,
BackoffMultiplier: 2.0,
}
writer := NewWriter(ring, 0, config)
// Write some items
for i := 0; i < 10; i++ {
writer.Write(i)
}
// Reset the writer
writer.Reset()
// State should be reset
if writer.state.backoffCount != 0 {
t.Errorf("backoffCount = %d after Reset, want 0", writer.state.backoffCount)
}
if writer.state.currentBackoff != config.BackoffDuration {
t.Errorf("currentBackoff = %v after Reset, want %v", writer.state.currentBackoff, config.BackoffDuration)
}
}
// TestDefaultWriteConfigWithStrategy tests default config includes strategy fields
func TestDefaultWriteConfigWithStrategy(t *testing.T) {
config := DefaultWriteConfig()
// Default is now AutoAdaptive for high-performance by default
if config.Strategy != AutoAdaptive {
t.Errorf("Default Strategy = %v, want AutoAdaptive", config.Strategy)
}
if config.MaxRetries != 10 {
t.Errorf("Default MaxRetries = %d, want 10", config.MaxRetries)
}
if config.BackoffDuration != 100*time.Microsecond {
t.Errorf("Default BackoffDuration = %v, want 100µs", config.BackoffDuration)
}
if config.MaxBackoffs != 0 {
t.Errorf("Default MaxBackoffs = %d, want 0 (unlimited)", config.MaxBackoffs)
}
if config.MaxBackoffDuration != 10*time.Millisecond {
t.Errorf("Default MaxBackoffDuration = %v, want 10ms", config.MaxBackoffDuration)
}
if config.BackoffMultiplier != 2.0 {
t.Errorf("Default BackoffMultiplier = %v, want 2.0", config.BackoffMultiplier)
}
// AutoAdaptive-specific defaults
if config.AdaptiveIdleIterations != 100000 {
t.Errorf("Default AdaptiveIdleIterations = %d, want 100000", config.AdaptiveIdleIterations)
}
if config.AdaptiveWarmupIterations != 1000 {
t.Errorf("Default AdaptiveWarmupIterations = %d, want 1000", config.AdaptiveWarmupIterations)
}
if config.AdaptiveSleepDuration != 100*time.Microsecond {
t.Errorf("Default AdaptiveSleepDuration = %v, want 100µs", config.AdaptiveSleepDuration)
}
}
// TestConfigPresets tests the various configuration presets
func TestConfigPresets(t *testing.T) {
// Test HighThroughputConfig
ht := HighThroughputConfig()
if ht.Strategy != AutoAdaptive {
t.Errorf("HighThroughputConfig Strategy = %v, want AutoAdaptive", ht.Strategy)
}
if ht.AdaptiveIdleIterations != 500000 {
t.Errorf("HighThroughputConfig AdaptiveIdleIterations = %d, want 500000", ht.AdaptiveIdleIterations)
}
// Test LowLatencyConfig
ll := LowLatencyConfig()
if ll.Strategy != SpinThenYield {
t.Errorf("LowLatencyConfig Strategy = %v, want SpinThenYield", ll.Strategy)
}
// Test CPUFriendlyConfig
cf := CPUFriendlyConfig()
if cf.Strategy != AutoAdaptive {
t.Errorf("CPUFriendlyConfig Strategy = %v, want AutoAdaptive", cf.Strategy)
}
if cf.AdaptiveIdleIterations != 10000 {
t.Errorf("CPUFriendlyConfig AdaptiveIdleIterations = %d, want 10000", cf.AdaptiveIdleIterations)
}
// Test BurstyTrafficConfig
bt := BurstyTrafficConfig()
if bt.Strategy != AutoAdaptive {
t.Errorf("BurstyTrafficConfig Strategy = %v, want AutoAdaptive", bt.Strategy)
}
if bt.AdaptiveWarmupIterations != 10000 {
t.Errorf("BurstyTrafficConfig AdaptiveWarmupIterations = %d, want 10000", bt.AdaptiveWarmupIterations)
}
// Test LegacySleepConfig
ls := LegacySleepConfig()
if ls.Strategy != SleepBackoff {
t.Errorf("LegacySleepConfig Strategy = %v, want SleepBackoff", ls.Strategy)
}
}