-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
examle_test.go
76 lines (63 loc) · 1.35 KB
/
examle_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
package racegroup_test
import (
"context"
"errors"
"fmt"
"time"
racegroup "github.com/zoncoen/go-racegroup"
)
func wait(ctx context.Context, d time.Duration) func() error {
return func() error {
select {
case <-time.After(d):
fmt.Printf("wait %s\n", d)
case <-ctx.Done():
return ctx.Err()
}
return nil
}
}
func errFunc() func() error {
return func() error {
return errors.New("error occurred")
}
}
func errPrinter(err error) {
fmt.Println(err)
}
func ExampleGroup() {
g, ctx, _ := racegroup.WithContext(context.Background())
g.Go(wait(ctx, 2*time.Second))
g.Go(wait(ctx, 1*time.Second))
g.Wait()
// Output:
// wait 1s
}
func ExampleErrorHandler() {
g, ctx, _ := racegroup.WithContext(context.Background(), racegroup.ErrorHandler(errPrinter))
g.Go(wait(ctx, 1*time.Second))
g.Go(errFunc())
g.Wait()
// Output:
// error occurred
// wait 1s
}
func ExampleConcurrency() {
g, ctx, _ := racegroup.WithContext(context.Background(), racegroup.Concurrency(2))
g.Go(wait(ctx, 3*time.Second))
g.Go(wait(ctx, 2*time.Second))
g.Go(wait(ctx, 1*time.Second))
g.Wait()
// Output:
// wait 2s
}
func ExampleDesired() {
g, ctx, _ := racegroup.WithContext(context.Background(), racegroup.Desired(2))
g.Go(wait(ctx, 3*time.Second))
g.Go(wait(ctx, 2*time.Second))
g.Go(wait(ctx, 1*time.Second))
g.Wait()
// Output:
// wait 1s
// wait 2s
}