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))
Exemple #2
0
    def disconnect(self):
        if self.connected:
            self.logger.info("Disconnect from AMQP Broker")

            self.cancel_queues()

            for exchange in self.exchanges:
                del exchange
            self.exchanges = {}

            try:
                pools.reset()
            except Exception as err:
                self.logger.error(
                    u"Impossible to reset kombu pools: {} ({})".format(
                        err, type(err)))

            try:
                self.conn.release()
                del self.conn
            except Exception as err:
                self.logger.error(
                    u"Impossible to release connection: {} ({})".format(
                        err, type(err)))

            self.connected = False
Exemple #3
0
    def disconnect(self):
        if self.connected:
            self.logger.info("Disconnect from AMQP Broker")

            self.cancel_queues()

            # Force producers closing to permit a clean reconnect after
            # ... especially on timeout errors
            self.producers[self.conn].force_close_all()

            for exchange in self.exchanges:
                del exchange
            self.exchanges = {}

            try:
                pools.reset()
            except Exception as err:
                self.logger.error(
                    "Impossible to reset kombu pools: {} ({})".format(
                        err, type(err)))

            try:
                self.conn.release()
                del self.conn
            except Exception as err:
                self.logger.error(
                    "Impossible to release connection: {} ({})".format(
                        err, type(err)))

            self.connected = False
Exemple #4
0
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))
Exemple #5
0
    def disconnect(self):
        if self.connected:
            self.logger.info("Disconnect from AMQP Broker")

            self.cancel_queues()

            # Force producers closing to permit a clean reconnect after
            # ... especially on timeout errors
            self.producers[self.conn].force_close_all()

            for exchange in self.exchanges:
                del exchange
            self.exchanges = {}

            try:
                pools.reset()
            except Exception as err:
                self.logger.error(
                    "Impossible to reset kombu pools: {} ({})".format(
                        err, type(err)))

            try:
                self.conn.release()
                del self.conn
            except Exception as err:
                self.logger.error(
                    "Impossible to release connection: {} ({})".format(
                        err, type(err)))

            self.connected = False
Exemple #6
0
    def test_set_limit(self):
        pools.reset()
        pools.set_limit(34576)
        limit = pools.get_limit()
        self.assertEqual(limit, 34576)

        pools.connections[Connection('memory://')]
        pools.set_limit(limit + 1)
        self.assertEqual(pools.get_limit(), limit + 1)
        limit = pools.get_limit()
        with self.assertRaises(RuntimeError):
            pools.set_limit(limit - 1)
        pools.set_limit(limit - 1, force=True)
        self.assertEqual(pools.get_limit(), limit - 1)

        pools.set_limit(pools.get_limit())
Exemple #7
0
    def test_set_limit(self):
        pools.reset()
        pools.set_limit(34576)
        limit = pools.get_limit()
        self.assertEqual(limit, 34576)

        pools.connections[Connection('memory://')]
        pools.set_limit(limit + 1)
        self.assertEqual(pools.get_limit(), limit + 1)
        limit = pools.get_limit()
        with self.assertRaises(RuntimeError):
            pools.set_limit(limit - 1)
        pools.set_limit(limit - 1, force=True)
        self.assertEqual(pools.get_limit(), limit - 1)

        pools.set_limit(pools.get_limit())
Exemple #8
0
def rabbit_config(request, rabbit_manager):
    import random
    import string
    from kombu import pools
    from six.moves.urllib.parse import urlparse  # pylint: disable=E0401
    from nameko.testing.utils import get_rabbit_connections

    amqp_uri = request.config.getoption('AMQP_URI')

    uri = urlparse(amqp_uri)
    username = uri.username
    vhost = uri.path[1:]

    use_random_vost = (vhost == ":random:")

    if use_random_vost:
        vhost = "test_{}".format(
            "".join(random.choice(string.ascii_lowercase) for _ in range(10))
        )
        amqp_uri = "{}://{}/{}".format(uri.scheme, uri.netloc, vhost)
        rabbit_manager.create_vhost(vhost)
        rabbit_manager.set_vhost_permissions(vhost, username, '.*', '.*', '.*')

    conf = {
        'AMQP_URI': amqp_uri,
        'username': username,
        'vhost': vhost
    }

    yield conf

    pools.reset()  # close connections in pools

    # raise a runtime error if the test leaves any connections lying around
    try:
        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)
            names = ", ".join(conn['name'] for conn in open_connections)
            raise RuntimeError(
                "{} rabbit connection(s) left open: {}".format(count, names))
    finally:
        if use_random_vost:
            rabbit_manager.delete_vhost(vhost)
Exemple #9
0
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 = 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))
Exemple #11
0
    def test_set_limit(self):
        pools.reset()
        pools.set_limit(34576)
        limit = pools.get_limit()
        assert limit == 34576

        conn = Connection('memory://')
        pool = pools.connections[conn]
        with pool.acquire():
            pools.set_limit(limit + 1)
            assert pools.get_limit() == limit + 1
            limit = pools.get_limit()
            with pytest.raises(RuntimeError):
                pools.set_limit(limit - 1)
            pools.set_limit(limit - 1, force=True)
            assert pools.get_limit() == limit - 1

        pools.set_limit(pools.get_limit())
Exemple #12
0
    def test_set_limit(self):
        pools.reset()
        pools.set_limit(34576)
        limit = pools.get_limit()
        assert limit == 34576

        conn = Connection('memory://')
        pool = pools.connections[conn]
        with pool.acquire():
            pools.set_limit(limit + 1)
            assert pools.get_limit() == limit + 1
            limit = pools.get_limit()
            with pytest.raises(RuntimeError):
                pools.set_limit(limit - 1)
            pools.set_limit(limit - 1, force=True)
            assert pools.get_limit() == limit - 1

        pools.set_limit(pools.get_limit())
