def test_dispatched_event(fake_receiver): from krules_core import event_types event_dispatcher_factory.override( providers.Singleton(lambda: CloudEventsDispatcher( fake_receiver.url, "pytest", test=True))) router = event_router_factory() subject = subject_factory("test-subject") subject.set_ext("ext1", "val1") subject.set_ext("ext2", "2") _id, code, sent_headers = router.route("test-type", subject, {"key1": "hello"}) assert (200 <= code < 300) assert (sent_headers.get("ce-id") == _id) assert (sent_headers.get("ce-source") == "pytest") assert (sent_headers.get("ce-subject") == "test-subject") assert (sent_headers.get("ce-type") == "test-type") assert (sent_headers.get("ce-Originid") == _id) assert (sent_headers.get("ce-ext1") == "val1") assert (sent_headers.get("ce-ext2") == "2") # with event info subject = subject_factory("test-subject", event_info={"originid": 1234}) _, _, sent_headers = router.route("test-type", subject, {"key1": "hello"}) assert (sent_headers.get("id") != sent_headers.get("ce-Originid")) assert (sent_headers.get("ce-Originid") == '1234') # property name _, _, sent_headers = router.route(event_types.SUBJECT_PROPERTY_CHANGED, subject, {PayloadConst.PROPERTY_NAME: "foo"}) assert (sent_headers.get("ce-propertyname") == 'foo')
def init(): configs_factory.override( providers.Singleton(lambda: krules_settings) ) proc_events_rx_factory.override( providers.Singleton(rx.subjects.ReplaySubject) ) event_router_factory.override( providers.Singleton(lambda: EventRouter()) ) exceptions_dumpers = exceptions_dumpers_factory() exceptions_dumpers.set(ExceptionDumperBase) exceptions_dumpers.set(RequestsHTTPErrorDumper) # TODO: do it better source = None if "K_SERVICE" in os.environ: source = os.environ["K_SERVICE"] elif "SERVICE" in os.environ: source = os.environ["SERVICE"] else: source = socket.gethostname() from krules_cloudevents.route.dispatcher import CloudEventsDispatcher event_dispatcher_factory.override( providers.Singleton(lambda: CloudEventsDispatcher(krules_settings["CLOUDEVENTS"]["send_to"], source)) )
def test_callable_dispatch_url(fake_receiver): def _get_dispatch_url(subject, type): assert not isinstance(subject, str) return fake_receiver.url event_dispatcher_factory.override( providers.Singleton(lambda: CloudEventsDispatcher( _get_dispatch_url, "pytest", test=True))) router = event_router_factory() _id, code, sent_headers = router.route("test-type", "test_subject", {"key1": "hello"}) assert (200 <= code < 300)
def init(): configs_factory.override(providers.Singleton(lambda: krules_settings)) proc_events_rx_factory.queue.clear() event_router_factory.override(providers.Singleton(lambda: EventRouter())) exceptions_dumpers = exceptions_dumpers_factory() exceptions_dumpers.set(ExceptionDumperBase) exceptions_dumpers.set(RequestsHTTPErrorDumper) # TODO: do it better source = None if "K_SERVICE" in os.environ: source = os.environ["K_SERVICE"] elif "SERVICE" in os.environ: source = os.environ["SERVICE"] else: source = socket.gethostname() from krules_cloudevents.route.dispatcher import CloudEventsDispatcher event_dispatcher_factory.override( providers.Singleton( lambda: CloudEventsDispatcher(_get_dispatch_url, source))) try: import env if "init" in dir(env) and callable(env.init): env.init() except ModuleNotFoundError: logger.warning("No application env.py found!") try: m_rules = importlib.import_module("ruleset") load_rules_from_rulesdata(m_rules.rulesdata) except ModuleNotFoundError as ex: if ex.name == "ruleset": logger.warning("No rules defined!") else: raise ex proc_events_filters = os.environ.get("PUBLISH_PROCEVENTS_MATCHING") if proc_events_filters: proc_events_rx_factory.subscribe( on_next=lambda x: publish_proc_events_filtered( x, proc_events_filters.split(";"), lambda match: match is not None)) else: proc_events_rx_factory.subscribe( on_next=lambda x: publish_proc_events_all(x))
def test_dispatch(subject, router): _dispatched_events = [] class _TestDispatcher(BaseDispatcher): def dispatch(self, type, subject, payload, **extra): _dispatched_events.append((type, subject, payload)) event_dispatcher_factory.override( providers.Singleton(lambda: _TestDispatcher())) router.route('test-unhandled-event', subject, {"data": 1}) type, subject, payload = _dispatched_events.pop() _assert(type == 'test-unhandled-event' and subject.name == subject.name and payload.get("data") == 1)
def init(): # This is an inmemory database and it is not persistent # you probably want to comment out this configuration and enable a more appropriate one from krules_core.tests.subject.sqlite_storage import SQLLiteSubjectStorage from k8s_subjects_storage import storage_impl as k8s_storage_impl from redis_subjects_storage import storage_impl as redis_storage_impl subjects_redis_storage_settings = configs_factory() \ .get("subjects-backends") \ .get("redis") subject_storage_factory.override( providers.Factory(lambda name, event_info, event_data: name.startswith("k8s:") and k8s_storage_impl.SubjectsK8sStorage( resource_path=name[4:], resource_body=event_data ) or redis_storage_impl.SubjectsRedisStorage(name, subjects_redis_storage_settings.get("url")) ) ) from krules_cloudevents.route.dispatcher import CloudEventsDispatcher from krules_core.providers import event_dispatcher_factory import os, socket event_dispatcher_factory.override( providers.Singleton(lambda: CloudEventsDispatcher(os.environ["K_SINK"], os.environ.get("K_SERVICE", socket.gethostname()))) ) class CallableWithCtxArgProcessor(BaseArgProcessor): @staticmethod def interested_in(arg): try: sig = inspect.signature(arg) return len(sig.parameters) == 1 and "ctx" in sig.parameters or "_" in sig.parameters except TypeError: return False def process(self, instance): return self._arg(instance) processors.append(CallableWithCtxArgProcessor)