Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return self.dispatch("event-type", payload)
class Handler(object):
name = "handler"
@event_handler("dispatch", "event-type")
def echo(self, payload):
return payload
dispatcher = container_factory(Dispatcher, rabbit_ssl_config)
dispatcher.start()
handler = container_factory(Handler, rabbit_config)
handler.start()
with entrypoint_waiter(handler, 'echo') as result:
with entrypoint_hook(dispatcher, 'method') as dispatch:
dispatch("payload")
assert result.get() == "payload"
def test_entrypoint_hook(runner_factory, rabbit_config):
service_classes = (Service, ServiceA, ServiceB, ServiceC)
runner = runner_factory(rabbit_config, *service_classes)
runner.start()
service_container = get_container(runner, Service)
event_payload = "msg"
with entrypoint_hook(service_container, 'handle') as handle:
with entrypoint_waiter(service_container, 'handle'):
handle(event_payload)
handle_event.assert_called_once_with(event_payload)
def test_entrypoint_waiter(container_factory, rabbit_config):
container = container_factory(Service, rabbit_config)
container.start()
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle'):
dispatch('srcservice', 'eventtype', "")
# disable the entrypoints on handler_one
restrict_entrypoints(container, "handler_two")
container.start()
# verify the rpc entrypoint on handler_one is disabled
with ServiceRpcProxy("service", rabbit_config) as service_proxy:
with pytest.raises(MethodNotFound) as exc_info:
service_proxy.handler_one("msg")
assert str(exc_info.value) == "handler_one"
# dispatch an event to handler_two
msg = "msg"
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handler_two'):
dispatch('srcservice', 'eventtype', msg)
# method_called should have exactly one call, derived from the event
# handler and not from the disabled @once entrypoint
method_called.assert_called_once_with(msg)
def test_event_broadcast(self, container_factory, rabbit_config):
from event_broadcast import ListenerService
container_1 = container_factory(ListenerService, rabbit_config)
container_2 = container_factory(ListenerService, rabbit_config)
container_1.start()
container_2.start()
dispatch = event_dispatcher(rabbit_config)
with patch.object(ListenerService, 'ping') as ping:
waiter_1 = entrypoint_waiter(container_1, 'ping')
waiter_2 = entrypoint_waiter(container_2, 'ping')
with waiter_1, waiter_2:
dispatch("monitor", "ping", "payload")
assert ping.call_count == 2
def test_entrypoint_hook(runner_factory, rabbit_config):
service_classes = (Service, ServiceA, ServiceB, ServiceC)
runner = runner_factory(rabbit_config, *service_classes)
runner.start()
service_container = get_container(runner, Service)
event_payload = "msg"
with entrypoint_hook(service_container, 'handle') as handle:
with entrypoint_waiter(service_container, 'handle'):
handle(event_payload)
handle_event.assert_called_once_with(event_payload)
raise TooMuch(msg)
return msg
container = container_factory(Service, rabbit_config)
container.start()
def cb(args, kwargs, res, exc_info):
return exc_info is not None
def increment_forever():
dispatch = event_dispatcher(rabbit_config)
for count in itertools.count():
dispatch('srcservice', 'eventtype', count)
time.sleep() # force yield
with entrypoint_waiter(container, 'handle_event', callback=cb) as result:
spawn_thread(increment_forever)
with pytest.raises(TooMuch):
result.get()
name = "service"
tracker = TrackingDependency()
@event_handler('srcservice', 'eventtype')
def handle(self, msg):
tracker.handle(msg)
container = container_factory(Service, rabbit_config)
container.start()
def wait_for_two_calls(args, kwargs, res, exc_info):
return counter.count() > 1
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle', callback=wait_for_two_calls):
# dispatch the first message
dispatch('srcservice', 'eventtype', "msg")
# wait until teardown has fired at least once
while tracker.worker_teardown.call_count == 0:
time.sleep(.1)
# dispatch the second event
dispatch('srcservice', 'eventtype', "msg")
# we should wait for the second teardown to complete before exiting
# the entrypoint waiter
assert tracker.worker_teardown.call_count == 2
assert tracker.worker_result.call_count == 2
assert tracker.handle.call_count == 2
@event_handler('srcservice', 'eventtype')
def handle_event(self, msg):
return msg
container = container_factory(Service, rabbit_config)
container.start()
call_ids = []
def cb(worker_ctx, res, exc_info):
call_ids.append(worker_ctx.call_id)
return len(call_ids) == 2
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle_event', worker_callback=cb):
dispatch('srcservice', 'eventtype', "msg1")
dispatch('srcservice', 'eventtype', "msg2")
assert call_ids == ["service.handle_event.0", "service.handle_event.1"]
def test_crawler(runner_factory, rpc_proxy_factory):
runner = runner_factory(CrawlerService)
crawler_container = get_container(runner, CrawlerService)
storage = replace_dependencies(crawler_container, 'storage')
runner.start()
config = {'AMQP_URI': 'amqp://guest:guest@localhost:5672/nameko_test'}
dispatch = event_dispatcher(config)
with entrypoint_waiter(crawler_container, 'check_url'):
dispatch('http_server', 'url_to_check', ['http://example.org', None, None])
assert storage.store_url.call_count == 1
assert storage.store_group.call_count == 0
assert storage.store_metadata.call_count == 1