示例#1
0
    def testRetriesDisconnectionErrors(self):
        """
        L{Transact.run} retries the transaction if a L{DisconnectionError}
        occurs during a transaction.
        """
        class FakeTransactionManager(object):
            def commit(self):
                self.committed = True

            def abort(self):
                self.aborted = True

        self.flag = False

        def function():
            if not self.flag:
                self.flag = True
                raise DisconnectionError('Disconnected')
            return 'success'

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        result = yield transact.run(function)
        self.assertEqual('success', result)
        self.assertTrue(manager.aborted)
        self.assertTrue(manager.committed)
示例#2
0
    def testRetries(self):
        """
        L{Transact.run} retries the transaction if a DB error occours during
        a transaction collision.
        """
        class FakeTransactionManager(object):
            def commit(self):
                self.committed = True

            def abort(self):
                self.aborted = True

        self.flag = False

        def function():
            if not self.flag:
                self.flag = True
                raise psycopg2.Error('Error')
            return 'success'

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager, lambda seconds: None)
        result = yield transact.run(function)
        self.assertEqual('success', result)
        self.assertTrue(manager.aborted)
        self.assertTrue(manager.committed)
示例#3
0
    def testRetryWriteWarningInLog(self):
        """
        L{Transact.run} writes a warning in the logs if a retry is done.
        """
        class FakeTransactionManager(object):
            def commit(self):
                self.committed = True

            def abort(self):
                self.aborted = True

        self.flag = False

        def function():
            if not self.flag:
                self.flag = True
                raise psycopg2.Error('Error')
            return 'success'

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager, lambda seconds: None)
        result = yield transact.run(function)
        self.assertEqual('success', result)
        self.assertTrue(manager.aborted)
        self.assertTrue(manager.committed)
        self.assertIn('Retrying a transaction', self.log.getvalue())
示例#4
0
    def testRetryWriteWarningInLog(self):
        """
        L{Transact.run} writes a warning in the logs if a retry is done.
        """

        class FakeTransactionManager(object):

            def commit(self):
                self.committed = True

            def abort(self):
                self.aborted = True

        self.flag = False

        def function():
            if not self.flag:
                self.flag = True
                raise psycopg2.Error('Error')
            return 'success'

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager, lambda seconds: None)
        result = yield transact.run(function)
        self.assertEqual('success', result)
        self.assertTrue(manager.aborted)
        self.assertTrue(manager.committed)
        self.assertIn('Retrying a transaction', self.log.getvalue())
示例#5
0
    def testRetriesDisconnectionErrors(self):
        """
        L{Transact.run} retries the transaction if a L{DisconnectionError}
        occurs during a transaction.
        """

        class FakeTransactionManager(object):

            def commit(self):
                self.committed = True

            def abort(self):
                self.aborted = True

        self.flag = False

        def function():
            if not self.flag:
                self.flag = True
                raise DisconnectionError('Disconnected')
            return 'success'

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        result = yield transact.run(function)
        self.assertEqual('success', result)
        self.assertTrue(manager.aborted)
        self.assertTrue(manager.committed)
示例#6
0
    def testRetries(self):
        """
        L{Transact.run} retries the transaction if a DB error occours during
        a transaction collision.
        """

        class FakeTransactionManager(object):

            def commit(self):
                self.committed = True

            def abort(self):
                self.aborted = True

        self.flag = False

        def function():
            if not self.flag:
                self.flag = True
                raise psycopg2.Error('Error')
            return 'success'

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager, lambda seconds: None)
        result = yield transact.run(function)
        self.assertEqual('success', result)
        self.assertTrue(manager.aborted)
        self.assertTrue(manager.committed)
示例#7
0
    def testRunWithFunctionFailure(self):
        """
        If the given function raises an error, then L{Transact.run} aborts the
        transaction and re-raises the same error.
        """
        class FakeTransactionManager(object):
            def abort(self):
                self.aborted = True

        def function():
            raise RuntimeError('Function call exploded!')

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        yield self.assertFailure(transact.run(function), RuntimeError)
        self.assertTrue(manager.aborted)
示例#8
0
    def testRun(self):
        """
        L{Transact.run} executes a function in a thread, commits the
        transaction and returns a C{Deferred} that fires with the function's
        result.
        """
        class FakeTransactionManager(object):
            def commit(self):
                self.committed = True

        def function(a, b=None):
            return a + b

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        result = yield transact.run(function, 1, b=2)
        self.assertEqual(3, result)
        self.assertTrue(manager.committed)
示例#9
0
    def testRunWithFunctionFailure(self):
        """
        If the given function raises an error, then L{Transact.run} aborts the
        transaction and re-raises the same error.
        """

        class FakeTransactionManager(object):

            def abort(self):
                self.aborted = True

        def function():
            raise RuntimeError('Function call exploded!')

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        yield self.assertFailure(transact.run(function), RuntimeError)
        self.assertTrue(manager.aborted)
示例#10
0
    def testRunWithCommitFailure(self):
        """
        If the specified function succeeds but the transaction fails to
        commit, then L{Transact.run} aborts the transaction and re-raises the
        commit exception.
        """
        class FakeTransactionManager(object):
            def abort(self):
                self.aborted = True

            def commit(self):
                raise RuntimeError('Commit exploded!')

        def function():
            pass

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        yield self.assertFailure(transact.run(function), RuntimeError)
        self.assertTrue(manager.aborted)
示例#11
0
    def testRun(self):
        """
        L{Transact.run} executes a function in a thread, commits the
        transaction and returns a C{Deferred} that fires with the function's
        result.
        """

        class FakeTransactionManager(object):

            def commit(self):
                self.committed = True

        def function(a, b=None):
            return a + b

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        result = yield transact.run(function, 1, b=2)
        self.assertEqual(3, result)
        self.assertTrue(manager.committed)
示例#12
0
    def testReturnStormObject(self):
        """
        A C{RuntimeError} is raised if a Storm object is returned from the
        transaction.  Storm objects may only be used in the thread they were
        created in, so this provides some safety checking to prevent strange
        behaviour.
        """
        class FakeTransactionManager(object):
            def abort(self):
                self.aborted = True

        class StormObject(object):

            __storm_table__ = 'storm_object'

        def function():
            return StormObject()

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        yield self.assertFailure(transact.run(function), RuntimeError)
        self.assertTrue(manager.aborted)
示例#13
0
    def testRunWithCommitFailure(self):
        """
        If the specified function succeeds but the transaction fails to
        commit, then L{Transact.run} aborts the transaction and re-raises the
        commit exception.
        """

        class FakeTransactionManager(object):

            def abort(self):
                self.aborted = True

            def commit(self):
                raise RuntimeError('Commit exploded!')

        def function():
            pass

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        yield self.assertFailure(transact.run(function), RuntimeError)
        self.assertTrue(manager.aborted)
示例#14
0
    def testReturnStormObject(self):
        """
        A C{RuntimeError} is raised if a Storm object is returned from the
        transaction.  Storm objects may only be used in the thread they were
        created in, so this provides some safety checking to prevent strange
        behaviour.
        """

        class FakeTransactionManager(object):

            def abort(self):
                self.aborted = True

        class StormObject(object):

            __storm_table__ = 'storm_object'

        def function():
            return StormObject()

        manager = FakeTransactionManager()
        transact = Transact(FakeThreadPool(), manager)
        yield self.assertFailure(transact.run(function), RuntimeError)
        self.assertTrue(manager.aborted)