-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
114 lines (94 loc) · 3.45 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
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
package recovererr
import (
"context"
"errors"
"time"
)
// Retry recoverable errors using constant backoff and fixed attempts
func ExampleRetry_First() {
recoverErrorAction := &mockAction{errors: []error{Recoverable(errors.New("failure"))}}
ctx, cancelFunc := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancelFunc()
_ = Retry(ctx, recoverErrorAction.Call, NewConstantBackoff(WithInterval(time.Millisecond), WithMaxAttempts(5)), RetryRecoverablePolicy)
// Output:
// action called 1 time(s)
// action called 2 time(s)
// action called 3 time(s)
// action called 4 time(s)
// action called 5 time(s)
// action called 6 time(s)
}
// Retry recoverable errors using exponential backoff.
func ExampleRetry_Second() {
recoverErrorAction := &mockAction{errors: []error{Recoverable(errors.New("failure"))}}
mockClock := mockClock{init: time.Unix(1659219915, 0), interval: time.Millisecond}
backoffStrategy := NewExponentialBackoff(
WithInitialInterval(time.Millisecond),
WithRandomisationFactory(0),
WithMultiplier(1),
WithMaxElapsedTime(4*time.Millisecond),
WithMaxInterval(time.Millisecond),
WithClock(&mockClock),
)
ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Millisecond)
defer cancelFunc()
_ = retry(ctx, recoverErrorAction.Call, &mockClock, backoffStrategy, RetryRecoverablePolicy)
// Output:
// action called 1 time(s)
// action called 2 time(s)
// action called 3 time(s)
// action called 4 time(s)
}
// Retry recoverable errors using constant backoff and default attempts.
func ExampleRetry_Third() {
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Millisecond)
defer cancelFunc()
recoverErrorAction := &mockAction{errors: []error{Recoverable(errors.New("failure"))}}
_ = Retry(ctx, recoverErrorAction.Call, NewConstantBackoff(WithInterval(time.Millisecond)), RetryRecoverablePolicy)
// Output:
// action called 1 time(s)
// action called 2 time(s)
}
// Do run function returning unrecoverable errors using exponential backoff.
func ExampleRetry_Fourth() {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second)
defer cancelFunc()
var (
mockClock = mockClock{init: time.Unix(1659219915, 0), interval: time.Millisecond}
nonUnrecoverableErrorAction = &mockAction{errors: []error{errors.New("failure")}}
backoffStretegy = func() BackoffStrategy {
return NewExponentialBackoff(
WithInitialInterval(time.Millisecond),
WithRandomisationFactory(0),
WithMultiplier(1),
WithMaxElapsedTime(2*time.Millisecond),
WithMaxInterval(time.Millisecond),
WithClock(&mockClock),
)
}
)
_ = do(ctx, nonUnrecoverableErrorAction.Call, &mockClock, backoffStretegy, RetryNonUnrecoverablePolicy)
// Output:
// action called 1 time(s)
// action called 2 time(s)
}
// Do run function returning unrecoverable errors using constant backoff.
func ExampleRetry_Fifth() {
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second)
defer cancelFunc()
var (
nonUnrecoverableErrorAction = &mockAction{errors: []error{errors.New("failure")}}
backoffStretegy = func() BackoffStrategy {
return NewConstantBackoff(
WithInterval(time.Millisecond),
WithMaxAttempts(3),
)
}
)
_ = Do(ctx, nonUnrecoverableErrorAction.Call, backoffStretegy, RetryNonUnrecoverablePolicy)
// Output:
// action called 1 time(s)
// action called 2 time(s)
// action called 3 time(s)
// action called 4 time(s)
}