Exemplo n.º 1
0
    def test_execute_lambda_retriable_http_exception(
            self, mock_start_transaction, mock_session, mock_transaction,
            mock_executor, mock_no_throw_abort):
        for error_class in RETRYABLE_HTTP_ERRORS:
            ce = error_class(endpoint_url="http://a-url")
            mock_start_transaction.return_value = mock_transaction
            qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                       mock_executor, MOCK_DRIVER_RELEASE)
            lambda_execution_context = _LambdaExecutionContext()
            mock_lambda = Mock()
            mock_lambda.side_effect = ce

            self.assertRaises(error_class, qldb_session._execute_lambda,
                              mock_lambda, MOCK_DEFAULT_RETRY_CONFIG,
                              lambda_execution_context)

            mock_start_transaction.assert_has_calls(
                [call(), call(), call(),
                 call(), call()])
            mock_no_throw_abort.assert_has_calls([
                call(mock_transaction),
                call(mock_transaction),
                call(mock_transaction),
                call(mock_transaction),
                call(mock_transaction)
            ])
            self.assertEqual(mock_lambda.call_count,
                             MOCK_DEFAULT_RETRY_CONFIG.retry_limit + 1)
            mock_transaction._commit.assert_not_called()
Exemplo n.º 2
0
    def test_execute_lambda_invalid_session_exception(
            self, mock_start_transaction, mock_session, mock_transaction,
            mock_is_invalid_session, mock_executor, mock_no_throw_abort):
        mock_client = Mock()
        ce = ClientError(MOCK_CLIENT_ERROR_MESSAGE, 'message')
        mock_start_transaction.return_value = mock_transaction
        type(mock_session).client = PropertyMock(return_value=mock_client)
        type(mock_session).ledger_name = PropertyMock(
            return_value=MOCK_LEDGER_NAME)
        mock_session._start_session = mock_session
        mock_is_invalid_session.return_value = True
        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()
        mock_lambda.side_effect = ce
        execution_attempt_before = lambda_execution_context.execution_attempt

        self.assertRaises(ClientError, qldb_session._execute_lambda,
                          mock_lambda, MOCK_RETRY_CONFIG_WITH_1_RETRY,
                          lambda_execution_context)
        mock_start_transaction.assert_called_with()
        mock_no_throw_abort.assert_not_called()
        mock_transaction._commit.assert_not_called()
        self.assertEqual(mock_session._start_session.call_count, 0)
        self.assertEqual(mock_lambda.call_count, 1)
        self.assertEqual(execution_attempt_before,
                         lambda_execution_context.execution_attempt)
        self.assertEqual(qldb_session._is_closed, True)
Exemplo n.º 3
0
    def test_execute_lambda_occ_conflict(self, mock_start_transaction,
                                         mock_session, mock_transaction,
                                         mock_is_occ_conflict_exception,
                                         mock_is_retriable_exception,
                                         mock_logger_warning, mock_executor,
                                         mock_no_throw_abort):
        ce = ClientError(MOCK_CLIENT_ERROR_MESSAGE, MOCK_MESSAGE)
        mock_start_transaction.return_value = mock_transaction
        mock_is_occ_conflict_exception.return_value = True
        mock_is_retriable_exception.return_value = False
        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()
        mock_lambda.side_effect = ce
        self.assertRaises(ClientError, qldb_session._execute_lambda,
                          mock_lambda, MOCK_DEFAULT_RETRY_CONFIG,
                          lambda_execution_context)

        mock_start_transaction.assert_has_calls(
            [call(), call(), call(), call(),
             call()])
        self.assertEqual(mock_no_throw_abort.call_count, 0)
        mock_is_occ_conflict_exception.assert_has_calls(
            [call(ce), call(ce),
             call(ce), call(ce),
             call(ce)])
        self.assertEqual(mock_lambda.call_count,
                         MOCK_DEFAULT_RETRY_CONFIG.retry_limit + 1)
        self.assertEqual(mock_logger_warning.call_count,
                         MOCK_DEFAULT_RETRY_CONFIG.retry_limit)
        mock_transaction._commit.assert_not_called()
