-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix any command of timer and signal combination (#80)
- Loading branch information
1 parent
fe375a9
commit a6e1e31
Showing
7 changed files
with
304 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package integ | ||
|
||
import ( | ||
"context" | ||
"github.com/indeedeng/iwf/gen/iwfidl" | ||
anytimersignal "github.com/indeedeng/iwf/integ/workflow/any_timer_signal" | ||
"github.com/indeedeng/iwf/service" | ||
"github.com/stretchr/testify/assert" | ||
"strconv" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestAnyTimerSignalWorkflowTemporal(t *testing.T) { | ||
doTestAnyTimerSignalWorkflow(t, service.BackendTypeTemporal) | ||
} | ||
|
||
// TODO this bug in Cadence SDK may cause the test to fail https://github.com/uber-go/cadence-client/issues/1198 | ||
func TestAnyTimerSignalWorkflowCadence(t *testing.T) { | ||
doTestAnyTimerSignalWorkflow(t, service.BackendTypeCadence) | ||
} | ||
|
||
func doTestAnyTimerSignalWorkflow(t *testing.T, backendType service.BackendType) { | ||
// start test workflow server | ||
wfHandler := anytimersignal.NewHandler() | ||
closeFunc1 := startWorkflowWorker(wfHandler) | ||
defer closeFunc1() | ||
|
||
closeFunc2 := startIwfService(backendType) | ||
defer closeFunc2() | ||
|
||
// create client | ||
apiClient := iwfidl.NewAPIClient(&iwfidl.Configuration{ | ||
Servers: []iwfidl.ServerConfiguration{ | ||
{ | ||
URL: "http://localhost:" + testIwfServerPort, | ||
}, | ||
}, | ||
}) | ||
|
||
// start a workflow | ||
wfId := anytimersignal.WorkflowType + strconv.Itoa(int(time.Now().Unix())) | ||
req := apiClient.DefaultApi.ApiV1WorkflowStartPost(context.Background()) | ||
_, httpResp, err := req.WorkflowStartRequest(iwfidl.WorkflowStartRequest{ | ||
WorkflowId: wfId, | ||
IwfWorkflowType: anytimersignal.WorkflowType, | ||
WorkflowTimeoutSeconds: 10, | ||
IwfWorkerUrl: "http://localhost:" + testWorkflowServerPort, | ||
StartStateId: anytimersignal.State1, | ||
}).Execute() | ||
panicAtHttpError(err, httpResp) | ||
|
||
// wait for 3 secs and send the signal | ||
time.Sleep(time.Second * 3) | ||
signalValue := iwfidl.EncodedObject{ | ||
Encoding: iwfidl.PtrString("json"), | ||
Data: iwfidl.PtrString("test-data-1"), | ||
} | ||
req2 := apiClient.DefaultApi.ApiV1WorkflowSignalPost(context.Background()) | ||
httpResp, err = req2.WorkflowSignalRequest(iwfidl.WorkflowSignalRequest{ | ||
WorkflowId: wfId, | ||
SignalChannelName: anytimersignal.SignalName, | ||
SignalValue: &signalValue, | ||
}).Execute() | ||
panicAtHttpError(err, httpResp) | ||
|
||
// wait for the workflow | ||
reqWait := apiClient.DefaultApi.ApiV1WorkflowGetWithWaitPost(context.Background()) | ||
_, httpResp, err = reqWait.WorkflowGetRequest(iwfidl.WorkflowGetRequest{ | ||
WorkflowId: wfId, | ||
}).Execute() | ||
panicAtHttpError(err, httpResp) | ||
|
||
history, data := wfHandler.GetTestResult() | ||
assertions := assert.New(t) | ||
assertions.Equalf(map[string]int64{ | ||
"S1_start": 2, | ||
"S1_decide": 2, | ||
"S2_start": 1, | ||
"S2_decide": 1, | ||
}, history, "anytimersignal test fail, %v", history) | ||
|
||
assertions.Equal(anytimersignal.SignalName, data["signalChannelName1"]) | ||
assertions.Equal("signal-cmd-id", data["signalCommandId1"]) | ||
assertions.Equal(service.SignalStatusWaiting, data["signalStatus1"]) | ||
|
||
assertions.Equal(anytimersignal.SignalName, data["signalChannelName2"]) | ||
assertions.Equal("signal-cmd-id", data["signalCommandId2"]) | ||
assertions.Equal(service.SignalStatusReceived, data["signalStatus2"]) | ||
assertions.Equal(signalValue, data["signalValue2"]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package anytimersignal | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/indeedeng/iwf/gen/iwfidl" | ||
"github.com/indeedeng/iwf/integ/workflow/common" | ||
"github.com/indeedeng/iwf/service" | ||
"log" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const ( | ||
WorkflowType = "any_timer_signal" | ||
State1 = "S1" | ||
State2 = "S2" | ||
SignalName = "test-signal-name" | ||
) | ||
|
||
type handler struct { | ||
invokeHistory map[string]int64 | ||
invokeData map[string]interface{} | ||
} | ||
|
||
func NewHandler() common.WorkflowHandler { | ||
return &handler{ | ||
invokeHistory: make(map[string]int64), | ||
invokeData: make(map[string]interface{}), | ||
} | ||
} | ||
|
||
// ApiV1WorkflowStartPost - for a workflow | ||
func (h *handler) ApiV1WorkflowStateStart(c *gin.Context) { | ||
var req iwfidl.WorkflowStateStartRequest | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
log.Println("received state start request, ", req) | ||
|
||
if req.GetWorkflowType() == WorkflowType { | ||
h.invokeHistory[req.GetWorkflowStateId()+"_start"]++ | ||
if req.GetWorkflowStateId() == State1 { | ||
var timerCommands []iwfidl.TimerCommand | ||
context := req.GetContext() | ||
if context.GetStateExecutionId() == State1+"-"+"1" { | ||
now := time.Now().Unix() | ||
timerCommands = []iwfidl.TimerCommand{ | ||
{ | ||
FiringUnixTimestampSeconds: now + 1, // fire after 1s | ||
}, | ||
} | ||
} | ||
|
||
c.JSON(http.StatusOK, iwfidl.WorkflowStateStartResponse{ | ||
CommandRequest: &iwfidl.CommandRequest{ | ||
SignalCommands: []iwfidl.SignalCommand{ | ||
{ | ||
CommandId: "signal-cmd-id", | ||
SignalChannelName: SignalName, | ||
}, | ||
}, | ||
TimerCommands: timerCommands, | ||
DeciderTriggerType: service.DeciderTypeAnyCommandCompleted, | ||
}, | ||
}) | ||
return | ||
} | ||
if req.GetWorkflowStateId() == State2 { | ||
c.JSON(http.StatusOK, iwfidl.WorkflowStateStartResponse{ | ||
CommandRequest: &iwfidl.CommandRequest{ | ||
DeciderTriggerType: service.DeciderTypeAllCommandCompleted, | ||
}, | ||
}) | ||
return | ||
} | ||
} | ||
|
||
c.JSON(http.StatusBadRequest, struct{}{}) | ||
} | ||
|
||
func (h *handler) ApiV1WorkflowStateDecide(c *gin.Context) { | ||
var req iwfidl.WorkflowStateDecideRequest | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) | ||
return | ||
} | ||
log.Println("received state decide request, ", req) | ||
|
||
if req.GetWorkflowType() == WorkflowType { | ||
h.invokeHistory[req.GetWorkflowStateId()+"_decide"]++ | ||
if req.GetWorkflowStateId() == State1 { | ||
signalResults := req.GetCommandResults() | ||
var movements []iwfidl.StateMovement | ||
|
||
context := req.GetContext() | ||
if context.GetStateExecutionId() == State1+"-"+"1" { | ||
h.invokeData["signalChannelName1"] = signalResults.SignalResults[0].GetSignalChannelName() | ||
h.invokeData["signalCommandId1"] = signalResults.SignalResults[0].GetCommandId() | ||
h.invokeData["signalStatus1"] = signalResults.SignalResults[0].GetSignalRequestStatus() | ||
movements = []iwfidl.StateMovement{{StateId: State1}} | ||
} else { | ||
h.invokeData["signalChannelName2"] = signalResults.SignalResults[0].GetSignalChannelName() | ||
h.invokeData["signalCommandId2"] = signalResults.SignalResults[0].GetCommandId() | ||
h.invokeData["signalStatus2"] = signalResults.SignalResults[0].GetSignalRequestStatus() | ||
h.invokeData["signalValue2"] = signalResults.SignalResults[0].GetSignalValue() | ||
movements = []iwfidl.StateMovement{{StateId: State2}} | ||
} | ||
|
||
c.JSON(http.StatusOK, iwfidl.WorkflowStateDecideResponse{ | ||
StateDecision: &iwfidl.StateDecision{ | ||
NextStates: movements, | ||
}, | ||
}) | ||
return | ||
} else if req.GetWorkflowStateId() == State2 { | ||
// go to complete | ||
c.JSON(http.StatusOK, iwfidl.WorkflowStateDecideResponse{ | ||
StateDecision: &iwfidl.StateDecision{ | ||
NextStates: []iwfidl.StateMovement{ | ||
{ | ||
StateId: service.GracefulCompletingWorkflowStateId, | ||
}, | ||
}, | ||
}, | ||
}) | ||
return | ||
} | ||
} | ||
|
||
c.JSON(http.StatusBadRequest, struct{}{}) | ||
} | ||
|
||
func (h *handler) GetTestResult() (map[string]int64, map[string]interface{}) { | ||
return h.invokeHistory, h.invokeData | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.