示例#1
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())
示例#2
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)
示例#3
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)
示例#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 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)
示例#6
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)
示例#7
0
 def setUp(self):
     super(FacadeUserMixinTest, self).setUp()
     self.transact = Transact(self.threadPool)
     factory = FluidinfoSessionFactory('API-9000')
     self.facade = Facade(self.transact, factory)
     system = createSystemData()
     self.admin = system.users[u'fluiddb']
示例#8
0
 def setUp(self):
     super(FacadeCheckerTest, self).setUp()
     factory = FluidinfoSessionFactory('API-9000')
     transact = Transact(self.threadPool)
     createSystemData()
     self.checker = FacadeChecker()
     self.checker.facadeClient = Facade(transact, factory)
示例#9
0
 def testWBDefaultTransactionManager(self):
     """
     By default L{Transact} uses the C{transaction} package as the default
     transaction manager.
     """
     transact = Transact(FakeThreadPool())
     self.assertIdentical(transaction, transact._transaction)
示例#10
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)
示例#11
0
 def setUp(self):
     super(RootResourceTest, self).setUp()
     factory = FluidinfoSessionFactory('API-9000')
     transact = Transact(self.threadPool)
     createSystemData()
     self.checker = AnonymousChecker()
     self.checker.facadeClient = Facade(transact, factory)
     getConfig().set('service', 'allow-anonymous-access', 'False')
示例#12
0
 def setUp(self):
     super(OAuthEchoResourceTest, self).setUp()
     self.agent = Agent(self.FakeReactor())
     self.transact = Transact(self.threadPool)
     system = createSystemData()
     self.anonymous = system.users[u'anon']
     OAuthConsumerAPI().register(self.anonymous)
     self.agent = Agent(self.FakeReactor())
示例#13
0
 def setUp(self):
     super(RecentObjectsActivityResourceTest, self).setUp()
     createSystemData()
     UserAPI().create([(u'username', u'password', u'User',
                        u'*****@*****.**')])
     self.user = getUser(u'username')
     factory = FluidinfoSessionFactory('API-9000')
     self.transact = Transact(self.threadPool)
     self.facade = Facade(self.transact, factory)
示例#14
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)
示例#15
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)
示例#16
0
 def setUp(self):
     super(FacadeTagMixinTest, self).setUp()
     createSystemData()
     self.transact = Transact(self.threadPool)
     factory = FluidinfoSessionFactory('API-9000')
     self.facade = Facade(self.transact, factory)
     UserAPI().create([(u'username', u'password', u'User',
                        u'*****@*****.**')])
     self.user = getUser(u'username')
     self.permissions = CachingPermissionAPI(self.user)
示例#17
0
 def setUp(self):
     super(VerifyUserPasswordResourceTest, self).setUp()
     self.transact = Transact(self.threadPool)
     createSystemData()
     UserAPI().create([
         (u'fluidinfo.com', 'secret', u'Fluidinfo', u'*****@*****.**'),
         (u'user', u'pass', u'Peter Parker', u'*****@*****.**')])
     consumer = getUser(u'anon')
     OAuthConsumerAPI().register(consumer)
     self.store.commit()
示例#18
0
 def setUp(self):
     super(FacadePermissionTest, self).setUp()
     self.transact = Transact(FakeThreadPool())
     factory = FluidinfoSessionFactory('API-9000')
     self.facade = Facade(self.transact, factory)
     createSystemData()
     UserAPI().create([(u'username', u'password', u'User',
                        u'*****@*****.**')])
     user = getUser(u'username')
     self.permissions = CachingPermissionAPI(user)
     SecureTagAPI(user).create([(u'username/tag', u'description')])
示例#19
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)
示例#20
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)
示例#21
0
def setupFacade(config):
    """Get the L{Facade} instance to use in the API service."""
    from fluiddb.api.facade import Facade
    from fluiddb.util.transact import Transact

    maxThreads = int(config.get('service', 'max-threads'))
    threadpool = ThreadPool(minthreads=0, maxthreads=maxThreads)
    reactor.callWhenRunning(threadpool.start)
    reactor.addSystemEventTrigger('during', 'shutdown', threadpool.stop)
    transact = Transact(threadpool)
    factory = FluidinfoSessionFactory('API-%s' % config.get('service', 'port'))
    return Facade(transact, factory)
示例#22
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)
示例#23
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)
示例#24
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)
示例#25
0
 def setUp(self):
     super(FacadeUserCreationTest, self).setUp()
     factory = FluidinfoSessionFactory('API-9000')
     self.transact = Transact(self.threadPool)
     self.facade = Facade(self.transact, factory)
     self.system = createSystemData()
示例#26
0
 def setUp(self):
     super(RenewOAuthTokenResourceTest, self).setUp()
     self.transact = Transact(self.threadPool)
     createSystemData()
示例#27
0
 def setUp(self):
     super(FacadeAuthenticateUserWithPasswordTest, self).setUp()
     factory = FluidinfoSessionFactory('API-9000')
     self.transact = Transact(self.threadPool)
     self.facade = Facade(self.transact, factory)
     self.system = createSystemData()
示例#28
0
 def setUp(self):
     super(FluidinfoSessionFactoryTest, self).setUp()
     self.transact = Transact(self.threadPool)
示例#29
0
 def setUp(self):
     super(TransactPluginTest, self).setUp()
     self.transact = Transact(self.threadPool, sleep=lambda seconds: None)
示例#30
0
 def setUp(self):
     super(LoggingPluginTest, self).setUp()
     self.transact = Transact(self.threadPool)
示例#31
0
 def setUp(self):
     super(DelegatorTest, self).setUp()
     self.agent = Agent(self.FakeReactor())
     self.transact = Transact(self.threadPool)
     createSystemData()