def _init_scheduled_topic(self, scheduled_topic: ScheduledTopic): """ Sets the send_message method as callback in the topic and schedules it. :param scheduled_topic: """ # assign the message_send_callback on the scheduled topic scheduled_topic.set_message_send_callback(self.send_message) # and schedule it self.container.schedule(scheduled_topic.interval_in_sec, scheduled_topic)
def test_scheduled_topic__trigger_message_send__no_message_send_handler__returns_and_logs_message( caplog): caplog.set_level(logging.DEBUG) data_handler = Mock() scheduled_topic = ScheduledTopic(topic_name='topic', data_handler=data_handler, interval_in_sec=5) scheduled_topic._trigger_message_send() log_message = caplog.records[0] expected_message = "Not able to send messages because no sender has been assigned yet for topic 'topic'" assert expected_message == log_message.message data_handler.assert_not_called()
def test_on_start__no_errors(): def data_handler1(context): return "data1" def data_handler2(context): return "data2" scheduled_topic = ScheduledTopic(topic_name='s_topic', data_handler=data_handler1, interval_in_sec=5) topic = Topic(topic_name='topic', data_handler=data_handler2) handler = PublisherBrokerHandler(mock.Mock()) sender = mock.Mock() handler._create_sender = mock.Mock(return_value=sender) mock_init_scheduled_topic = Mock() handler._init_scheduled_topic = mock_init_scheduled_topic handler.add_topic(topic) handler.add_topic(scheduled_topic) event = mock.Mock() with mock.patch.object(BrokerHandler, 'on_start'): handler.on_start(event) assert sender == handler._sender mock_init_scheduled_topic.assert_called_once_with(scheduled_topic)
def test_scheduled_topic__on_timer_task__message_send_is_triggered_and_task_is_rescheduled( caplog): caplog.set_level(logging.DEBUG) def data_handler(context=None): return "data" scheduled_topic = ScheduledTopic(topic_name='topic', data_handler=data_handler, interval_in_sec=5) scheduled_topic._trigger_message_send = Mock() event = Mock() event.container = Mock() event.container.schedule = Mock() scheduled_topic.on_timer_task(event) scheduled_topic._trigger_message_send.assert_called_once() event.container.schedule.assert_called_once_with(5, scheduled_topic)
def test_init_scheduled_topic(): handler = PublisherBrokerHandler(mock.Mock()) handler.container = mock.Mock() handler.container.schedule = mock.Mock() def data_handler(context=None): return "data" scheduled_topic = ScheduledTopic(topic_name='topic', data_handler=data_handler, interval_in_sec=5) handler._init_scheduled_topic(scheduled_topic) assert scheduled_topic._message_send_callback == handler.send_message handler.container.schedule.assert_called_once_with( scheduled_topic.interval_in_sec, scheduled_topic)
def test_add_topic__scheduled_topic_is_added_and_initialized( handler_started, scheduled_topic_initialized): handler = PublisherBrokerHandler(mock.Mock()) handler.started = handler_started mock_init_scheduled_topic = Mock() handler._init_scheduled_topic = mock_init_scheduled_topic def data_handler(context=None): return "data" scheduled_topic = ScheduledTopic(topic_name='topic', data_handler=data_handler, interval_in_sec=5) handler.add_topic(scheduled_topic) assert 1 == len(handler.topics) assert scheduled_topic in handler.topics if scheduled_topic_initialized: mock_init_scheduled_topic.assert_called_once_with(scheduled_topic) else: mock_init_scheduled_topic.assert_not_called()
def test_scheduled_topic__trigger_message_send__topicdatahandlererror_returns_and_logs_message( caplog): caplog.set_level(logging.DEBUG) data_handler = Mock( side_effect=TopicDataHandlerError('data handler error')) scheduled_topic = ScheduledTopic(topic_name='topic', data_handler=data_handler, interval_in_sec=5) scheduled_topic.set_message_send_callback(Mock()) scheduled_topic._trigger_message_send() log_message = caplog.records[0] expected_message = f"Error while getting data of scheduled topic {scheduled_topic.name}: data handler error" assert expected_message == log_message.message
def test_scheduled_topic__trigger_message_send_is_called_normally_and_logs_message( caplog): caplog.set_level(logging.DEBUG) def data_handler(context=None): return "data" scheduled_topic = ScheduledTopic(topic_name='topic', data_handler=data_handler, interval_in_sec=5) mock_message_send_handler = Mock() scheduled_topic.set_message_send_callback(mock_message_send_handler) scheduled_topic._trigger_message_send() log_message = caplog.records[0] expected_message = f"Sending message for scheduled topic {scheduled_topic.name}" assert expected_message == log_message.message mock_message_send_handler.assert_called_once_with( message="data", subject=scheduled_topic.name)
opensky = OpenSkyNetworkDataHandler() publisher = app.register_publisher('swim-adsb', 'rsdyhdsrhdyh ') airports = { 'Brussels': 'EBBR', 'Amsterdam': 'EHAM', 'Paris': 'LFPG', 'Berlin': 'EDDB', 'Athens': 'LGAV', 'Madrid': 'LECU' } # create a new arrivals and a new departures topic per airport and chain it to the respective root for city, icao24 in airports.items(): city_arrivals_topic = Topic(topic_name=f'arrivals.{city}', data_handler=partial( opensky.arrivals_today_handler, icao24)) city_departures_topic = ScheduledTopic( topic_name=f'departures.{city}', data_handler=partial(opensky.departures_today_handler, icao24), interval_in_sec=5) # register topics publisher.register_topic(topic=city_arrivals_topic) publisher.register_topic(topic=city_departures_topic) app.run(threaded=True)