def decorator(cls): import service if not issubclass(cls, service.ServiceController): raise exceptions.NeedToBeController(service=str(cls)) cls.service_name = service_name for method_name in dir(cls): method = getattr(cls, method_name) # if method is handler then register it in dispatcher if hasattr(method, "_method_name") \ and hasattr(method, "_service_name") \ and hasattr(method, "_method_type"): # register service in router to define message controller # class router.Router().register(method._service_name, cls) ep = entry_point.EntryPoint(method._service_name, method._method_name) cls.get_dispatcher().register(ep, method._method_type, method_name) return cls
def test_register_handler_subscription(self): ep = mock.MagicMock() ep.service = "ep_service" ep.method = "ep_method" @dispatcher.rpc_service(ep.service) class SomeControllerClass(service.ServiceController): @dispatcher.subscription_method(service=ep.service, method=ep.method) def handler(self): pass message_type = "notification" message = mock.MagicMock() message.message_type = message_type message.type = message_type message.source = ep service_cls = router.Router()._get_service_cls(message.source) self.assertEqual(service_cls, SomeControllerClass) controller = SomeControllerClass(mock.MagicMock(), mock.MagicMock()) handler = controller.get_subscription().get_handler(ep) self.assertEqual("handler", handler)
def test_register_handler_subscription(self): src = entry_point.EntryPointFactory().create("src_service.src_method") dst = entry_point.EntryPointFactory().create("dst_service.rpl_method") @dispatcher.rpc_service(dst.service) class SomeControllerClass(service.ServiceController): @dispatcher.subscription_method(service=src.service, method=src.method) def handler(self): pass headers = { "source": str(src), "destination": "", "reply_to": "", "correlation_id": "123", "message_type": "notification" } context = {} payload = {} message = messages.IncomingNotification(headers, context, payload) service_classes = router.Router().get_subscription_cls(message) self.assertEqual(service_classes, [SomeControllerClass]) controller = SomeControllerClass(mock.MagicMock()) disp = controller.get_dispatcher() handler = controller.get_dispatcher().get_handler( disp._get_dispatching_entry_point(message), "notification") self.assertEqual("handler", handler)
def test_register_handler_response(self): ep = mock.MagicMock() ep.service = "ep_service" ep.method = "ep_method" @dispatcher.rpc_service(ep.service) class SomeControllerClass(service.ServiceController): @dispatcher.rpc_response_method(service=ep.service, method=ep.method) def handler(self): pass message_type = "response" message = mock.MagicMock() message.message_type = message_type message.type = message_type message.destination = ep service_cls = router.Router()._get_service_cls(message.destination) self.assertEqual(service_cls, SomeControllerClass) controller = SomeControllerClass(mock.MagicMock(), mock.MagicMock()) handler = controller.get_dispatcher().get_handler(ep, message_type) self.assertEqual("handler", handler)
def _get_router(self): return router.Router()