예제 #1
0
    def test_deriver(self, service_available, xmlrpc_cache, fakeredis):
        context = pretend.stub()
        purger = pretend.call_recorder(lambda tags: None)
        service = RedisXMLRPCCache("redis://127.0.0.2:6379/0", purger)
        service.redis_conn = fakeredis
        service.redis_lru.conn = fakeredis
        if service_available:
            _find_service = pretend.call_recorder(
                lambda *args, **kwargs: service)
        else:
            _find_service = pretend.raiser(LookupError)
        request = pretend.stub(find_service=_find_service,
                               rpc_method="rpc_method",
                               rpc_args=(0, 1))
        response = {}

        @pretend.call_recorder
        def view(context, request):
            return response

        info = pretend.stub(options={}, exception_only=False)
        info.options["xmlrpc_cache"] = xmlrpc_cache
        derived_view = cached_return_view(view, info)

        assert derived_view(context, request) is response
        assert view.calls == [pretend.call(context, request)]
예제 #2
0
    def test_deriver(self, service_available, xmlrpc_cache, fakeredis):
        context = pretend.stub()
        purger = pretend.call_recorder(lambda tags: None)
        service = RedisXMLRPCCache("redis://127.0.0.2:6379/0", purger)
        service.redis_conn = fakeredis
        service.redis_lru.conn = fakeredis
        if service_available:
            _find_service = pretend.call_recorder(lambda *args, **kwargs: service)
        else:
            _find_service = pretend.raiser(LookupError)
        request = pretend.stub(
            find_service=_find_service, rpc_method="rpc_method", rpc_args=(0, 1)
        )
        response = {}

        @pretend.call_recorder
        def view(context, request):
            return response

        info = pretend.stub(options={}, exception_only=False)
        info.options["xmlrpc_cache"] = xmlrpc_cache
        derived_view = cached_return_view(view, info)

        assert derived_view(context, request) is response
        assert view.calls == [pretend.call(context, request)]
예제 #3
0
    def test_redis_cache(self, monkeypatch):
        strict_redis_obj = pretend.stub()
        strict_redis_cls = pretend.stub(from_url=pretend.call_recorder(
            lambda url, db=None: strict_redis_obj), )
        monkeypatch.setattr(redis, "StrictRedis", strict_redis_cls)

        redis_lru_obj = pretend.stub(
            fetch=pretend.call_recorder(lambda func, args, kwargs, key, tag,
                                        expires: func(*args, **kwargs)),
            purge=pretend.call_recorder(lambda tag: None))
        redis_lru_cls = pretend.call_recorder(
            lambda redis_conn, **kwargs: redis_lru_obj)
        monkeypatch.setattr(
            warehouse.legacy.api.xmlrpc.cache,
            "RedisLru",
            redis_lru_cls,
        )

        purger = pretend.call_recorder(lambda tags: None)

        service = RedisXMLRPCCache('redis://localhost:6379', purger)

        assert strict_redis_cls.from_url.calls == [
            pretend.call("redis://localhost:6379", db=0)
        ]
        assert redis_lru_cls.calls == [
            pretend.call(
                strict_redis_obj,
                name='lru',
                expires=None,
                metric_reporter=None,
            )
        ]

        assert service.fetch(
            func_test, (1, 2), {'kwarg0': 3, 'kwarg1': 4}, None, None, None) \
            == [[1, 2], {'kwarg0': 3, 'kwarg1': 4}]

        assert service.purge(None) is None

        assert redis_lru_obj.fetch.calls == [
            pretend.call(
                func_test,
                (1, 2),
                {
                    'kwarg0': 3,
                    'kwarg1': 4
                },
                None,
                None,
                None,
            )
        ]
        assert redis_lru_obj.purge.calls == [pretend.call(None)]
