-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeyed_test.go
658 lines (575 loc) · 13.8 KB
/
keyed_test.go
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package keyed
import (
"context"
"errors"
"runtime"
"slices"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/aperturerobotics/util/backoff"
"github.com/sirupsen/logrus"
)
// testData contains some test metadata.
type testData struct {
value string
}
// TestKeyed tests the keyed goroutine manager.
func TestKeyed(t *testing.T) {
ctx := context.Background()
vals := make(chan string, 10)
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
k := NewKeyed(func(key string) (Routine, *testData) {
return func(ctx context.Context) error {
select {
case <-ctx.Done():
return context.Canceled
case vals <- key:
return nil
}
}, &testData{}
}, WithExitLogger[string, *testData](le))
nsend := 101
keys := make([]string, nsend)
for i := range nsend {
key := "routine-" + strconv.Itoa(i)
keys[i] = key
}
added, removed := k.SyncKeys(keys, false)
if len(removed) != 0 || !slices.Equal(added, keys) {
t.FailNow()
}
nsend--
keys = keys[:nsend]
added, removed = k.SyncKeys(keys, false)
if len(removed) != 1 || len(added) != 0 {
t.FailNow()
}
// expect nothing to have been pushed to vals yet
<-time.After(time.Millisecond * 10)
select {
case val := <-vals:
t.Fatalf("unexpected value before set context: %s", val)
default:
}
// start execution
k.SetContext(ctx, false)
seen := make(map[string]struct{})
for {
select {
case <-ctx.Done():
t.Fatal(ctx.Err().Error())
case val := <-vals:
if _, ok := seen[val]; ok {
t.Fatalf("duplicate value: %s", val)
}
seen[val] = struct{}{}
if len(seen) == nsend {
// success
return
}
}
}
}
// TestKeyed_WithDelay tests the delay removing unreferenced keys.
func TestKeyed_WithDelay(t *testing.T) {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
var called, canceled atomic.Bool
calledCh := make(chan struct{})
canceledCh := make(chan struct{})
k := NewKeyed(
func(key string) (Routine, *testData) {
return func(ctx context.Context) error {
called.Store(true)
close(calledCh)
<-ctx.Done()
canceled.Store(true)
close(canceledCh)
return nil
}, &testData{}
},
WithExitLogger[string, *testData](le),
WithReleaseDelay[string, *testData](time.Millisecond*180),
)
// start execution
k.SetContext(ctx, false)
k.SetKey("test", true)
<-calledCh
if !called.Load() || canceled.Load() {
t.Fail()
}
// Remove the key, but it should still be running due to delay
_ = k.RemoveKey("test")
// Create a timer to check if the routine is still running after some time
// This is one case where we need a timer since we're testing time-based behavior
timer := time.NewTimer(time.Millisecond * 100)
select {
case <-canceledCh:
t.Fatal("routine should not have been canceled yet")
case <-timer.C:
// Expected - routine should still be running
}
// Now wait for cancellation to happen after the delay
<-canceledCh
if !called.Load() || !canceled.Load() {
t.Fail()
}
// Reset for second test
canceled.Store(false)
called.Store(false)
calledCh = make(chan struct{})
canceledCh = make(chan struct{})
k.SetKey("test", false)
<-calledCh
if !called.Load() || canceled.Load() {
t.Fail()
}
// Remove the key, but it should still be running due to delay
_ = k.RemoveKey("test")
// Set the key again before the delay expires
k.SetKey("test", false)
// Verify the routine is still running and wasn't canceled
timer.Reset(time.Millisecond * 200)
select {
case <-canceledCh:
t.Fatal("routine should not have been canceled")
case <-timer.C:
// Expected - routine should still be running
}
if !called.Load() || canceled.Load() {
t.Fail()
}
}
// TestKeyedWithRetry tests the keyed goroutine manager.
func TestKeyedWithRetry(t *testing.T) {
ctx := context.Background()
vals := make(chan string, 10)
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
i := 5
k := NewKeyed(
func(key string) (Routine, *testData) {
return func(ctx context.Context) error {
if i == 0 {
select {
case <-ctx.Done():
return context.Canceled
case vals <- key:
return nil
}
}
i--
return errors.New("returning error to test retry")
}, &testData{}
},
WithExitLogger[string, *testData](le),
WithRetry[string, *testData](&backoff.Backoff{
BackoffKind: backoff.BackoffKind_BackoffKind_EXPONENTIAL,
Exponential: &backoff.Exponential{
InitialInterval: 200,
MaxInterval: 1000,
RandomizationFactor: 0,
},
}),
)
k.SetContext(ctx, true)
_, existed := k.SetKey("test-key", true)
if existed {
t.FailNow()
}
val := <-vals
if val != "test-key" {
t.FailNow()
}
if i != 0 {
t.FailNow()
}
}
// TestKeyedRefCount tests the reference counting functionality.
func TestKeyedRefCount(t *testing.T) {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
startCount := atomic.Int32{}
stopCount := atomic.Int32{}
k := NewKeyedRefCountWithLogger(
func(key string) (Routine, *testData) {
return func(ctx context.Context) error {
startCount.Add(1)
<-ctx.Done()
stopCount.Add(1)
return context.Canceled
}, &testData{value: key}
},
le,
)
k.SetContext(ctx, false)
// Add multiple references to the same key
ref1, data1, existed1 := k.AddKeyRef("test-key")
if existed1 {
t.Fatal("key should not exist yet")
}
if data1.value != "test-key" {
t.Fatal("unexpected data value")
}
ref2, data2, existed2 := k.AddKeyRef("test-key")
if !existed2 {
t.Fatal("key should exist now")
}
if data2.value != "test-key" {
t.Fatal("unexpected data value")
}
// Create a channel to wait for the routine to start
startCh := make(chan struct{})
// Wait for the routine to start
for range 100 {
if startCount.Load() == 1 {
close(startCh)
break
}
// Small yield to allow other goroutines to run
runtime.Gosched()
}
<-startCh
if startCount.Load() != 1 {
t.Fatal("routine should have started once")
}
if stopCount.Load() != 0 {
t.Fatal("routine should not have stopped")
}
// Release one reference, routine should still be running
ref1.Release()
// Verify state hasn't changed
if startCount.Load() != 1 {
t.Fatal("routine should have started once")
}
if stopCount.Load() != 0 {
t.Fatal("routine should not have stopped")
}
// Release the second reference, routine should stop
ref2.Release()
// Wait for the routine to stop
stopCh := make(chan struct{})
for range 100 {
if stopCount.Load() == 1 {
close(stopCh)
break
}
runtime.Gosched()
}
<-stopCh
if startCount.Load() != 1 {
t.Fatal("routine should have started once")
}
if stopCount.Load() != 1 {
t.Fatal("routine should have stopped")
}
// Add a reference again, routine should restart
ref3, _, _ := k.AddKeyRef("test-key")
// Wait for the routine to start again
startCh2 := make(chan struct{})
for range 100 {
if startCount.Load() == 2 {
close(startCh2)
break
}
runtime.Gosched()
}
<-startCh2
if startCount.Load() != 2 {
t.Fatal("routine should have started twice")
}
if stopCount.Load() != 1 {
t.Fatal("routine should have stopped once")
}
// Remove the key directly, should stop the routine
k.RemoveKey("test-key")
// Wait for the routine to stop again
stopCh2 := make(chan struct{})
for range 100 {
if stopCount.Load() == 2 {
close(stopCh2)
break
}
runtime.Gosched()
}
<-stopCh2
if startCount.Load() != 2 {
t.Fatal("routine should have started twice")
}
if stopCount.Load() != 2 {
t.Fatal("routine should have stopped twice")
}
// Releasing the reference after removal should be a no-op
ref3.Release()
}
// TestExitCallbacks tests the exit callback functionality.
func TestExitCallbacks(t *testing.T) {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
var exitKey string
var exitErr error
var callbackCalled atomic.Bool
callbackCh := make(chan struct{})
exitCb := func(key string, routine Routine, data *testData, err error) {
exitKey = key
exitErr = err
callbackCalled.Store(true)
close(callbackCh)
}
k := NewKeyed(
func(key string) (Routine, *testData) {
return func(ctx context.Context) error {
return errors.New("test error")
}, &testData{}
},
WithExitLogger[string, *testData](le),
WithExitCb(exitCb),
)
k.SetContext(ctx, true)
_, existed := k.SetKey("test-key", true)
if existed {
t.Fatal("key should not exist yet")
}
// Wait for callback to be called
<-callbackCh
if !callbackCalled.Load() {
t.Fatal("exit callback should have been called")
}
if exitKey != "test-key" {
t.Fatal("wrong exit key")
}
if exitErr == nil || exitErr.Error() != "test error" {
t.Fatal("wrong exit error")
}
}
// TestRestartReset tests the restart and reset functionality.
func TestRestartReset(t *testing.T) {
ctx := context.Background()
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
startCount := atomic.Int32{}
resetCount := atomic.Int32{}
startCh := make(chan struct{})
k := NewKeyed(
func(key string) (Routine, *testData) {
resetCount.Add(1)
return func(ctx context.Context) error {
count := startCount.Add(1)
if count == 1 {
close(startCh)
}
<-ctx.Done()
return context.Canceled
}, &testData{value: key + "-" + strconv.Itoa(int(resetCount.Load()))}
},
WithExitLogger[string, *testData](le),
)
k.SetContext(ctx, false)
_, existed := k.SetKey("test-key", true)
if existed {
t.Fatal("key should not exist yet")
}
<-startCh
if startCount.Load() != 1 {
t.Fatal("routine should have started once")
}
if resetCount.Load() != 1 {
t.Fatal("constructor should have been called once")
}
// Test restart
startCh2 := make(chan struct{})
var startWg sync.WaitGroup
startWg.Add(1)
go func() {
defer startWg.Done()
for {
if startCount.Load() == 2 {
close(startCh2)
return
}
runtime.Gosched()
}
}()
existed, restarted := k.RestartRoutine("test-key")
if !existed || !restarted {
t.Fatal("restart should have succeeded")
}
<-startCh2
startWg.Wait()
if startCount.Load() != 2 {
t.Fatal("routine should have started twice")
}
if resetCount.Load() != 1 {
t.Fatal("constructor should still have been called once")
}
// Test reset
startCh3 := make(chan struct{})
var startWg2 sync.WaitGroup
startWg2.Add(1)
go func() {
defer startWg2.Done()
for {
if startCount.Load() == 3 {
close(startCh3)
return
}
runtime.Gosched()
}
}()
existed, reset := k.ResetRoutine("test-key")
if !existed || !reset {
t.Fatal("reset should have succeeded")
}
<-startCh3
startWg2.Wait()
if startCount.Load() != 3 {
t.Fatal("routine should have started three times")
}
if resetCount.Load() != 2 {
t.Fatal("constructor should have been called twice")
}
// Test conditional reset
startCh4 := make(chan struct{})
var startWg3 sync.WaitGroup
startWg3.Add(1)
go func() {
defer startWg3.Done()
for {
if startCount.Load() == 4 {
close(startCh4)
return
}
runtime.Gosched()
}
}()
existed, reset = k.ResetRoutine("test-key", func(k string, v *testData) bool {
return v.value == "test-key-2"
})
if !existed || !reset {
t.Fatal("conditional reset should have succeeded")
}
<-startCh4
startWg3.Wait()
if startCount.Load() != 4 {
t.Fatal("routine should have started four times")
}
if resetCount.Load() != 3 {
t.Fatal("constructor should have been called three times")
}
// Test reset all
startCh5 := make(chan struct{})
var startWg4 sync.WaitGroup
startWg4.Add(1)
go func() {
defer startWg4.Done()
for {
if startCount.Load() == 5 {
close(startCh5)
return
}
runtime.Gosched()
}
}()
resetCount2, totalCount := k.ResetAllRoutines()
if resetCount2 != 1 || totalCount != 1 {
t.Fatal("reset all should have reset one routine")
}
<-startCh5
startWg4.Wait()
if startCount.Load() != 5 {
t.Fatal("routine should have started five times")
}
if resetCount.Load() != 4 {
t.Fatal("constructor should have been called four times")
}
}
// TestContextCancellation tests handling of context cancellation.
func TestContextCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
log := logrus.New()
log.SetLevel(logrus.DebugLevel)
le := logrus.NewEntry(log)
var mu sync.Mutex
var exitErrors []error
exitCh := make(chan struct{})
k := NewKeyed(
func(key string) (Routine, *testData) {
return func(ctx context.Context) error {
<-ctx.Done()
return ctx.Err()
}, &testData{}
},
WithExitLogger[string, *testData](le),
WithExitCb[string, *testData](func(_ string, _ Routine, _ *testData, err error) {
mu.Lock()
exitErrors = append(exitErrors, err)
if len(exitErrors) == 1 {
close(exitCh)
}
mu.Unlock()
}),
)
k.SetContext(ctx, false)
_, existed := k.SetKey("test-key", true)
if existed {
t.Fatal("key should not exist yet")
}
// Cancel the context
cancel()
// Wait for callback to be called
<-exitCh
mu.Lock()
if len(exitErrors) != 1 {
t.Fatal("should have one exit error")
}
if exitErrors[0] != context.Canceled {
t.Fatalf("expected context.Canceled error, got: %v", exitErrors[0])
}
mu.Unlock()
// Set a new context
newCtx := context.Background()
k.SetContext(newCtx, true)
// Create a channel for the second exit
exitCh2 := make(chan struct{})
var exitWg sync.WaitGroup
exitWg.Add(1)
go func() {
defer exitWg.Done()
for {
mu.Lock()
count := len(exitErrors)
mu.Unlock()
if count == 2 {
close(exitCh2)
return
}
runtime.Gosched()
}
}()
// Cancel the key
k.RemoveKey("test-key")
// Wait for callback to be called again
<-exitCh2
exitWg.Wait()
mu.Lock()
if len(exitErrors) != 2 {
t.Fatal("should have two exit errors")
}
if exitErrors[1] != context.Canceled {
t.Fatalf("expected context.Canceled error, got: %v", exitErrors[1])
}
mu.Unlock()
}