-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathany_command_combination_test.go
212 lines (191 loc) · 8 KB
/
any_command_combination_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package integ
import (
"context"
"encoding/json"
"github.com/indeedeng/iwf/integ/helpers"
"github.com/indeedeng/iwf/service/common/ptr"
"strconv"
"testing"
"time"
"github.com/indeedeng/iwf/gen/iwfidl"
anycommandcombination "github.com/indeedeng/iwf/integ/workflow/any_command_combination"
"github.com/indeedeng/iwf/service"
"github.com/stretchr/testify/assert"
)
func TestAnyCommandCombinationWorkflowTemporal(t *testing.T) {
if !*temporalIntegTest {
t.Skip()
}
for i := 0; i < *repeatIntegTest; i++ {
doTestAnyCommandCombinationWorkflow(t, service.BackendTypeTemporal, nil)
smallWaitForFastTest()
}
}
func TestAnyCommandCombinationWorkflowTemporalContinueAsNew(t *testing.T) {
if !*temporalIntegTest {
t.Skip()
}
for i := 0; i < *repeatIntegTest; i++ {
// TODO not sure why using minimumContinueAsNewConfig(true) will fail
doTestAnyCommandCombinationWorkflow(t, service.BackendTypeTemporal, minimumContinueAsNewConfig(false))
smallWaitForFastTest()
}
}
func TestAnyCommandCombinationWorkflowCadence(t *testing.T) {
if !*cadenceIntegTest {
t.Skip()
}
for i := 0; i < *repeatIntegTest; i++ {
doTestAnyCommandCloseWorkflow(t, service.BackendTypeCadence, nil)
smallWaitForFastTest()
}
}
func TestAnyCommandCombinationWorkflowCadenceContinueAsNew(t *testing.T) {
if !*cadenceIntegTest {
t.Skip()
}
for i := 0; i < *repeatIntegTest; i++ {
doTestAnyCommandCloseWorkflow(t, service.BackendTypeCadence, minimumContinueAsNewConfig(false))
smallWaitForFastTest()
}
}
func doTestAnyCommandCombinationWorkflow(t *testing.T, backendType service.BackendType, config *iwfidl.WorkflowConfig) {
assertions := assert.New(t)
// start test workflow server
wfHandler := anycommandcombination.NewHandler()
closeFunc1 := startWorkflowWorker(wfHandler, t)
defer closeFunc1()
closeFunc2 := startIwfService(backendType)
defer closeFunc2()
// start a workflow
apiClient := iwfidl.NewAPIClient(&iwfidl.Configuration{
Servers: []iwfidl.ServerConfiguration{
{
URL: "http://localhost:" + testIwfServerPort,
},
},
})
wfId := anycommandcombination.WorkflowType + strconv.Itoa(int(time.Now().UnixNano()))
req := apiClient.DefaultApi.ApiV1WorkflowStartPost(context.Background())
_, httpResp, err := req.WorkflowStartRequest(iwfidl.WorkflowStartRequest{
WorkflowId: wfId,
IwfWorkflowType: anycommandcombination.WorkflowType,
WorkflowTimeoutSeconds: 40,
IwfWorkerUrl: "http://localhost:" + testWorkflowServerPort,
StartStateId: ptr.Any(anycommandcombination.State1),
WorkflowStartOptions: &iwfidl.WorkflowStartOptions{
WorkflowConfigOverride: config,
},
}).Execute()
failTestAtHttpError(err, httpResp, t)
signalValue := iwfidl.EncodedObject{
Encoding: iwfidl.PtrString("json"),
Data: iwfidl.PtrString("test-data-1"),
}
// send the signals to S1
req2 := apiClient.DefaultApi.ApiV1WorkflowSignalPost(context.Background())
httpResp, err = req2.WorkflowSignalRequest(iwfidl.WorkflowSignalRequest{
WorkflowId: wfId,
SignalChannelName: anycommandcombination.SignalNameAndId1,
SignalValue: &signalValue,
}).Execute()
failTestAtHttpError(err, httpResp, t)
httpResp, err = req2.WorkflowSignalRequest(iwfidl.WorkflowSignalRequest{
WorkflowId: wfId,
SignalChannelName: anycommandcombination.SignalNameAndId1,
SignalValue: &signalValue,
}).Execute()
failTestAtHttpError(err, httpResp, t)
// skip the timer for S1
time.Sleep(time.Second * 5) // wait for a few seconds so that timer is ready to be skipped
req3 := apiClient.DefaultApi.ApiV1WorkflowTimerSkipPost(context.Background())
httpResp, err = req3.WorkflowSkipTimerRequest(iwfidl.WorkflowSkipTimerRequest{
WorkflowId: wfId,
WorkflowStateExecutionId: "S1-1",
TimerCommandId: iwfidl.PtrString(anycommandcombination.TimerId1),
}).Execute()
failTestAtHttpError(err, httpResp, t)
// now it should be running at S2
// Future: we can check it is already done S1
// send first signal for s2
httpResp, err = req2.WorkflowSignalRequest(iwfidl.WorkflowSignalRequest{
WorkflowId: wfId,
SignalChannelName: anycommandcombination.SignalNameAndId1,
SignalValue: &signalValue,
}).Execute()
failTestAtHttpError(err, httpResp, t)
// wait and check the workflow, it should be still running
time.Sleep(time.Second)
reqDesc := apiClient.DefaultApi.ApiV1WorkflowGetPost(context.Background())
descResp, httpResp, err := reqDesc.WorkflowGetRequest(iwfidl.WorkflowGetRequest{
WorkflowId: wfId,
}).Execute()
failTestAtHttpError(err, httpResp, t)
assertions.Equal(iwfidl.RUNNING, descResp.GetWorkflowStatus())
httpResp, err = req2.WorkflowSignalRequest(iwfidl.WorkflowSignalRequest{
WorkflowId: wfId,
SignalChannelName: anycommandcombination.SignalNameAndId3,
SignalValue: &signalValue,
}).Execute()
failTestAtHttpError(err, httpResp, t)
// send 2nd signal for s2
httpResp, err = req2.WorkflowSignalRequest(iwfidl.WorkflowSignalRequest{
WorkflowId: wfId,
SignalChannelName: anycommandcombination.SignalNameAndId2,
SignalValue: &signalValue,
}).Execute()
failTestAtHttpError(err, httpResp, t)
// workflow should be completed now
if config == nil {
time.Sleep(time.Second)
descResp, httpResp, err = reqDesc.WorkflowGetRequest(iwfidl.WorkflowGetRequest{
WorkflowId: wfId,
}).Execute()
failTestAtHttpError(err, httpResp, t)
assertions.Equal(iwfidl.COMPLETED, descResp.GetWorkflowStatus())
} else {
reqWait := apiClient.DefaultApi.ApiV1WorkflowGetWithWaitPost(context.Background())
respWait, httpResp, err := reqWait.WorkflowGetRequest(iwfidl.WorkflowGetRequest{
WorkflowId: wfId,
}).Execute()
failTestAtHttpErrorOrWorkflowUncompleted(err, httpResp, respWait, t)
}
history, data := wfHandler.GetTestResult()
assertions.Equalf(map[string]int64{
"S1_start": 2,
"S1_decide": 1,
"S2_start": 2,
"S2_decide": 1,
}, history, "anycommandcombination test fail, %v", history)
var s1CommandResults iwfidl.CommandResults
var s2CommandResults iwfidl.CommandResults
s1ResultJsonStr := "{\"signalResults\":[" +
"{\"commandId\":\"test-signal-name1\",\"signalChannelName\":\"test-signal-name1\",\"signalRequestStatus\":\"RECEIVED\",\"signalValue\":{\"data\":\"test-data-1\",\"encoding\":\"json\"}}, " +
"{\"commandId\":\"test-signal-name1\",\"signalChannelName\":\"test-signal-name1\",\"signalRequestStatus\":\"RECEIVED\",\"signalValue\":{\"data\":\"test-data-1\",\"encoding\":\"json\"}}, " +
"{\"commandId\":\"test-signal-name2\",\"signalChannelName\":\"test-signal-name2\",\"signalRequestStatus\":\"WAITING\"}," +
"{\"commandId\":\"test-signal-name3\",\"signalChannelName\":\"test-signal-name3\",\"signalRequestStatus\":\"WAITING\"}" +
"],\"timerResults\":[" +
"{\"commandId\":\"test-timer-1\",\"timerStatus\":\"FIRED\"}]," +
"\"stateStartApiSucceeded\":true}"
err = json.Unmarshal([]byte(s1ResultJsonStr), &s1CommandResults)
if err != nil {
helpers.FailTestWithError(err, t)
}
s2ResultsJsonStr := "{\"signalResults\":[" +
"{\"commandId\":\"test-signal-name1\",\"signalChannelName\":\"test-signal-name1\",\"signalRequestStatus\":\"RECEIVED\",\"signalValue\":{\"data\":\"test-data-1\",\"encoding\":\"json\"}}, " +
"{\"commandId\":\"test-signal-name1\",\"signalChannelName\":\"test-signal-name1\",\"signalRequestStatus\":\"WAITING\"}," +
"{\"commandId\":\"test-signal-name2\",\"signalChannelName\":\"test-signal-name2\",\"signalRequestStatus\":\"RECEIVED\",\"signalValue\":{\"data\":\"test-data-1\",\"encoding\":\"json\"}}," +
"{\"commandId\":\"test-signal-name3\",\"signalChannelName\":\"test-signal-name3\",\"signalRequestStatus\":\"RECEIVED\",\"signalValue\":{\"data\":\"test-data-1\",\"encoding\":\"json\"}}" +
"],\"timerResults\":[" +
"{\"commandId\":\"test-timer-1\",\"timerStatus\":\"SCHEDULED\"}]," +
"\"stateStartApiSucceeded\":true}"
err = json.Unmarshal([]byte(s2ResultsJsonStr), &s2CommandResults)
if err != nil {
helpers.FailTestWithError(err, t)
}
expectedData := map[string]interface{}{
"s1_commandResults": s1CommandResults,
"s2_commandResults": s2CommandResults,
}
assertions.Equal(expectedData, data)
}