示例#1
0
def vhost_pipeline(request, rabbit_manager):
    from six.moves.urllib.parse import urlparse  # pylint: disable=E0401
    import random
    import string
    from nameko.testing.utils import ResourcePipeline

    rabbit_amqp_uri = request.config.getoption('RABBIT_AMQP_URI')
    uri_parts = urlparse(rabbit_amqp_uri)
    username = uri_parts.username

    def create():
        vhost = "nameko_test_{}".format(
            "".join(random.choice(string.ascii_lowercase) for _ in range(10))
        )
        rabbit_manager.create_vhost(vhost)
        rabbit_manager.set_vhost_permissions(
            vhost, username, '.*', '.*', '.*'
        )
        return vhost

    def destroy(vhost):
        rabbit_manager.delete_vhost(vhost)

    pipeline = ResourcePipeline(create, destroy)

    with pipeline.run() as vhosts:
        yield vhosts
示例#2
0
    def test_create_shutdown_race(self):
        """
        Test the race condition where the pipeline shuts down while
        `create` is still executing.
        """
        created = []
        destroyed = []

        counter = itertools.count()
        creating = Event()

        def create():
            creating.send(True)
            eventlet.sleep()
            obj = next(counter)
            created.append(obj)
            return obj

        def destroy(obj):
            destroyed.append(obj)

        with ResourcePipeline(create, destroy).run():
            creating.wait()
            assert created == []

        assert created == destroyed == list(range(1))
示例#3
0
def vhost_pipeline(request, rabbit_manager):
    try:
        from collections.abc import Iterable
    except ImportError:  # pragma: no cover
        # py2 compatibility
        from collections import Iterable  # pylint: disable=E0611
    from six.moves.urllib.parse import urlparse  # pylint: disable=E0401
    import random
    import socket
    import string
    from kombu.pools import connections
    from nameko.testing.utils import ResourcePipeline
    from nameko.utils.retry import retry
    from requests.exceptions import HTTPError

    rabbit_amqp_uri = request.config.getoption('RABBIT_AMQP_URI')
    uri_parts = urlparse(rabbit_amqp_uri)
    username = uri_parts.username

    def create():
        vhost = "nameko_test_{}".format("".join(
            random.choice(string.ascii_lowercase) for _ in range(10)))
        rabbit_manager.create_vhost(vhost)
        rabbit_manager.set_vhost_permissions(vhost, username, '.*', '.*', '.*')
        return vhost

    @retry(for_exceptions=(HTTPError, socket.timeout), delay=1, max_attempts=9)
    def destroy(vhost):
        rabbit_manager.delete_vhost(vhost)

        # make sure connections for this vhost are also destroyed.
        vhost_pools = [
            pool for key, pool in list(connections.items())
            if isinstance(key, Iterable) and key[4] == vhost
        ]
        for pool in vhost_pools:
            pool.force_close_all()

    pipeline = ResourcePipeline(create, destroy)

    with pipeline.run() as vhosts:
        yield vhosts
示例#4
0
def vhost_pipeline(request, rabbit_manager):
    from collections import Iterable
    from six.moves.urllib.parse import urlparse  # pylint: disable=E0401
    import random
    import socket
    import string
    from kombu.pools import connections
    from nameko.testing.utils import ResourcePipeline
    from nameko.utils.retry import retry
    from requests.exceptions import HTTPError

    rabbit_amqp_uri = request.config.getoption('RABBIT_AMQP_URI')
    uri_parts = urlparse(rabbit_amqp_uri)
    username = uri_parts.username

    def create():
        vhost = "nameko_test_{}".format(
            "".join(random.choice(string.ascii_lowercase) for _ in range(10))
        )
        rabbit_manager.create_vhost(vhost)
        rabbit_manager.set_vhost_permissions(
            vhost, username, '.*', '.*', '.*'
        )
        return vhost

    @retry(for_exceptions=(HTTPError, socket.timeout), delay=1, max_attempts=9)
    def destroy(vhost):
        rabbit_manager.delete_vhost(vhost)

        # make sure connections for this vhost are also destroyed.
        vhost_pools = [
            pool for key, pool in list(connections.items())
            if isinstance(key, Iterable) and key[4] == vhost
        ]
        for pool in vhost_pools:
            pool.force_close_all()

    pipeline = ResourcePipeline(create, destroy)

    with pipeline.run() as vhosts:
        yield vhosts
示例#5
0
    def test_shutdown_immediately(self):

        created = []
        destroyed = []

        counter = itertools.count()

        def create():  # pragma: no cover
            obj = next(counter)
            created.append(obj)
            return obj

        def destroy(obj):  # pragma: no cover
            destroyed.append(obj)

        with ResourcePipeline(create, destroy).run():
            pass

        assert created == destroyed == []
示例#6
0
    def test_pipeline(self, size):
        created = []
        destroyed = []

        counter = itertools.count()

        def create():
            obj = next(counter)
            created.append(obj)
            return obj

        def destroy(obj):
            destroyed.append(obj)

        with ResourcePipeline(create, destroy, size).run() as pipeline:

            # check initial size
            eventlet.sleep()  # let pipeline fill up
            # when full, created is always exactly one more than `size`
            assert pipeline.ready.qsize() == size
            assert len(created) == size + 1

            # get an item
            with pipeline.get() as item:
                # let pipeline process
                eventlet.sleep()
                # expect pipeline to have created another item
                assert pipeline.ready.qsize() == size
                assert len(created) == size + 2

            # after putting the item back
            # let pipeline process
            eventlet.sleep()
            # expect item to have been destroyed
            assert pipeline.trash.qsize() == 0
            assert destroyed == [item]

        # after shutdown (no need to yield because shutdown is blocking)
        # expect all created items to have been destroyed
        assert created == destroyed == list(range(size + 2))
示例#7
0
    def test_zero_size(self):

        with pytest.raises(RuntimeError):
            ResourcePipeline(None, None, 0)