Пример #1
0
    def test_should_not_release_when_lock_when_lock_active_error_is_raised(
            self):
        with CacheLock(key='test') as lock:
            with pytest.raises(LockActiveError):
                with CacheLock(key='test'):
                    pass

            assert lock.cache.get(lock._key)
Пример #2
0
    def test_should_not_release_when_lock_is_already_acquired(self):
        """
        It should release the lock only if it was acquired successfully.
        A lock that do not acquired a lock should not release it.
        """
        with CacheLock(key='test', raise_exception=False) as lock:
            with CacheLock(key='test', raise_exception=False):
                pass

            assert lock.cache.get(lock._key)

        assert not lock.cache.get(lock._key)
Пример #3
0
    def retrieve_favorites(self, request, client_id=None):
        try:
            with CacheLock(
                key=f'cachelock:retrieve_favorites_{client_id}',
                cache_alias='concurrent',
                expire=30,
            ):
                logger.info(
                    'Searching for favorite products',
                    client_id=client_id
                )

                queryset = self.get_queryset()
                serializer = self.get_serializer(queryset, many=True)
                favorites = get_details_products_favorites(
                    favorites=serializer.data
                )

                return Response(
                    data=favorites,
                    status=status.HTTP_200_OK
                )
        except ProductException:
            raise ClientProductFavoritesException
        except LockActiveError:
            raise ClientProductFavoritesLockException
Пример #4
0
 def test_should_raise_an_exception_when_an_error_happens_on_release_lock(
     self
 ):
     lock = CacheLock(key='test')
     with mock.patch.object(
         lock.cache,
         'delete',
         side_effect=Exception('oops')
     ):
         with pytest.raises(LockReleaseError):
             with lock:
                 pass
Пример #5
0
 def test_should_persist_lock_when_delete_on_exit_is_false(
     self
 ):
     with CacheLock(key='test_persist', delete_on_exit=False) as lock:
         assert lock.active
     assert lock.cache.get(lock._key)
Пример #6
0
 def test_should_expire_immediately_when_expire_is_zero(self):
     with CacheLock(key='test', expire=0) as lock:
         assert not lock.cache.get(lock._key)
Пример #7
0
 def test_should_use_default_cache_timeout_when_expire_is_not_given(self):
     with CacheLock(cache_alias='explicit_timeout', key='test') as lock:
         assert lock.cache.get(lock._key)
Пример #8
0
    def test_should_not_raise_exception_when_raise_exception_is_false(self):
        with CacheLock(key='test', expire=1000) as lock:
            assert lock.active is True

            with CacheLock(key='test', raise_exception=False) as lock:
                assert lock.active is False
Пример #9
0
 def test_should_raise_exception_when_try_lock_some_lock_key(self):
     with pytest.raises(LockActiveError):
         with CacheLock(key='test', expire=1000):
             with CacheLock(key='test'):
                 pass
Пример #10
0
    def test_should_release_lock(self):
        with CacheLock(key='test') as lock:
            pass

        assert not lock.active
Пример #11
0
 def test_should_be_true_when_lock_is_acquired(self):
     with CacheLock(key='test') as lock:
         assert lock.active
Пример #12
0
 def test_should_release_lock_when_delete_on_exit_is_true(
     self
 ):
     with CacheLock(key='test', delete_on_exit=True) as lock:
         pass
     assert not lock.cache.get(lock._key)