예제 #4
0
    def test_redis_cache(self, monkeypatch):
        strict_redis_obj = pretend.stub()
        strict_redis_cls = pretend.stub(
            from_url=pretend.call_recorder(lambda url, db=None: strict_redis_obj)
        )
        monkeypatch.setattr(redis, "StrictRedis", strict_redis_cls)

        redis_lru_obj = pretend.stub(
            fetch=pretend.call_recorder(
                lambda func, args, kwargs, key, tag, expires: func(*args, **kwargs)
            ),
            purge=pretend.call_recorder(lambda tag: None),
        )
        redis_lru_cls = pretend.call_recorder(
            lambda redis_conn, **kwargs: redis_lru_obj
        )
        monkeypatch.setattr(
            warehouse.legacy.api.xmlrpc.cache, "RedisLru", redis_lru_cls
        )

        purger = pretend.call_recorder(lambda tags: None)

        service = RedisXMLRPCCache("redis://localhost:6379", purger)

        assert strict_redis_cls.from_url.calls == [
            pretend.call("redis://localhost:6379", db=0)
        ]
        assert redis_lru_cls.calls == [
            pretend.call(
                strict_redis_obj, name="lru", expires=None, metric_reporter=None
            )
        ]

        assert service.fetch(
            func_test, (1, 2), {"kwarg0": 3, "kwarg1": 4}, None, None, None
        ) == [[1, 2], {"kwarg0": 3, "kwarg1": 4}]

        assert service.purge(None) is None

        assert redis_lru_obj.fetch.calls == [
            pretend.call(
                func_test, (1, 2), {"kwarg0": 3, "kwarg1": 4}, None, None, None
            )
        ]
        assert redis_lru_obj.purge.calls == [pretend.call(None)]
예제 #5
0
 def test_create_redis_service(self):
     purge_tags = pretend.stub(delay=pretend.call_recorder(lambda tag: None))
     request = pretend.stub(
         registry=pretend.stub(settings={"warehouse.xmlrpc.cache.url": "redis://"}),
         task=lambda f: purge_tags,
     )
     service = RedisXMLRPCCache.create_service(None, request)
     service.purge_tags(["wu", "tang", "4", "evah"])
     assert isinstance(service, RedisXMLRPCCache)
     assert service._purger is purge_tags.delay
     assert purge_tags.delay.calls == [
         pretend.call("wu"),
         pretend.call("tang"),
         pretend.call("4"),
         pretend.call("evah"),
     ]
예제 #6
0
 def test_create_redis_service(self):
     purge_tags = pretend.stub(delay=pretend.call_recorder(lambda tag: None))
     request = pretend.stub(
         registry=pretend.stub(settings={"warehouse.xmlrpc.cache.url": "redis://"}),
         task=lambda f: purge_tags,
     )
     service = RedisXMLRPCCache.create_service(None, request)
     service.purge_tags(["wu", "tang", "4", "evah"])
     assert isinstance(service, RedisXMLRPCCache)
     assert service._purger is purge_tags.delay
     assert purge_tags.delay.calls == [
         pretend.call("wu"),
         pretend.call("tang"),
         pretend.call("4"),
         pretend.call("evah"),
     ]
예제 #7
0
 def test_create_redis_service(self):
     purge_tags = pretend.stub(
         delay=pretend.call_recorder(lambda tag: None))
     request = pretend.stub(
         registry=pretend.stub(settings={
             "warehouse.xmlrpc.cache.url": "redis://",
         }, ),
         task=lambda f: purge_tags,
     )
     service = RedisXMLRPCCache.create_service(None, request)
     service.purge_tags(['wu', 'tang', '4', 'evah'])
     assert isinstance(service, RedisXMLRPCCache)
     assert service._purger is purge_tags.delay
     assert purge_tags.delay.calls == [
         pretend.call('wu'),
         pretend.call('tang'),
         pretend.call('4'),
         pretend.call('evah'),
     ]
예제 #8
0
 def test_create_redis_service(self):
     purge_tags = pretend.stub(
         delay=pretend.call_recorder(lambda tag: None)
     )
     request = pretend.stub(
         registry=pretend.stub(
             settings={
                 "warehouse.xmlrpc.cache.url": "redis://",
             },
         ),
         task=lambda f: purge_tags,
     )
     service = RedisXMLRPCCache.create_service(None, request)
     service.purge_tags(['wu', 'tang', '4', 'evah'])
     assert isinstance(service, RedisXMLRPCCache)
     assert service._purger is purge_tags.delay
     assert purge_tags.delay.calls == [
         pretend.call('wu'),
         pretend.call('tang'),
         pretend.call('4'),
         pretend.call('evah'),
     ]