def get_notification_listener(transport, targets, endpoints, executor='blocking', serializer=None, allow_requeue=False): """Construct a notification listener The executor parameter controls how incoming messages will be received and dispatched. By default, the most simple executor is used - the blocking executor. :param transport: the messaging transport :type transport: Transport :param targets: the exchanges and topics to listen on :type targets: list of Target :param endpoints: a list of endpoint objects :type endpoints: list :param executor: name of a message executor - e.g. 'eventlet', 'blocking' :type executor: str :param serializer: an optional entity serializer :type serializer: Serializer :param allow_requeue: whether NotificationResult.REQUEUE support is needed :type allow_requeue: bool :raises: NotImplementedError """ transport._require_driver_features(requeue=allow_requeue) dispatcher = notify_dispatcher.NotificationDispatcher( targets, endpoints, serializer, allow_requeue) return msg_server.MessageHandlingServer(transport, dispatcher, executor)
def _prepare_notification_service(server_id): endpoints = [report_notification, track_instance, untrack_instance] transport = messaging.get_transport(config.CONF) s_target = target.Target(topic='murano', server=server_id) dispatcher = oslo_dispatcher.NotificationDispatcher([s_target], endpoints, None, True) return messaging.MessageHandlingServer(transport, dispatcher, 'eventlet')
def test_dispatcher_unknown_prio(self, mylog): msg = notification_msg.copy() msg['priority'] = 'what???' dispatcher = notify_dispatcher.NotificationDispatcher( [mock.Mock()], [mock.Mock()], None, allow_requeue=True) with dispatcher(mock.Mock(ctxt={}, message=msg)) as callback: callback() mylog.warning.assert_called_once_with('Unknown priority "what???"')
def test_dispatcher(self): endpoints = [] for endpoint_methods in self.endpoints: e = mock.Mock(spec=endpoint_methods) endpoints.append(e) for m in endpoint_methods: method = getattr(e, m) if self.ex: method.side_effect = self.ex() else: method.return_value = self.return_value msg = notification_msg.copy() msg['priority'] = self.priority targets = [messaging.Target(topic='notifications')] dispatcher = notify_dispatcher.NotificationDispatcher( targets, endpoints, None, allow_requeue=True) # check it listen on wanted topics self.assertEqual(sorted(set((targets[0], prio) for prio in itertools.chain.from_iterable( self.endpoints))), sorted(dispatcher._targets_priorities)) incoming = mock.Mock(ctxt={}, message=msg) with dispatcher(incoming) as callback: callback() # check endpoint callbacks are called or not for i, endpoint_methods in enumerate(self.endpoints): for m in endpoint_methods: if m == self.endpoints_expect_calls[i]: method = getattr(endpoints[i], m) method.assert_called_once_with( {}, msg['publisher_id'], msg['event_type'], msg['payload'], { 'timestamp': mock.ANY, 'message_id': mock.ANY }) else: self.assertEqual(0, endpoints[i].call_count) if self.ex: self.assertEqual(1, incoming.acknowledge.call_count) self.assertEqual(0, incoming.requeue.call_count) elif self.return_value == messaging.NotificationResult.HANDLED \ or self.return_value is None: self.assertEqual(1, incoming.acknowledge.call_count) self.assertEqual(0, incoming.requeue.call_count) elif self.return_value == messaging.NotificationResult.REQUEUE: self.assertEqual(0, incoming.acknowledge.call_count) self.assertEqual(1, incoming.requeue.call_count)
def test_dispatcher_executor_callback(self): endpoint = mock.Mock(spec=['warn']) endpoint_method = endpoint.warn endpoint_method.return_value = messaging.NotificationResult.HANDLED targets = [messaging.Target(topic='notifications')] dispatcher = notify_dispatcher.NotificationDispatcher( targets, [endpoint], None, allow_requeue=True) msg = notification_msg.copy() msg['priority'] = 'warn' incoming = mock.Mock(ctxt={}, message=msg) executor_callback = mock.Mock() with dispatcher(incoming, executor_callback) as callback: callback() self.assertTrue(executor_callback.called) self.assertEqual(executor_callback.call_args[0][0], endpoint_method)