def rabbit_config(request, rabbit_manager): from kombu import pools from nameko.testing.utils import ( reset_rabbit_vhost, reset_rabbit_connections, get_rabbit_connections, get_rabbit_config, ) amqp_uri = request.config.getoption("AMQP_URI") conf = get_rabbit_config(amqp_uri) reset_rabbit_connections(conf["vhost"], rabbit_manager) reset_rabbit_vhost(conf["vhost"], conf["username"], rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(conf["vhost"], rabbit_manager) open_connections = [conn for conn in connections if conn["state"] != "closed"] if open_connections: count = len(open_connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))
def test_reset_rabbit_connection_errors(): rabbit_manager = Mock() with patch('nameko.testing.utils.get_rabbit_connections') as connections: connections.return_value = [{ 'vhost': 'vhost_name', 'name': 'connection_name' }] # 500 error response_500 = Response() response_500.status_code = 500 error_500 = HTTPError(response=response_500) rabbit_manager.delete_connection.side_effect = error_500 with pytest.raises(HTTPError): reset_rabbit_connections("vhost_name", rabbit_manager) # 404 error response_404 = Response() response_404.status_code = 404 error_404 = HTTPError(response=response_404) rabbit_manager.delete_connection.side_effect = error_404 # does not raise reset_rabbit_connections("vhost_name", rabbit_manager)
def rabbit_config(request, rabbit_manager): from kombu import pools from nameko.testing.utils import (reset_rabbit_vhost, reset_rabbit_connections, get_rabbit_connections, get_rabbit_config) amqp_uri = request.config.getoption('AMQP_URI') conf = get_rabbit_config(amqp_uri) reset_rabbit_connections(conf['vhost'], rabbit_manager) reset_rabbit_vhost(conf['vhost'], conf['username'], rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(conf['vhost'], rabbit_manager) open_connections = [ conn for conn in connections if conn['state'] != "closed" ] if open_connections: count = len(open_connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))
def test_idle_disconnect(container_factory, rabbit_manager, rabbit_config): """ Break the connection to rabbit while a service is started but idle (i.e. without active workers) """ container = container_factory(ExampleService, rabbit_config) container.start() vhost = rabbit_config['vhost'] reset_rabbit_connections(vhost, rabbit_manager) with ServiceRpcProxy('exampleservice', rabbit_config) as proxy: assert proxy.echo("hello") == "hello"
def test_idle_disconnect(container_factory, rabbit_manager, rabbit_config): """ Break the connection to rabbit while a service is started but idle (i.e. without active workers) """ container = container_factory(ExampleService, rabbit_config) container.start() vhost = rabbit_config['vhost'] reset_rabbit_connections(vhost, rabbit_manager) with RpcProxy('exampleservice', rabbit_config) as proxy: assert proxy.echo("hello") == "hello"
def test_reset_rabbit_connections(): with patch('nameko.testing.utils.get_rabbit_connections') as connections: connections.return_value = [{ 'vhost': 'vhost', 'name': 'connection_name' }] rabbit_manager = Mock() reset_rabbit_connections('vhost', rabbit_manager) assert rabbit_manager.delete_connection.call_args_list == [ call("connection_name")]
def rabbit_config(request, rabbit_manager): amqp_uri = request.config.getoption('AMQP_URI') conf = get_rabbit_config(amqp_uri) reset_rabbit_connections(conf['vhost'], rabbit_manager) reset_rabbit_vhost(conf['vhost'], conf['username'], rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(conf['vhost'], rabbit_manager) if connections: count = len(connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))
def rabbit_config(request, rabbit_manager): amqp_uri = request.config.getoption('AMQP_URI') conf = {'AMQP_URI': amqp_uri} uri = urlparse(amqp_uri) vhost = uri.path[1:].replace('/', '%2F') username = uri.username conf['vhost'] = vhost conf['username'] = username reset_rabbit_connections(vhost, rabbit_manager) reset_rabbit_vhost(vhost, username, rabbit_manager) yield conf pools.reset() # close connections in pools # raise a runtime error if the test leaves any connections lying around connections = get_rabbit_connections(vhost, rabbit_manager) if connections: count = len(connections) raise RuntimeError("{} rabbit connection(s) left open.".format(count))