Skip to content
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
16 changes: 11 additions & 5 deletions choice-exclusive/workflow.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package choice

import (
"fmt"
"time"

"go.temporal.io/sdk/workflow"
Expand Down Expand Up @@ -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.")
Expand Down
30 changes: 29 additions & 1 deletion choice-exclusive/workflow_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package choice

import (
"fmt"
"testing"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"
)
Expand All @@ -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{
Expand All @@ -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())
}
Loading