@@ -40,7 +40,7 @@ func TestKeyed(t *testing.T) {
40
40
41
41
nsend := 101
42
42
keys := make ([]string , nsend )
43
- for i := 0 ; i < nsend ; i ++ {
43
+ for i := range nsend {
44
44
key := "routine-" + strconv .Itoa (i )
45
45
keys [i ] = key
46
46
}
@@ -96,7 +96,7 @@ func TestKeyed_WithDelay(t *testing.T) {
96
96
var called , canceled atomic.Bool
97
97
calledCh := make (chan struct {})
98
98
canceledCh := make (chan struct {})
99
-
99
+
100
100
k := NewKeyed (
101
101
func (key string ) (Routine , * testData ) {
102
102
return func (ctx context.Context ) error {
@@ -120,10 +120,10 @@ func TestKeyed_WithDelay(t *testing.T) {
120
120
if ! called .Load () || canceled .Load () {
121
121
t .Fail ()
122
122
}
123
-
123
+
124
124
// Remove the key, but it should still be running due to delay
125
125
_ = k .RemoveKey ("test" )
126
-
126
+
127
127
// Create a timer to check if the routine is still running after some time
128
128
// This is one case where we need a timer since we're testing time-based behavior
129
129
timer := time .NewTimer (time .Millisecond * 100 )
@@ -133,7 +133,7 @@ func TestKeyed_WithDelay(t *testing.T) {
133
133
case <- timer .C :
134
134
// Expected - routine should still be running
135
135
}
136
-
136
+
137
137
// Now wait for cancellation to happen after the delay
138
138
<- canceledCh
139
139
if ! called .Load () || ! canceled .Load () {
@@ -151,13 +151,13 @@ func TestKeyed_WithDelay(t *testing.T) {
151
151
if ! called .Load () || canceled .Load () {
152
152
t .Fail ()
153
153
}
154
-
154
+
155
155
// Remove the key, but it should still be running due to delay
156
156
_ = k .RemoveKey ("test" )
157
-
157
+
158
158
// Set the key again before the delay expires
159
159
k .SetKey ("test" , false )
160
-
160
+
161
161
// Verify the routine is still running and wasn't canceled
162
162
timer .Reset (time .Millisecond * 200 )
163
163
select {
@@ -166,7 +166,7 @@ func TestKeyed_WithDelay(t *testing.T) {
166
166
case <- timer .C :
167
167
// Expected - routine should still be running
168
168
}
169
-
169
+
170
170
if ! called .Load () || canceled .Load () {
171
171
t .Fail ()
172
172
}
@@ -264,17 +264,17 @@ func TestKeyedRefCount(t *testing.T) {
264
264
265
265
// Create a channel to wait for the routine to start
266
266
startCh := make (chan struct {})
267
-
267
+
268
268
// Wait for the routine to start
269
- for i := 0 ; i < 100 ; i ++ {
269
+ for range 100 {
270
270
if startCount .Load () == 1 {
271
271
close (startCh )
272
272
break
273
273
}
274
274
// Small yield to allow other goroutines to run
275
275
runtime .Gosched ()
276
276
}
277
-
277
+
278
278
<- startCh
279
279
if startCount .Load () != 1 {
280
280
t .Fatal ("routine should have started once" )
@@ -285,7 +285,7 @@ func TestKeyedRefCount(t *testing.T) {
285
285
286
286
// Release one reference, routine should still be running
287
287
ref1 .Release ()
288
-
288
+
289
289
// Verify state hasn't changed
290
290
if startCount .Load () != 1 {
291
291
t .Fatal ("routine should have started once" )
@@ -296,17 +296,17 @@ func TestKeyedRefCount(t *testing.T) {
296
296
297
297
// Release the second reference, routine should stop
298
298
ref2 .Release ()
299
-
299
+
300
300
// Wait for the routine to stop
301
301
stopCh := make (chan struct {})
302
- for i := 0 ; i < 100 ; i ++ {
302
+ for range 100 {
303
303
if stopCount .Load () == 1 {
304
304
close (stopCh )
305
305
break
306
306
}
307
307
runtime .Gosched ()
308
308
}
309
-
309
+
310
310
<- stopCh
311
311
if startCount .Load () != 1 {
312
312
t .Fatal ("routine should have started once" )
@@ -317,17 +317,17 @@ func TestKeyedRefCount(t *testing.T) {
317
317
318
318
// Add a reference again, routine should restart
319
319
ref3 , _ , _ := k .AddKeyRef ("test-key" )
320
-
320
+
321
321
// Wait for the routine to start again
322
322
startCh2 := make (chan struct {})
323
- for i := 0 ; i < 100 ; i ++ {
323
+ for range 100 {
324
324
if startCount .Load () == 2 {
325
325
close (startCh2 )
326
326
break
327
327
}
328
328
runtime .Gosched ()
329
329
}
330
-
330
+
331
331
<- startCh2
332
332
if startCount .Load () != 2 {
333
333
t .Fatal ("routine should have started twice" )
@@ -338,17 +338,17 @@ func TestKeyedRefCount(t *testing.T) {
338
338
339
339
// Remove the key directly, should stop the routine
340
340
k .RemoveKey ("test-key" )
341
-
341
+
342
342
// Wait for the routine to stop again
343
343
stopCh2 := make (chan struct {})
344
- for i := 0 ; i < 100 ; i ++ {
344
+ for range 100 {
345
345
if stopCount .Load () == 2 {
346
346
close (stopCh2 )
347
347
break
348
348
}
349
349
runtime .Gosched ()
350
350
}
351
-
351
+
352
352
<- stopCh2
353
353
if startCount .Load () != 2 {
354
354
t .Fatal ("routine should have started twice" )
@@ -387,7 +387,7 @@ func TestExitCallbacks(t *testing.T) {
387
387
}, & testData {}
388
388
},
389
389
WithExitLogger [string , * testData ](le ),
390
- WithExitCb [ string , * testData ] (exitCb ),
390
+ WithExitCb (exitCb ),
391
391
)
392
392
393
393
k .SetContext (ctx , true )
@@ -463,7 +463,7 @@ func TestRestartReset(t *testing.T) {
463
463
runtime .Gosched ()
464
464
}
465
465
}()
466
-
466
+
467
467
existed , restarted := k .RestartRoutine ("test-key" )
468
468
if ! existed || ! restarted {
469
469
t .Fatal ("restart should have succeeded" )
@@ -492,7 +492,7 @@ func TestRestartReset(t *testing.T) {
492
492
runtime .Gosched ()
493
493
}
494
494
}()
495
-
495
+
496
496
existed , reset := k .ResetRoutine ("test-key" )
497
497
if ! existed || ! reset {
498
498
t .Fatal ("reset should have succeeded" )
@@ -521,7 +521,7 @@ func TestRestartReset(t *testing.T) {
521
521
runtime .Gosched ()
522
522
}
523
523
}()
524
-
524
+
525
525
existed , reset = k .ResetRoutine ("test-key" , func (k string , v * testData ) bool {
526
526
return v .value == "test-key-2"
527
527
})
@@ -552,7 +552,7 @@ func TestRestartReset(t *testing.T) {
552
552
runtime .Gosched ()
553
553
}
554
554
}()
555
-
555
+
556
556
resetCount2 , totalCount := k .ResetAllRoutines ()
557
557
if resetCount2 != 1 || totalCount != 1 {
558
558
t .Fatal ("reset all should have reset one routine" )
@@ -608,7 +608,7 @@ func TestContextCancellation(t *testing.T) {
608
608
609
609
// Wait for callback to be called
610
610
<- exitCh
611
-
611
+
612
612
mu .Lock ()
613
613
if len (exitErrors ) != 1 {
614
614
t .Fatal ("should have one exit error" )
@@ -621,7 +621,7 @@ func TestContextCancellation(t *testing.T) {
621
621
// Set a new context
622
622
newCtx := context .Background ()
623
623
k .SetContext (newCtx , true )
624
-
624
+
625
625
// Create a channel for the second exit
626
626
exitCh2 := make (chan struct {})
627
627
var exitWg sync.WaitGroup
@@ -639,14 +639,14 @@ func TestContextCancellation(t *testing.T) {
639
639
runtime .Gosched ()
640
640
}
641
641
}()
642
-
642
+
643
643
// Cancel the key
644
644
k .RemoveKey ("test-key" )
645
-
645
+
646
646
// Wait for callback to be called again
647
647
<- exitCh2
648
648
exitWg .Wait ()
649
-
649
+
650
650
mu .Lock ()
651
651
if len (exitErrors ) != 2 {
652
652
t .Fatal ("should have two exit errors" )
0 commit comments