Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_crawling_head_offender_url(
container_factory, web_container_config, rmock=None):
url_to_check = 'http://example-head.com/test_crawling_url'
rmock.head(url_to_check)
rmock.get(url_to_check, text='xxx')
web_container_config['HEAD_DOMAINS_BLACKLIST'] = ['example-head.com']
crawler_container = container_factory(CrawlerService, web_container_config)
storage = replace_dependencies(crawler_container, 'storage')
crawler_container.start()
dispatch = event_dispatcher(web_container_config)
with entrypoint_waiter(crawler_container, 'check_url'):
dispatch('http_server', 'url_to_check',
[url_to_check, None, None])
assert storage.store_url.call_count == 1
assert storage.store_group.call_count == 0
assert storage.store_metadata.call_count == 1
# check that no HEAD method was called
requests_l = filter_mock_requests(url_to_check, rmock.request_history)
assert len(requests_l) == 1
assert requests_l[0].method == 'GET'
class Service(object):
name = "service"
@event_handler('srcservice', 'eventtype1')
def handle_event1(self, msg):
handle_event(1)
@event_handler('srcservice', 'eventtype2')
def handle_event2(self, msg):
handle_event(2)
container = container_factory(Service, rabbit_config)
container.start()
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle_event1'):
with entrypoint_waiter(container, 'handle_event2'):
dispatch('srcservice', 'eventtype1', "")
dispatch('srcservice', 'eventtype2', "")
assert call(1) in handle_event.call_args_list
assert call(2) in handle_event.call_args_list
name = "service"
@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_entrypoint_waiter_duplicate(container_factory, rabbit_config):
class Service(object):
name = "service"
@event_handler('srcservice', 'eventtype')
def handle_event(self, msg):
handle_event(msg)
container = container_factory(Service, rabbit_config)
container.start()
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle_event'):
with entrypoint_waiter(container, 'handle_event'):
dispatch('srcservice', 'eventtype', "msg")
assert handle_event.call_args_list == [call("msg")]
def test_entrypoint_waiter_duplicate(container_factory, rabbit_config):
class Service(object):
name = "service"
@event_handler('srcservice', 'eventtype')
def handle_event(self, msg):
handle_event(msg)
container = container_factory(Service, rabbit_config)
container.start()
dispatch = event_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle_event'):
with entrypoint_waiter(container, 'handle_event'):
dispatch('srcservice', 'eventtype', "msg")
assert handle_event.call_args_list == [call("msg")]
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', "")
def test_custom_event_handler(rabbit_manager, rabbit_config, start_containers):
"""Uses a custom handler subclass for the event_handler entrypoint"""
(container,) = start_containers(CustomHandler, ('custom-events',))
payload = {'custom': 'data'}
dispatch = standalone_dispatcher(rabbit_config)
with entrypoint_waiter(container, 'handle'):
dispatch('srcservice', "eventtype", payload)
assert CustomEventHandler._calls[0].payload == payload
def increment_forever():
dispatch = event_dispatcher(rabbit_config)
for count in itertools.count():
dispatch('srcservice', 'eventtype', count)
time.sleep() # force yield
container = container_factory(Service, rabbit_config)
# 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 get_event_dispatcher():
global nameko_event_dispatcher
if not nameko_event_dispatcher:
NAMEKO_CONFIG = getattr(settings, 'NAMEKO_CONFIG', {})
if not NAMEKO_CONFIG:
raise ImproperlyConfigured('NAMEKO_CONFIG must be specified')
# Lazy instantiation, acquire lock first to prevent dupication init
with create_event_dispatcher_lock:
if not nameko_event_dispatcher: # double check inside lock is importance
# init nameko_event_dispatcher
nameko_event_dispatcher = event_dispatcher(
NAMEKO_CONFIG['default'] if 'default' in NAMEKO_CONFIG else NAMEKO_CONFIG
)
# Finish instantiation, lock will be released automaticaly when exit this block
return nameko_event_dispatcher