-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
example_test.go
83 lines (72 loc) · 1.59 KB
/
example_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
package gollback_test
import (
"context"
"errors"
"fmt"
"time"
"github.com/vardius/gollback"
)
func Example_race() {
r, err := gollback.Race(
context.Background(),
func(ctx context.Context) (interface{}, error) {
time.Sleep(3 * time.Second)
return 1, nil
},
func(ctx context.Context) (interface{}, error) {
return nil, errors.New("failed")
},
func(ctx context.Context) (interface{}, error) {
return 3, nil
},
)
fmt.Println(r)
fmt.Println(err)
// Output:
// 3
// <nil>
}
func Example_all() {
rs, errs := gollback.All(
context.Background(),
func(ctx context.Context) (interface{}, error) {
time.Sleep(3 * time.Second)
return 1, nil
},
func(ctx context.Context) (interface{}, error) {
return nil, errors.New("failed")
},
func(ctx context.Context) (interface{}, error) {
return 3, nil
},
)
fmt.Println(rs)
fmt.Println(errs)
// Output:
// [1 <nil> 3]
// [<nil> failed <nil>]
}
func Example_retryTimeout() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Will retry infinitely until timeouts by context (after 5 seconds)
res, err := gollback.Retry(ctx, 0, func(ctx context.Context) (interface{}, error) {
return nil, errors.New("failed")
})
fmt.Println(res)
fmt.Println(err)
// Output:
// <nil>
// context deadline exceeded
}
func Example_retryFiveTimes() {
// Will retry 5 times
res, err := gollback.Retry(context.Background(), 5, func(ctx context.Context) (interface{}, error) {
return nil, errors.New("failed")
})
fmt.Println(res)
fmt.Println(err)
// Output:
// <nil>
// failed
}