当前位置: 首页>>代码示例>>Python>>正文


Python exceptions.Aborted方法代码示例

本文整理汇总了Python中google.api_core.exceptions.Aborted方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.Aborted方法的具体用法?Python exceptions.Aborted怎么用?Python exceptions.Aborted使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在google.api_core.exceptions的用法示例。


在下文中一共展示了exceptions.Aborted方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test__maybe_commit_failure_read_only

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test__maybe_commit_failure_read_only(self):
        from google.api_core import exceptions

        wrapped = self._make_one(mock.sentinel.callable_)

        txn_id = b"failed"
        transaction = _make_transaction(txn_id, read_only=True)
        transaction._id = txn_id  # We won't call ``begin()``.
        wrapped.current_id = txn_id  # We won't call ``_pre_commit()``.
        wrapped.retry_id = txn_id  # We won't call ``_pre_commit()``.

        # Actually force the ``commit`` to fail (use ABORTED, but cannot
        # retry since read-only).
        exc = exceptions.Aborted("Read-only did a bad.")
        firestore_api = transaction._client._firestore_api
        firestore_api.commit.side_effect = exc

        with self.assertRaises(exceptions.Aborted) as exc_info:
            wrapped._maybe_commit(transaction)
        self.assertIs(exc_info.exception, exc)

        self.assertEqual(transaction._id, txn_id)
        self.assertEqual(wrapped.current_id, txn_id)
        self.assertEqual(wrapped.retry_id, txn_id)

        # Verify mocks.
        firestore_api.begin_transaction.assert_not_called()
        firestore_api.rollback.assert_not_called()
        firestore_api.commit.assert_called_once_with(
            transaction._client._database_string,
            [],
            transaction=txn_id,
            metadata=transaction._client._rpc_metadata,
        ) 
开发者ID:googleapis,项目名称:python-firestore,代码行数:36,代码来源:test_transaction.py

示例2: test__maybe_commit_failure_can_retry

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test__maybe_commit_failure_can_retry(self):
        from google.api_core import exceptions

        wrapped = self._make_one(mock.sentinel.callable_)

        txn_id = b"failed-but-retry"
        transaction = _make_transaction(txn_id)
        transaction._id = txn_id  # We won't call ``begin()``.
        wrapped.current_id = txn_id  # We won't call ``_pre_commit()``.
        wrapped.retry_id = txn_id  # We won't call ``_pre_commit()``.

        # Actually force the ``commit`` to fail.
        exc = exceptions.Aborted("Read-write did a bad.")
        firestore_api = transaction._client._firestore_api
        firestore_api.commit.side_effect = exc

        succeeded = wrapped._maybe_commit(transaction)
        self.assertFalse(succeeded)

        self.assertEqual(transaction._id, txn_id)
        self.assertEqual(wrapped.current_id, txn_id)
        self.assertEqual(wrapped.retry_id, txn_id)

        # Verify mocks.
        firestore_api.begin_transaction.assert_not_called()
        firestore_api.rollback.assert_not_called()
        firestore_api.commit.assert_called_once_with(
            transaction._client._database_string,
            [],
            transaction=txn_id,
            metadata=transaction._client._rpc_metadata,
        ) 
开发者ID:googleapis,项目名称:python-firestore,代码行数:34,代码来源:test_transaction.py

示例3: _maybe_commit

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def _maybe_commit(self, transaction):
        """Try to commit the transaction.

        If the transaction is read-write and the ``Commit`` fails with the
        ``ABORTED`` status code, it will be retried. Any other failure will
        not be caught.

        Args:
            transaction
                (:class:`~google.cloud.firestore_v1.transaction.Transaction`):
                The transaction to be ``Commit``-ed.

        Returns:
            bool: Indicating if the commit succeeded.
        """
        try:
            transaction._commit()
            return True
        except exceptions.GoogleAPICallError as exc:
            if transaction._read_only:
                raise

            if isinstance(exc, exceptions.Aborted):
                # If a read-write transaction returns ABORTED, retry.
                return False
            else:
                raise 
开发者ID:googleapis,项目名称:python-firestore,代码行数:29,代码来源:transaction.py

示例4: _maybe_commit

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def _maybe_commit(self, transaction):
        """Try to commit the transaction.

        If the transaction is read-write and the ``Commit`` fails with the
        ``ABORTED`` status code, it will be retried. Any other failure will
        not be caught.

        Args:
            transaction (~.firestore_v1beta1.transaction.Transaction): The
                transaction to be ``Commit``-ed.

        Returns:
            bool: Indicating if the commit succeeded.
        """
        try:
            transaction._commit()
            return True
        except exceptions.GoogleAPICallError as exc:
            if transaction._read_only:
                raise

            if isinstance(exc, exceptions.Aborted):
                # If a read-write transaction returns ABORTED, retry.
                return False
            else:
                raise 
