Skip to content

Commit 736e568

Browse files
committed
test: more test cases for Paralleler.
1 parent 5277622 commit 736e568

File tree

1 file changed

+58
-4
lines changed

1 file changed

+58
-4
lines changed

paralleler_test.go

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,46 @@ func TestParalleler(t *testing.T) {
2525

2626
a.EqualNow(cnt.Load(), 0)
2727
_, err := p.Run()
28-
a.Nil(err)
28+
a.NilNow(err)
2929
a.EqualNow(cnt.Load(), 5)
3030
}
3131

32+
func TestParallelerWithoutTasks(t *testing.T) {
33+
a := assert.New(t)
34+
p := new(async.Paralleler)
35+
36+
out, err := p.Run()
37+
a.NilNow(err)
38+
a.EqualNow(out, [][]any{})
39+
40+
out, err = p.RunCompleted()
41+
a.NilNow(err)
42+
a.EqualNow(out, [][]any{})
43+
}
44+
45+
func TestParallelerRunWithFailure(t *testing.T) {
46+
a := assert.New(t)
47+
cnt := atomic.Int32{}
48+
expectedErr := errors.New("n = 2")
49+
50+
p := new(async.Paralleler).WithConcurrency(1)
51+
for i := 0; i < 5; i++ {
52+
n := i
53+
p.Add(func() error {
54+
cnt.Add(1)
55+
if n == 2 {
56+
return expectedErr
57+
}
58+
return nil
59+
})
60+
}
61+
62+
out, err := p.Run()
63+
a.NotNilNow(err)
64+
a.EqualNow(out, [][]any{{nil}, {nil}, {expectedErr}, {}, {}})
65+
a.EqualNow(cnt.Load(), 3)
66+
}
67+
3268
func TestParallelerAddTasks(t *testing.T) {
3369
a := assert.New(t)
3470
cnt := atomic.Int32{}
@@ -41,7 +77,7 @@ func TestParallelerAddTasks(t *testing.T) {
4177
}
4278

4379
_, err := p.Run()
44-
a.Nil(err)
80+
a.NilNow(err)
4581
a.EqualNow(cnt.Load(), 5)
4682

4783
for i := 0; i < 3; i++ {
@@ -108,7 +144,7 @@ func TestParallelerWithContext(t *testing.T) {
108144
ctx, canFunc := context.WithTimeout(context.Background(), 80*time.Millisecond)
109145
defer canFunc()
110146

111-
p := new(async.Paralleler).WithConcurrency(2).WithContext(ctx)
147+
p := new(async.Paralleler).WithConcurrency(1).WithContext(ctx)
112148
for i := 0; i < 5; i++ {
113149
p.Add(func(ctx context.Context) {
114150
select {
@@ -123,12 +159,30 @@ func TestParallelerWithContext(t *testing.T) {
123159

124160
_, err := p.Run()
125161
a.EqualNow(err, async.ErrContextCanceled)
126-
a.EqualNow(cnt.Load(), 2)
162+
a.EqualNow(cnt.Load(), 1)
127163
}
128164

129165
func TestParallelerRunCompleted(t *testing.T) {
130166
a := assert.New(t)
131167
cnt := atomic.Int32{}
168+
169+
p := new(async.Paralleler).WithConcurrency(2)
170+
for i := 0; i < 5; i++ {
171+
p.Add(func() error {
172+
cnt.Add(1)
173+
return nil
174+
})
175+
}
176+
177+
out, err := p.RunCompleted()
178+
a.NilNow(err)
179+
a.EqualNow(out, [][]any{{nil}, {nil}, {nil}, {nil}, {nil}})
180+
a.EqualNow(cnt.Load(), 5)
181+
}
182+
183+
func TestParallelerRunCompletedWithFailure(t *testing.T) {
184+
a := assert.New(t)
185+
cnt := atomic.Int32{}
132186
expectedErr := errors.New("n = 2")
133187

134188
p := new(async.Paralleler).WithConcurrency(2)

0 commit comments

Comments
 (0)