Exemplo n.º 1
0
    def test_run_with_integrity_error_on_commit_retries(self):
        """
        If the given function raises a L{IntegrityError}, then the function
        will be retried another two times before letting the exception bubble
        up.
        """
        self.transactor.sleep = self.mocker.mock()
        self.transactor.uniform = self.mocker.mock()
        self.mocker.order()

        self.expect(self.function())
        self.expect(self.transaction.commit()).throw(IntegrityError())
        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())
        self.expect(self.transaction.commit()).throw(IntegrityError())
        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())
        self.expect(self.transaction.commit()).throw(IntegrityError())
        self.expect(self.transaction.abort())
        self.mocker.replay()

        deferred = self.transactor.run(self.function)
        self.assertFailure(deferred, IntegrityError)
        return deferred
Exemplo n.º 2
0
    def test_run_with_on_retry_callback(self):
        """
        If a retry callback is passed with the C{on_retry} parameter, then
        it's invoked with the number of retries performed so far.
        """
        calls = []

        def on_retry(context):
            calls.append(context)

        self.transactor.on_retry = on_retry

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

        self.expect(self.function(1, a=2))
        error = IntegrityError()
        self.expect(self.transaction.commit()).throw(error)
        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(1, a=2))
        self.expect(self.transaction.commit())
        self.mocker.replay()

        deferred = self.transactor.run(self.function, 1, a=2)

        def check(_):
            [context] = calls

            self.assertEqual(self.function, context.function)
            self.assertEqual((1, ), context.args)
            self.assertEqual({"a": 2}, context.kwargs)
            self.assertEqual(1, context.retry)
            self.assertEqual(1, context.retry)
            self.assertIs(error, context.error)

        return deferred.addCallback(check)