forked from grab/async
/
fork_join_test.go
58 lines (45 loc) · 1.24 KB
/
fork_join_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
// Copyright 2019 Grabtaxi Holdings PTE LTE (GRAB), All rights reserved.
// Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
package async
import (
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestForkJoin(t *testing.T) {
first := NewTask(func(context.Context) (any, error) {
return 1, nil
})
second := NewTask(func(context.Context) (any, error) {
return nil, errors.New("some error")
})
third := NewTask(func(context.Context) (any, error) {
return 3, nil
})
ForkJoin(context.Background(), []Task{first, second, third})
outcome1, error1 := first.Outcome()
assert.Equal(t, 1, outcome1)
assert.Nil(t, error1)
outcome2, error2 := second.Outcome()
assert.Nil(t, outcome2)
assert.NotNil(t, error2)
outcome3, error3 := third.Outcome()
assert.Equal(t, 3, outcome3)
assert.Nil(t, error3)
}
func ExampleForkJoin() {
first := NewTask(func(context.Context) (any, error) {
return 1, nil
})
second := NewTask(func(context.Context) (any, error) {
return nil, errors.New("some error")
})
ForkJoin(context.Background(), []Task{first, second})
fmt.Println(first.Outcome())
fmt.Println(second.Outcome())
// Output:
// 1 <nil>
// <nil> some error
}