@@ -117,6 +117,17 @@ func createTestEventDecisionTaskStarted(eventID int64) *s.HistoryEvent {
117117 EventType : common .EventTypePtr (s .EventType_DecisionTaskStarted )}
118118}
119119
120+ func createTestEventWorkflowExecutionSignaled (eventID int64 , signalName string ) * s.HistoryEvent {
121+ return & s.HistoryEvent {
122+ EventId : common .Int64Ptr (eventID ),
123+ EventType : common .EventTypePtr (s .EventType_WorkflowExecutionSignaled ),
124+ WorkflowExecutionSignaledEventAttributes : & s.WorkflowExecutionSignaledEventAttributes {
125+ SignalName : common .StringPtr (signalName ),
126+ Identity : common .StringPtr ("test-identity" ),
127+ },
128+ }
129+ }
130+
120131func createTestEventDecisionTaskCompleted (eventID int64 , attr * s.DecisionTaskCompletedEventAttributes ) * s.HistoryEvent {
121132 return & s.HistoryEvent {
122133 EventId : common .Int64Ptr (eventID ),
@@ -140,6 +151,19 @@ func createWorkflowTask(
140151 }
141152}
142153
154+ func createQueryTask (
155+ events []* s.HistoryEvent ,
156+ previousStartEventID int64 ,
157+ workflowName string ,
158+ queryType string ,
159+ ) * s.PollForDecisionTaskResponse {
160+ task := createWorkflowTask (events , previousStartEventID , workflowName )
161+ task .Query = & s.WorkflowQuery {
162+ QueryType : common .StringPtr (queryType ),
163+ }
164+ return task
165+ }
166+
143167func (t * TaskHandlersTestSuite ) TestWorkflowTask_WorkflowExecutionStarted () {
144168 taskList := "tl1"
145169 testEvents := []* s.HistoryEvent {
@@ -152,7 +176,8 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_WorkflowExecutionStarted() {
152176 Logger : t .logger ,
153177 }
154178 taskHandler := newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
155- response , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
179+ request , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
180+ response := request .(* s.RespondDecisionTaskCompletedRequest )
156181 t .NoError (err )
157182 t .NotNil (response )
158183 t .Equal (1 , len (response .GetDecisions ()))
@@ -181,7 +206,8 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_ActivityTaskScheduled() {
181206 Logger : t .logger ,
182207 }
183208 taskHandler := newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
184- response , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
209+ request , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
210+ response := request .(* s.RespondDecisionTaskCompletedRequest )
185211
186212 t .NoError (err )
187213 t .NotNil (response )
@@ -191,14 +217,86 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_ActivityTaskScheduled() {
191217
192218 // Schedule an activity and see if we complete workflow, Having only one last decision.
193219 task = createWorkflowTask (testEvents , 2 , "HelloWorld_Workflow" )
194- response , _ , err = taskHandler .ProcessWorkflowTask (task , nil , false )
220+ request , _ , err = taskHandler .ProcessWorkflowTask (task , nil , false )
221+ response = request .(* s.RespondDecisionTaskCompletedRequest )
195222 t .NoError (err )
196223 t .NotNil (response )
197224 t .Equal (1 , len (response .GetDecisions ()))
198225 t .Equal (s .DecisionType_CompleteWorkflowExecution , response .GetDecisions ()[0 ].GetDecisionType ())
199226 t .NotNil (response .GetDecisions ()[0 ].GetCompleteWorkflowExecutionDecisionAttributes ())
200227}
201228
229+ func (t * TaskHandlersTestSuite ) TestWorkflowTask_QueryWorkflow () {
230+ // Schedule an activity and see if we complete workflow.
231+ taskList := "tl1"
232+ testEvents := []* s.HistoryEvent {
233+ createTestEventWorkflowExecutionStarted (1 , & s.WorkflowExecutionStartedEventAttributes {TaskList : & s.TaskList {Name : & taskList }}),
234+ createTestEventDecisionTaskScheduled (2 , & s.DecisionTaskScheduledEventAttributes {TaskList : & s.TaskList {Name : & taskList }}),
235+ createTestEventDecisionTaskStarted (3 ),
236+ createTestEventDecisionTaskCompleted (4 , & s.DecisionTaskCompletedEventAttributes {ScheduledEventId : common .Int64Ptr (2 )}),
237+ createTestEventActivityTaskScheduled (5 , & s.ActivityTaskScheduledEventAttributes {
238+ ActivityId : common .StringPtr ("0" ),
239+ ActivityType : & s.ActivityType {Name : common .StringPtr ("Greeter_Activity" )},
240+ TaskList : & s.TaskList {Name : & taskList },
241+ }),
242+ createTestEventActivityTaskStarted (6 , & s.ActivityTaskStartedEventAttributes {}),
243+ createTestEventActivityTaskCompleted (7 , & s.ActivityTaskCompletedEventAttributes {ScheduledEventId : common .Int64Ptr (5 )}),
244+ createTestEventDecisionTaskStarted (8 ),
245+ createTestEventWorkflowExecutionSignaled (9 , "test-signal" ),
246+ }
247+ params := workerExecutionParameters {
248+ TaskList : taskList ,
249+ Identity : "test-id-1" ,
250+ Logger : t .logger ,
251+ }
252+
253+ // query after first decision task (notice the previousStartEventID is always the last eventID for query task)
254+ task := createQueryTask (testEvents [0 :3 ], 3 , "HelloWorld_Workflow" , "test-query" )
255+ taskHandler := newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
256+ response , _ , _ := taskHandler .ProcessWorkflowTask (task , nil , false )
257+ t .verifyQueryResult (response , "waiting-activity-result" )
258+
259+ // query after activity task complete but before second decision task started
260+ task = createQueryTask (testEvents [0 :7 ], 7 , "HelloWorld_Workflow" , "test-query" )
261+ taskHandler = newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
262+ response , _ , _ = taskHandler .ProcessWorkflowTask (task , nil , false )
263+ t .verifyQueryResult (response , "waiting-activity-result" )
264+
265+ // query after second decision task
266+ task = createQueryTask (testEvents [0 :8 ], 8 , "HelloWorld_Workflow" , "test-query" )
267+ taskHandler = newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
268+ response , _ , _ = taskHandler .ProcessWorkflowTask (task , nil , false )
269+ t .verifyQueryResult (response , "done" )
270+
271+ // query after second decision task with extra events
272+ task = createQueryTask (testEvents [0 :9 ], 9 , "HelloWorld_Workflow" , "test-query" )
273+ taskHandler = newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
274+ response , _ , _ = taskHandler .ProcessWorkflowTask (task , nil , false )
275+ t .verifyQueryResult (response , "done" )
276+
277+ task = createQueryTask (testEvents [0 :9 ], 9 , "HelloWorld_Workflow" , "invalid-query-type" )
278+ taskHandler = newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
279+ response , _ , _ = taskHandler .ProcessWorkflowTask (task , nil , false )
280+ t .NotNil (response )
281+ queryResp , ok := response .(* s.RespondQueryTaskCompletedRequest )
282+ t .True (ok )
283+ t .NotNil (queryResp .ErrorMessage )
284+ t .Contains (* queryResp .ErrorMessage , "unkonwn queryType" )
285+ }
286+
287+ func (t * TaskHandlersTestSuite ) verifyQueryResult (response interface {}, expectedResult string ) {
288+ t .NotNil (response )
289+ queryResp , ok := response .(* s.RespondQueryTaskCompletedRequest )
290+ t .True (ok )
291+ t .Nil (queryResp .ErrorMessage )
292+ t .NotNil (queryResp .QueryResult_ )
293+ encodedValue := EncodedValue (queryResp .QueryResult_ )
294+ var queryResult string
295+ err := encodedValue .Get (& queryResult )
296+ t .NoError (err )
297+ t .Equal (expectedResult , queryResult )
298+ }
299+
202300func (t * TaskHandlersTestSuite ) TestWorkflowTask_NondeterministicDetection () {
203301 taskList := "taskList"
204302 testEvents := []* s.HistoryEvent {
@@ -216,17 +314,17 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_NondeterministicDetection() {
216314 Logger : zap .NewNop (),
217315 }
218316 taskHandler := newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
219- response , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
220-
317+ request , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
318+ response := request .( * s. RespondDecisionTaskCompletedRequest )
221319 // there should be no error as the history events matched the decisions.
222320 t .NoError (err )
223321 t .NotNil (response )
224322
225323 // now change the history event so it does not match to decision produced via replay
226324 testEvents [1 ].ActivityTaskScheduledEventAttributes .ActivityType .Name = common .StringPtr ("some-other-activity" )
227- response , _ , err = taskHandler .ProcessWorkflowTask (task , nil , false )
325+ request , _ , err = taskHandler .ProcessWorkflowTask (task , nil , false )
228326 t .Error (err )
229- t .Nil (response )
327+ t .Nil (request )
230328 t .Contains (err .Error (), "nondeterministic" )
231329}
232330
@@ -246,8 +344,8 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_CancelActivityBeforeSent() {
246344 Logger : t .logger ,
247345 }
248346 taskHandler := newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
249- response , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
250-
347+ request , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
348+ response := request .( * s. RespondDecisionTaskCompletedRequest )
251349 t .NoError (err )
252350 t .NotNil (response )
253351 //t.printAllDecisions(response.GetDecisions())
@@ -275,10 +373,9 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_PressurePoints() {
275373 Logger : t .logger ,
276374 }
277375 taskHandler := newWorkflowTaskHandler (testDomain , params , ppMgr , getHostEnvironment ())
278- response , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
279-
376+ request , _ , err := taskHandler .ProcessWorkflowTask (task , nil , false )
280377 t .Error (err )
281- t .Nil (response )
378+ t .Nil (request )
282379}
283380
284381func (t * TaskHandlersTestSuite ) TestWorkflowTask_PageToken () {
@@ -304,8 +401,8 @@ func (t *TaskHandlersTestSuite) TestWorkflowTask_PageToken() {
304401 return & s.History {nextEvents }, nil , nil
305402 }
306403 taskHandler := newWorkflowTaskHandler (testDomain , params , nil , getHostEnvironment ())
307- response , _ , err := taskHandler .ProcessWorkflowTask (task , iteratorfn , false )
308-
404+ request , _ , err := taskHandler .ProcessWorkflowTask (task , iteratorfn , false )
405+ response := request .( * s. RespondDecisionTaskCompletedRequest )
309406 t .NoError (err )
310407 t .NotNil (response )
311408}
0 commit comments