Skip to content

Commit e9168ce

Browse files
authored
Waiting activity completion after ExecuteActivityCall (#382)
1 parent dd7a7c9 commit e9168ce

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

choice-exclusive/workflow.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package choice
22

33
import (
4+
"fmt"
45
"time"
56

67
"go.temporal.io/sdk/workflow"
@@ -31,18 +32,23 @@ func ExclusiveChoiceWorkflow(ctx workflow.Context) error {
3132
logger := workflow.GetLogger(ctx)
3233

3334
// choose next activity based on order result
35+
3436
switch orderChoice {
3537
case OrderChoiceApple:
36-
workflow.ExecuteActivity(ctx, orderActivities.OrderApple, orderChoice)
38+
err = workflow.ExecuteActivity(ctx, orderActivities.OrderApple, orderChoice).Get(ctx, nil)
3739
case OrderChoiceBanana:
38-
workflow.ExecuteActivity(ctx, orderActivities.OrderBanana, orderChoice)
40+
err = workflow.ExecuteActivity(ctx, orderActivities.OrderBanana, orderChoice).Get(ctx, nil)
3941
case OrderChoiceCherry:
40-
workflow.ExecuteActivity(ctx, orderActivities.OrderCherry, orderChoice)
42+
err = workflow.ExecuteActivity(ctx, orderActivities.OrderCherry, orderChoice).Get(ctx, nil)
4143
case OrderChoiceOrange:
4244
// Activity can be also called by its name
43-
workflow.ExecuteActivity(ctx, "OrderOrange", orderChoice)
45+
err = workflow.ExecuteActivity(ctx, "OrderOrange", orderChoice).Get(ctx, nil)
4446
default:
45-
logger.Error("Unexpected order", "Choice", orderChoice)
47+
err = fmt.Errorf("unknown order choice: %v", orderChoice)
48+
}
49+
50+
if err != nil {
51+
return err
4652
}
4753

4854
logger.Info("Workflow completed.")

choice-exclusive/workflow_test.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package choice
22

33
import (
4+
"fmt"
45
"testing"
56

7+
"github.com/stretchr/testify/mock"
68
"github.com/stretchr/testify/suite"
79
"go.temporal.io/sdk/testsuite"
810
)
@@ -16,7 +18,7 @@ func TestUnitTestSuite(t *testing.T) {
1618
suite.Run(t, new(UnitTestSuite))
1719
}
1820

19-
func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflow() {
21+
func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflowSucceeds() {
2022
env := s.NewTestWorkflowEnvironment()
2123

2224
orderChoices := []string{
@@ -29,3 +31,29 @@ func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflow() {
2931
s.True(env.IsWorkflowCompleted())
3032
s.NoError(env.GetWorkflowError())
3133
}
34+
35+
func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflowFailOnGetOrderFailure() {
36+
env := s.NewTestWorkflowEnvironment()
37+
activities := &OrderActivities{}
38+
env.OnActivity(activities.GetOrder, mock.Anything).Return("", fmt.Errorf("Get Order Error"))
39+
40+
env.ExecuteWorkflow(ExclusiveChoiceWorkflow)
41+
42+
s.True(env.IsWorkflowCompleted())
43+
s.Error(env.GetWorkflowError())
44+
}
45+
46+
func (s *UnitTestSuite) Test_ExclusiveChoiceWorkflowFailOnOrdering() {
47+
env := s.NewTestWorkflowEnvironment()
48+
orderChoices := []string{
49+
OrderChoiceApple,
50+
}
51+
activities := &OrderActivities{orderChoices}
52+
env.RegisterActivity(activities.GetOrder)
53+
env.OnActivity(activities.OrderOrange, mock.Anything).Return(nil, fmt.Errorf("Get Order Error"))
54+
55+
env.ExecuteWorkflow(ExclusiveChoiceWorkflow)
56+
57+
s.True(env.IsWorkflowCompleted())
58+
s.Error(env.GetWorkflowError())
59+
}

0 commit comments

Comments
 (0)