-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrgroup_test.go
86 lines (73 loc) · 1.78 KB
/
errgroup_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
77
78
79
80
81
82
83
84
85
86
package errgroup_test
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/heppu/errgroup"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestErrGroup_MultipleErrors(t *testing.T) {
err1 := errors.New("err1")
err2 := errors.New("err2")
eg := &errgroup.ErrGroup{}
eg.Go(func() error { return err1 })
eg.Go(func() error { return err2 })
err := eg.Wait()
assert.ErrorIs(t, err, err1)
assert.ErrorIs(t, err, err2)
}
func TestErrGroup_Mixed(t *testing.T) {
err1 := errors.New("err1")
err2 := errors.New("err2")
eg := &errgroup.ErrGroup{}
eg.Go(func() error { return nil })
eg.Go(func() error { return err1 })
eg.Go(func() error { return nil })
eg.Go(func() error { return err2 })
eg.Go(func() error { return nil })
err := eg.Wait()
assert.ErrorIs(t, err, err1)
assert.ErrorIs(t, err, err2)
}
func TestErrGroup_NoError(t *testing.T) {
eg := &errgroup.ErrGroup{}
eg.Go(func() error { return nil })
eg.Go(func() error { return nil })
eg.Go(func() error { return nil })
err := eg.Wait()
require.NoError(t, err)
}
func TestErrGroup_NoTask(t *testing.T) {
eg := &errgroup.ErrGroup{}
err := eg.Wait()
require.NoError(t, err)
}
// This example fetches several URLs concurrently,
// using a WaitGroup to block until all the fetches are complete.
func ExampleErrGroup() {
urls := []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.example.com/",
}
// Create a new ErrGroup.
eg := &errgroup.ErrGroup{}
for _, url := range urls {
url := url
// Launch a goroutine to fetch the URL.
eg.Go(func() error {
resp, err := http.Get(url)
if err != nil {
return err
}
return resp.Body.Close()
})
}
// Wait for all HTTP fetches to complete.
err := eg.Wait()
if err != nil {
fmt.Println(err)
}
}