-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathworkflow_test.go
74 lines (61 loc) · 2.23 KB
/
workflow_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package pso
import (
"context"
"strings"
"testing"
"github.com/stretchr/testify/require"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/testsuite"
"go.temporal.io/sdk/workflow"
)
func Test_Workflow(t *testing.T) {
testSuite := &testsuite.WorkflowTestSuite{}
env := testSuite.NewTestWorkflowEnvironment()
env.RegisterWorkflow(PSOChildWorkflow)
env.RegisterActivityWithOptions(
InitParticleActivity,
activity.RegisterOptions{Name: InitParticleActivityName},
)
env.RegisterActivityWithOptions(
UpdateParticleActivity,
activity.RegisterOptions{Name: UpdateParticleActivityName},
)
var activityCalled []string
var dataConverter = NewJSONDataConverter()
env.SetDataConverter(dataConverter)
// env.SetWorkflowTimeout(5 * time.Minute)
// env.SetTestTimeout(5 * time.Minute)
env.SetOnActivityStartedListener(func(activityInfo *activity.Info, ctx context.Context, args converter.EncodedValues) {
activityType := activityInfo.ActivityType.Name
activityCalled = append(activityCalled, activityType)
switch activityType {
case "initParticleActivityName":
case "updateParticleActivityName":
default:
panic("unexpected activity call")
}
})
var childWorkflowID string
env.SetOnChildWorkflowStartedListener(func(workflowInfo *workflow.Info, ctx workflow.Context, args converter.EncodedValues) {
childWorkflowID = workflowInfo.WorkflowExecution.ID
})
env.ExecuteWorkflow(PSOWorkflow, "sphere")
require.True(t, env.IsWorkflowCompleted())
queryAndVerify(t, env, "child", childWorkflowID)
//queryAndVerify(t, env, "iteration", "???")
// consider recreating a new test env on every iteration and calling execute workflow
// with the arguments from the previous iteration (contained in ContinueAsNewError)
require.NoError(t, env.GetWorkflowError())
var result string
require.NoError(t, env.GetWorkflowResult(&result))
require.True(t, strings.HasPrefix(result, "Optimization was successful at attempt #1"))
}
func queryAndVerify(t *testing.T, env *testsuite.TestWorkflowEnvironment, query string, expectedState string) {
result, err := env.QueryWorkflow(query)
require.NoError(t, err)
var state string
err = result.Get(&state)
require.NoError(t, err)
require.Equal(t, expectedState, state)
}