Exemplo n.º 4
0
    def test_execute_lambda_start_transaction_error(self,
                                                    mock_start_transaction,
                                                    mock_session,
                                                    mock_executor,
                                                    mock_no_throw_abort):
        mock_bad_request_exception_message = {
            'Error': {
                'Code': 'BadRequestException',
                'Message': MOCK_MESSAGE
            }
        }
        mock_bad_request_exception = ClientError(
            mock_bad_request_exception_message, MOCK_MESSAGE)
        ste = StartTransactionError(mock_bad_request_exception)
        mock_start_transaction.side_effect = ste
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()

        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        self.assertRaises(ClientError, qldb_session._execute_lambda,
                          mock_lambda, MOCK_DEFAULT_RETRY_CONFIG,
                          lambda_execution_context)
        self.assertEqual(mock_no_throw_abort.call_count,
                         MOCK_DEFAULT_RETRY_CONFIG.retry_limit + 1)
Exemplo n.º 5
0
    def test_execute_lambda(self, mock_start_transaction, mock_session,
                            mock_transaction, mock_stream_cursor,
                            mock_buffered_cursor, mock_is_instance,
                            mock_executor):
        mock_start_transaction.return_value = mock_transaction
        mock_transaction.execute_lambda.return_value = MOCK_RESULT
        mock_transaction._commit.return_value = None
        mock_is_instance.return_value = True
        mock_stream_cursor.return_value = mock_stream_cursor
        mock_buffered_cursor.return_value = MOCK_RESULT
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()
        mock_lambda.return_value = MOCK_RESULT

        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        result = qldb_session._execute_lambda(mock_lambda,
                                              MOCK_DEFAULT_RETRY_CONFIG,
                                              lambda_execution_context)

        mock_start_transaction.assert_called_once_with()
        mock_lambda.assert_called_once()
        mock_transaction._commit.assert_called_once_with()
        mock_is_instance.assert_called_with(MOCK_RESULT, mock_stream_cursor)
        mock_buffered_cursor.assert_called_once_with(MOCK_RESULT)
        self.assertEqual(result, MOCK_RESULT)
Exemplo n.º 6
0
    def test_execute_lambda_lambda_aborted_error(
            self, mock_start_transaction, mock_session, mock_transaction,
            mock_is_occ_conflict, mock_is_retriable_exception, mock_executor,
            mock_no_throw_abort):
        mock_start_transaction.return_value = mock_transaction
        mock_is_occ_conflict.return_value = True
        mock_is_retriable_exception.return_value = False
        retry_indicator = Mock()
        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()
        mock_lambda.side_effect = LambdaAbortedError

        self.assertRaises(LambdaAbortedError, qldb_session._execute_lambda,
                          mock_lambda, MOCK_DEFAULT_RETRY_CONFIG,
                          lambda_execution_context)
        mock_no_throw_abort.assert_called_once_with(mock_transaction)
Exemplo n.º 7
0
    def test_execute_lambda_client_error(self, mock_start_transaction,
                                         mock_session, mock_executor,
                                         mock_is_retriable_exception,
                                         mock_is_occ_conflict_exception,
                                         mock_no_throw_abort):
        ce = ClientError(MOCK_CLIENT_ERROR_MESSAGE, MOCK_MESSAGE)
        mock_start_transaction.side_effect = ce
        mock_is_retriable_exception.return_value = False
        mock_is_occ_conflict_exception.return_value = False
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()

        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        self.assertRaises(ClientError, qldb_session._execute_lambda,
                          mock_lambda, MOCK_DEFAULT_RETRY_CONFIG,
                          lambda_execution_context)
        mock_no_throw_abort.assert_called_once_with(None)
Exemplo n.º 8
0
    def test_execute_lambda_unknow_exception(self, mock_start_transaction,
                                             mock_session, mock_transaction,
                                             mock_is_occ_conflict_exception,
                                             mock_is_retriable_exception,
                                             mock_executor,
                                             mock_no_throw_abort):
        error = KeyError()
        mock_start_transaction.return_value = mock_transaction
        mock_is_occ_conflict_exception.return_value = True
        mock_is_retriable_exception.return_value = False
        qldb_session = QldbSession(mock_session, MOCK_READ_AHEAD,
                                   mock_executor, MOCK_DRIVER_RELEASE)
        lambda_execution_context = _LambdaExecutionContext()
        mock_lambda = Mock()
        mock_lambda.side_effect = error
        self.assertRaises(KeyError, qldb_session._execute_lambda, mock_lambda,
                          MOCK_DEFAULT_RETRY_CONFIG, lambda_execution_context)

        self.assertEqual(mock_start_transaction.call_count, 1)
        self.assertEqual(mock_no_throw_abort.call_count, 1)
        self.assertEqual(mock_lambda.call_count, 1)
        mock_transaction._commit.assert_not_called()