diff --git a/_example/example01/main.go b/_example/example01/main.go index 833eba8..1edd800 100644 --- a/_example/example01/main.go +++ b/_example/example01/main.go @@ -21,19 +21,19 @@ func main() { defer q.Release() // assign tasks in queue - for i := 0; i < taskN; i++ { - go func(i int) { + for i := range taskN { + go func() { if err := q.QueueTask(func(ctx context.Context) error { rets <- fmt.Sprintf("Hi Gopher, handle the job: %02d", +i) return nil }); err != nil { panic(err) } - }(i) + }() } // wait until all tasks done - for i := 0; i < taskN; i++ { + for range taskN { fmt.Println("message:", <-rets) time.Sleep(20 * time.Millisecond) } diff --git a/_example/example02/main.go b/_example/example02/main.go index e117f27..0e82dc7 100644 --- a/_example/example02/main.go +++ b/_example/example02/main.go @@ -44,19 +44,19 @@ func main() { defer q.Release() // assign tasks in queue - for i := 0; i < taskN; i++ { - go func(i int) { + for i := range taskN { + go func() { if err := q.Queue(&job{ Name: "Gopher", Message: fmt.Sprintf("handle the job: %d", i+1), }); err != nil { log.Println(err) } - }(i) + }() } // wait until all tasks done - for i := 0; i < taskN; i++ { + for range taskN { fmt.Println("message:", <-rets) time.Sleep(20 * time.Millisecond) } diff --git a/benchmark_test.go b/benchmark_test.go index d97d9ce..c0bfb3c 100644 --- a/benchmark_test.go +++ b/benchmark_test.go @@ -53,13 +53,12 @@ func BenchmarkQueueTask(b *testing.B) { WithLogger(emptyLogger{}), ) b.ReportAllocs() - b.ResetTimer() m := job.NewTask(func(context.Context) error { return nil }) - for n := 0; n < b.N; n++ { + for b.Loop() { if err := q.queue(&m); err != nil { b.Fatal(err) } @@ -73,13 +72,12 @@ func BenchmarkQueue(b *testing.B) { WithLogger(emptyLogger{}), ) b.ReportAllocs() - b.ResetTimer() m := job.NewMessage(&mockMessage{ message: testMessageFoo, }) - for n := 0; n < b.N; n++ { + for b.Loop() { if err := q.queue(&m); err != nil { b.Fatal(err) } @@ -129,7 +127,7 @@ func BenchmarkRingWithTask(b *testing.B) { WithLogger(emptyLogger{}), ) - for n := 0; n < b.N; n++ { + for b.Loop() { _ = q.run(&task) } } diff --git a/handle_performance_test.go b/handle_performance_test.go index 33a430c..b60d76a 100644 --- a/handle_performance_test.go +++ b/handle_performance_test.go @@ -33,8 +33,7 @@ func BenchmarkHandleRetry(b *testing.B) { }, } b.ReportAllocs() - b.ResetTimer() - for range b.N { + for b.Loop() { attempts = 0 m.RetryCount = retries if err := q.handle(&m); err != nil { diff --git a/job/benchmark_test.go b/job/benchmark_test.go index 68f5714..67caabe 100644 --- a/job/benchmark_test.go +++ b/job/benchmark_test.go @@ -8,7 +8,7 @@ import ( func BenchmarkNewTask(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { NewTask(func(context.Context) error { return nil }, @@ -23,7 +23,7 @@ func BenchmarkNewTask(b *testing.B) { func BenchmarkNewMessage(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { _ = NewMessage(mockMessage{ message: "foo", }, @@ -38,7 +38,7 @@ func BenchmarkNewMessage(b *testing.B) { func BenchmarkNewOption(b *testing.B) { b.ReportAllocs() - for i := 0; i < b.N; i++ { + for b.Loop() { _ = NewOptions( AllowOption{ RetryCount: Int64(100), diff --git a/ring_performance_test.go b/ring_performance_test.go index 0196286..64fbcab 100644 --- a/ring_performance_test.go +++ b/ring_performance_test.go @@ -22,8 +22,7 @@ func BenchmarkRingBoundary(b *testing.B) { } } b.ReportAllocs() - b.ResetTimer() - for range b.N { + for b.Loop() { if err := w.Queue(&m); err != nil { b.Fatal(err) } diff --git a/ring_test.go b/ring_test.go index 79ddebb..d4c07eb 100644 --- a/ring_test.go +++ b/ring_test.go @@ -636,8 +636,7 @@ func BenchmarkRingQueue(b *testing.B) { w := NewRing() m := mockMessage{message: testMessageTest} - b.ResetTimer() - for i := 0; i < b.N; i++ { + for b.Loop() { for range 100 { _ = w.Queue(&m) } diff --git a/thread_test.go b/thread_test.go index 7a5b9fe..910df22 100644 --- a/thread_test.go +++ b/thread_test.go @@ -9,35 +9,35 @@ import ( func TestRoutineGroupRun(t *testing.T) { t.Run("execute single function", func(t *testing.T) { g := newRoutineGroup() - var counter int32 + var counter atomic.Int32 g.Run(func() { - atomic.AddInt32(&counter, 1) + counter.Add(1) }) g.Wait() - if atomic.LoadInt32(&counter) != 1 { - t.Errorf("expected counter to be 1, got %d", counter) + if counter.Load() != 1 { + t.Errorf("expected counter to be 1, got %d", counter.Load()) } }) t.Run("execute multiple functions", func(t *testing.T) { g := newRoutineGroup() - var counter int32 + var counter atomic.Int32 numRoutines := 10 for range numRoutines { g.Run(func() { - atomic.AddInt32(&counter, 1) + counter.Add(1) time.Sleep(10 * time.Millisecond) }) } g.Wait() - if atomic.LoadInt32(&counter) != int32(numRoutines) { - t.Errorf("expected counter to be %d, got %d", numRoutines, counter) + if counter.Load() != int32(numRoutines) { + t.Errorf("expected counter to be %d, got %d", numRoutines, counter.Load()) } }) }