Exemple #13
0
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))
Exemple #14
0
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))
Exemple #15
0
    def test_reset(self):
        pools.reset()

        class MyGroup(dict):
            clear_called = False

            def clear(self):
                self.clear_called = True

        p1 = pools.connections['foo'] = Mock()
        g1 = MyGroup()
        pools._groups.append(g1)

        pools.reset()
        p1.force_close_all.assert_called_with()
        assert g1.clear_called

        p1 = pools.connections['foo'] = Mock()
        p1.force_close_all.side_effect = KeyError()
        pools.reset()
Exemple #16
0
    def test_reset(self):
        pools.reset()

        class MyGroup(dict):
            clear_called = False

            def clear(self):
                self.clear_called = True

        p1 = pools.connections['foo'] = Mock()
        g1 = MyGroup()
        pools._groups.append(g1)

        pools.reset()
        p1.force_close_all.assert_called_with()
        self.assertTrue(g1.clear_called)

        p1 = pools.connections['foo'] = Mock()
        p1.force_close_all.side_effect = KeyError()
        pools.reset()
Exemple #17
0
def rabbit_config(request, rabbit_manager):
    import itertools
    import random
    import string
    import time
    from kombu import pools
    from six.moves.urllib.parse import urlparse  # pylint: disable=E0401
    from nameko.testing.utils import get_rabbit_connections

    amqp_uri = request.config.getoption('AMQP_URI')

    uri = urlparse(amqp_uri)
    username = uri.username
    vhost = uri.path[1:]

    use_random_vost = (vhost == ":random:")

    if use_random_vost:
        vhost = "test_{}".format(
            "".join(random.choice(string.ascii_lowercase) for _ in range(10))
        )
        amqp_uri = "{}://{}/{}".format(uri.scheme, uri.netloc, vhost)
        rabbit_manager.create_vhost(vhost)
        rabbit_manager.set_vhost_permissions(vhost, username, '.*', '.*', '.*')

    conf = {
        'AMQP_URI': amqp_uri,
        'username': username,
        'vhost': vhost
    }

    yield conf

    pools.reset()  # close connections in pools

    def retry(fn):
        """ Barebones retry decorator
        """
        def wrapper():
            max_retries = 3
            delay = 1
            exceptions = RuntimeError

            counter = itertools.count()
            while True:
                try:
                    return fn()
                except exceptions:
                    if next(counter) == max_retries:
                        raise
                    time.sleep(delay)
        return wrapper

    @retry
    def check_connections():
        """ Raise a runtime error if the test leaves any connections open.

        Allow a few retries because the rabbit api is eventually consistent.
        """
        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)
            names = ", ".join(conn['name'] for conn in open_connections)
            raise RuntimeError(
                "{} rabbit connection(s) left open: {}".format(count, names))
    try:
        check_connections()
    finally:
        if use_random_vost:
            rabbit_manager.delete_vhost(vhost)
Exemple #18
0
 def errback(self, exc, interval):
     logger.error('push_event errback: %r', exc, exc_info=1)
     logger.info(f'push_event retry in {interval} seconds')
     reset()
Exemple #19
0
def reset_kombu_pools():
    yield
    reset()
Exemple #20
0
def rabbit_config(request, rabbit_manager):
    import itertools
    import random
    import string
    import time
    from kombu import pools
    from six.moves.urllib.parse import urlparse  # pylint: disable=E0401
    from nameko.testing.utils import get_rabbit_connections

    amqp_uri = request.config.getoption('AMQP_URI')

    uri = urlparse(amqp_uri)
    username = uri.username
    vhost = uri.path[1:]

    use_random_vost = (vhost == ":random:")

    if use_random_vost:
        vhost = "test_{}".format(
            "".join(random.choice(string.ascii_lowercase) for _ in range(10))
        )
        amqp_uri = "{}://{}/{}".format(uri.scheme, uri.netloc, vhost)
        rabbit_manager.create_vhost(vhost)
        rabbit_manager.set_vhost_permissions(vhost, username, '.*', '.*', '.*')

    conf = {
        'AMQP_URI': amqp_uri,
        'username': username,
        'vhost': vhost
    }

    yield conf

    pools.reset()  # close connections in pools

    def retry(fn):
        """ Barebones retry decorator
        """
        def wrapper():
            max_retries = 3
            delay = 1
            exceptions = RuntimeError

            counter = itertools.count()
            while True:
                try:
                    return fn()
                except exceptions:
                    if next(counter) == max_retries:
                        raise
                    time.sleep(delay)
        return wrapper

    @retry
    def check_connections():
        """ Raise a runtime error if the test leaves any connections open.

        Allow a few retries because the rabbit api is eventually consistent.
        """
        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)
            names = ", ".join(conn['name'] for conn in open_connections)
            raise RuntimeError(
                "{} rabbit connection(s) left open: {}".format(count, names))
    try:
        check_connections()
    finally:
        if use_random_vost:
            rabbit_manager.delete_vhost(vhost)
Exemple #21
0
 def _after_fork(self, obj_):
     self._pool = None
     pools.reset()
Exemple #22
0
def reset_kombu_pools(request):
    from kombu.pools import reset
    reset()
Exemple #23
0
def reset_kombu_pools():
    yield
    reset()
Exemple #24
0
def reset_kombu_pools(request):
    from kombu.pools import reset
    reset()