Ejemplo n.º 1
0
    def test_run_with_transaction_rollback_error_retries(self):
        """
        If the given function raises a L{TransactionRollbackError}, then the
        function will be retried another two times before letting the exception
        bubble up.
        """
        if not has_psycopg:
            return

        self.transactor.sleep = self.mocker.mock()
        self.transactor.uniform = self.mocker.mock()
        self.mocker.order()

        self.expect(self.function()).throw(TransactionRollbackError())
        self.expect(self.transaction.abort())
        self.expect(self.transactor.uniform(1, 2**1)).result(1)
        self.expect(self.transactor.sleep(1))

        self.expect(self.function()).throw(TransactionRollbackError())
        self.expect(self.transaction.abort())
        self.expect(self.transactor.uniform(1, 2**2)).result(2)
        self.expect(self.transactor.sleep(2))

        self.expect(self.function()).throw(TransactionRollbackError())
        self.expect(self.transaction.abort())
        self.mocker.replay()

        deferred = self.transactor.run(self.function)
        self.assertFailure(deferred, TransactionRollbackError)
        return deferred
Ejemplo n.º 2
0
    def test_psycopg2_non_standard_err(self):
        from psycopg2.extensions import TransactionRollbackError
        import psycopg2

        exception = exc.DBAPIError.instance("some statement", {},
                                            TransactionRollbackError("foo"),
                                            psycopg2.Error)
        assert isinstance(exception, exc.OperationalError)
 def lock_table(self, table_name):
     # Lock a table, triggering multiple simultaneous accesses to fail
     from sqlalchemy import exc
     from psycopg2.extensions import TransactionRollbackError
     command = "LOCK TABLE {} IN EXCLUSIVE MODE".format(table_name)
     try:
         self.session.connection().execute(command)
     except exc.OperationalError as e:
         e.orig = TransactionRollbackError()
         raise e
Ejemplo n.º 4
0
    def test_message_requeued_on_deadlock(self):
        self.receiver.test.side_effect = [
            OperationalError("SELECT 1", [],
                             TransactionRollbackError("deadlock detected")),
            OperationalError("SELECT 1", [], TransactionRollbackError),
        ]
        self.consumer.consume_msg(self.body, self.msg)
        self.assertFalse(self.msg.ack.called)
        self.assertEqual(self.msg.requeue.call_count, 1)

        self.consumer.consume_msg(self.body, self.msg)
        self.assertFalse(self.msg.ack.called)
        self.assertEqual(self.msg.requeue.call_count, 2)
Ejemplo n.º 5
0
def simulate_possible_serialization_error(request, success_chance_one_in_n):
    # We need to open the DBSession. Any query will do
    count = request.dbsession.query(MyModel).count()
    # Roll the dice and throw a serialization error if we got unlucky
    dice_roll = random.randint(0, success_chance_one_in_n - 1)
    if dice_roll:
        print("\n\tSERIALIZATION ERROR, ATTEMPT #{}\n".format(
            get_retry_attempt_count(request)))
        raise OperationalError(statement="SELECT herpaderp",
                               params=[],
                               orig=TransactionRollbackError("""\
could not serialize access due to read/write dependencies among transactions
DETAIL:  Reason code: Canceled on identification as a pivot, during write.
HINT:  The transaction might succeed if retried.
            """))
Ejemplo n.º 6
0
 def function():
     """Fail with TransactionRollbackError."""
     calls.append('function')
     raise TransactionRollbackError("message")