Skip to content

Commit

Permalink
add unit tests (#5551)
Browse files Browse the repository at this point in the history
  • Loading branch information
arzonus authored Jan 8, 2024
1 parent 0cdd8d0 commit 9363c1e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 21 deletions.
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"),
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

0 comments on commit 9363c1e

Please sign in to comment.