-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackoff_test.go
108 lines (85 loc) · 2.8 KB
/
backoff_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
package grpc
import (
"fmt"
"testing"
"time"
"github.com/cenkalti/backoff"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
type invokerFunc func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, retryCount *int, opDur int, opts ...grpc.CallOption) error
func TestExpBackoffUnaryClientInterceptor(t *testing.T) {
testExpBackoffUnaryClientInterceptorImpl(t, testInvokerNoErr)
testExpBackoffUnaryClientInterceptorImpl(t, testInvokerPermanentErr)
}
func testExpBackoffUnaryClientInterceptorImpl(t *testing.T, f invokerFunc) {
ebo := GetMediumExpBackOff()
ctx := WithExpBackOff(context.Background(), ebo)
retryCount := 0
opDur := 100
invoker := func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, opts ...grpc.CallOption) error {
return f(ctx, method, req, reply, cc, &retryCount, opDur, opts...)
}
opts := []grpc.CallOption{}
err := ExpBackoffUnaryClientInterceptor(ctx, "testMethod", nil, nil, nil, invoker, opts...)
if err != nil {
t.Errorf("ExpBackoffUnaryClientInterceptor should succeed, %v", err)
}
if retryCount < 2 {
t.Errorf("ExpBackoffUnaryClientInterceptor expect at least 1 retry")
}
}
func TestExpBackOffWithContext(t *testing.T) {
ebo := GetExpBackOff(context.Background())
if ebo != nil {
t.Errorf("Context should NOT enable retry with exponential backoff")
}
ctx := WithExpBackOff(context.Background(), GetFastExpBackOff())
ebo = GetExpBackOff(ctx)
if ebo == nil {
t.Errorf("Context should enable retry with exponential backoff, %+v", ctx)
}
}
func TestGetSlowExpBackOff(t *testing.T) {
opDur := 30
retryCount := 0
op := func() error {
a := 1
b := 2
c := "abc"
d := "efg"
return funcWithRetryErr(opDur, a, b, c, d, &retryCount)
}
ebo := GetSlowExpBackOff()
backoff.Retry(op, ebo)
if retryCount < 3 {
t.Errorf("Expected at least %d retries, actually %d", 2, retryCount)
}
}
func funcWithRetryErr(opDur, a, b int, c, d string, retryCount *int) error {
*retryCount++
time.Sleep(time.Duration(opDur) * time.Millisecond)
return fmt.Errorf("Some retryable error")
}
func testInvokerPermanentErr(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, retryCount *int, opDur int, opts ...grpc.CallOption) error {
*retryCount++
time.Sleep(time.Duration(opDur) * time.Millisecond)
// Return PermanentError after 2 retries.
if *retryCount > 3 {
return &backoff.PermanentError{}
}
return fmt.Errorf("Some retryable error")
}
func testInvokerNoErr(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, retryCount *int, opDur int, opts ...grpc.CallOption) error {
*retryCount++
time.Sleep(time.Duration(opDur) * time.Millisecond)
// Succeeds after 2 retries.
if *retryCount > 3 {
return nil
}
return fmt.Errorf("Some retryable error")
}