Hi Chad, thanks a lot for quickly responding with such great example code. I have created sample version of my actual code which re-produces the problem i am facing in actual code. could you help me identify what am i doing wrong?
sample_activities.go
package temporal_workflow
import (
"context"
)
type SampleActivityParam struct {
}
type SampleActivityResponse struct {
orderIds []string
}
func SampleActivity(ctx context.Context, param ParseOrderLinesActivityParam) (SampleActivityResponse, error) {
return SampleActivityResponse{}, nil
}
sample_workflow.go
package temporal_workflow
import (
//"context"
"time"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)
type MyWorkflowParam struct{}
func MyWorkflow(ctx workflow.Context) error {
retryPolicy := temporal.RetryPolicy{
InitialInterval: 1 * time.Second,
BackoffCoefficient: 2,
MaximumInterval: 1 * time.Minute,
MaximumAttempts: 2,
}
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
ScheduleToCloseTimeout: 5 * time.Second,
//ScheduleToCloseTimeout: 5 * time.Second,
//StartToCloseTimeout: 10 * time.Second,
RetryPolicy: &retryPolicy,
})
var response SampleActivityResponse
err := workflow.ExecuteActivity(ctx, SampleActivity, SampleActivityParam{}).Get(ctx, &response)
//workflow.GetLogger(ctx).Info("Log workflow param", param)
workflow.GetLogger(ctx).Info("Log response", response)
return err
}
sample_workflow_test.go
package temporal_workflow
import (
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"
)
type SampleUnitTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
env *testsuite.TestWorkflowEnvironment
}
func (s *SampleUnitTestSuite) SetupTest() {
s.env = s.NewTestWorkflowEnvironment()
s.env.RegisterWorkflow(MyWorkflow)
s.env.RegisterActivity(SampleActivity)
}
func (s *SampleUnitTestSuite) AfterTest(suiteName, testName string) {
s.env.AssertExpectations(s.T())
}
func (s *SampleUnitTestSuite) Test_MyWorkflow_Success() {
var strs []string
strs = append(strs, "1", "2")
response := SampleActivityResponse{orderIds: strs}
s.env.OnActivity(SampleActivity, mock.Anything, mock.Anything).Return(response, nil)
s.env.ExecuteWorkflow(MyWorkflow)
s.True(s.env.IsWorkflowCompleted())
s.NoError(s.env.GetWorkflowError())
}
func TestSampleUnitTestSuite(t *testing.T) {
suite.Run(t, new(SampleUnitTestSuite))
}
Logs for running the tests:
2022/02/20 06:43:48 DEBUG handleActivityResult: *workflowservice.RespondActivityTaskCompletedRequest. ActivityID 1 ActivityType SampleActivity
2022/02/20 06:43:48 INFO Log response {[]}
PASS
================================================================================
(12:13:48) INFO: Elapsed time: 5.255s, Critical Path: 4.96s
(12:13:48) INFO: 8 processes: 1 internal, 7 darwin-sandbox.
(12:13:48) INFO: Build completed successfully, 8 total actions
Test cases: finished with 0 passing and 0 failing out of 0 test cases
Executed 1 out of 1 test: 1 test passes.
(12:13:48) INFO: Build completed successfully, 8 total actions