Skip to content

Commit 2c43ae5

Browse files
author
Rajeev N B
committed
add tests for invalid jitter value for backoff strategies
1 parent d0a7d83 commit 2c43ae5

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

backoff.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ func init() {
2222

2323
// NewConstantBackoff returns an instance of ConstantBackoff
2424
func NewConstantBackoff(backoffInterval, maximumJitterInterval time.Duration) Backoff {
25+
// protect against panic when generating random jitter
2526
if maximumJitterInterval < 0 {
2627
maximumJitterInterval = 0
2728
}
29+
2830
return &constantBackoff{
2931
backoffInterval: int64(backoffInterval / time.Millisecond),
3032
maximumJitterInterval: int64(maximumJitterInterval / time.Millisecond),
@@ -45,9 +47,11 @@ type exponentialBackoff struct {
4547

4648
// NewExponentialBackoff returns an instance of ExponentialBackoff
4749
func NewExponentialBackoff(initialTimeout, maxTimeout time.Duration, exponentFactor float64, maximumJitterInterval time.Duration) Backoff {
50+
// protect against panic when generating random jitter
4851
if maximumJitterInterval < 0 {
49-
maximumJitterInterval = 0 // protect against panic when generating random jitter
52+
maximumJitterInterval = 0
5053
}
54+
5155
return &exponentialBackoff{
5256
exponentFactor: exponentFactor,
5357
initialTimeout: float64(initialTimeout / time.Millisecond),

backoff_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ func TestExponentialBackoffNextTime(t *testing.T) {
1616
assert.Equal(t, 800*time.Millisecond, exponentialBackoff.Next(3))
1717
}
1818

19+
func TestExponentialBackoffWithInvalidJitter(t *testing.T) {
20+
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, -1*time.Millisecond)
21+
22+
assert.Equal(t, 100*time.Millisecond, exponentialBackoff.Next(0))
23+
assert.Equal(t, 200*time.Millisecond, exponentialBackoff.Next(1))
24+
assert.Equal(t, 400*time.Millisecond, exponentialBackoff.Next(2))
25+
assert.Equal(t, 800*time.Millisecond, exponentialBackoff.Next(3))
26+
}
27+
1928
func TestExponentialBackoffMaxTimeoutCrossed(t *testing.T) {
2029
exponentialBackoff := NewExponentialBackoff(100*time.Millisecond, 1000*time.Millisecond, 2.0, 0*time.Millisecond)
2130

@@ -64,6 +73,15 @@ func TestConstantBackoffNextTime(t *testing.T) {
6473
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(3))
6574
}
6675

76+
func TestConstantBackoffWithInvalidJitter(t *testing.T) {
77+
constantBackoff := NewConstantBackoff(100*time.Millisecond, -1*time.Millisecond)
78+
79+
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(0))
80+
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(1))
81+
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(2))
82+
assert.Equal(t, 100*time.Millisecond, constantBackoff.Next(3))
83+
}
84+
6785
func TestConstantBackoffWhenRetryIsLessThanZero(t *testing.T) {
6886
constantBackoff := NewConstantBackoff(100*time.Millisecond, 0*time.Millisecond)
6987

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
module github.com/gojektech/heimdall/v6
22

3-
go 1.11
3+
go 1.14
44

55
require (
66
github.com/afex/hystrix-go v0.0.0-20180209013831-27fae8d30f1a
77
github.com/davecgh/go-spew v1.1.0 // indirect
88
github.com/gojektech/valkyrie v0.0.0-20180215180059-6aee720afcdf
99
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e // indirect
1010
github.com/jtolds/gls v4.2.1+incompatible // indirect
11+
github.com/mattn/goveralls v0.0.6 // indirect
1112
github.com/pkg/errors v0.8.0
1213
github.com/pmezard/go-difflib v1.0.0 // indirect
1314
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect
1415
github.com/smartystreets/goconvey v0.0.0-20181108003508-044398e4856c // indirect
1516
github.com/stretchr/objx v0.1.1 // indirect
1617
github.com/stretchr/testify v1.2.1
18+
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
19+
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51 // indirect
1720
)

go.sum

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1
88
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
99
github.com/jtolds/gls v4.2.1+incompatible h1:fSuqC+Gmlu6l/ZYAoZzx2pyucC8Xza35fpRVWLVmUEE=
1010
github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
11+
github.com/mattn/goveralls v0.0.6 h1:cr8Y0VMo/MnEZBjxNN/vh6G90SZ7IMb6lms1dzMoO+Y=
12+
github.com/mattn/goveralls v0.0.6/go.mod h1:h8b4ow6FxSPMQHF6o2ve3qsclnffZjYTNEKmLesRwqw=
1113
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
1214
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1315
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -20,3 +22,27 @@ github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
2022
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2123
github.com/stretchr/testify v1.2.1 h1:52QO5WkIUcHGIR7EnGagH88x1bUzqGXTC5/1bDTUQ7U=
2224
github.com/stretchr/testify v1.2.1/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
25+
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
26+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
27+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
28+
golang.org/x/lint v0.0.0-20200302205851-738671d3881b h1:Wh+f8QHJXR411sJR8/vRBTZ7YapZaRvUcLFFJhusH0k=
29+
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
30+
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
31+
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
32+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
33+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
34+
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
35+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
36+
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
37+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
38+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
39+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
40+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
41+
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7 h1:EBZoQjiKKPaLbPrbpssUfuHtwM6KV/vb4U85g/cigFY=
42+
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
43+
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
44+
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51 h1:Wec8/IO8hAraBf0it7/dPQYOslIrgM938wZYNkLnOYc=
45+
golang.org/x/tools v0.0.0-20200530233709-52effbd89c51/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
46+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
47+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
48+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)