def test_instantiation_with_redis_info(self): """A Redis client must be initialized if connection info is provided""" lock = downloaders.DownloadLock('url', 2, redis_host='test', redis_port=6379) self.assertIsInstance(lock.redis, Redis)
def test_acquired_if_max_downloads_is_none(self): """The lock must be acquired if there is the max_downloads is left as None""" lock = downloaders.DownloadLock('url', None, redis_host='test', redis_port=6379) with mock.patch('geospaas_processing.downloaders.Redis.eval'): self.assertTrue(lock.__enter__())
def test_locked_if_increment_returns_none(self): """`__enter__` must return False if the increment script returns None""" lock = downloaders.DownloadLock('url', 2, redis_host='test', redis_port=6379) with mock.patch('geospaas_processing.downloaders.Redis.eval', return_value=None): self.assertFalse(lock.__enter__())
def test_acquired_if_increment_returns_int(self): """`__enter__` must return True if the increment script doesn't return None""" lock = downloaders.DownloadLock('url', 2, redis_host='test', redis_port=6379) with mock.patch('geospaas_processing.downloaders.Redis.eval', return_value=1): self.assertTrue(lock.__enter__())
def test_no_decrement_if_not_redis_and_acquired(self): """ The download count must not be decremented if redis is unavailable, even if the lock was acquired """ lock = downloaders.DownloadLock('url', 2) lock.acquired = True with mock.patch( 'geospaas_processing.downloaders.Redis.eval') as mock_eval: lock.__exit__() mock_eval.assert_not_called()
def test_decrement_if_redis_and_acquired(self): """The download count must be decremented if redis is available and the lock was acquired""" lock = downloaders.DownloadLock('url', 2, redis_host='test', redis_port=6379) lock.acquired = True with mock.patch( 'geospaas_processing.downloaders.Redis.eval') as mock_eval: lock.__exit__() mock_eval.assert_called_with(lock.DECREMENT_SCRIPT, 1, lock.CURRENT_DOWNLOADS_KEY, 'url')
def test_no_decrement_if_redis_and_not_acquired(self): """ The download count must not be decremented if redis is available but the lock was not acquired """ lock = downloaders.DownloadLock('url', 2, redis_host='test', redis_port=6379) lock.acquired = False with mock.patch( 'geospaas_processing.downloaders.Redis.eval') as mock_eval: lock.__exit__() mock_eval.assert_not_called()
def test_always_acquired_if_no_redis(self): """`__enter__` must return True if no Redis connection info is provided""" lock = downloaders.DownloadLock('url', 2) self.assertTrue(lock.__enter__())
def test_instantiation_without_redis_info(self): """The redis attribute must be set to None if no connection info is provided""" lock = downloaders.DownloadLock('url', 2) self.assertIsNone(lock.redis)