Skip to content

Commit

Permalink
Harden bucket teardown against '429 Too Many Requests'.
Browse files Browse the repository at this point in the history
Closes #6096.
  • Loading branch information
tseaver committed Sep 25, 2018
1 parent 48ae436 commit 874af5d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions bigquery/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@
IPython = None

from google.api_core.exceptions import PreconditionFailed
from google.api_core.exceptions import BadRequest
from google.api_core.exceptions import Conflict
from google.api_core.exceptions import Forbidden
from google.api_core.exceptions import NotFound
from google.api_core.exceptions import TooManyRequests
from google.cloud import bigquery
from google.cloud.bigquery.dataset import Dataset, DatasetReference
from google.cloud.bigquery.dataset import Dataset
from google.cloud.bigquery.dataset import DatasetReference
from google.cloud.bigquery.table import Table
from google.cloud._helpers import UTC
from google.cloud.bigquery import dbapi
from google.cloud.exceptions import BadRequest, Forbidden, NotFound
from google.cloud import storage

from test_utils.retry import RetryErrors
Expand Down Expand Up @@ -135,19 +140,16 @@ def setUp(self):
self.to_delete = []

def tearDown(self):
from google.cloud.storage import Bucket
from google.cloud.exceptions import BadRequest
from google.cloud.exceptions import Conflict

def _still_in_use(bad_request):
return any(error['reason'] == 'resourceInUse'
for error in bad_request._errors)

retry_in_use = RetryErrors(BadRequest, error_predicate=_still_in_use)
retry_409 = RetryErrors(Conflict)
retry_409_429 = RetryErrors((Conflict, TooManyRequests))
for doomed in self.to_delete:
if isinstance(doomed, Bucket):
retry_409(doomed.delete)(force=True)
if isinstance(doomed, storage.Bucket):
retry_409_429(doomed.delete)(force=True)
elif isinstance(doomed, (Dataset, bigquery.DatasetReference)):
retry_in_use(Config.CLIENT.delete_dataset)(
doomed, delete_contents=True)
Expand Down

0 comments on commit 874af5d

Please sign in to comment.