diff --git a/choice-exclusive/workflow.go b/choice-exclusive/workflow.go index 1dc06b88..1256d130 100644 --- a/choice-exclusive/workflow.go +++ b/choice-exclusive/workflow.go @@ -1,6 +1,7 @@ package choice import ( + "fmt" "time" "go.temporal.io/sdk/workflow" @@ -31,18 +32,23 @@ func ExclusiveChoiceWorkflow(ctx workflow.Context) error { logger := workflow.GetLogger(ctx) // choose next activity based on order result + switch orderChoice { case OrderChoiceApple: - workflow.ExecuteActivity(ctx, orderActivities.OrderApple, orderChoice) + err = workflow.ExecuteActivity(ctx, orderActivities.OrderApple, orderChoice).Get(ctx, nil) case OrderChoiceBanana: - workflow.ExecuteActivity(ctx, orderActivities.OrderBanana, orderChoice) + err = workflow.ExecuteActivity(ctx, orderActivities.OrderBanana, orderChoice).Get(ctx, nil) case OrderChoiceCherry: - workflow.ExecuteActivity(ctx, orderActivities.OrderCherry, orderChoice) + err = workflow.ExecuteActivity(ctx, orderActivities.OrderCherry, orderChoice).Get(ctx, nil) case OrderChoiceOrange: // Activity can be also called by its name - workflow.ExecuteActivity(ctx, "OrderOrange", orderChoice) + err = workflow.ExecuteActivity(ctx, "OrderOrange", orderChoice).Get(ctx, nil) default: - logger.Error("Unexpected order", "Choice", orderChoice) + err = fmt.Errorf("unknown order choice: %v", orderChoice) + } + + if err != nil { + return err } logger.Info("Workflow completed.") diff --git a/choice-exclusive/workflow_test.go b/choice-exclusive/workflow_test.go index 34a27f2b..388ad1a7 100644 --- a/choice-exclusive/workflow_test.go +++ b/choice-exclusive/workflow_test.go @@ -1,8 +1,10 @@ package choice import ( + "fmt" "testing" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/suite" "go.temporal.io/sdk/testsuite" ) @@ -16,7 +18,7 @@ func TestUnitTestSuite(t *testing.T) { suite.Run(t, new(UnitTestSuite)) } -func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflow() { +func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflowSucceeds() { env := s.NewTestWorkflowEnvironment() orderChoices := []string{ @@ -29,3 +31,29 @@ func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflow() { s.True(env.IsWorkflowCompleted()) s.NoError(env.GetWorkflowError()) } + +func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflowFailOnGetOrderFailure() { + env := s.NewTestWorkflowEnvironment() + activities := &OrderActivities{} + env.OnActivity(activities.GetOrder, mock.Anything).Return("", fmt.Errorf("Get Order Error")) + + env.ExecuteWorkflow(ExclusiveChoiceWorkflow) + + s.True(env.IsWorkflowCompleted()) + s.Error(env.GetWorkflowError()) +} + +func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflowFailOnOrdering() { + env := s.NewTestWorkflowEnvironment() + orderChoices := []string{ + OrderChoiceApple, + } + activities := &OrderActivities{orderChoices} + env.RegisterActivity(activities.GetOrder) + env.OnActivity(activities.OrderOrange, mock.Anything).Return(nil, fmt.Errorf("Get Order Error")) + + env.ExecuteWorkflow(ExclusiveChoiceWorkflow) + + s.True(env.IsWorkflowCompleted()) + s.Error(env.GetWorkflowError()) +}