@@ -114,9 +114,7 @@ func TestDeleteFunc(t *testing.T) {
114114 for i := range 10 {
115115 cache .Set (i , i )
116116 }
117- wg .Add (1 )
118- go func () {
119- defer wg .Done ()
117+ wg .Go (func () {
120118 for i := 10 ; i < 30 ; i ++ {
121119 v , _ , err := cache .GetOrCreate (i , func (key int ) (any , error ) {
122120 if key % 2 == 0 {
@@ -133,7 +131,7 @@ func TestDeleteFunc(t *testing.T) {
133131 }
134132
135133 }
136- }( )
134+ })
137135 time .Sleep (3 * time .Microsecond )
138136 c .Assert (cache .DeleteFunc (func (key int , value any ) bool {
139137 return key % 2 == 0
@@ -214,22 +212,18 @@ func TestGetOrCreateConcurrent(t *testing.T) {
214212
215213 for i := range 20 {
216214 expect := fmt .Sprintf ("%d-0" , i )
217- wg .Add (1 )
218- go func () {
219- defer wg .Done ()
215+ wg .Go (func () {
220216 for range 12 {
221217 res , _ , err := cache .GetOrCreate (i , create )
222218 c .Assert (err , qt .IsNil )
223219 c .Assert (res , qt .Equals , expect )
224220 }
225- }( )
221+ })
226222 }
227223
228224 for i := range 20 {
229225 expect := fmt .Sprintf ("%d-0" , i )
230- wg .Add (1 )
231- go func () {
232- defer wg .Done ()
226+ wg .Go (func () {
233227 for range 12 {
234228 countersmu .Lock ()
235229 _ , created := counters [i ]
@@ -241,7 +235,7 @@ func TestGetOrCreateConcurrent(t *testing.T) {
241235 c .Assert (res , qt .Equals , expect )
242236 }
243237 }
244- }( )
238+ })
245239 }
246240
247241 wg .Wait ()
@@ -262,25 +256,21 @@ func TestGetOrCreateAndResizeConcurrent(t *testing.T) {
262256 var wg sync.WaitGroup
263257
264258 for i := range 100 {
265- wg .Add (1 )
266- go func () {
267- defer wg .Done ()
259+ wg .Go (func () {
268260 for range 12 {
269261 v , _ , err := cache .GetOrCreate (i , create )
270262 c .Assert (err , qt .IsNil )
271263 c .Assert (v , qt .Not (qt .Equals ), 0 )
272264 }
273- }( )
265+ })
274266 }
275267
276- wg .Add (1 )
277- go func () {
278- defer wg .Done ()
268+ wg .Go (func () {
279269 for i := 100 ; i >= 0 ; i -- {
280270 cache .Resize (i )
281271 time .Sleep (10 * time .Millisecond )
282272 }
283- }( )
273+ })
284274
285275 wg .Wait ()
286276}
@@ -296,9 +286,7 @@ func TestGetOrCreateRecursive(t *testing.T) {
296286 cache := New (Options [int , any ]{MaxEntries : 1000 })
297287
298288 for range 10 {
299- wg .Add (1 )
300- go func () {
301- defer wg .Done ()
289+ wg .Go (func () {
302290 for range 10 {
303291 // This test was added to test a deadlock situation with nested GetOrCreate calls on the same cache.
304292 // Note that the keys below are carefully selected to not overlap, as this case may still deadlock:
@@ -328,7 +316,7 @@ func TestGetOrCreateRecursive(t *testing.T) {
328316 c .Assert (v , qt .Equals , "inner" )
329317 }
330318 }
331- }( )
319+ })
332320
333321 }
334322 wg .Wait ()
@@ -353,9 +341,7 @@ func TestGetOrCreateEvictionRace(t *testing.T) {
353341 )
354342
355343 // Goroutine 1: Start creating key 1, but wait before completing
356- wg .Add (1 )
357- go func () {
358- defer wg .Done ()
344+ wg .Go (func () {
359345 v , _ , err := cache .GetOrCreate (1 , func (key int ) (string , error ) {
360346 close (key1Started ) // Signal that we've started
361347 <- key1Continue // Wait for signal to continue
@@ -364,7 +350,7 @@ func TestGetOrCreateEvictionRace(t *testing.T) {
364350 c .Assert (err , qt .ErrorMatches , "intentional failure" )
365351 c .Assert (v , qt .Equals , "" )
366352 close (key1Done )
367- }( )
353+ })
368354
369355 // Wait for goroutine 1 to start creating
370356 <- key1Started
@@ -374,16 +360,14 @@ func TestGetOrCreateEvictionRace(t *testing.T) {
374360 cache .Set (3 , "value3" ) // This should evict key 1
375361
376362 // Now create a new entry for key 1 that will succeed
377- wg .Add (1 )
378- go func () {
379- defer wg .Done ()
363+ wg .Go (func () {
380364 v , _ , err := cache .GetOrCreate (1 , func (key int ) (string , error ) {
381365 return "new-value-1" , nil
382366 })
383367 c .Assert (err , qt .IsNil )
384368 c .Assert (v , qt .Equals , "new-value-1" )
385369 close (key1NewCreated )
386- }( )
370+ })
387371
388372 // Wait for the new entry to be created before letting the old one fail
389373 <- key1NewCreated
@@ -415,16 +399,14 @@ func TestSetDuringGetOrCreate(t *testing.T) {
415399 )
416400
417401 // Start a GetOrCreate that will fail
418- wg .Add (1 )
419- go func () {
420- defer wg .Done ()
402+ wg .Go (func () {
421403 _ , _ , err := cache .GetOrCreate (1 , func (key int ) (string , error ) {
422404 close (createStarted )
423405 <- createContinue
424406 return "" , errors .New ("intentional failure" )
425407 })
426408 c .Assert (err , qt .ErrorMatches , "intentional failure" )
427- }( )
409+ })
428410
429411 <- createStarted
430412
@@ -467,17 +449,15 @@ func TestOnEvictWithPendingEntry(t *testing.T) {
467449 var wg sync.WaitGroup
468450
469451 // Start creating key 1 with a slow create function
470- wg .Add (1 )
471- go func () {
472- defer wg .Done ()
452+ wg .Go (func () {
473453 v , _ , err := cache .GetOrCreate (1 , func (key int ) (string , error ) {
474454 close (createStarted )
475455 time .Sleep (50 * time .Millisecond )
476456 return "value1" , nil
477457 })
478458 c .Assert (err , qt .IsNil )
479459 c .Assert (v , qt .Equals , "value1" )
480- }( )
460+ })
481461
482462 <- createStarted
483463
@@ -520,16 +500,14 @@ func TestGetOrCreateConcurrentErrors(t *testing.T) {
520500
521501 // Multiple goroutines try to get/create the same failing key
522502 for range 10 {
523- wg .Add (1 )
524- go func () {
525- defer wg .Done ()
503+ wg .Go (func () {
526504 _ , _ , err := cache .GetOrCreate (1 , func (key int ) (string , error ) {
527505 createCount .Add (1 )
528506 time .Sleep (10 * time .Millisecond )
529507 return "" , errors .New ("create failed" )
530508 })
531509 c .Assert (err , qt .ErrorMatches , "create failed" )
532- }( )
510+ })
533511 }
534512
535513 wg .Wait ()
@@ -553,25 +531,21 @@ func TestDeleteFuncConcurrentCreate(t *testing.T) {
553531 var wg sync.WaitGroup
554532
555533 // Concurrently create new entries
556- wg .Add (1 )
557- go func () {
558- defer wg .Done ()
534+ wg .Go (func () {
559535 for i := 10 ; i < 20 ; i ++ {
560536 cache .GetOrCreate (i , func (key int ) (int , error ) {
561537 time .Sleep (time .Microsecond )
562538 return key * 2 , nil
563539 })
564540 }
565- }( )
541+ })
566542
567543 // Concurrently delete entries matching a condition
568- wg .Add (1 )
569- go func () {
570- defer wg .Done ()
544+ wg .Go (func () {
571545 cache .DeleteFunc (func (key int , value int ) bool {
572546 return key % 2 == 0
573547 })
574- }( )
548+ })
575549
576550 wg .Wait ()
577551 }
@@ -634,9 +608,7 @@ func TestConcurrentGetAndGetOrCreate(t *testing.T) {
634608
635609 // GetOrCreate goroutines
636610 for i := range 10 {
637- wg .Add (1 )
638- go func () {
639- defer wg .Done ()
611+ wg .Go (func () {
640612 for j := range 20 {
641613 key := (i + j ) % 15
642614 v , _ , err := cache .GetOrCreate (key , func (k int ) (int , error ) {
@@ -646,22 +618,20 @@ func TestConcurrentGetAndGetOrCreate(t *testing.T) {
646618 c .Assert (err , qt .IsNil )
647619 c .Assert (v , qt .Equals , key * 10 )
648620 }
649- }( )
621+ })
650622 }
651623
652624 // Get goroutines (may or may not find entries)
653625 for range 10 {
654- wg .Add (1 )
655- go func () {
656- defer wg .Done ()
626+ wg .Go (func () {
657627 for j := range 20 {
658628 key := j % 15
659629 v , found := cache .Get (key )
660630 if found {
661631 c .Assert (v , qt .Equals , key * 10 )
662632 }
663633 }
664- }( )
634+ })
665635 }
666636
667637 wg .Wait ()
0 commit comments