-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrouper_test.go
195 lines (173 loc) · 4.9 KB
/
grouper_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package grouper_test
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"testing"
"github.com/danlock/grouper"
)
var (
Web = fakeSearch("web")
Image = fakeSearch("image")
Video = fakeSearch("video")
)
type Result string
type Search func(ctx context.Context, query string) (Result, error)
func fakeSearch(kind string) Search {
return func(_ context.Context, query string) (Result, error) {
return Result(fmt.Sprintf("%s result for %q", kind, query)), nil
}
}
// 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 ExampleGroup_justErrors() {
var urls = []string{
"http://www.golang.org/",
"http://www.google.com/",
"http://www.somestupidname.com/",
}
funcs := make([]func(context.Context) (*http.Response, error), len(urls))
for i, url := range urls {
// Launch a goroutine to fetch the URL.
url := url // https://golang.org/doc/faq#closures_and_goroutines
funcs[i] = func(context.Context) (*http.Response, error) {
// Fetch the URL.
resp, err := http.Get(url)
if err == nil {
resp.Body.Close()
}
return resp, err
}
}
g := grouper.New(funcs...)
// Wait for all HTTP fetches to complete.
if _, err := g.Wait(context.Background()); 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 ExampleGroup_parallel() {
Google := func(ctx context.Context, query string) ([]Result, error) {
searches := []Search{Web, Image, Video}
funcs := make([]func(context.Context) (Result, error), len(searches))
for i, search := range searches {
search := search // https://golang.org/doc/faq#closures_and_goroutines
funcs[i] = func(context.Context) (Result, error) {
return search(ctx, query)
}
}
g := grouper.New(funcs...)
if results, err := g.Wait(ctx); 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)
}
// Output:
// web result for "golang"
// image result for "golang"
// video result for "golang"
}
func TestZeroGroup(t *testing.T) {
err1 := errors.New("grouper_test: 1")
cases := []struct {
errs []error
}{
// {errs: []error{}},
{errs: []error{nil}},
{errs: []error{err1}},
{errs: []error{err1, nil}},
{errs: []error{err1, nil, err1, nil}},
}
for _, tc := range cases {
funcs := make([]func(context.Context) (error, error), len(tc.errs))
var firstErr error
for i, err := range tc.errs {
err := err
funcs[i] = func(context.Context) (error, error) { return err, err }
if firstErr == nil && err != nil {
firstErr = err
}
}
g := grouper.New(funcs...)
if _, gErr := g.Wait(context.Background()); gErr != firstErr {
t.Errorf("after %T.Go(func() error { return err }) for err in %v\n"+
"g.Wait() = %v; want %v",
g, tc.errs, gErr, firstErr)
}
}
}
func TestWithContext(t *testing.T) {
errDoom := errors.New("group_test: doomed")
cases := []struct {
errs []error
want error
}{
{errs: []error{nil}, want: nil},
{errs: []error{errDoom}, want: errDoom},
{errs: []error{errDoom, nil}, want: errDoom},
}
for _, tc := range cases {
funcs := make([]func(context.Context) (context.Context, error), len(tc.errs))
for i, err := range tc.errs {
err := err
funcs[i] = func(ctx context.Context) (context.Context, error) { return ctx, err }
}
g := grouper.New(funcs...)
ctxs, err := g.Wait(context.Background())
if 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 <-ctxs[0].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)
}
}
}
func TestValueGroup(t *testing.T) {
cases := []struct {
results []uint
err error
}{
{results: []uint{0, 2, 4}},
{results: []uint{6, 2, 4}, err: errors.New("ugh")},
{results: []uint{0, 8, 4}},
{results: []uint{0, 2, 9}, err: errors.New("ugh")},
}
for _, tc := range cases {
funcs := make([]func(context.Context) (uint, error), len(tc.results))
for i, res := range tc.results {
res := res
funcs[i] = func(context.Context) (uint, error) { return res, tc.err }
}
g := grouper.New(funcs...)
if _, gErr := g.Wait(context.Background()); gErr != tc.err {
t.Errorf("after %T.Go(func() error { return err }) for err in %v\n"+
"g.Wait() = %v; want %v",
g, tc.err, gErr, tc.err)
}
}
}