def test_policy_success(self) -> None: callable = mock.MagicMock(side_effect=[self.bomb, mock.sentinel.OK]) always_retry = lambda i, e: True assert ConditionalRetryPolicy(always_retry)( callable) is mock.sentinel.OK assert callable.call_count == 2
def test_policy_counter(self) -> None: callable = mock.MagicMock(side_effect=self.bomb) retry_once = lambda i, e: i < 2 with pytest.raises(Exception) as e: ConditionalRetryPolicy(retry_once)(callable) assert callable.call_count == 2 assert e.value is self.bomb
def test_poilcy_failure(self) -> None: callable = mock.MagicMock(side_effect=self.bomb) never_retry = lambda i, e: False with pytest.raises(Exception) as e: ConditionalRetryPolicy(never_retry)(callable) assert callable.call_count == 1 assert e.value is self.bomb
def test_policy_exception_filtering(self) -> None: errors = [Exception(), Exception(), Exception()] callable = mock.MagicMock(side_effect=errors) sometimes_retry = lambda i, e: e is not errors[-1] with pytest.raises(Exception) as e: ConditionalRetryPolicy(sometimes_retry)(callable) assert e.value is errors[-1] assert callable.call_count == 3
def get_release_file_cache_key_meta(release_id, releasefile_ident): return "meta:%s" % get_release_file_cache_key(release_id, releasefile_ident) MAX_FETCH_ATTEMPTS = 3 def should_retry_fetch(attempt: int, e: Exception) -> bool: return not attempt > MAX_FETCH_ATTEMPTS and isinstance( e, OSError) and e.errno == errno.ESTALE fetch_retry_policy = ConditionalRetryPolicy(should_retry_fetch, exponential_delay(0.05)) def fetch_release_file(filename, release, dist=None): """ Attempt to retrieve a release artifact from the database. Caches the result of that attempt (whether successful or not). """ dist_name = dist and dist.name or None releasefile_ident = ReleaseFile.get_ident(filename, dist_name) cache_key = get_release_file_cache_key(release_id=release.id, releasefile_ident=releasefile_ident) # Cache key to store file metadata, currently only the size of the # compressed version of file. We cannot use the cache_key because large