Skip to content

Commit

Permalink
Add tests for using a custom failure converter
Browse files Browse the repository at this point in the history
  • Loading branch information
boxofrad authored and Quinn-With-Two-Ns committed May 29, 2024
1 parent dcc8aae commit 5c558d7
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions internal/workflow_testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ import (
"context"
"errors"
"strings"
"sync/atomic"
"testing"
"time"

"github.com/stretchr/testify/assert"
failurepb "go.temporal.io/api/failure/v1"
"go.temporal.io/sdk/converter"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -533,3 +536,64 @@ func TestMockCallWrapperNotBefore(t *testing.T) {
require.ErrorAs(t, env.GetWorkflowError(), &expectedErr)
require.ErrorContains(t, expectedErr, "Must not be called before")
}

func TestCustomFailureConverter(t *testing.T) {
t.Parallel()

var suite WorkflowTestSuite
env := suite.NewTestWorkflowEnvironment()
env.SetFailureConverter(testFailureConverter{
fallback: defaultFailureConverter,
})

var calls atomic.Int32
activity := func(context.Context) error {
_ = calls.Add(1)
return testCustomError{}
}
env.RegisterActivity(activity)

env.ExecuteWorkflow(func(ctx Context) error {
ctx = WithActivityOptions(ctx, ActivityOptions{
StartToCloseTimeout: time.Hour,
})
return ExecuteActivity(ctx, activity).Get(ctx, nil)
})
require.True(t, env.IsWorkflowCompleted())

// Failure converter should've reconstructed the custom error type.
require.True(t, errors.As(env.GetWorkflowError(), &testCustomError{}))

// Activity should've only been called once because the failure converter
// set the NonRetryable flag.
require.Equal(t, 1, int(calls.Load()))
}

type testCustomError struct{}

func (testCustomError) Error() string { return "this is a custom error type" }

type testFailureConverter struct {
fallback converter.FailureConverter
}

func (c testFailureConverter) ErrorToFailure(err error) *failurepb.Failure {
if errors.As(err, &testCustomError{}) {
return &failurepb.Failure{
FailureInfo: &failurepb.Failure_ApplicationFailureInfo{
ApplicationFailureInfo: &failurepb.ApplicationFailureInfo{
Type: "CUSTOM ERROR",
NonRetryable: true,
},
},
}
}
return c.fallback.ErrorToFailure(err)
}

func (c testFailureConverter) FailureToError(failure *failurepb.Failure) error {
if failure.GetApplicationFailureInfo().GetType() == "CUSTOM ERROR" {
return testCustomError{}
}
return c.fallback.FailureToError(failure)
}

0 comments on commit 5c558d7

Please sign in to comment.