Skip to content

Commit

Permalink
fix: retry duration recognize strings. Fixes #1200 (#1201)
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Wang <whynowy@gmail.com>
  • Loading branch information
whynowy authored May 6, 2021
1 parent bd45e0a commit 08b4fed
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
16 changes: 12 additions & 4 deletions common/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
var (
defaultFactor = apicommon.NewAmount("1.0")
defaultJitter = apicommon.NewAmount("1")
defaultDuration = apicommon.FromString("5s")
defaultDuration = apicommon.FromString("1s")

DefaultBackoff = apicommon.Backoff{
Steps: 5,
Expand All @@ -53,10 +53,18 @@ func IsRetryableKubeAPIError(err error) bool {
func Convert2WaitBackoff(backoff *apicommon.Backoff) (*wait.Backoff, error) {
result := wait.Backoff{}

if backoff.Duration != nil {
result.Duration = time.Duration(backoff.Duration.Int64Value())
d := backoff.Duration
if d == nil {
d = &defaultDuration
}
if d.Type == apicommon.Int64 {
result.Duration = time.Duration(d.Int64Value())
} else {
result.Duration = time.Duration(defaultDuration.Int64Value())
parsedDuration, err := time.ParseDuration(d.StrVal)
if err != nil {
return nil, err
}
result.Duration = parsedDuration
}

factor := backoff.Factor
Expand Down
50 changes: 50 additions & 0 deletions common/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ import (
"fmt"
"strings"
"testing"
"time"

"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/api/errors"

apicommon "github.com/argoproj/argo-events/pkg/apis/common"
)

func TestRetryableKubeAPIError(t *testing.T) {
Expand Down Expand Up @@ -51,3 +54,50 @@ func TestConnect(t *testing.T) {
})
assert.Nil(t, err)
}

func TestConnectDurationString(t *testing.T) {

start := time.Now()
count := 2
err := Connect(nil, func() error {
if count == 0 {
return nil
} else {
count--
return fmt.Errorf("new error")
}
})
end := time.Now()
elapsed := end.Sub(start)
assert.NoError(t, err)
assert.Equal(t, 0, count)
assert.True(t, elapsed >= 2*time.Second)
}

func TestConnectRetry(t *testing.T) {

factor := apicommon.NewAmount("1.0")
jitter := apicommon.NewAmount("1")
duration := apicommon.FromInt64(1000000000)
backoff := apicommon.Backoff{
Duration: &duration,
Factor: &factor,
Jitter: &jitter,
Steps: 5,
}
count := 2
start := time.Now()
err := Connect(&backoff, func() error {
if count == 0 {
return nil
} else {
count--
return fmt.Errorf("new error")
}
})
end := time.Now()
elapsed := end.Sub(start)
assert.NoError(t, err)
assert.Equal(t, 0, count)
assert.True(t, elapsed >= 2*time.Second)
}
2 changes: 1 addition & 1 deletion pkg/apis/common/int64str.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Int64OrString struct {
StrVal string `json:"strVal,omitempty" protobuf:"bytes,3,opt,name=strVal"`
}

// Type represents the stored type of IntOrString.
// Type represents the stored type of Int64OrString.
type Type int64

const (
Expand Down

0 comments on commit 08b4fed

Please sign in to comment.