def setUp(self) -> None: worker: Worker = Mock() worker.get_workflow_method = MagicMock( return_value=(DummyWorkflow, lambda *args: None)) self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=worker)
def mock_decider(run_event_loop_once): worker = Mock() decider = ReplayDecider("execution-id", WorkflowType(name="workflow-type"), worker) decider.event_loop = Mock() decider.event_loop.run_event_loop_once = run_event_loop_once decider.workflow_task = Mock() decider.service = Mock() return decider
class ReplayDeciderDestroyTest(TestCase): def setUp(self) -> None: self.workflow_task = Mock() self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=Mock()) self.decider.workflow_task = self.workflow_task def test_destroy(self): self.decider.destroy() self.workflow_task.destroy.assert_called()
def version_decision_context(version_marker_recorded_event): decider = ReplayDecider(execution_id=Mock(), workflow_type=Mock(), worker=Mock()) decision_context = DecisionContext(decider=decider) decider.decision_context = decision_context decider.decision_context.workflow_clock.version_handler.decision_context = decision_context decision_context.decider.decision_events = DecisionEvents(events=[], decision_events=[version_marker_recorded_event], replay=True, replay_current_time_milliseconds=0, next_decision_event_id=20) decision_context.decider.next_decision_event_id = 20 return decision_context
def setUp(self) -> None: events = make_history([ EventType.WorkflowExecutionStarted, EventType.DecisionTaskScheduled, EventType.DecisionTaskStarted]) events[0].workflow_execution_started_event_attributes = WorkflowExecutionStartedEventAttributes() helper = HistoryHelper(events) self.decision_events = helper.next() worker: Worker = Mock() worker.get_workflow_method = MagicMock(return_value=(DummyWorkflow, lambda *args: None)) self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=worker) self.decider.event_loop = Mock()
class TestDecideNextDecisionId(TestCase): def setUp(self) -> None: events = make_history([ EventType.WorkflowExecutionStarted, EventType.DecisionTaskScheduled, EventType.DecisionTaskStarted]) events[0].workflow_execution_started_event_attributes = WorkflowExecutionStartedEventAttributes() helper = HistoryHelper(events) self.decision_events = helper.next() worker: Worker = Mock() worker.get_workflow_method = MagicMock(return_value=(DummyWorkflow, lambda *args: None)) self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=worker) self.decider.event_loop = Mock() def test_first_decision_next_decision_id(self): self.decider.process_decision_events(self.decision_events) self.assertEqual(5, self.decider.next_decision_event_id)
class TestScheduleActivityTask(TestCase): def setUp(self) -> None: self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=Mock()) def test_schedule_activity_task(self): schedule_attributes = ScheduleActivityTaskDecisionAttributes() self.decider.schedule_activity_task(schedule_attributes) expected_decision_id = DecisionId(DecisionTarget.ACTIVITY, 0) self.assertEqual(1, self.decider.next_decision_event_id) self.assertEqual(1, len(self.decider.decisions)) state_machine: ActivityDecisionStateMachine = self.decider.decisions[expected_decision_id] self.assertIs(schedule_attributes, state_machine.schedule_attributes) self.assertEqual(expected_decision_id, state_machine.id) def test_activity_id(self): activity_id = 20 schedule_attributes = ScheduleActivityTaskDecisionAttributes() schedule_attributes.activity_id = activity_id self.decider.schedule_activity_task(schedule_attributes) self.assertEqual(0, self.decider.activity_id_to_scheduled_event_id[activity_id])
class TestReplayDecider(TestCase): def setUp(self) -> None: worker: Worker = Mock() worker.get_workflow_method = MagicMock( return_value=(DummyWorkflow, lambda *args: None)) self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=worker) def test_get_and_increment_next_id(self): self.assertEqual("0", self.decider.get_and_increment_next_id()) self.assertEqual("1", self.decider.get_and_increment_next_id()) def test_get_decisions(self): decision = Decision() state_machine: DecisionStateMachine = Mock() state_machine.get_decision = MagicMock(return_value=decision) self.decider.decisions[DecisionId(DecisionTarget.ACTIVITY, 10)] = state_machine decisions = self.decider.get_decisions() self.assertEqual(1, len(decisions)) self.assertIs(decision, decisions[0]) def test_get_decisions_none(self): state_machine: DecisionStateMachine = Mock() state_machine.get_decision = MagicMock(return_value=None) self.decider.decisions[DecisionId(DecisionTarget.ACTIVITY, 10)] = state_machine decisions = self.decider.get_decisions() self.assertEqual(0, len(decisions)) def test_get_decision(self): state_machine = DecisionStateMachine() decision_id = DecisionId(DecisionTarget.ACTIVITY, 20) self.decider.decisions[decision_id] = state_machine self.assertIs(state_machine, self.decider.get_decision(decision_id)) def test_get_decision_not_found(self): decision_id = DecisionId(DecisionTarget.ACTIVITY, 20) with self.assertRaises(NonDeterministicWorkflowException): self.decider.get_decision(decision_id) def test_notify_decision_sent(self): state_machine: DecisionStateMachine = Mock() self.decider.decisions[DecisionId(DecisionTarget.ACTIVITY, 10)] = state_machine self.decider.notify_decision_sent() state_machine.handle_decision_task_started_event.assert_called_once() def test_process_decision_events_notifies_when_replay(self): self.decider.event_loop = Mock() events = [ HistoryEvent(event_type=EventType.WorkflowExecutionStarted, workflow_execution_started_event_attributes= WorkflowExecutionStartedEventAttributes()), HistoryEvent(event_type=EventType.DecisionTaskScheduled) ] decision_events = DecisionEvents(events, [], replay=True, replay_current_time_milliseconds=0, next_decision_event_id=5) self.decider.notify_decision_sent = MagicMock() self.decider.process_decision_events(decision_events) self.decider.notify_decision_sent.assert_called_once() def test_activity_task_closed(self): state_machine: DecisionStateMachine = Mock() state_machine.is_done = MagicMock(return_value=True) self.decider.decisions[DecisionId(DecisionTarget.ACTIVITY, 10)] = state_machine ret = self.decider.handle_activity_task_closed(10) self.assertTrue(ret) state_machine.handle_completion_event.assert_called_once() state_machine.is_done.assert_called_once() def test_handle_activity_task_scheduled(self): state_machine: DecisionStateMachine = Mock() self.decider.decisions[DecisionId(DecisionTarget.ACTIVITY, 10)] = state_machine event = HistoryEvent(event_id=10) self.decider.handle_activity_task_scheduled(event) state_machine.handle_initiated_event.assert_called() args, kwargs = state_machine.handle_initiated_event.call_args_list[0] self.assertIn(event, args) def test_handle_activity_task_started(self): state_machine: DecisionStateMachine = Mock() self.decider.decisions[DecisionId(DecisionTarget.ACTIVITY, 10)] = state_machine event = HistoryEvent(event_id=15) event.activity_task_started_event_attributes = ActivityTaskStartedEventAttributes( ) event.activity_task_started_event_attributes.scheduled_event_id = 10 self.decider.handle_activity_task_started(event) state_machine.handle_started_event.assert_called() args, kwargs = state_machine.handle_started_event.call_args_list[0] self.assertIn(event, args) def tearDown(self) -> None: self.decider.destroy()
def setUp(self) -> None: self.workflow_task = Mock() self.decider = ReplayDecider(execution_id="", workflow_type=Mock(), worker=Mock()) self.decider.workflow_task = self.workflow_task
def mock_decision_context(decider: ReplayDecider): decision_context = MagicMock() decider.decision_context = decision_context return decision_context
def decider(): worker = MagicMock() decider = ReplayDecider("execution-id", WorkflowType(name="workflow-type"), worker) decider.next_decision_event_id = DECISION_EVENT_ID return decider
def decider(worker): return ReplayDecider("run-id", MagicMock(), worker)