Files
tasks/common/retrypolicy/retry_policy_test.go
David ReissandGitHub 765cad2242 Move some code around to avoid import cycles (#5716)
## What changed?
Move some code around to avoid import cycles with future changes:
- `common` depends on `common/dynamicconfig` (not changing)
- I want to move dynamic config default values from all over into
`common/dynamicconfig`
- Some constants in `common` are used as default values. Moving them
into `common/dynamicconfig` would create an import cycle.
- So move the constants into `common/primitives` instead and reference
them there.

Also move `GetDefaultRetryPolicyConfigOptions`, `DefaultRetrySettings`,
`FromConfigToDefaultRetrySettings`, `EnsureRetryPolicyDefaults`,
`ValidateRetryPolicy` and associated unit test into new package
`common/retrypolicy` with shorter names.

Arguably more constants should move out of `common`, that package is too
big. But these are the only ones that needed to be moved for now.

## Why?
Split off from future PR to make reviews easier.

## How did you test it?
existing tests
2024-04-16 20:12:59 -07:00

264 lines
7.9 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// 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 A PARTICULAR PURPOSE AND NONINFRINGEMENT. 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 retrypolicy
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
"google.golang.org/protobuf/types/known/durationpb"
)
func TestEnsureRetryPolicyDefaults(t *testing.T) {
defaultRetrySettings := DefaultRetrySettings{
InitialInterval: time.Second,
MaximumIntervalCoefficient: 100,
BackoffCoefficient: 2.0,
MaximumAttempts: 120,
}
defaultRetryPolicy := &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
MaximumInterval: durationpb.New(100 * time.Second),
BackoffCoefficient: 2.0,
MaximumAttempts: 120,
}
testCases := []struct {
name string
input *commonpb.RetryPolicy
want *commonpb.RetryPolicy
}{
{
name: "default fields are set ",
input: &commonpb.RetryPolicy{},
want: defaultRetryPolicy,
},
{
name: "non-default InitialIntervalInSeconds is not set",
input: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(2 * time.Second),
},
want: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(2 * time.Second),
MaximumInterval: durationpb.New(200 * time.Second),
BackoffCoefficient: 2,
MaximumAttempts: 120,
},
},
{
name: "non-default MaximumIntervalInSeconds is not set",
input: &commonpb.RetryPolicy{
MaximumInterval: durationpb.New(1000 * time.Second),
},
want: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
MaximumInterval: durationpb.New(1000 * time.Second),
BackoffCoefficient: 2,
MaximumAttempts: 120,
},
},
{
name: "non-default BackoffCoefficient is not set",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 1.5,
},
want: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
MaximumInterval: durationpb.New(100 * time.Second),
BackoffCoefficient: 1.5,
MaximumAttempts: 120,
},
},
{
name: "non-default Maximum attempts is not set",
input: &commonpb.RetryPolicy{
MaximumAttempts: 49,
},
want: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
MaximumInterval: durationpb.New(100 * time.Second),
BackoffCoefficient: 2,
MaximumAttempts: 49,
},
},
{
name: "non-retryable errors are set",
input: &commonpb.RetryPolicy{
NonRetryableErrorTypes: []string{"testFailureType"},
},
want: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
MaximumInterval: durationpb.New(100 * time.Second),
BackoffCoefficient: 2.0,
MaximumAttempts: 120,
NonRetryableErrorTypes: []string{"testFailureType"},
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
EnsureDefaults(tt.input, defaultRetrySettings)
assert.Equal(t, tt.want, tt.input)
})
}
}
func Test_FromConfigToRetryPolicy(t *testing.T) {
options := map[string]interface{}{
initialIntervalInSecondsConfigKey: 2,
maximumIntervalCoefficientConfigKey: 100.0,
backoffCoefficientConfigKey: 4.0,
maximumAttemptsConfigKey: 5,
}
defaultSettings := FromConfigToDefault(options)
assert.Equal(t, 2*time.Second, defaultSettings.InitialInterval)
assert.Equal(t, 100.0, defaultSettings.MaximumIntervalCoefficient)
assert.Equal(t, 4.0, defaultSettings.BackoffCoefficient)
assert.Equal(t, int32(5), defaultSettings.MaximumAttempts)
}
func TestValidateRetryPolicy(t *testing.T) {
testCases := []struct {
name string
input *commonpb.RetryPolicy
wantErr bool
wantErrString string
}{
{
name: "nil policy is okay",
input: nil,
wantErr: false,
wantErrString: "",
},
{
name: "maxAttempts is 1, coefficient < 1",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 0.5,
MaximumAttempts: 1,
},
wantErr: false,
wantErrString: "",
},
{
name: "initial interval negative",
input: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(-22 * time.Second),
},
wantErr: true,
wantErrString: "InitialInterval cannot be negative on retry policy.",
},
{
name: "coefficient < 1",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 0.8,
},
wantErr: true,
wantErrString: "BackoffCoefficient cannot be less than 1 on retry policy.",
},
{
name: "maximum interval in seconds is negative",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 2.0,
MaximumInterval: durationpb.New(-2 * time.Second),
},
wantErr: true,
wantErrString: "MaximumInterval cannot be negative on retry policy.",
},
{
name: "maximum interval in less than initial interval",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 2.0,
MaximumInterval: durationpb.New(5 * time.Second),
InitialInterval: durationpb.New(10 * time.Second),
},
wantErr: true,
wantErrString: "MaximumInterval cannot be less than InitialInterval on retry policy.",
},
{
name: "maximum attempts negative",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 2.0,
MaximumAttempts: -3,
},
wantErr: true,
wantErrString: "MaximumAttempts cannot be negative on retry policy.",
},
{
name: "timeout nonretryable error - valid type",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 1,
NonRetryableErrorTypes: []string{
TimeoutFailureTypePrefix + enumspb.TIMEOUT_TYPE_START_TO_CLOSE.String(),
TimeoutFailureTypePrefix + enumspb.TIMEOUT_TYPE_SCHEDULE_TO_START.String(),
TimeoutFailureTypePrefix + enumspb.TIMEOUT_TYPE_SCHEDULE_TO_CLOSE.String(),
TimeoutFailureTypePrefix + enumspb.TIMEOUT_TYPE_HEARTBEAT.String(),
},
},
wantErr: false,
wantErrString: "",
},
{
name: "timeout nonretryable error - unspecified type",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 1,
NonRetryableErrorTypes: []string{
TimeoutFailureTypePrefix + enumspb.TIMEOUT_TYPE_UNSPECIFIED.String(),
},
},
wantErr: true,
wantErrString: "Invalid timeout type value: Unspecified.",
},
{
name: "timeout nonretryable error - unknown type",
input: &commonpb.RetryPolicy{
BackoffCoefficient: 1,
NonRetryableErrorTypes: []string{
TimeoutFailureTypePrefix + "unknown",
},
},
wantErr: true,
wantErrString: "Invalid timeout type value: unknown.",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
err := Validate(tt.input)
if tt.wantErr {
assert.NotNil(t, err, "expected error - did not get one")
assert.Equal(t, err.Error(), tt.wantErrString, "unexpected error message")
} else {
assert.Nil(t, err, "unexpected error")
}
})
}
}