This repository has been archived by the owner on Apr 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_exp_with_context_test.go
89 lines (84 loc) · 3.38 KB
/
max_exp_with_context_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
// Copyright 2019 Chris Wojno
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above
// copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR Scaling PARTICULAR PURPOSE AND NON-INFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
// AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
package retry
import (
"context"
"testing"
"time"
)
// TestMaxExponentialWithContext_New ensures that the context governs the maximum run time of a retry
func TestMaxExponentialWithContext_New(t *testing.T) {
cases := map[string]struct {
cfg Exponential
ctxDuration time.Duration
expected []time.Duration
}{
"linear": {
cfg: Exponential{
Base: 0,
YOffset: 10 * time.Microsecond,
},
ctxDuration: 31 * time.Microsecond,
expected: []time.Duration{10 * time.Microsecond, 10 * time.Microsecond, 10 * time.Microsecond},
},
"exponential: 10us*2^x + 10us (limit 50 with max exec context of 205ms)": {
cfg: Exponential{
Base: 2,
Scaling: 10 * time.Microsecond,
YOffset: 10 * time.Microsecond,
MaxAttemptWaitTime: 50 * time.Microsecond,
},
ctxDuration: 205 * time.Microsecond,
expected: []time.Duration{20 * time.Microsecond, 30 * time.Microsecond, 50 * time.Microsecond, 50 * time.Microsecond},
},
"exponential: 10us*2^x + 10us (limit 50 with max exec context of 105ms)": {
cfg: Exponential{
Base: 2,
Scaling: 10 * time.Microsecond,
YOffset: 10 * time.Microsecond,
MaxAttemptWaitTime: 50 * time.Microsecond,
},
ctxDuration: 105 * time.Microsecond,
expected: []time.Duration{20 * time.Microsecond, 30 * time.Microsecond, 50 * time.Microsecond},
},
}
for caseName, c := range cases {
t.Run(caseName, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), c.ctxDuration)
svcInterface := c.cfg.NewWithContext(ctx)
svc := svcInterface.(*maxExponentialContextService)
startAt := time.Now()
for i := 0; i < len(c.expected); i++ {
svc.Yield()
}
cancel()
endAt := time.Now()
expectedEndAt := startAt.Add(c.ctxDuration)
if expectedEndAt.Add(1 * time.Microsecond).After(endAt) {
t.Errorf(`exceeded context timeout, over by %v`, endAt.Sub(expectedEndAt))
}
})
}
}
func TestExpBase2_NewWithContext(t *testing.T) {
base2Svc := ExpBase2{
Scaling: 10 * time.Second,
}.NewWithContext(context.Background()).(*maxExponentialContextService)
if base2Svc.ctx != context.Background() {
t.Error("expected ctx to be background, but was not")
}
if base2Svc.config.Base != 2 {
t.Error("expected Base == 2, but was not")
}
}