-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_test.go
76 lines (73 loc) · 1.43 KB
/
parallel_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
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
package pp_test
import (
"context"
"sync"
"testing"
pp "github.com/sonalys/pipego"
"github.com/stretchr/testify/require"
)
func Test_Parallel(t *testing.T) {
ctx := context.Background()
t.Run("empty", func(t *testing.T) {
err := pp.Parallel(5)(ctx)
require.NoError(t, err)
})
t.Run("is parallel", func(t *testing.T) {
var wg, ready sync.WaitGroup
wg.Add(1)
ready.Add(2)
type state struct {
a, b int
}
var s state
err := pp.Parallel(2,
func(_ context.Context) (err error) {
s.a = 1
ready.Done()
wg.Wait()
return nil
},
func(_ context.Context) (err error) {
s.b = 2
ready.Done()
wg.Wait()
return nil
},
)(ctx)
ready.Wait()
require.NoError(t, err)
require.Equal(t, 1, s.a)
require.Equal(t, 2, s.b)
wg.Done()
})
t.Run("runs at the specified parallelism number", func(t *testing.T) {
var wg, ready sync.WaitGroup
wg.Add(1)
ready.Add(1)
type state struct {
a, b int
}
var s state
go require.NotPanics(t, func() {
err := pp.Parallel(1,
func(_ context.Context) (err error) {
s.a = 1
ready.Done()
wg.Wait()
return nil
},
func(_ context.Context) (err error) {
s.b = 1
ready.Done() // If you set parallelism = 2 you will see this panics, because weight is 1.
wg.Wait()
return nil
},
)(ctx)
require.NoError(t, err)
})
ready.Wait()
require.NotEqual(t, s.b, s.a)
ready.Add(1)
wg.Done()
})
}