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_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_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