1717import unittest
1818
1919from google .cloud import bigquery
20- from google .cloud .exceptions import Conflict
2120from google .cloud .exceptions import Forbidden
2221
2322from retry import RetryErrors
@@ -35,6 +34,7 @@ def _rate_limit_exceeded(forbidden):
3534 return any (error ['reason' ] == 'rateLimitExceeded'
3635 for error in forbidden ._errors )
3736
37+
3838# We need to wait to stay within the rate limits.
3939# The alternative outcome is a 403 Forbidden response from upstream, which
4040# they return instead of the more appropriate 429.
@@ -61,11 +61,22 @@ def setUp(self):
6161 self .to_delete = []
6262
6363 def tearDown (self ):
64+ from google .cloud .bigquery .dataset import Dataset
6465 from google .cloud .storage import Bucket
66+ from google .cloud .exceptions import BadRequest
67+ from google .cloud .exceptions import Conflict
68+
69+ def _still_in_use (bad_request ):
70+ return any (error ['reason' ] == 'resourceInUse'
71+ for error in bad_request ._errors )
72+
73+ retry_in_use = RetryErrors (BadRequest , error_predicate = _still_in_use )
74+ retry_409 = RetryErrors (Conflict )
6575 for doomed in self .to_delete :
6676 if isinstance (doomed , Bucket ):
67- retry = RetryErrors (Conflict )
68- retry (doomed .delete )(force = True )
77+ retry_409 (doomed .delete )(force = True )
78+ elif isinstance (doomed , Dataset ):
79+ retry_in_use (doomed .delete )()
6980 else :
7081 doomed .delete ()
7182
0 commit comments