diff --git a/client_test.go b/client_test.go index 1c4173f..ae6086e 100644 --- a/client_test.go +++ b/client_test.go @@ -252,10 +252,10 @@ func TestClientInvalidURI(t *testing.T) { t.Parallel() ln := fasthttputil.NewInmemoryListener() - requests := int64(0) + var requests atomic.Int64 s := &Server{ Handler: func(_ *RequestCtx) { - atomic.AddInt64(&requests, 1) + requests.Add(1) }, } go s.Serve(ln) //nolint:errcheck @@ -275,7 +275,7 @@ func TestClientInvalidURI(t *testing.T) { if err == nil { t.Fatal("expected error (missing required Host header in request)") } - if n := atomic.LoadInt64(&requests); n != 0 { + if n := requests.Load(); n != 0 { t.Fatalf("0 requests expected, got %d", n) } } @@ -1470,12 +1470,12 @@ func TestHostClientMaxConnDuration(t *testing.T) { ln := fasthttputil.NewInmemoryListener() - connectionCloseCount := uint32(0) + var connectionCloseCount atomic.Uint32 s := &Server{ Handler: func(ctx *RequestCtx) { ctx.WriteString("abcd") //nolint:errcheck if ctx.Request.ConnectionClose() { - atomic.AddUint32(&connectionCloseCount, 1) + connectionCloseCount.Add(1) } }, } @@ -1518,7 +1518,7 @@ func TestHostClientMaxConnDuration(t *testing.T) { t.Fatalf("timeout") } - if connectionCloseCount == 0 { + if connectionCloseCount.Load() == 0 { t.Fatalf("expecting at least one 'Connection: close' request header") } } @@ -2936,7 +2936,7 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) { MaxConnWaitTimeout: 10 * time.Millisecond, } - var errNoFreeConnsCount uint32 + var errNoFreeConnsCount atomic.Uint32 for i := 0; i < 5; i++ { wg.Add(1) go func() { @@ -2952,7 +2952,7 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) { if err != ErrNoFreeConns { t.Errorf("unexpected error: %v. Expecting %v", err, ErrNoFreeConns) } - atomic.AddUint32(&errNoFreeConnsCount, 1) + errNoFreeConnsCount.Add(1) } else { if resp.StatusCode() != StatusOK { t.Errorf("unexpected status code %d. Expecting %d", resp.StatusCode(), StatusOK) @@ -2976,8 +2976,8 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) { if c.connsWait.len() > 0 { t.Errorf("connsWait has %v items remaining", c.connsWait.len()) } - if errNoFreeConnsCount == 0 { - t.Errorf("unexpected errorCount: %d. Expecting > 0", errNoFreeConnsCount) + if count := errNoFreeConnsCount.Load(); count == 0 { + t.Errorf("unexpected errorCount: %d. Expecting > 0", count) } if err := ln.Close(); err != nil { t.Fatalf("unexpected error: %v", err) @@ -3033,7 +3033,7 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) { MaxConnWaitTimeout: maxConnWaitTimeout, } - var errTimeoutCount uint32 + var errTimeoutCount atomic.Uint32 for i := 0; i < 5; i++ { wg.Add(1) go func() { @@ -3049,7 +3049,7 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) { if err != ErrTimeout { t.Errorf("unexpected error: %v. Expecting %v", err, ErrTimeout) } - atomic.AddUint32(&errTimeoutCount, 1) + errTimeoutCount.Add(1) } else { if resp.StatusCode() != StatusOK { t.Errorf("unexpected status code %d. Expecting %d", resp.StatusCode(), StatusOK) @@ -3077,8 +3077,8 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) { w.mu.Unlock() } c.connsLock.Unlock() - if errTimeoutCount == 0 { - t.Errorf("unexpected errTimeoutCount: %d. Expecting > 0", errTimeoutCount) + if count := errTimeoutCount.Load(); count == 0 { + t.Errorf("unexpected errTimeoutCount: %d. Expecting > 0", count) } if err := ln.Close(); err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/client_timing_test.go b/client_timing_test.go index ece0487..738655c 100644 --- a/client_timing_test.go +++ b/client_timing_test.go @@ -101,9 +101,9 @@ func BenchmarkClientGetTimeoutFastServer(b *testing.B) { }, } - nn := uint32(0) + var nn atomic.Uint32 b.RunParallel(func(pb *testing.PB) { - url := fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)) + url := fmt.Sprintf("http://foobar%d.com/aaa/bbb", nn.Add(1)) var statusCode int var bodyBuf []byte var err error @@ -132,11 +132,11 @@ func BenchmarkClientDoFastServer(b *testing.B) { MaxConnsPerHost: runtime.GOMAXPROCS(-1), } - nn := uint32(0) + var nn atomic.Uint32 b.RunParallel(func(pb *testing.PB) { var req Request var resp Response - req.Header.SetRequestURI(fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1))) + req.Header.SetRequestURI(fmt.Sprintf("http://foobar%d.com/aaa/bbb", nn.Add(1))) for pb.Next() { if err := c.Do(&req, &resp); err != nil { b.Fatalf("unexpected error: %v", err) @@ -163,9 +163,9 @@ func BenchmarkNetHTTPClientDoFastServer(b *testing.B) { }, } - nn := uint32(0) + var nn atomic.Uint32 b.RunParallel(func(pb *testing.PB) { - req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)), http.NoBody) + req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", nn.Add(1)), http.NoBody) if err != nil { b.Fatalf("unexpected error: %v", err) } diff --git a/coarsetime_test.go b/coarsetime_test.go index b2f2334..5b6a942 100644 --- a/coarsetime_test.go +++ b/coarsetime_test.go @@ -7,31 +7,31 @@ import ( ) func BenchmarkCoarseTimeNow(b *testing.B) { - var zeroTimeCount uint64 + var zeroTimeCount atomic.Uint64 b.RunParallel(func(pb *testing.PB) { for pb.Next() { t := CoarseTimeNow() if t.IsZero() { - atomic.AddUint64(&zeroTimeCount, 1) + zeroTimeCount.Add(1) } } }) - if zeroTimeCount > 0 { + if zeroTimeCount.Load() > 0 { b.Fatalf("zeroTimeCount must be zero") } } func BenchmarkTimeNow(b *testing.B) { - var zeroTimeCount uint64 + var zeroTimeCount atomic.Uint64 b.RunParallel(func(pb *testing.PB) { for pb.Next() { t := time.Now() if t.IsZero() { - atomic.AddUint64(&zeroTimeCount, 1) + zeroTimeCount.Add(1) } } }) - if zeroTimeCount > 0 { + if zeroTimeCount.Load() > 0 { b.Fatalf("zeroTimeCount must be zero") } } diff --git a/server_timing_test.go b/server_timing_test.go index 427e1dd..0717725 100644 --- a/server_timing_test.go +++ b/server_timing_test.go @@ -170,11 +170,11 @@ func BenchmarkServerTimeoutError(b *testing.B) { clientsCount := 10 requestsPerConn := 1 ch := make(chan struct{}, b.N) - n := uint32(0) + var n atomic.Uint32 responseBody := []byte("123") s := &Server{ Handler: func(ctx *RequestCtx) { - if atomic.AddUint32(&n, 1)&7 == 0 { + if n.Add(1)&7 == 0 { ctx.TimeoutError("xxx") go func() { ctx.Success("foobar", responseBody) @@ -196,7 +196,7 @@ type fakeServerConn struct { ln *fakeListener requestsCount int pos int - closed uint32 + closed atomic.Bool } func (c *fakeServerConn) Read(b []byte) (int, error) { @@ -235,7 +235,7 @@ func (c *fakeServerConn) RemoteAddr() net.Addr { } func (c *fakeServerConn) Close() error { - if atomic.AddUint32(&c.closed, 1) == 1 { + if c.closed.CompareAndSwap(false, true) { c.ln.ch <- c } return nil @@ -283,7 +283,7 @@ func (ln *fakeListener) Accept() (net.Conn, error) { c := <-ln.ch c.requestsCount = requestsCount - c.closed = 0 + c.closed.Store(false) c.pos = 0 return c, nil diff --git a/stackless/func_test.go b/stackless/func_test.go index 0d8f1d5..36c8034 100644 --- a/stackless/func_test.go +++ b/stackless/func_test.go @@ -10,9 +10,9 @@ import ( func TestNewFuncSimple(t *testing.T) { t.Parallel() - var n uint64 + var n atomic.Uint64 f := NewFunc(func(ctx any) { - atomic.AddUint64(&n, uint64(ctx.(int))) + n.Add(uint64(ctx.(int))) }) iterations := 4 * 1024 @@ -21,20 +21,20 @@ func TestNewFuncSimple(t *testing.T) { t.Fatalf("f mustn't return false") } } - if n != uint64(2*iterations) { - t.Fatalf("Unexpected n: %d. Expecting %d", n, 2*iterations) + if got := n.Load(); got != uint64(2*iterations) { + t.Fatalf("Unexpected n: %d. Expecting %d", got, 2*iterations) } } func TestNewFuncMulti(t *testing.T) { t.Parallel() - var n1, n2 uint64 + var n1, n2 atomic.Uint64 f1 := NewFunc(func(ctx any) { - atomic.AddUint64(&n1, uint64(ctx.(int))) + n1.Add(uint64(ctx.(int))) }) f2 := NewFunc(func(ctx any) { - atomic.AddUint64(&n2, uint64(ctx.(int))) + n2.Add(uint64(ctx.(int))) }) iterations := 4 * 1024 @@ -81,10 +81,10 @@ func TestNewFuncMulti(t *testing.T) { t.Fatalf("timeout") } - if n1 != uint64(3*iterations) { - t.Fatalf("unexpected n1: %d. Expecting %d", n1, 3*iterations) + if got1 := n1.Load(); got1 != uint64(3*iterations) { + t.Fatalf("unexpected n1: %d. Expecting %d", got1, 3*iterations) } - if n2 != uint64(5*iterations) { - t.Fatalf("unexpected n2: %d. Expecting %d", n2, 5*iterations) + if got2 := n2.Load(); got2 != uint64(5*iterations) { + t.Fatalf("unexpected n2: %d. Expecting %d", got2, 5*iterations) } } diff --git a/stackless/func_timing_test.go b/stackless/func_timing_test.go index 8335e4d..9708a88 100644 --- a/stackless/func_timing_test.go +++ b/stackless/func_timing_test.go @@ -6,9 +6,9 @@ import ( ) func BenchmarkFuncOverhead(b *testing.B) { - var n uint64 + var n atomic.Uint64 f := NewFunc(func(ctx any) { - atomic.AddUint64(&n, *(ctx.(*uint64))) + n.Add(*(ctx.(*uint64))) }) b.RunParallel(func(pb *testing.PB) { x := uint64(1) @@ -18,15 +18,15 @@ func BenchmarkFuncOverhead(b *testing.B) { } } }) - if n != uint64(b.N) { - b.Fatalf("unexpected n: %d. Expecting %d", n, b.N) + if got := n.Load(); got != uint64(b.N) { + b.Fatalf("unexpected n: %d. Expecting %d", got, b.N) } } func BenchmarkFuncPure(b *testing.B) { - var n uint64 + var n atomic.Uint64 f := func(x *uint64) { - atomic.AddUint64(&n, *x) + n.Add(*x) } b.RunParallel(func(pb *testing.PB) { x := uint64(1) @@ -34,7 +34,7 @@ func BenchmarkFuncPure(b *testing.B) { f(&x) } }) - if n != uint64(b.N) { - b.Fatalf("unexpected n: %d. Expecting %d", n, b.N) + if got := n.Load(); got != uint64(b.N) { + b.Fatalf("unexpected n: %d. Expecting %d", got, b.N) } }