def test_connection_is_reused_in_app_context(flask_app): with patch('flask_nameko.proxies.ClusterRpcProxy'): rpc = FlaskPooledClusterRpcProxy(flask_app, connect_on_method_call=False) with flask_app.test_request_context(): connection = rpc.get_connection() connection1 = rpc.get_connection() assert connection == connection1
def test_connection_is_reused_in_app_context(flask_app): with patch('flask_nameko.proxies.ClusterRpcProxy'): rpc = FlaskPooledClusterRpcProxy( flask_app, connect_on_method_call=False ) with flask_app.test_request_context(): connection = rpc.get_connection() connection1 = rpc.get_connection() assert connection == connection1
def test_connection_is_returned_and_reused_when_app_context_ends(flask_app): flask_app.config.update(dict(NAMEKO_INITIAL_CONNECTIONS=1)) with patch('flask_nameko.proxies.ClusterRpcProxy'): rpc = FlaskPooledClusterRpcProxy(flask_app, connect_on_method_call=False) with flask_app.test_request_context(): connection = rpc.get_connection() with flask_app.test_request_context(): connection1 = rpc.get_connection() assert connection1 == connection
def test_connection_is_returned_and_reused_when_app_context_ends(flask_app): flask_app.config.update(dict(NAMEKO_INITIAL_CONNECTIONS=1)) with patch('flask_nameko.proxies.ClusterRpcProxy'): rpc = FlaskPooledClusterRpcProxy( flask_app, connect_on_method_call=False ) with flask_app.test_request_context(): connection = rpc.get_connection() with flask_app.test_request_context(): connection1 = rpc.get_connection() assert connection1 == connection
def _init_rpc(self) -> FlaskPooledClusterRpcProxy: """Initialize the RPC proxy. Returns: The instantiated FlaskPooledClusterRpcProxy object """ self._service.config.update({ "NAMEKO_AMQP_URI": f"pyamqp://{settings.RABBIT_USER}:{settings.RABBIT_PASSWORD}" f"@{settings.RABBIT_HOST}:{settings.RABBIT_PORT}" }) rpc = FlaskPooledClusterRpcProxy() rpc.init_app(self._service) return rpc
def _init_rpc(self) -> FlaskPooledClusterRpcProxy: """Initalizes the RPC proxy Returns: FlaskPooledClusterRpcProxy -- The instantiated FlaskPooledClusterRpcProxy object """ self._service.config.update({ "NAMEKO_AMQP_URI": "pyamqp://{0}:{1}@{2}:{3}".format(environ.get("RABBIT_USER"), environ.get("RABBIT_PASSWORD"), environ.get("RABBIT_HOST"), environ.get("RABBIT_PORT")) }) rpc = FlaskPooledClusterRpcProxy() rpc.init_app(self._service) return rpc
def test_new_connection_is_used_for_new_app_context(flask_app): class FakeClusterRpcProxy(object): n = 0 def start(self): self.n = self.n + 1 return self.n mock = FakeClusterRpcProxy() with patch('flask_nameko.proxies.ClusterRpcProxy', return_value=mock): rpc = FlaskPooledClusterRpcProxy(flask_app, connect_on_method_call=False) with flask_app.test_request_context(): connection = rpc.get_connection() with flask_app.test_request_context(): connection1 = rpc.get_connection() assert connection1 != connection
def test_new_connection_is_used_for_new_app_context(flask_app): class FakeClusterRpcProxy(object): n = 0 def start(self): self.n = self.n + 1 return self.n mock = FakeClusterRpcProxy() with patch('flask_nameko.proxies.ClusterRpcProxy', return_value=mock): rpc = FlaskPooledClusterRpcProxy( flask_app, connect_on_method_call=False ) with flask_app.test_request_context(): connection = rpc.get_connection() with flask_app.test_request_context(): connection1 = rpc.get_connection() assert connection1 != connection
def init(application): global flask_tracer global rpc flask_tracer = FlaskTracer(init_tracer("item rental"), True, application) rpc = FlaskPooledClusterRpcProxy() def create_app(): rpc_app = Flask(__name__) rpc_app.config.update( dict(NAMEKO_AMQP_URI='pyamqp://{}:{}@{}'.format( os.getenv('RABBITMQ_USER', "guest"), os.getenv('RABBITMQ_PASSWORD', "guest"), os.getenv('RABBITMQ_HOST', "localhost")))) rpc.init_app(rpc_app) create_app()
from flask import Flask,request from flask_nameko import FlaskPooledClusterRpcProxy rpc = FlaskPooledClusterRpcProxy() def create_app(): app = Flask(__name__) app.config.update(dict( NAMEKO_AMQP_URI='pyamqp://*****:*****@localhost' )) rpc.init_app(app) return app app = create_app()
def test_pool_recycle_is_passed_through_to_cluster(flask_app): flask_app.config.update(dict(NAMEKO_POOL_RECYCLE=3600)) with patch('flask_nameko.proxies.ConnectionPool', spec_set=ConnectionPool) as mock: FlaskPooledClusterRpcProxy(flask_app) mock.assert_called_with(ANY, initial_connections=ANY, max_connections=ANY, recycle=3600)
def test_timeout_is_passed_through_to_cluster(flask_app): flask_app.config.update(dict(NAMEKO_RPC_TIMEOUT=10)) with patch('flask_nameko.proxies.ClusterRpcProxy', spec_set=ClusterRpcProxy) as mock: FlaskPooledClusterRpcProxy(flask_app, connect_on_method_call=True) mock.assert_called_with(ANY, timeout=10)
def test_connect_on_method_call_returns_lazy_proxy(flask_app): with patch('flask_nameko.proxies.ClusterRpcProxy', return_value=MagicMock()): rpc = FlaskPooledClusterRpcProxy(flask_app, connect_on_method_call=True) assert isinstance(rpc.service, LazyServiceProxy)
def test_connect_on_method_call_false_returns_connection(flask_app): with flask_app.test_request_context(): with patch('flask_nameko.proxies.ClusterRpcProxy', return_value=MagicMock()): rpc = FlaskPooledClusterRpcProxy(flask_app, connect_on_method_call=False) assert isinstance(rpc.service, Mock)
def test_not_configured_raises_exception(flask_app): rpc = FlaskPooledClusterRpcProxy() with flask_app.test_request_context(): with pytest.raises(ClusterNotConfiguredError): rpc.not_configured_service.test()
def test_configuration_pulls_nameko_names(flask_app): with patch.object(FlaskPooledClusterRpcProxy, 'configure') as configure: rpc = FlaskPooledClusterRpcProxy(flask_app) configure.assert_called_once_with(dict(AMQP_URI='test'))
from flask import Flask from flask_cors import CORS from flask_nameko import FlaskPooledClusterRpcProxy from dotenv import load_dotenv import os BASEDIR = os.path.dirname(os.path.abspath(__file__)) load_dotenv(dotenv_path="{}/.env".format(BASEDIR)) app = Flask(__name__) CORS(app) CONFIG_METHOD = "main.config.{}".format( os.environ.get("APP_SETTINGS", "Develop")) app.config.from_object(CONFIG_METHOD) rpc = FlaskPooledClusterRpcProxy() rpc.init_app(app)
monkey.patch_all() # 用于协程异步感知IO操作,打的补丁 def create_app(): flask_app = Flask(__name__) flask_app.config['JSON_SORT_KEYS'] = False flask_app.config['JSON_AS_ASCII'] = False flask_app.config.update( dict(NAMEKO_AMQP_URI="pyamqp://*****:*****@localhost")) return flask_app app = create_app() Swagger(app) # 用于API注释可视化 rpc = FlaskPooledClusterRpcProxy() rpc.init_app(app) # 一次注册,绑定使用 @app.route('/', methods=['GET']) def get_apis(): return json.dumps({"web_state": "success"}), 200 @app.route('/hello_world', methods=['GET']) def hello(): """ Micro Service for hello, say hello to you. 中文说明:这是一个say hello的接口。 --- parameters: