def setUp(self) -> None: fp = open(os.path.join(__location__, "workflow_started_decision_task_response.json")) self.poll_response: PollForDecisionTaskResponse = json_to_data_class(json.loads(fp.read()), PollForDecisionTaskResponse) fp.close() self.worker = Worker() self.loop = DecisionTaskLoop(worker=self.worker) global dummy_workflow_self dummy_workflow_self = None
def test_replay_decider_respond_query_error(): service: WorkflowService = Mock() service.respond_query_task_completed = Mock(return_value=(None, None)) decision_task_loop = DecisionTaskLoop(worker=Mock(), service=service) decision_task_loop.respond_query(task_token=b"the-task-token", result=None, error_message=b"the-error") service.respond_query_task_completed.assert_called_once() args, kwargs = service.respond_query_task_completed.call_args_list[0] request = args[0] assert isinstance(request, RespondQueryTaskCompletedRequest) assert request.task_token == b"the-task-token" assert request.error_message == b"the-error"
def start(self): from cadence.activity_loop import activity_task_loop from cadence.decision_loop import DecisionTaskLoop self.threads_stopped = 0 self.threads_started = 0 self.stop_requested = False if self.activities: thread = threading.Thread(target=activity_task_loop, args=(self, )) thread.start() self.threads_started += 1 if self.workflow_methods: decision_task_loop = DecisionTaskLoop(worker=self) decision_task_loop.start() self.threads_started += 1
class TestDecisionTaskLoop(TestCase): def setUp(self) -> None: fp = open( os.path.join(__location__, "workflow_started_decision_task_response.json")) self.poll_response: PollForDecisionTaskResponse = json_to_data_class( json.loads(fp.read()), PollForDecisionTaskResponse) fp.close() self.worker = Worker() self.loop = DecisionTaskLoop(worker=self.worker) global dummy_workflow_self dummy_workflow_self = None def test_create_workflow_object(self): class DummyWorkflow: @workflow_method() async def dummy(self): nonlocal dummy_workflow_self dummy_workflow_self = self dummy_workflow_self = None self.worker.register_workflow_implementation_type(DummyWorkflow) self.loop.process_task(self.poll_response) self.assertIsInstance(dummy_workflow_self, DummyWorkflow) def test_return_none(self): class DummyWorkflow: @workflow_method() async def dummy(self): return None self.worker.register_workflow_implementation_type(DummyWorkflow) decisions = self.loop.process_task(self.poll_response) complete_workflow = decisions[ 0].complete_workflow_execution_decision_attributes self.assertEqual("null", complete_workflow.result) def test_one_arg(self): class DummyWorkflow: @workflow_method() async def dummy(self, arg1): nonlocal arg1_value arg1_value = arg1 arg1_value = None self.worker.register_workflow_implementation_type(DummyWorkflow) self.poll_response.history.events[ 0].workflow_execution_started_event_attributes.input = json.dumps( ["first"]) self.loop.process_task(self.poll_response) self.assertEqual(arg1_value, "first") def test_args(self): class DummyWorkflow: @workflow_method() async def dummy(self, arg1, arg2): nonlocal arg1_value, arg2_value arg1_value = arg1 arg2_value = arg2 arg1_value = None arg2_value = None self.worker.register_workflow_implementation_type(DummyWorkflow) self.poll_response.history.events[ 0].workflow_execution_started_event_attributes.input = json.dumps( ["first", "second"]) self.loop.process_task(self.poll_response) self.assertEqual(arg1_value, "first") self.assertEqual(arg2_value, "second") def test_no_args(self): class DummyWorkflow: @workflow_method() async def dummy(self): nonlocal executed executed = True executed = False self.worker.register_workflow_implementation_type(DummyWorkflow) self.poll_response.history.events[ 0].workflow_execution_started_event_attributes.input = json.dumps( []) self.loop.process_task(self.poll_response) self.assertTrue(executed) def test_return_value(self): class DummyWorkflow: @workflow_method() async def dummy(self): return "value" self.worker.register_workflow_implementation_type(DummyWorkflow) decisions = self.loop.process_task(self.poll_response) complete_workflow = decisions[ 0].complete_workflow_execution_decision_attributes self.assertEqual('"value"', complete_workflow.result)