本文整理汇总了Python中google.api_core.exceptions.ServiceUnavailable方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.ServiceUnavailable方法的具体用法?Python exceptions.ServiceUnavailable怎么用?Python exceptions.ServiceUnavailable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.api_core.exceptions
的用法示例。
在下文中一共展示了exceptions.ServiceUnavailable方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_grpc_error
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_grpc_error(stub):
api = stub.return_value
future = tasklets.Future()
api.foo.future.return_value = future
class DummyError(grpc.Call, Exception):
def code(self):
return grpc.StatusCode.UNAVAILABLE
def details(self):
return "Where is the devil in?"
try:
raise DummyError("Have to raise in order to get traceback")
except Exception as error:
future.set_exception(error)
request = object()
with pytest.raises(core_exceptions.ServiceUnavailable):
_api.make_call("foo", request, retries=0).result()
示例2: tearDown
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def tearDown(self):
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_storage_errors_conflict = RetryErrors(
(Conflict, TooManyRequests, InternalServerError, ServiceUnavailable)
)
for doomed in self.to_delete:
if isinstance(doomed, storage.Bucket):
retry_storage_errors_conflict(doomed.delete)(force=True)
elif isinstance(doomed, (Dataset, bigquery.DatasetReference)):
retry_in_use(Config.CLIENT.delete_dataset)(doomed, delete_contents=True)
elif isinstance(doomed, (Table, bigquery.TableReference)):
retry_in_use(Config.CLIENT.delete_table)(doomed)
else:
doomed.delete()
示例3: _gcs_should_retry_on
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def _gcs_should_retry_on(e):
# Retry on all 503 errors and 500, as recommended by https://cloud.google.com/apis/design/errors#error_retries
return isinstance(e, (InternalServerError, ServiceUnavailable, requests.exceptions.ConnectionError))
示例4: test_w_unavailable
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_w_unavailable(self):
from google.api_core.exceptions import ServiceUnavailable
exception = ServiceUnavailable("testing")
self.assertTrue(self._callFUT(exception))
示例5: test_success_third_attempt
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_success_third_attempt(self, _sleep):
from google.api_core import exceptions
from google.cloud.firestore_v1.gapic import firestore_client
# Create a minimal fake GAPIC with a dummy result.
firestore_api = mock.create_autospec(
firestore_client.FirestoreClient, instance=True
)
# Make sure the first two requests fail and the third succeeds.
firestore_api.commit.side_effect = [
exceptions.ServiceUnavailable("Server sleepy."),
exceptions.ServiceUnavailable("Server groggy."),
mock.sentinel.commit_response,
]
# Attach the fake GAPIC to a real client.
client = _make_client("outside")
client._firestore_api_internal = firestore_api
# Call function and check result.
txn_id = b"the-world\x00"
commit_response = self._call_fut(client, mock.sentinel.write_pbs, txn_id)
self.assertIs(commit_response, mock.sentinel.commit_response)
# Verify mocks used.
self.assertEqual(_sleep.call_count, 2)
_sleep.assert_any_call(1.0)
_sleep.assert_any_call(2.0)
# commit() called same way 3 times.
commit_call = mock.call(
client._database_string,
mock.sentinel.write_pbs,
transaction=txn_id,
metadata=client._rpc_metadata,
)
self.assertEqual(
firestore_api.commit.mock_calls, [commit_call, commit_call, commit_call]
)
示例6: test_failure_second_attempt
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_failure_second_attempt(self, _sleep):
from google.api_core import exceptions
from google.cloud.firestore_v1.gapic import firestore_client
# Create a minimal fake GAPIC with a dummy result.
firestore_api = mock.create_autospec(
firestore_client.FirestoreClient, instance=True
)
# Make sure the first request fails retry-able and second
# fails non-retryable.
exc1 = exceptions.ServiceUnavailable("Come back next time.")
exc2 = exceptions.InternalServerError("Server on fritz.")
firestore_api.commit.side_effect = [exc1, exc2]
# Attach the fake GAPIC to a real client.
client = _make_client("peanut-butter")
client._firestore_api_internal = firestore_api
# Call function and check result.
txn_id = b"the-journey-when-and-where-well-go"
with self.assertRaises(exceptions.InternalServerError) as exc_info:
self._call_fut(client, mock.sentinel.write_pbs, txn_id)
self.assertIs(exc_info.exception, exc2)
# Verify mocks used.
_sleep.assert_called_once_with(1.0)
# commit() called same way 2 times.
commit_call = mock.call(
client._database_string,
mock.sentinel.write_pbs,
transaction=txn_id,
metadata=client._rpc_metadata,
)
self.assertEqual(firestore_api.commit.mock_calls, [commit_call, commit_call])
示例7: test_success_third_attempt
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_success_third_attempt(self, _sleep):
from google.api_core import exceptions
from google.cloud.firestore_v1beta1.gapic import firestore_client
# Create a minimal fake GAPIC with a dummy result.
firestore_api = mock.create_autospec(
firestore_client.FirestoreClient, instance=True
)
# Make sure the first two requests fail and the third succeeds.
firestore_api.commit.side_effect = [
exceptions.ServiceUnavailable("Server sleepy."),
exceptions.ServiceUnavailable("Server groggy."),
mock.sentinel.commit_response,
]
# Attach the fake GAPIC to a real client.
client = _make_client("outside")
client._firestore_api_internal = firestore_api
# Call function and check result.
txn_id = b"the-world\x00"
commit_response = self._call_fut(client, mock.sentinel.write_pbs, txn_id)
self.assertIs(commit_response, mock.sentinel.commit_response)
# Verify mocks used.
self.assertEqual(_sleep.call_count, 2)
_sleep.assert_any_call(1.0)
_sleep.assert_any_call(2.0)
# commit() called same way 3 times.
commit_call = mock.call(
client._database_string,
mock.sentinel.write_pbs,
transaction=txn_id,
metadata=client._rpc_metadata,
)
self.assertEqual(
firestore_api.commit.mock_calls, [commit_call, commit_call, commit_call]
)
示例8: test_failure_second_attempt
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_failure_second_attempt(self, _sleep):
from google.api_core import exceptions
from google.cloud.firestore_v1beta1.gapic import firestore_client
# Create a minimal fake GAPIC with a dummy result.
firestore_api = mock.create_autospec(
firestore_client.FirestoreClient, instance=True
)
# Make sure the first request fails retry-able and second
# fails non-retryable.
exc1 = exceptions.ServiceUnavailable("Come back next time.")
exc2 = exceptions.InternalServerError("Server on fritz.")
firestore_api.commit.side_effect = [exc1, exc2]
# Attach the fake GAPIC to a real client.
client = _make_client("peanut-butter")
client._firestore_api_internal = firestore_api
# Call function and check result.
txn_id = b"the-journey-when-and-where-well-go"
with self.assertRaises(exceptions.InternalServerError) as exc_info:
self._call_fut(client, mock.sentinel.write_pbs, txn_id)
self.assertIs(exc_info.exception, exc2)
# Verify mocks used.
_sleep.assert_called_once_with(1.0)
# commit() called same way 2 times.
commit_call = mock.call(
client._database_string,
mock.sentinel.write_pbs,
transaction=txn_id,
metadata=client._rpc_metadata,
)
self.assertEqual(firestore_api.commit.mock_calls, [commit_call, commit_call])
示例9: _commit_with_retry
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def _commit_with_retry(client, write_pbs, transaction_id):
"""Call ``Commit`` on the GAPIC client with retry / sleep.
Retries the ``Commit`` RPC on Unavailable. Usually this RPC-level
retry is handled by the underlying GAPICd client, but in this case it
doesn't because ``Commit`` is not always idempotent. But here we know it
is "idempotent"-like because it has a transaction ID. We also need to do
our own retry to special-case the ``INVALID_ARGUMENT`` error.
Args:
client (~.firestore_v1beta1.client.Client): A client with
GAPIC client and configuration details.
write_pbs (List[google.cloud.proto.firestore.v1beta1.\
write_pb2.Write, ...]): A ``Write`` protobuf instance to
be committed.
transaction_id (bytes): ID of an existing transaction that
this commit will run in.
Returns:
google.cloud.firestore_v1beta1.types.CommitResponse:
The protobuf response from ``Commit``.
Raises:
~google.api_core.exceptions.GoogleAPICallError: If a non-retryable
exception is encountered.
"""
current_sleep = _INITIAL_SLEEP
while True:
try:
return client._firestore_api.commit(
client._database_string,
write_pbs,
transaction=transaction_id,
metadata=client._rpc_metadata,
)
except exceptions.ServiceUnavailable:
# Retry
pass
current_sleep = _sleep(current_sleep)
示例10: test_unavailable
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_unavailable(core_retry):
error = core_exceptions.ServiceUnavailable("testing")
core_retry.if_transient_error.return_value = False
assert _retry.is_transient_error(error) is True
core_retry.if_transient_error.assert_called_once_with(error)
示例11: test__should_recover_true
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test__should_recover_true():
manager = make_manager()
details = "UNAVAILABLE. Service taking nap."
exc = exceptions.ServiceUnavailable(details)
assert manager._should_recover(exc) is True
示例12: retry_on_exceptions
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def retry_on_exceptions(exception):
return isinstance(
exception, (Aborted, ServiceUnavailable, DeadlineExceeded))
示例13: test_create_resumable_upload_session_with_failure
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def test_create_resumable_upload_session_with_failure(self):
from google.resumable_media import InvalidResponse
from google.cloud import exceptions
message = "5-oh-3 woe is me."
response = self._mock_requests_response(
status_code=http_client.SERVICE_UNAVAILABLE, headers={}
)
side_effect = InvalidResponse(response, message)
with self.assertRaises(exceptions.ServiceUnavailable) as exc_info:
self._create_resumable_upload_session_helper(side_effect=side_effect)
self.assertIn(message, exc_info.exception.message)
self.assertEqual(exc_info.exception.errors, [])
示例14: tearDown
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def tearDown(self):
errors = (exceptions.TooManyRequests, exceptions.ServiceUnavailable)
retry = RetryErrors(errors, max_tries=6)
for blob in self.case_blobs_to_delete:
retry(blob.delete)()
示例15: tearDownClass
# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import ServiceUnavailable [as 别名]
def tearDownClass(cls):
errors = (exceptions.TooManyRequests, exceptions.ServiceUnavailable)
retry = RetryErrors(errors, max_tries=6)
for blob in cls.suite_blobs_to_delete:
retry(blob.delete)()