def test_script_worker_calls_correct_function_on_message_type( self, mock_env_fn, mock_load_fn, mock_run_fn, caplog): mp = multiprocessing.get_context() script = GitScript("git://test.py", GitArgs()) env_evt = EventMessage("test", "ENV", script) load_evt = EventMessage("test", "LOAD", script) run_evt = EventMessage("test", "RUN", ("init", None)) work_q = MPQueue(ctx=mp) work_q.put(env_evt) work_q.put(load_evt) work_q.put(run_evt) _proc_worker_wrapper_helper(mp, caplog, ScriptWorker, args=(work_q, ), expect_shutdown_evt=True) env_args, _ = mock_env_fn.call_args assert env_args[0].msg_type == env_evt.msg_type mock_env_fn.assert_called_once() load_args, _ = mock_load_fn.call_args assert load_args[0].msg_type == load_evt.msg_type mock_load_fn.assert_called_once() run_args, _ = mock_run_fn.call_args assert run_args[0].msg_type == run_evt.msg_type mock_run_fn.assert_called_once()
def test_external_messages_are_published_locally(self, mp_fixture, caplog): """ Verify that message event is published if the event originates from an external source. """ pubsub.pub.unsubAll() helper = PubSubHelper() work_q = MPQueue(ctx=mp_fixture) msg = EventMessage( "EXTERNAL COMPONENT", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}), ) work_q.put(msg) with mock.patch.object(pubsub.pub, "unsubAll", return_value=[]): _proc_worker_wrapper_helper( mp_fixture, caplog, EventBusWorker, args=(work_q, ), expect_shutdown_evt=True, ) assert topics.request.procedure.list in helper.topic_list work_q.safe_close()
def assert_command_request_and_response(mp_fixture, caplog, mock_method, request_topic, response_topic, cmd): pubsub.pub.unsubAll() helper = PubSubHelper() work_q = MPQueue(ctx=mp_fixture) msg = EventMessage( "UNITTEST", "PUBSUB", dict(topic=request_topic, kwargs={ "request_id": "1234", "cmd": cmd }), ) work_q.put(msg) event = mp_fixture.Event() mock_method.side_effect = partial(set_event, event) with mock.patch.object(pubsub.pub, "unsubAll", return_value=[]): _proc_worker_wrapper_helper( mp_fixture, caplog, ScriptExecutionServiceWorker, args=(work_q, mp_fixture), expect_shutdown_evt=True, ) assert event.is_set() mock_method.assert_called_once() assert mock_method.call_args[0][0] == cmd assert helper.topic_list == [request_topic, response_topic] work_q.safe_close()
def test_internal_messages_not_republished(self, mp, caplog): """ Verify that message event is not published if the event originates from an internal source. """ helper = PubSubHelper() work_q = MPQueue(ctx=mp) # TEST is the default component name assigned in # _proc_worker_wrapper_helper. This message should not be published to pypubsub msg = EventMessage( "TEST", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}), ) work_q.put(msg) _proc_worker_wrapper_helper(mp, caplog, ScriptWorker, args=(work_q, ), expect_shutdown_evt=True) msgs_on_topic = helper.messages_on_topic(topics.request.procedure.list) assert len(msgs_on_topic) == 0
def test_on_load(self, mock_module_fn, mp, caplog): """ """ mock_module_fn.side_effect = MagicMock() script = GitScript("git://test.py", GitArgs()) evt = EventMessage("test", "LOAD", script) work_q = MPQueue(ctx=mp) work_q.put(evt) _proc_worker_wrapper_helper(mp, caplog, ScriptWorker, args=(work_q, ), expect_shutdown_evt=True) assert mock_module_fn.called_once_with(script)
def test_flask_worker_starts_flask(mp_fixture, caplog): """ Verify that the FlaskWorker starts Flask. """ with mock.patch("flask.Flask") as mock_flask: # mock Flask causes connection error in shutdown as shutdown URL is accessed with mock.patch("requests.post"): _proc_worker_wrapper_helper( mp_fixture, caplog, FlaskWorker, args=(MPQueue(ctx=mp_fixture), ), expect_shutdown_evt=True, ) mock_app_instance = mock_flask.return_value mock_app_instance.run.assert_called_once()
def test_internal_messages_not_republished(self, mp_fixture, caplog): """ Verify that message event is not published if the event originates from an internal source. """ pubsub.pub.unsubAll() helper = PubSubHelper() work_q = MPQueue(ctx=mp_fixture) # TEST is the default component name assigned in # _proc_worker_wrapper_helper. This message should be ignored. msg = EventMessage( "TEST", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}), ) work_q.put(msg) # But coming from NONTEST, this message should be republished. msg = EventMessage( "NONTEST", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "456"}), ) work_q.put(msg) with mock.patch.object(pubsub.pub, "unsubAll", return_value=[]): _proc_worker_wrapper_helper( mp_fixture, caplog, EventBusWorker, args=(work_q, ), expect_shutdown_evt=True, ) assert len(helper.messages) == 1 assert helper.messages[0][1] == dict(msg_src="NONTEST", request_id="456") work_q.safe_close()
def test_handles_request_to_list_invalid_id(self, mp_fixture, caplog): """ The ValueError raised when SES.summarise is given an invalid PID should be handled. """ pubsub.pub.unsubAll() helper = PubSubHelper() work_q = MPQueue(ctx=mp_fixture) msg = EventMessage( "TEST_SUMMARY", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}), ) work_q.put(msg) with mock.patch( "ska_oso_oet.procedure.application.main.ScriptExecutionService.summarise" ) as mock_cls: with mock.patch.object(pubsub.pub, "unsubAll", return_value=[]): mock_cls.side_effect = ValueError _proc_worker_wrapper_helper( mp_fixture, caplog, ScriptExecutionServiceWorker, args=(work_q, mp_fixture), expect_shutdown_evt=True, ) mock_cls.assert_called_once() assert helper.topic_list == [ topics.request.procedure.list, # list requested topics.procedure.pool.list, # response published ] assert helper.messages[1][1] == dict(msg_src="TEST", request_id="123", result=[]) work_q.safe_close()
def test_list_method_called(self, mp_fixture, caplog): """ SES.summarise should be called when 'request.procedure.list' message is received """ pubsub.pub.unsubAll() helper = PubSubHelper() work_q = MPQueue(ctx=mp_fixture) msg = EventMessage( "TEST_SUMMARY", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}), ) work_q.put(msg) event = mp_fixture.Event() with mock.patch( "ska_oso_oet.procedure.application.main.ScriptExecutionService.summarise" ) as mock_cls: with mock.patch.object(pubsub.pub, "unsubAll", return_value=[]): mock_cls.side_effect = partial(set_event, event) _proc_worker_wrapper_helper( mp_fixture, caplog, ScriptExecutionServiceWorker, args=(work_q, mp_fixture), expect_shutdown_evt=True, ) assert event.is_set() is True mock_cls.assert_called_once() assert helper.topic_list == [ topics.request.procedure.list, # list requested topics.procedure.pool.list, # response published ] work_q.safe_close()
def test_external_messages_are_published_locally(self, mp, caplog): """ Verify that message event is published if the event originates from an external source. """ work_q = MPQueue(ctx=mp) msg = EventMessage( "EXTERNAL COMPONENT", "PUBSUB", dict(topic=topics.request.procedure.list, kwargs={"request_id": "123"}), ) work_q.put(msg) _proc_worker_wrapper_helper(mp, caplog, ScriptWorker, args=(work_q, ), expect_shutdown_evt=True) # there's no easy way to assert that the external event was republished # on an independent pypubsub bus. Workaround is to assert that the # republishing code was run via the log message assert "Republishing external event: EXTERNAL COMPONENT" in caplog.text