Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for IsServiceTransientError #5551

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package common
import (
"context"
"encoding/json"
"errors"
"fmt"
"math"
"math/rand"
Expand Down Expand Up @@ -244,14 +245,22 @@ func ToServiceTransientError(err error) error {

// IsServiceTransientError checks if the error is a transient error.
func IsServiceTransientError(err error) bool {
switch err.(type) {
case *types.InternalServiceError:

var (
typesInternalServiceError *types.InternalServiceError
typesServiceBusyError *types.ServiceBusyError
typesShardOwnershipLostError *types.ShardOwnershipLostError
yarpcErrorsStatus *yarpcerrors.Status
)

switch {
case errors.As(err, &typesInternalServiceError):
return true
case *types.ServiceBusyError:
case errors.As(err, &typesServiceBusyError):
return true
case *types.ShardOwnershipLostError:
case errors.As(err, &typesShardOwnershipLostError):
return true
case *yarpcerrors.Status:
case errors.As(err, &yarpcErrorsStatus):
// We only selectively retry the following yarpc errors client can safe retry with a backoff
if yarpcerrors.IsUnavailable(err) ||
yarpcerrors.IsUnknown(err) ||
Expand Down
62 changes: 46 additions & 16 deletions common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,53 @@ import (
"github.com/uber/cadence/common/types"
)

func TestIsServiceTransientError_ContextTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
time.Sleep(100 * time.Millisecond)

require.False(t, IsServiceTransientError(ctx.Err()))
}

func TestIsServiceTransientError_YARPCDeadlineExceeded(t *testing.T) {
yarpcErr := yarpcerrors.DeadlineExceededErrorf("yarpc deadline exceeded")
require.False(t, IsServiceTransientError(yarpcErr))
}
func TestIsServiceTransientError(t *testing.T) {
for name, c := range map[string]struct {
err error
want bool
}{
"ContextTimeout": {
err: context.DeadlineExceeded,
want: false,
},
"YARPCDeadlineExceeded": {
err: yarpcerrors.DeadlineExceededErrorf("yarpc deadline exceeded"),
want: false,
},
"YARPCUnavailable": {
err: yarpcerrors.UnavailableErrorf("yarpc unavailable"),
arzonus marked this conversation as resolved.
Show resolved Hide resolved
want: true,
},
"YARPCUnavailable wrapped": {
err: fmt.Errorf("wrapped err: %w", yarpcerrors.UnavailableErrorf("yarpc unavailable")),
want: true,
},
"YARPCUnknown": {
err: yarpcerrors.UnknownErrorf("yarpc unknown"),
want: true,
},
"YARPCInternal": {
err: yarpcerrors.InternalErrorf("yarpc internal"),
want: true,
},
"ContextCancel": {
err: context.Canceled,
want: false,
},
"ServiceBusyError": {
err: &types.ServiceBusyError{},
want: true,
},
"ShardOwnershipLostError": {
err: &types.ShardOwnershipLostError{},
want: true,
},
} {
t.Run(name, func(t *testing.T) {
require.Equal(t, c.want, IsServiceTransientError(c.err))
})
}

func TestIsServiceTransientError_ContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
require.False(t, IsServiceTransientError(ctx.Err()))
}

func TestIsContextTimeoutError(t *testing.T) {
Expand Down