-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_test.go
150 lines (132 loc) · 3.64 KB
/
dynamic_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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package grouper_test
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"testing"
"github.com/danlock/grouper"
)
// JustErrors illustrates the use of a Group in place of a sync.WaitGroup to
// simplify goroutine counting and error handling. This example is derived from
// the sync.WaitGroup example at https://golang.org/pkg/sync/#example_WaitGroup.
func ExampleDynamicGroup_justErrors() {
g, _ := grouper.NewDynamic[*http.Response](context.Background())
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
for _, url := range urls {
// Launch a goroutine to fetch the URL.
url := url // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func(context.Context) (*http.Response, error) {
// Fetch the URL.
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
}
return resp, err
})
}
// Wait for all HTTP fetches to complete.
if _, err := g.Wait(); err == nil {
fmt.Println("Successfully fetched all URLs.")
}
}
// Parallel illustrates the use of a Group for synchronizing a simple parallel
// task: the "Google Search 2.0" function from
// https://talks.golang.org/2012/concurrency.slide#46, augmented with a Context
// and error-handling.
func ExampleDynamicGroup_parallel() {
Google := func(ctx context.Context, query string) ([]Result, error) {
g, ctx := grouper.NewDynamic[Result](ctx)
searches := []Search{Web, Image, Video}
for _, search := range searches {
search := search // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func(ctx context.Context) (Result, error) { return search(ctx, query) })
}
if results, err := g.Wait(); err != nil {
return nil, err
} else {
return results, nil
}
}
results, err := Google(context.Background(), "golang")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
for _, result := range results {
fmt.Println(result)
}
// Unordered Output:
// web result for "golang"
// image result for "golang"
// video result for "golang"
}
func TestDynamicGroup(t *testing.T) {
err1 := errors.New("grouper_test: 1")
err2 := errors.New("grouper_test: 2")
cases := []struct {
errs []error
}{
{errs: []error{}},
{errs: []error{nil}},
{errs: []error{err1}},
{errs: []error{err1, nil}},
{errs: []error{err1, nil, err2}},
}
for _, tc := range cases {
g, _ := grouper.NewDynamic[int](context.Background())
println("testing with case ", tc.errs)
var firstErr error
for i, err := range tc.errs {
err := err
g.Go(func(context.Context) (int, error) { return i, err })
if firstErr == nil && err != nil {
firstErr = err
}
if _, gErr := g.Wait(); gErr != firstErr {
t.Errorf("after %T.Go(func() error { return err }) for err in %+v want %v",
g, tc.errs, firstErr)
}
}
}
}
func TestDynamicWithContext(t *testing.T) {
errDoom := errors.New("group_test: doomed")
cases := []struct {
errs []error
want error
}{
{want: nil},
{errs: []error{nil}, want: nil},
{errs: []error{errDoom}, want: errDoom},
{errs: []error{errDoom, nil}, want: errDoom},
}
for _, tc := range cases {
g, ctx := grouper.NewDynamic[int](context.Background())
for _, err := range tc.errs {
err := err
g.Go(func(context.Context) (int, error) { return 0, err })
}
if _, err := g.Wait(); err != tc.want {
t.Errorf("after %T.Go(func() error { return err }) for err in %v\n"+
"g.Wait() = %v; want %v",
g, tc.errs, err, tc.want)
}
canceled := false
select {
case <-ctx.Done():
canceled = true
default:
}
if !canceled {
t.Errorf("after %T.Go(func() error { return err }) for err in %v\n"+
"ctx.Done() was not closed",
g, tc.errs)
}
}
}