Files
tasks/common/retrypolicy/retry_policy_test.go
04e868e94c Add timeout tasks for Standalone Activities (#8573)
## What changed?
Added timeout tasks for standalone activities. Refactored existing
backoff algorithm to common. Added outcome field. Fixed a few errors as
the result of main rebase. Regen go.sum. Fixed broken redirection tests.
Uncommented `LifecycleStateTimedout` and added supporting logic in
`closeTransactionHandleRootLifecycleChange`

## Why?
We need schedule-to-start, schedule-to-close, and start-to-close timeout
support for standalone activities. Also reusing existing activity retry
backoff code, so refactored it to a common package. We need to bring
back LifecycleStateTimedout
because when a timeout lifecycle occurs the workflow status needs to
update to `WORKFLOW_EXECUTION_STATUS_TIMED_OUT`, else we encounter an
invalidate state change: `unable to change workflow state from Created
to Completed, status Failed`

## How did you test it?
- [X] built
- [X] run locally and tested manually
- [X] covered by existing tests
- [X] added new unit test(s)
- [ ] added new functional test(s)

## Potential risks
E2E timeout tests not added yet as we need complete workflow API plumbed
before we can test this.

---------

Co-authored-by: Roey Berman <[email protected]>
Co-authored-by: Dan Davison <[email protected]>
Co-authored-by: Roey Berman <[email protected]>
2025-12-19 11:01:46 -05:00

235 lines
6.4 KiB
Go

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"},
},
},
{
name: "empty policy has non-zero defaults",
input: &commonpb.RetryPolicy{},
want: &commonpb.RetryPolicy{
InitialInterval: durationpb.New(1 * time.Second),
MaximumInterval: durationpb.New(100 * time.Second),
BackoffCoefficient: 2,
MaximumAttempts: 120,
},
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
EnsureDefaults(tt.input, defaultRetrySettings)
assert.Equal(t, tt.want, tt.input)
})
}
}
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: "invalid InitialInterval set on retry policy: negative duration",
},
{
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: "invalid MaximumInterval set on retry policy: negative duration",
},
{
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")
}
})
}
}