-
Notifications
You must be signed in to change notification settings - Fork 202
/
workflow_test.go
132 lines (110 loc) · 3.29 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package safe_message_handler
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/suite"
"go.temporal.io/sdk/testsuite"
)
type updateCallback struct {
accept func()
reject func(error)
complete func(interface{}, error)
}
func (uc *updateCallback) Accept() {
if uc.accept == nil {
return
}
uc.accept()
}
func (uc *updateCallback) Reject(err error) {
if uc.reject == nil {
return
}
uc.reject(err)
}
func (uc *updateCallback) Complete(success interface{}, err error) {
if uc.complete == nil {
return
}
uc.complete(success, err)
}
type UnitTestSuite struct {
suite.Suite
testsuite.WorkflowTestSuite
}
func TestUnitTestSuite(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}
func (s *UnitTestSuite) Test_Workflow() {
env := s.NewTestWorkflowEnvironment()
env.RegisterActivity(AssignNodesToJobsActivity)
env.RegisterActivity(UnassignNodesForJobActivity)
env.RegisterActivity(FindBadNodesActivity)
env.RegisterDelayedCallback(func() {
env.SignalWorkflow(StartCluster, nil)
}, time.Second*2)
for i := range 6 {
env.RegisterDelayedCallback(func() {
env.UpdateWorkflow(AssignNodesToJobs, fmt.Sprintf("TestUpdateID-%d", i), &updateCallback{
complete: func(response interface{}, err error) {
s.NoError(err)
r := response.(ClusterManagerAssignNodesToJobResult)
s.Equal(len(r.NodesAssigned), 2)
env.RegisterDelayedCallback(func() {
env.UpdateWorkflow(DeleteJob, fmt.Sprintf("TestUpdateID-%d", i), &updateCallback{}, ClusterManagerDeleteJobInput{
JobName: fmt.Sprintf("TestJobID-%d", i),
})
}, 2*time.Duration(i)*time.Second)
},
}, ClusterManagerAssignNodesToJobInput{
JobName: fmt.Sprintf("TestJobID-%d", i),
TotalNumNodes: 2,
})
}, time.Duration(i)*time.Second)
}
env.RegisterDelayedCallback(func() {
env.SignalWorkflow(ShutdownCluster, nil)
}, time.Minute)
env.ExecuteWorkflow(ClusterManagerWorkflow, ClusterManagerInput{})
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
env.AssertExpectations(s.T())
}
func (s *UnitTestSuite) Test_UpdateFailure() {
env := s.NewTestWorkflowEnvironment()
env.RegisterActivity(AssignNodesToJobsActivity)
env.RegisterActivity(UnassignNodesForJobActivity)
env.RegisterActivity(FindBadNodesActivity)
env.RegisterDelayedCallback(func() {
env.SignalWorkflow(StartCluster, nil)
}, time.Second*2)
env.RegisterDelayedCallback(func() {
env.UpdateWorkflow(AssignNodesToJobs, "TestUpdateID", &updateCallback{
complete: func(response interface{}, err error) {
s.NoError(err)
env.RegisterDelayedCallback(func() {
env.UpdateWorkflow(AssignNodesToJobs, "TestUpdateID2", &updateCallback{
complete: func(response interface{}, err error) {
s.Error(err)
s.Contains(err.Error(), "not enough nodes to assign to job")
},
}, ClusterManagerAssignNodesToJobInput{
JobName: "little-task",
TotalNumNodes: 3,
})
}, time.Second)
},
}, ClusterManagerAssignNodesToJobInput{
JobName: "big-task",
TotalNumNodes: 24,
})
}, time.Second)
env.RegisterDelayedCallback(func() {
env.SignalWorkflow(ShutdownCluster, nil)
}, time.Minute)
env.ExecuteWorkflow(ClusterManagerWorkflow, ClusterManagerInput{})
s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
env.AssertExpectations(s.T())
}