Skip to content

Commit 0ab6dc1

Browse files
committed
chore(keyed): use modern ranges in tests
Signed-off-by: Christian Stewart <[email protected]>
1 parent f5de106 commit 0ab6dc1

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

keyed/keyed_test.go

+32-32
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestKeyed(t *testing.T) {
4040

4141
nsend := 101
4242
keys := make([]string, nsend)
43-
for i := 0; i < nsend; i++ {
43+
for i := range nsend {
4444
key := "routine-" + strconv.Itoa(i)
4545
keys[i] = key
4646
}
@@ -96,7 +96,7 @@ func TestKeyed_WithDelay(t *testing.T) {
9696
var called, canceled atomic.Bool
9797
calledCh := make(chan struct{})
9898
canceledCh := make(chan struct{})
99-
99+
100100
k := NewKeyed(
101101
func(key string) (Routine, *testData) {
102102
return func(ctx context.Context) error {
@@ -120,10 +120,10 @@ func TestKeyed_WithDelay(t *testing.T) {
120120
if !called.Load() || canceled.Load() {
121121
t.Fail()
122122
}
123-
123+
124124
// Remove the key, but it should still be running due to delay
125125
_ = k.RemoveKey("test")
126-
126+
127127
// Create a timer to check if the routine is still running after some time
128128
// This is one case where we need a timer since we're testing time-based behavior
129129
timer := time.NewTimer(time.Millisecond * 100)
@@ -133,7 +133,7 @@ func TestKeyed_WithDelay(t *testing.T) {
133133
case <-timer.C:
134134
// Expected - routine should still be running
135135
}
136-
136+
137137
// Now wait for cancellation to happen after the delay
138138
<-canceledCh
139139
if !called.Load() || !canceled.Load() {
@@ -151,13 +151,13 @@ func TestKeyed_WithDelay(t *testing.T) {
151151
if !called.Load() || canceled.Load() {
152152
t.Fail()
153153
}
154-
154+
155155
// Remove the key, but it should still be running due to delay
156156
_ = k.RemoveKey("test")
157-
157+
158158
// Set the key again before the delay expires
159159
k.SetKey("test", false)
160-
160+
161161
// Verify the routine is still running and wasn't canceled
162162
timer.Reset(time.Millisecond * 200)
163163
select {
@@ -166,7 +166,7 @@ func TestKeyed_WithDelay(t *testing.T) {
166166
case <-timer.C:
167167
// Expected - routine should still be running
168168
}
169-
169+
170170
if !called.Load() || canceled.Load() {
171171
t.Fail()
172172
}
@@ -264,17 +264,17 @@ func TestKeyedRefCount(t *testing.T) {
264264

265265
// Create a channel to wait for the routine to start
266266
startCh := make(chan struct{})
267-
267+
268268
// Wait for the routine to start
269-
for i := 0; i < 100; i++ {
269+
for range 100 {
270270
if startCount.Load() == 1 {
271271
close(startCh)
272272
break
273273
}
274274
// Small yield to allow other goroutines to run
275275
runtime.Gosched()
276276
}
277-
277+
278278
<-startCh
279279
if startCount.Load() != 1 {
280280
t.Fatal("routine should have started once")
@@ -285,7 +285,7 @@ func TestKeyedRefCount(t *testing.T) {
285285

286286
// Release one reference, routine should still be running
287287
ref1.Release()
288-
288+
289289
// Verify state hasn't changed
290290
if startCount.Load() != 1 {
291291
t.Fatal("routine should have started once")
@@ -296,17 +296,17 @@ func TestKeyedRefCount(t *testing.T) {
296296

297297
// Release the second reference, routine should stop
298298
ref2.Release()
299-
299+
300300
// Wait for the routine to stop
301301
stopCh := make(chan struct{})
302-
for i := 0; i < 100; i++ {
302+
for range 100 {
303303
if stopCount.Load() == 1 {
304304
close(stopCh)
305305
break
306306
}
307307
runtime.Gosched()
308308
}
309-
309+
310310
<-stopCh
311311
if startCount.Load() != 1 {
312312
t.Fatal("routine should have started once")
@@ -317,17 +317,17 @@ func TestKeyedRefCount(t *testing.T) {
317317

318318
// Add a reference again, routine should restart
319319
ref3, _, _ := k.AddKeyRef("test-key")
320-
320+
321321
// Wait for the routine to start again
322322
startCh2 := make(chan struct{})
323-
for i := 0; i < 100; i++ {
323+
for range 100 {
324324
if startCount.Load() == 2 {
325325
close(startCh2)
326326
break
327327
}
328328
runtime.Gosched()
329329
}
330-
330+
331331
<-startCh2
332332
if startCount.Load() != 2 {
333333
t.Fatal("routine should have started twice")
@@ -338,17 +338,17 @@ func TestKeyedRefCount(t *testing.T) {
338338

339339
// Remove the key directly, should stop the routine
340340
k.RemoveKey("test-key")
341-
341+
342342
// Wait for the routine to stop again
343343
stopCh2 := make(chan struct{})
344-
for i := 0; i < 100; i++ {
344+
for range 100 {
345345
if stopCount.Load() == 2 {
346346
close(stopCh2)
347347
break
348348
}
349349
runtime.Gosched()
350350
}
351-
351+
352352
<-stopCh2
353353
if startCount.Load() != 2 {
354354
t.Fatal("routine should have started twice")
@@ -387,7 +387,7 @@ func TestExitCallbacks(t *testing.T) {
387387
}, &testData{}
388388
},
389389
WithExitLogger[string, *testData](le),
390-
WithExitCb[string, *testData](exitCb),
390+
WithExitCb(exitCb),
391391
)
392392

393393
k.SetContext(ctx, true)
@@ -463,7 +463,7 @@ func TestRestartReset(t *testing.T) {
463463
runtime.Gosched()
464464
}
465465
}()
466-
466+
467467
existed, restarted := k.RestartRoutine("test-key")
468468
if !existed || !restarted {
469469
t.Fatal("restart should have succeeded")
@@ -492,7 +492,7 @@ func TestRestartReset(t *testing.T) {
492492
runtime.Gosched()
493493
}
494494
}()
495-
495+
496496
existed, reset := k.ResetRoutine("test-key")
497497
if !existed || !reset {
498498
t.Fatal("reset should have succeeded")
@@ -521,7 +521,7 @@ func TestRestartReset(t *testing.T) {
521521
runtime.Gosched()
522522
}
523523
}()
524-
524+
525525
existed, reset = k.ResetRoutine("test-key", func(k string, v *testData) bool {
526526
return v.value == "test-key-2"
527527
})
@@ -552,7 +552,7 @@ func TestRestartReset(t *testing.T) {
552552
runtime.Gosched()
553553
}
554554
}()
555-
555+
556556
resetCount2, totalCount := k.ResetAllRoutines()
557557
if resetCount2 != 1 || totalCount != 1 {
558558
t.Fatal("reset all should have reset one routine")
@@ -608,7 +608,7 @@ func TestContextCancellation(t *testing.T) {
608608

609609
// Wait for callback to be called
610610
<-exitCh
611-
611+
612612
mu.Lock()
613613
if len(exitErrors) != 1 {
614614
t.Fatal("should have one exit error")
@@ -621,7 +621,7 @@ func TestContextCancellation(t *testing.T) {
621621
// Set a new context
622622
newCtx := context.Background()
623623
k.SetContext(newCtx, true)
624-
624+
625625
// Create a channel for the second exit
626626
exitCh2 := make(chan struct{})
627627
var exitWg sync.WaitGroup
@@ -639,14 +639,14 @@ func TestContextCancellation(t *testing.T) {
639639
runtime.Gosched()
640640
}
641641
}()
642-
642+
643643
// Cancel the key
644644
k.RemoveKey("test-key")
645-
645+
646646
// Wait for callback to be called again
647647
<-exitCh2
648648
exitWg.Wait()
649-
649+
650650
mu.Lock()
651651
if len(exitErrors) != 2 {
652652
t.Fatal("should have two exit errors")

0 commit comments

Comments
 (0)