Skip to content

Commit 51b4947

Browse files
authored
test: replace atomic operations with atomic types (#2048)
1 parent 1d8fe19 commit 51b4947

File tree

6 files changed

+50
-50
lines changed

6 files changed

+50
-50
lines changed

client_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,10 +252,10 @@ func TestClientInvalidURI(t *testing.T) {
252252
t.Parallel()
253253

254254
ln := fasthttputil.NewInmemoryListener()
255-
requests := int64(0)
255+
var requests atomic.Int64
256256
s := &Server{
257257
Handler: func(_ *RequestCtx) {
258-
atomic.AddInt64(&requests, 1)
258+
requests.Add(1)
259259
},
260260
}
261261
go s.Serve(ln) //nolint:errcheck
@@ -275,7 +275,7 @@ func TestClientInvalidURI(t *testing.T) {
275275
if err == nil {
276276
t.Fatal("expected error (missing required Host header in request)")
277277
}
278-
if n := atomic.LoadInt64(&requests); n != 0 {
278+
if n := requests.Load(); n != 0 {
279279
t.Fatalf("0 requests expected, got %d", n)
280280
}
281281
}
@@ -1470,12 +1470,12 @@ func TestHostClientMaxConnDuration(t *testing.T) {
14701470

14711471
ln := fasthttputil.NewInmemoryListener()
14721472

1473-
connectionCloseCount := uint32(0)
1473+
var connectionCloseCount atomic.Uint32
14741474
s := &Server{
14751475
Handler: func(ctx *RequestCtx) {
14761476
ctx.WriteString("abcd") //nolint:errcheck
14771477
if ctx.Request.ConnectionClose() {
1478-
atomic.AddUint32(&connectionCloseCount, 1)
1478+
connectionCloseCount.Add(1)
14791479
}
14801480
},
14811481
}
@@ -1518,7 +1518,7 @@ func TestHostClientMaxConnDuration(t *testing.T) {
15181518
t.Fatalf("timeout")
15191519
}
15201520

1521-
if connectionCloseCount == 0 {
1521+
if connectionCloseCount.Load() == 0 {
15221522
t.Fatalf("expecting at least one 'Connection: close' request header")
15231523
}
15241524
}
@@ -2936,7 +2936,7 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) {
29362936
MaxConnWaitTimeout: 10 * time.Millisecond,
29372937
}
29382938

2939-
var errNoFreeConnsCount uint32
2939+
var errNoFreeConnsCount atomic.Uint32
29402940
for i := 0; i < 5; i++ {
29412941
wg.Add(1)
29422942
go func() {
@@ -2952,7 +2952,7 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) {
29522952
if err != ErrNoFreeConns {
29532953
t.Errorf("unexpected error: %v. Expecting %v", err, ErrNoFreeConns)
29542954
}
2955-
atomic.AddUint32(&errNoFreeConnsCount, 1)
2955+
errNoFreeConnsCount.Add(1)
29562956
} else {
29572957
if resp.StatusCode() != StatusOK {
29582958
t.Errorf("unexpected status code %d. Expecting %d", resp.StatusCode(), StatusOK)
@@ -2976,8 +2976,8 @@ func TestHostClientMaxConnWaitTimeoutError(t *testing.T) {
29762976
if c.connsWait.len() > 0 {
29772977
t.Errorf("connsWait has %v items remaining", c.connsWait.len())
29782978
}
2979-
if errNoFreeConnsCount == 0 {
2980-
t.Errorf("unexpected errorCount: %d. Expecting > 0", errNoFreeConnsCount)
2979+
if count := errNoFreeConnsCount.Load(); count == 0 {
2980+
t.Errorf("unexpected errorCount: %d. Expecting > 0", count)
29812981
}
29822982
if err := ln.Close(); err != nil {
29832983
t.Fatalf("unexpected error: %v", err)
@@ -3033,7 +3033,7 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) {
30333033
MaxConnWaitTimeout: maxConnWaitTimeout,
30343034
}
30353035

3036-
var errTimeoutCount uint32
3036+
var errTimeoutCount atomic.Uint32
30373037
for i := 0; i < 5; i++ {
30383038
wg.Add(1)
30393039
go func() {
@@ -3049,7 +3049,7 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) {
30493049
if err != ErrTimeout {
30503050
t.Errorf("unexpected error: %v. Expecting %v", err, ErrTimeout)
30513051
}
3052-
atomic.AddUint32(&errTimeoutCount, 1)
3052+
errTimeoutCount.Add(1)
30533053
} else {
30543054
if resp.StatusCode() != StatusOK {
30553055
t.Errorf("unexpected status code %d. Expecting %d", resp.StatusCode(), StatusOK)
@@ -3077,8 +3077,8 @@ func TestHostClientMaxConnWaitTimeoutWithEarlierDeadline(t *testing.T) {
30773077
w.mu.Unlock()
30783078
}
30793079
c.connsLock.Unlock()
3080-
if errTimeoutCount == 0 {
3081-
t.Errorf("unexpected errTimeoutCount: %d. Expecting > 0", errTimeoutCount)
3080+
if count := errTimeoutCount.Load(); count == 0 {
3081+
t.Errorf("unexpected errTimeoutCount: %d. Expecting > 0", count)
30823082
}
30833083
if err := ln.Close(); err != nil {
30843084
t.Fatalf("unexpected error: %v", err)

client_timing_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ func BenchmarkClientGetTimeoutFastServer(b *testing.B) {
101101
},
102102
}
103103

104-
nn := uint32(0)
104+
var nn atomic.Uint32
105105
b.RunParallel(func(pb *testing.PB) {
106-
url := fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1))
106+
url := fmt.Sprintf("http://foobar%d.com/aaa/bbb", nn.Add(1))
107107
var statusCode int
108108
var bodyBuf []byte
109109
var err error
@@ -132,11 +132,11 @@ func BenchmarkClientDoFastServer(b *testing.B) {
132132
MaxConnsPerHost: runtime.GOMAXPROCS(-1),
133133
}
134134

135-
nn := uint32(0)
135+
var nn atomic.Uint32
136136
b.RunParallel(func(pb *testing.PB) {
137137
var req Request
138138
var resp Response
139-
req.Header.SetRequestURI(fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)))
139+
req.Header.SetRequestURI(fmt.Sprintf("http://foobar%d.com/aaa/bbb", nn.Add(1)))
140140
for pb.Next() {
141141
if err := c.Do(&req, &resp); err != nil {
142142
b.Fatalf("unexpected error: %v", err)
@@ -163,9 +163,9 @@ func BenchmarkNetHTTPClientDoFastServer(b *testing.B) {
163163
},
164164
}
165165

166-
nn := uint32(0)
166+
var nn atomic.Uint32
167167
b.RunParallel(func(pb *testing.PB) {
168-
req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", atomic.AddUint32(&nn, 1)), http.NoBody)
168+
req, err := http.NewRequest(MethodGet, fmt.Sprintf("http://foobar%d.com/aaa/bbb", nn.Add(1)), http.NoBody)
169169
if err != nil {
170170
b.Fatalf("unexpected error: %v", err)
171171
}

coarsetime_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ import (
77
)
88

99
func BenchmarkCoarseTimeNow(b *testing.B) {
10-
var zeroTimeCount uint64
10+
var zeroTimeCount atomic.Uint64
1111
b.RunParallel(func(pb *testing.PB) {
1212
for pb.Next() {
1313
t := CoarseTimeNow()
1414
if t.IsZero() {
15-
atomic.AddUint64(&zeroTimeCount, 1)
15+
zeroTimeCount.Add(1)
1616
}
1717
}
1818
})
19-
if zeroTimeCount > 0 {
19+
if zeroTimeCount.Load() > 0 {
2020
b.Fatalf("zeroTimeCount must be zero")
2121
}
2222
}
2323

2424
func BenchmarkTimeNow(b *testing.B) {
25-
var zeroTimeCount uint64
25+
var zeroTimeCount atomic.Uint64
2626
b.RunParallel(func(pb *testing.PB) {
2727
for pb.Next() {
2828
t := time.Now()
2929
if t.IsZero() {
30-
atomic.AddUint64(&zeroTimeCount, 1)
30+
zeroTimeCount.Add(1)
3131
}
3232
}
3333
})
34-
if zeroTimeCount > 0 {
34+
if zeroTimeCount.Load() > 0 {
3535
b.Fatalf("zeroTimeCount must be zero")
3636
}
3737
}

server_timing_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,11 @@ func BenchmarkServerTimeoutError(b *testing.B) {
170170
clientsCount := 10
171171
requestsPerConn := 1
172172
ch := make(chan struct{}, b.N)
173-
n := uint32(0)
173+
var n atomic.Uint32
174174
responseBody := []byte("123")
175175
s := &Server{
176176
Handler: func(ctx *RequestCtx) {
177-
if atomic.AddUint32(&n, 1)&7 == 0 {
177+
if n.Add(1)&7 == 0 {
178178
ctx.TimeoutError("xxx")
179179
go func() {
180180
ctx.Success("foobar", responseBody)
@@ -196,7 +196,7 @@ type fakeServerConn struct {
196196
ln *fakeListener
197197
requestsCount int
198198
pos int
199-
closed uint32
199+
closed atomic.Bool
200200
}
201201

202202
func (c *fakeServerConn) Read(b []byte) (int, error) {
@@ -235,7 +235,7 @@ func (c *fakeServerConn) RemoteAddr() net.Addr {
235235
}
236236

237237
func (c *fakeServerConn) Close() error {
238-
if atomic.AddUint32(&c.closed, 1) == 1 {
238+
if c.closed.CompareAndSwap(false, true) {
239239
c.ln.ch <- c
240240
}
241241
return nil
@@ -283,7 +283,7 @@ func (ln *fakeListener) Accept() (net.Conn, error) {
283283

284284
c := <-ln.ch
285285
c.requestsCount = requestsCount
286-
c.closed = 0
286+
c.closed.Store(false)
287287
c.pos = 0
288288

289289
return c, nil

stackless/func_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
func TestNewFuncSimple(t *testing.T) {
1111
t.Parallel()
1212

13-
var n uint64
13+
var n atomic.Uint64
1414
f := NewFunc(func(ctx any) {
15-
atomic.AddUint64(&n, uint64(ctx.(int)))
15+
n.Add(uint64(ctx.(int)))
1616
})
1717

1818
iterations := 4 * 1024
@@ -21,20 +21,20 @@ func TestNewFuncSimple(t *testing.T) {
2121
t.Fatalf("f mustn't return false")
2222
}
2323
}
24-
if n != uint64(2*iterations) {
25-
t.Fatalf("Unexpected n: %d. Expecting %d", n, 2*iterations)
24+
if got := n.Load(); got != uint64(2*iterations) {
25+
t.Fatalf("Unexpected n: %d. Expecting %d", got, 2*iterations)
2626
}
2727
}
2828

2929
func TestNewFuncMulti(t *testing.T) {
3030
t.Parallel()
3131

32-
var n1, n2 uint64
32+
var n1, n2 atomic.Uint64
3333
f1 := NewFunc(func(ctx any) {
34-
atomic.AddUint64(&n1, uint64(ctx.(int)))
34+
n1.Add(uint64(ctx.(int)))
3535
})
3636
f2 := NewFunc(func(ctx any) {
37-
atomic.AddUint64(&n2, uint64(ctx.(int)))
37+
n2.Add(uint64(ctx.(int)))
3838
})
3939

4040
iterations := 4 * 1024
@@ -81,10 +81,10 @@ func TestNewFuncMulti(t *testing.T) {
8181
t.Fatalf("timeout")
8282
}
8383

84-
if n1 != uint64(3*iterations) {
85-
t.Fatalf("unexpected n1: %d. Expecting %d", n1, 3*iterations)
84+
if got1 := n1.Load(); got1 != uint64(3*iterations) {
85+
t.Fatalf("unexpected n1: %d. Expecting %d", got1, 3*iterations)
8686
}
87-
if n2 != uint64(5*iterations) {
88-
t.Fatalf("unexpected n2: %d. Expecting %d", n2, 5*iterations)
87+
if got2 := n2.Load(); got2 != uint64(5*iterations) {
88+
t.Fatalf("unexpected n2: %d. Expecting %d", got2, 5*iterations)
8989
}
9090
}

stackless/func_timing_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
)
77

88
func BenchmarkFuncOverhead(b *testing.B) {
9-
var n uint64
9+
var n atomic.Uint64
1010
f := NewFunc(func(ctx any) {
11-
atomic.AddUint64(&n, *(ctx.(*uint64)))
11+
n.Add(*(ctx.(*uint64)))
1212
})
1313
b.RunParallel(func(pb *testing.PB) {
1414
x := uint64(1)
@@ -18,23 +18,23 @@ func BenchmarkFuncOverhead(b *testing.B) {
1818
}
1919
}
2020
})
21-
if n != uint64(b.N) {
22-
b.Fatalf("unexpected n: %d. Expecting %d", n, b.N)
21+
if got := n.Load(); got != uint64(b.N) {
22+
b.Fatalf("unexpected n: %d. Expecting %d", got, b.N)
2323
}
2424
}
2525

2626
func BenchmarkFuncPure(b *testing.B) {
27-
var n uint64
27+
var n atomic.Uint64
2828
f := func(x *uint64) {
29-
atomic.AddUint64(&n, *x)
29+
n.Add(*x)
3030
}
3131
b.RunParallel(func(pb *testing.PB) {
3232
x := uint64(1)
3333
for pb.Next() {
3434
f(&x)
3535
}
3636
})
37-
if n != uint64(b.N) {
38-
b.Fatalf("unexpected n: %d. Expecting %d", n, b.N)
37+
if got := n.Load(); got != uint64(b.N) {
38+
b.Fatalf("unexpected n: %d. Expecting %d", got, b.N)
3939
}
4040
}

0 commit comments

Comments
 (0)