generated from lovelysunlight/go-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conc_test.go
51 lines (42 loc) · 903 Bytes
/
conc_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package conc
import (
"context"
"fmt"
"runtime"
"sync/atomic"
"testing"
"github.com/stretchr/testify/require"
)
func TestWaitGroup(t *testing.T) {
t.Parallel()
t.Run("all spawned run", func(t *testing.T) {
t.Parallel()
var count atomic.Int64
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wg := New(ctx)
for i := 0; i < 100; i++ {
wg.Go(func(context.Context) error {
count.Add(1)
return nil
})
}
_ = wg.Wait()
require.Equal(t, count.Load(), int64(100))
})
t.Run("all spawned return err", func(t *testing.T) {
t.Parallel()
var count atomic.Int64
wg := New(context.Background())
for i := 0; i < 100; i++ {
wg.Go(func(context.Context) error {
return fmt.Errorf("error: %d", count.Add(1))
})
}
err := wg.Wait()
require.Equal(t, count.Load(), int64(100))
require.Error(t, err)
_ = wg
runtime.GC()
})
}