Skip to content

Commit

Permalink
Error mapper tests (#5795)
Browse files Browse the repository at this point in the history
What changed?

Refactored the error test in the proto mapper
Added test to make sure all fields are set in the test errors (if not, the check will pass, even though data would be lost).
Added tests for the thrift mapper
Why?
Make sure data is preserved in the mappings

How did you test it?
It's a test in itself.

Potential risks

Release notes

Documentation Changes
  • Loading branch information
jakobht authored Mar 19, 2024
1 parent c521fa3 commit b04f984
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 28 deletions.
47 changes: 19 additions & 28 deletions common/types/mapper/proto/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,30 @@ import (
)

func TestErrors(t *testing.T) {
for _, err := range []error{
nil, // OK - no error
&testdata.AccessDeniedError,
&testdata.BadRequestError,
&testdata.CancellationAlreadyRequestedError,
&testdata.ClientVersionNotSupportedError,
&testdata.CurrentBranchChangedError,
&testdata.DomainAlreadyExistsError,
&testdata.DomainNotActiveError,
&testdata.EntityNotExistsError,
&testdata.WorkflowExecutionAlreadyCompletedError,
&testdata.EventAlreadyStartedError,
&testdata.InternalDataInconsistencyError,
&testdata.InternalServiceError,
&testdata.LimitExceededError,
&testdata.QueryFailedError,
&testdata.RemoteSyncMatchedError,
&testdata.RetryTaskV2Error,
&testdata.ServiceBusyError,
&testdata.ShardOwnershipLostError,
&testdata.WorkflowExecutionAlreadyStartedError,
&testdata.StickyWorkerUnavailableError,
errors.New("unknown error"),
} {
name := "OK"
if err != nil {
name = reflect.TypeOf(err).Elem().Name()
}
for _, err := range testdata.Errors {
name := reflect.TypeOf(err).Elem().Name()
t.Run(name, func(t *testing.T) {
// Test that the mappings does not lose information
assert.Equal(t, err, ToError(FromError(err)))
})
}
}

func TestNilMapsToOK(t *testing.T) {
protoNoError := FromError(nil)
assert.Equal(t, yarpcerrors.CodeOK, yarpcerrors.FromError(protoNoError).Code())
assert.Nil(t, ToError(protoNoError))
}

func TestFromUnknownErrorMapsToUnknownError(t *testing.T) {
err := errors.New("unknown error")
protobufErr := FromError(err)
assert.True(t, yarpcerrors.IsUnknown(protobufErr))

assert.Equal(t, err, ToError(protobufErr))
}

func TestToUnknownErrorMapsToItself(t *testing.T) {
timeout := yarpcerrors.DeadlineExceededErrorf("timeout")
assert.Equal(t, timeout, ToError(timeout))
}
57 changes: 57 additions & 0 deletions common/types/mapper/thrift/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2021 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 thrift

import (
"errors"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/yarpc/yarpcerrors"

"github.com/uber/cadence/common/types/testdata"
)

func TestErrors(t *testing.T) {
for _, err := range testdata.Errors {
name := reflect.TypeOf(err).Elem().Name()
t.Run(name, func(t *testing.T) {
// Test that the mappings does not lose information
assert.Equal(t, err, ToError(FromError(err)))
})
}
}

func TestNilMapsToNil(t *testing.T) {
assert.Nil(t, FromError(nil))
assert.Nil(t, ToError(nil))
}

func TestFromUnknownErrorMapsToItself(t *testing.T) {
err := errors.New("unknown error")
assert.Equal(t, err, FromError(err))
}

func TestToUnknownErrorMapsToItself(t *testing.T) {
err := yarpcerrors.DeadlineExceededErrorf("timeout")
assert.Equal(t, err, ToError(err))
}
1 change: 1 addition & 0 deletions common/types/testdata/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const (
ClientImpl = "ClientImpl"
ClientLibraryVersion = "ClientLibraryVersion"
SupportedVersions = "SupportedVersions"
FeatureFlag = "FeatureFlag"

Attempt = 2
PageSize = 10
Expand Down
27 changes: 27 additions & 0 deletions common/types/testdata/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ var (
ClientImpl: ClientImpl,
SupportedVersions: SupportedVersions,
}
FeatureNotEnabledError = types.FeatureNotEnabledError{
FeatureFlag: FeatureFlag,
}
CurrentBranchChangedError = types.CurrentBranchChangedError{
Message: ErrorMessage,
CurrentBranchToken: BranchToken,
Expand Down Expand Up @@ -109,3 +112,27 @@ var (
Message: ErrorMessage,
}
)

var Errors = []error{
&AccessDeniedError,
&BadRequestError,
&CancellationAlreadyRequestedError,
&ClientVersionNotSupportedError,
&FeatureNotEnabledError,
&CurrentBranchChangedError,
&DomainAlreadyExistsError,
&DomainNotActiveError,
&EntityNotExistsError,
&WorkflowExecutionAlreadyCompletedError,
&EventAlreadyStartedError,
&InternalDataInconsistencyError,
&InternalServiceError,
&LimitExceededError,
&QueryFailedError,
&RemoteSyncMatchedError,
&RetryTaskV2Error,
&ServiceBusyError,
&ShardOwnershipLostError,
&WorkflowExecutionAlreadyStartedError,
&StickyWorkerUnavailableError,
}
56 changes: 56 additions & 0 deletions common/types/testdata/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// The MIT License (MIT)

// Copyright (c) 2017-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 testdata

import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestAllFieldsSetInTestErrors(t *testing.T) {
for _, err := range Errors {
name := reflect.TypeOf(err).Elem().Name()
t.Run(name, func(t *testing.T) {
// Test all fields are set in the error
assert.True(t, checkAllIsSet(err))
})
}
}

func checkAllIsSet(err error) bool {
// All the errors are pointers, so we get the value with .Elem
errValue := reflect.ValueOf(err).Elem()

for i := 0; i < errValue.NumField(); i++ {
field := errValue.Field(i)

// IsZero checks if the value is the default value (e.g. nil, "", 0 etc)
if field.IsZero() {
return false
}
}

return true
}

0 comments on commit b04f984

Please sign in to comment.