开发者ID:googleapis,项目名称:python-firestore,代码行数:28,代码来源:transaction.py

示例5: test_aborted

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test_aborted(core_retry):
        error = core_exceptions.Aborted("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) 
开发者ID:googleapis,项目名称:python-ndb,代码行数:7,代码来源:test__retry.py

示例6: retry_on_exceptions

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def retry_on_exceptions(exception):
    return isinstance(
        exception, (Aborted, ServiceUnavailable, DeadlineExceeded)) 
开发者ID:GoogleCloudPlatform,项目名称:python-docs-samples,代码行数:5,代码来源:snippets_test.py

示例7: test___call__success_second_attempt

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test___call__success_second_attempt(self):
        from google.api_core import exceptions
        from google.cloud.firestore_v1.proto import common_pb2
        from google.cloud.firestore_v1.proto import firestore_pb2
        from google.cloud.firestore_v1.proto import write_pb2

        to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
        wrapped = self._make_one(to_wrap)

        txn_id = b"whole-enchilada"
        transaction = _make_transaction(txn_id)

        # Actually force the ``commit`` to fail on first / succeed on second.
        exc = exceptions.Aborted("Contention junction.")
        firestore_api = transaction._client._firestore_api
        firestore_api.commit.side_effect = [
            exc,
            firestore_pb2.CommitResponse(write_results=[write_pb2.WriteResult()]),
        ]

        # Call the __call__-able ``wrapped``.
        result = wrapped(transaction, "a", b="c")
        self.assertIs(result, mock.sentinel.result)

        self.assertIsNone(transaction._id)
        self.assertEqual(wrapped.current_id, txn_id)
        self.assertEqual(wrapped.retry_id, txn_id)

        # Verify mocks.
        wrapped_call = mock.call(transaction, "a", b="c")
        self.assertEqual(to_wrap.mock_calls, [wrapped_call, wrapped_call])
        firestore_api = transaction._client._firestore_api
        db_str = transaction._client._database_string
        options_ = common_pb2.TransactionOptions(
            read_write=common_pb2.TransactionOptions.ReadWrite(retry_transaction=txn_id)
        )
        self.assertEqual(
            firestore_api.begin_transaction.mock_calls,
            [
                mock.call(
                    db_str, options_=None, metadata=transaction._client._rpc_metadata
                ),
                mock.call(
                    db_str,
                    options_=options_,
                    metadata=transaction._client._rpc_metadata,
                ),
            ],
        )
        firestore_api.rollback.assert_not_called()
        commit_call = mock.call(
            db_str, [], transaction=txn_id, metadata=transaction._client._rpc_metadata
        )
        self.assertEqual(firestore_api.commit.mock_calls, [commit_call, commit_call]) 
开发者ID:googleapis,项目名称:python-firestore,代码行数:56,代码来源:test_transaction.py

示例8: test___call__failure

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test___call__failure(self):
        from google.api_core import exceptions
        from google.cloud.firestore_v1.transaction import _EXCEED_ATTEMPTS_TEMPLATE

        to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
        wrapped = self._make_one(to_wrap)

        txn_id = b"only-one-shot"
        transaction = _make_transaction(txn_id, max_attempts=1)

        # Actually force the ``commit`` to fail.
        exc = exceptions.Aborted("Contention just once.")
        firestore_api = transaction._client._firestore_api
        firestore_api.commit.side_effect = exc

        # Call the __call__-able ``wrapped``.
        with self.assertRaises(ValueError) as exc_info:
            wrapped(transaction, "here", there=1.5)

        err_msg = _EXCEED_ATTEMPTS_TEMPLATE.format(transaction._max_attempts)
        self.assertEqual(exc_info.exception.args, (err_msg,))

        self.assertIsNone(transaction._id)
        self.assertEqual(wrapped.current_id, txn_id)
        self.assertEqual(wrapped.retry_id, txn_id)

        # Verify mocks.
        to_wrap.assert_called_once_with(transaction, "here", there=1.5)
        firestore_api.begin_transaction.assert_called_once_with(
            transaction._client._database_string,
            options_=None,
            metadata=transaction._client._rpc_metadata,
        )
        firestore_api.rollback.assert_called_once_with(
            transaction._client._database_string,
            txn_id,
            metadata=transaction._client._rpc_metadata,
        )
        firestore_api.commit.assert_called_once_with(
            transaction._client._database_string,
            [],
            transaction=txn_id,
            metadata=transaction._client._rpc_metadata,
        ) 
开发者ID:googleapis,项目名称:python-firestore,代码行数:46,代码来源:test_transaction.py

示例9: test___call__success_second_attempt

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test___call__success_second_attempt(self):
        from google.api_core import exceptions
        from google.cloud.firestore_v1beta1.proto import common_pb2
        from google.cloud.firestore_v1beta1.proto import firestore_pb2
        from google.cloud.firestore_v1beta1.proto import write_pb2

        to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
        wrapped = self._make_one(to_wrap)

        txn_id = b"whole-enchilada"
        transaction = _make_transaction(txn_id)

        # Actually force the ``commit`` to fail on first / succeed on second.
        exc = exceptions.Aborted("Contention junction.")
        firestore_api = transaction._client._firestore_api
        firestore_api.commit.side_effect = [
            exc,
            firestore_pb2.CommitResponse(write_results=[write_pb2.WriteResult()]),
        ]

        # Call the __call__-able ``wrapped``.
        result = wrapped(transaction, "a", b="c")
        self.assertIs(result, mock.sentinel.result)

        self.assertIsNone(transaction._id)
        self.assertEqual(wrapped.current_id, txn_id)
        self.assertEqual(wrapped.retry_id, txn_id)

        # Verify mocks.
        wrapped_call = mock.call(transaction, "a", b="c")
        self.assertEqual(to_wrap.mock_calls, [wrapped_call, wrapped_call])
        firestore_api = transaction._client._firestore_api
        db_str = transaction._client._database_string
        options_ = common_pb2.TransactionOptions(
            read_write=common_pb2.TransactionOptions.ReadWrite(retry_transaction=txn_id)
        )
        self.assertEqual(
            firestore_api.begin_transaction.mock_calls,
            [
                mock.call(
                    db_str, options_=None, metadata=transaction._client._rpc_metadata
                ),
                mock.call(
                    db_str,
                    options_=options_,
                    metadata=transaction._client._rpc_metadata,
                ),
            ],
        )
        firestore_api.rollback.assert_not_called()
        commit_call = mock.call(
            db_str, [], transaction=txn_id, metadata=transaction._client._rpc_metadata
        )
        self.assertEqual(firestore_api.commit.mock_calls, [commit_call, commit_call]) 
开发者ID:googleapis,项目名称:python-firestore,代码行数:56,代码来源:test_transaction.py

示例10: test___call__failure

# 需要导入模块: from google.api_core import exceptions [as 别名]
# 或者: from google.api_core.exceptions import Aborted [as 别名]
def test___call__failure(self):
        from google.api_core import exceptions
        from google.cloud.firestore_v1beta1.transaction import _EXCEED_ATTEMPTS_TEMPLATE

        to_wrap = mock.Mock(return_value=mock.sentinel.result, spec=[])
        wrapped = self._make_one(to_wrap)

        txn_id = b"only-one-shot"
        transaction = _make_transaction(txn_id, max_attempts=1)

        # Actually force the ``commit`` to fail.
        exc = exceptions.Aborted("Contention just once.")
        firestore_api = transaction._client._firestore_api
        firestore_api.commit.side_effect = exc

        # Call the __call__-able ``wrapped``.
        with self.assertRaises(ValueError) as exc_info:
            wrapped(transaction, "here", there=1.5)

        err_msg = _EXCEED_ATTEMPTS_TEMPLATE.format(transaction._max_attempts)
        self.assertEqual(exc_info.exception.args, (err_msg,))

        self.assertIsNone(transaction._id)
        self.assertEqual(wrapped.current_id, txn_id)
        self.assertEqual(wrapped.retry_id, txn_id)

        # Verify mocks.
        to_wrap.assert_called_once_with(transaction, "here", there=1.5)
        firestore_api.begin_transaction.assert_called_once_with(
            transaction._client._database_string,
            options_=None,
            metadata=transaction._client._rpc_metadata,
        )
        firestore_api.rollback.assert_called_once_with(
            transaction._client._database_string,
            txn_id,
            metadata=transaction._client._rpc_metadata,
        )
        firestore_api.commit.assert_called_once_with(
            transaction._client._database_string,
            [],
            transaction=txn_id,
            metadata=transaction._client._rpc_metadata,
        ) 
开发者ID:googleapis,项目名称:python-firestore,代码行数:46,代码来源:test_transaction.py


注:本文中的google.api_core.exceptions.Aborted方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。