forked from cschleiden/go-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrgroup_test.go
More file actions
91 lines (67 loc) · 1.71 KB
/
Copy patherrgroup_test.go
File metadata and controls
91 lines (67 loc) · 1.71 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package sync
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
func Test_ErrGroup_Success(t *testing.T) {
s := NewScheduler()
ctx := Background()
s.NewCoroutine(ctx, func(ctx Context) error {
gctx, g := WithErrGroup(ctx)
g.Go(func(ctx Context) error { return nil })
g.Go(func(ctx Context) error { return nil })
err := g.Wait(gctx)
require.NoError(t, err)
return nil
})
err := s.Execute()
require.NoError(t, err)
require.Equal(t, 0, s.RunningCoroutines())
}
func Test_ErrGroup_FirstError(t *testing.T) {
s := NewScheduler()
ctx := Background()
s.NewCoroutine(ctx, func(ctx Context) error {
gctx, g := WithErrGroup(ctx)
e1 := errors.New("boom")
g.Go(func(ctx Context) error { return e1 })
g.Go(func(ctx Context) error { return nil })
err := g.Wait(gctx)
require.Equal(t, e1, err)
return nil
})
err := s.Execute()
require.NoError(t, err)
}
func Test_ErrGroup_GoAfterWait_Panics(t *testing.T) {
s := NewScheduler()
ctx := Background()
s.NewCoroutine(ctx, func(ctx Context) error {
gctx, g := WithErrGroup(ctx)
g.Go(func(ctx Context) error { return nil })
_ = g.Wait(gctx)
require.Panics(t, func() {
g.Go(func(ctx Context) error { return nil })
})
return nil
})
err := s.Execute()
require.NoError(t, err)
}
func Test_ErrGroup_MultipleErrors_FirstWins(t *testing.T) {
s := NewScheduler()
ctx := Background()
s.NewCoroutine(ctx, func(ctx Context) error {
gctx, g := WithErrGroup(ctx)
e1 := errors.New("first")
e2 := errors.New("second")
g.Go(func(ctx Context) error { return e1 })
g.Go(func(ctx Context) error { return e2 })
err := g.Wait(gctx)
require.Equal(t, e1, err)
return nil
})
err := s.Execute()
require.NoError(t, err)
}