def test_describe_workflow_execution_invalid_workflow(self): request = DescribeWorkflowExecutionRequest() request.domain = "test-domain" request.execution = WorkflowExecution() request.execution.workflow_id = str(uuid4()) request.execution.run_id = str(uuid4()) response, err = self.service.describe_workflow_execution(request) self.assertIsNone(response) self.assertIsInstance(err, EntityNotExistsError)
def new_workflow_stub_from_workflow_id(self, cls: Type, workflow_id: str): """ Use it to send signals or queries to a running workflow. Do not call workflow methods on it """ stub_instance = self.new_workflow_stub(cls) execution = WorkflowExecution(workflow_id=workflow_id, run_id=None) stub_instance._execution = execution return stub_instance
def exec_workflow(workflow_client, wm: WorkflowMethod, args, workflow_options: WorkflowOptions = None, stub_instance: object = None) -> WorkflowExecutionContext: start_request = create_start_workflow_request(workflow_client, wm, args) start_response, err = workflow_client.service.start_workflow(start_request) if err: raise Exception(err) execution = WorkflowExecution(workflow_id=start_request.workflow_id, run_id=start_response.run_id) stub_instance._execution = execution return WorkflowExecutionContext(workflow_type=wm._name, workflow_execution=execution)
def test_reset_sticky_task_list(self): start_response, _ = self.service.start_workflow(self.request) request = ResetStickyTaskListRequest() request.domain = "test-domain" request.execution = WorkflowExecution() request.execution.workflow_id = self.request.workflow_id request.execution.run_id = start_response.run_id response, err = self.service.reset_sticky_task_list(request) self.assertIsNone(err) self.assertIsNotNone(response)
def test_terminate_workflow_execution(self): start_response, _ = self.service.start_workflow(self.request) request = TerminateWorkflowExecutionRequest() request.domain = "test-domain" request.workflow_execution = WorkflowExecution() request.workflow_execution.workflow_id = self.request.workflow_id request.workflow_execution.run_id = start_response.run_id response, err = self.service.terminate_workflow_execution(request) self.assertIsNone(err) self.assertIsNone(response)
def create_close_history_event_request(workflow_client: WorkflowClient, workflow_id: str, run_id: str) -> GetWorkflowExecutionHistoryRequest: history_request = GetWorkflowExecutionHistoryRequest() history_request.domain = workflow_client.domain history_request.execution = WorkflowExecution() history_request.execution.workflow_id = workflow_id history_request.execution.run_id = run_id history_request.wait_for_new_event = True history_request.history_event_filter_type = HistoryEventFilterType.CLOSE_EVENT return history_request
def test_describe_workflow_execution(self): start_response, _ = self.service.start_workflow(self.request) request = DescribeWorkflowExecutionRequest() request.domain = "test-domain" request.execution = WorkflowExecution() request.execution.workflow_id = self.request.workflow_id request.execution.run_id = start_response.run_id response, err = self.service.describe_workflow_execution(request) self.assertIsNone(err) self.assertIsNotNone(response) self.assertIsInstance(response, DescribeWorkflowExecutionResponse)
def test_signal_workflow_execution(self): start_response, _ = self.service.start_workflow(self.request) request = SignalWorkflowExecutionRequest() request.domain = "test-domain" request.signal_name = "dummy-signal" request.workflow_execution = WorkflowExecution() request.workflow_execution.workflow_id = self.request.workflow_id request.workflow_execution.run_id = start_response.run_id response, err = self.service.signal_workflow_execution(request) self.assertIsNone(err) self.assertIsNone(response)
def test_get_workflow_execution_history(self): response, err = self.service.start_workflow(self.request) request = GetWorkflowExecutionHistoryRequest() request.domain = "test-domain" request.execution = WorkflowExecution() request.execution.workflow_id = self.request.workflow_id request.execution.run_id = response.run_id response, err = self.service.get_workflow_execution_history(request) self.assertIsNone(err) self.assertIsNotNone(response) self.assertIsNotNone(response.history) self.assertIsNotNone(response.history.events)
def test_query_workflow_timeout(self): start_response, _ = self.service.start_workflow(self.request) request = QueryWorkflowRequest() request.domain = "test-domain" request.execution = WorkflowExecution() request.execution.workflow_id = self.request.workflow_id request.execution.run_id = start_response.run_id request.query = WorkflowQuery() request.query.query_type = "getDummy" request.query.query_args = None with self.assertRaisesRegex(TChannelException, "timeout") as context: self.service.query_workflow(request)
def test_exec_query_query_rejected(): stub = Mock() stub._execution = WorkflowExecution() response = QueryWorkflowResponse() response.query_rejected = QueryRejected(close_status=WorkflowExecutionCloseStatus.COMPLETED) workflow_client = Mock() workflow_client.domain = "the-domain" workflow_client.service = Mock() workflow_client.service.query_workflow = MagicMock(return_value=(response, None)) with pytest.raises(QueryRejectedException) as exc_info: exec_query(workflow_client, QueryMethod(name="the_query_method"), [1, 2, 3], stub) assert exc_info.value.close_status == WorkflowExecutionCloseStatus.COMPLETED
def wait_for_close_with_workflow_id(self, workflow_id: str, run_id: str = None, workflow_type: str = None): while True: history_request = create_close_history_event_request( self, workflow_id, run_id) history_response, err = self.service.get_workflow_execution_history( history_request) if err: raise Exception(err) if not history_response.history.events: continue history_event = history_response.history.events[0] if history_event.event_type == EventType.WorkflowExecutionCompleted: attributes = history_event.workflow_execution_completed_event_attributes return json.loads(attributes.result) elif history_event.event_type == EventType.WorkflowExecutionFailed: attributes = history_event.workflow_execution_failed_event_attributes if attributes.reason == "WorkflowFailureException": exception = deserialize_exception(attributes.details) if isinstance(exception, ActivityFailureException): exception.set_cause() workflow_execution = WorkflowExecution( workflow_id=workflow_id, run_id=run_id) raise WorkflowFailureException( workflow_type=workflow_type, execution=workflow_execution) from exception else: details: Dict = json.loads(attributes.details) detail_message = details.get("detailMessage", "") raise WorkflowExecutionFailedException( attributes.reason, details=details, detail_message=detail_message) elif history_event.event_type == EventType.WorkflowExecutionTimedOut: raise WorkflowExecutionTimedOutException() elif history_event.event_type == EventType.WorkflowExecutionTerminated: attributes = history_event.workflow_execution_terminated_event_attributes raise WorkflowExecutionTerminatedException( reason=attributes.reason, details=attributes.details, identity=attributes.identity) elif history_event.event_type == EventType.WorkflowExecutionCanceled: raise WorkflowExecutionCanceledException() else: raise Exception("Unexpected history close event: " + str(history_event))
def test_exec_query(): stub = Mock() stub._execution = WorkflowExecution() response = QueryWorkflowResponse() response.query_result = '"blah"' workflow_client = Mock() workflow_client.domain = "the-domain" workflow_client.service = Mock() workflow_client.service.query_workflow = MagicMock(return_value=(response, None)) ret = exec_query(workflow_client, QueryMethod(name="the_query_method"), [1, 2, 3], stub) assert ret == "blah" args, kwargs = workflow_client.service.query_workflow.call_args_list[0] request: QueryWorkflowRequest = args[0] assert request.execution is stub._execution assert request.query.query_type == "the_query_method" assert request.query.query_args == json.dumps([1, 2, 3]) assert request.domain == "the-domain"
def setUp(self) -> None: self.python_object = PollForActivityTaskResponse() self.python_object.task_token = "TASK_TOKEN" self.python_object.workflow_execution = WorkflowExecution() self.python_object.workflow_execution.workflow_id = "WORKFLOW_ID" self.python_object.workflow_execution.run_id = "RUN_ID"
def workflow_execution(): return WorkflowExecution(workflow_id="the-workflow-id", run_id="the-run-id")
import json from cadence.cadence_types import GetWorkflowExecutionHistoryRequest, WorkflowExecution from cadence.workflowservice import WorkflowService class Encoder(json.JSONEncoder): def default(self, o): if dataclasses.is_dataclass(o): return dataclasses.asdict(o) if isinstance(o, bytes): return str(o, 'utf-8') return super().default(o) if __name__ == "__main__": service = WorkflowService.create("localhost", 7933) history_request = GetWorkflowExecutionHistoryRequest() history_request.domain = "sample" history_request.execution = WorkflowExecution() history_request.execution.workflow_id = sys.argv[1] history_request.maximum_page_size = 100 history_response, err = service.get_workflow_execution_history( history_request) if err: print(err) else: print(json.dumps(history_response, cls=Encoder, indent=2))