File tree Expand file tree Collapse file tree 3 files changed +21
-20
lines changed Expand file tree Collapse file tree 3 files changed +21
-20
lines changed Original file line number Diff line number Diff line change @@ -23,22 +23,22 @@ func TestOnce(t *testing.T) {
2323
2424func TestOnceConcurrent (t * testing.T ) {
2525var once Once [int32 ]
26- var count int32
26+ var count atomic. Int32
2727var wg sync.WaitGroup
2828
2929for i := 0 ; i < 10 ; i ++ {
3030wg .Add (1 )
3131go func () {
3232defer wg .Done ()
3333result , _ := once .Do (func () (int32 , error ) {
34- newCount := atomic . AddInt32 ( & count , 1 )
34+ newCount := count . Add ( 1 )
3535return newCount , nil
3636})
37- atomic . StoreInt32 ( & count , result )
37+ count . Store ( result )
3838}()
3939}
4040wg .Wait ()
41- assert .Equal (t , count , 1 )
41+ assert .Equal (t , int ( count . Load ()) , 1 )
4242}
4343
4444func TestOncePanic (t * testing.T ) {
Original file line number Diff line number Diff line change @@ -13,8 +13,8 @@ import (
1313type WaitGroupContext struct {
1414ctx context.Context
1515done chan struct {}
16- counter int32
17- state int32
16+ counter atomic. Int32
17+ state atomic. Int32
1818}
1919
2020// NewWaitGroupContext returns a new WaitGroupContext with Context ctx.
@@ -29,10 +29,10 @@ func NewWaitGroupContext(ctx context.Context) *WaitGroupContext {
2929// If the counter becomes zero, all goroutines blocked on Wait are released.
3030// If the counter goes negative, Add panics.
3131func (wgc * WaitGroupContext ) Add (delta int ) {
32- counter := atomic . AddInt32 ( & wgc .counter , int32 (delta ))
33- if counter == 0 && atomic . CompareAndSwapInt32 ( & wgc .state , 0 , 1 ) {
32+ counter := wgc .counter . Add ( int32 (delta ))
33+ if counter == 0 && wgc .state . CompareAndSwap ( 0 , 1 ) {
3434wgc .release ()
35- } else if counter < 0 && atomic . LoadInt32 ( & wgc .state ) == 0 {
35+ } else if counter < 0 && wgc .state . Load ( ) == 0 {
3636panic ("async: negative WaitGroupContext counter" )
3737}
3838}
Original file line number Diff line number Diff line change @@ -2,44 +2,45 @@ package async
22
33import (
44"context"
5+ "sync/atomic"
56"testing"
67"time"
78
89"github.com/reugn/async/internal/assert"
910)
1011
1112func TestWaitGroupContext (t * testing.T ) {
12- result := 0
13+ var result atomic. Int32
1314wgc := NewWaitGroupContext (context .Background ())
1415wgc .Add (2 )
1516
1617go func () {
1718defer wgc .Done ()
1819time .Sleep (time .Millisecond * 10 )
19- result ++
20+ result . Add ( 1 )
2021}()
2122go func () {
2223defer wgc .Done ()
2324time .Sleep (time .Millisecond * 20 )
24- result += 2
25+ result . Add ( 2 )
2526}()
2627go func () {
2728wgc .Wait ()
28- result += 3
29+ result . Add ( 3 )
2930}()
3031
3132wgc .Wait ()
3233time .Sleep (time .Millisecond * 10 )
3334
34- assert .Equal (t , result , 6 )
35+ assert .Equal (t , int ( result . Load ()) , 6 )
3536}
3637
3738func TestWaitGroupContextCanceled (t * testing.T ) {
38- result := 0
39+ var result atomic. Int32
3940ctx , cancelFunc := context .WithCancel (context .Background ())
4041go func () {
4142time .Sleep (time .Millisecond * 100 )
42- result += 10
43+ result . Add ( 10 )
4344cancelFunc ()
4445}()
4546wgc := NewWaitGroupContext (ctx )
@@ -48,22 +49,22 @@ func TestWaitGroupContextCanceled(t *testing.T) {
4849go func () {
4950defer wgc .Done ()
5051time .Sleep (time .Millisecond * 10 )
51- result ++
52+ result . Add ( 1 )
5253}()
5354go func () {
5455defer wgc .Done ()
5556time .Sleep (time .Millisecond * 300 )
56- result += 2
57+ result . Add ( 2 )
5758}()
5859go func () {
5960wgc .Wait ()
60- result += 100
61+ result . Add ( 100 )
6162}()
6263
6364wgc .Wait ()
6465time .Sleep (time .Millisecond * 10 )
6566
66- assert .Equal (t , result , 111 )
67+ assert .Equal (t , int ( result . Load ()) , 111 )
6768}
6869
6970func TestWaitGroupContextPanic (t * testing.T ) {
You can’t perform that action at this time.
0 commit comments