Skip to content

Commit 5902a9a

Browse files
committed
test: add example for SeqGroups.
Signed-off-by: ghosind <ghosind@gmail.com>
1 parent e793543 commit 5902a9a

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

seq_test.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,17 @@ func TestSeqGroupsWithEmptyGroup(t *testing.T) {
129129
a := assert.New(t)
130130
cnts := make([]atomic.Int32, 4)
131131
groups := make([][]async.AsyncFn, 0, 3)
132-
expectedCnts := []int{2, 0, 4, 5}
132+
expectedCnts := []int32{2, 0, 4, 5}
133133

134134
for i := 0; i < 4; i++ {
135135
tasks := make([]async.AsyncFn, 0)
136136
if i != 1 {
137137
idx := i
138138
for j := 0; j < i+2; j++ {
139139
tasks = append(tasks, func() {
140-
cnts[idx].Add(1)
140+
if idx == 0 || cnts[idx-1].Load() == expectedCnts[idx-1] {
141+
cnts[idx].Add(1)
142+
}
141143
})
142144
}
143145
}
@@ -220,3 +222,46 @@ func TestSeqGroupsWithContext(t *testing.T) {
220222
a.EqualNow(cnts[i].Load(), expectedCnts[i])
221223
}
222224
}
225+
226+
func ExampleSeqGroups() {
227+
flags := [][]bool{{false, false}, {false}, {true, true}}
228+
229+
err := async.SeqGroups([]async.AsyncFn{
230+
func() error {
231+
flags[0][0] = true
232+
return nil
233+
},
234+
func() error {
235+
flags[0][1] = true
236+
return nil
237+
},
238+
}, []async.AsyncFn{
239+
func() error {
240+
if !flags[0][0] || !flags[0][1] {
241+
return errors.New("unexpected error")
242+
}
243+
flags[1][0] = true
244+
return nil
245+
},
246+
}, []async.AsyncFn{
247+
func() error {
248+
if !flags[1][0] {
249+
return errors.New("unexpected error")
250+
}
251+
flags[2][0] = true
252+
return nil
253+
},
254+
func() error {
255+
if !flags[1][0] {
256+
return errors.New("unexpected error")
257+
}
258+
flags[2][1] = true
259+
return nil
260+
},
261+
})
262+
fmt.Println(err)
263+
fmt.Println(flags)
264+
// Output:
265+
// <nil>
266+
// [[true true] [true] [true true]]
267+
}

0 commit comments

Comments
 (0)