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())
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)
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)
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']
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)
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)
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)
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')
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())
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)
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)
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)
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()
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')])
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)
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)
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)
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()
def setUp(self): super(RenewOAuthTokenResourceTest, self).setUp() self.transact = Transact(self.threadPool) createSystemData()
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()
def setUp(self): super(FluidinfoSessionFactoryTest, self).setUp() self.transact = Transact(self.threadPool)
def setUp(self): super(TransactPluginTest, self).setUp() self.transact = Transact(self.threadPool, sleep=lambda seconds: None)
def setUp(self): super(LoggingPluginTest, self).setUp() self.transact = Transact(self.threadPool)
def setUp(self): super(DelegatorTest, self).setUp() self.agent = Agent(self.FakeReactor()) self.transact = Transact(self.threadPool) createSystemData()