Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
self.mappers = setup_mappers()
metadata.drop_all(engine)
metadata.create_all(engine)
self.tm1 = transaction.TransactionManager()
self.tm2 = transaction.TransactionManager()
# With psycopg2 you might supply isolation_level='SERIALIZABLE' here,
# unfortunately that is not supported by cx_Oracle.
e1 = sa.create_engine(TEST_DSN)
e2 = sa.create_engine(TEST_DSN)
self.s1 = orm.sessionmaker(bind=e1, twophase=TEST_TWOPHASE)()
tx.register(self.s1, transaction_manager=self.tm1)
self.s2 = orm.sessionmaker(bind=e2, twophase=TEST_TWOPHASE)()
tx.register(self.s2, transaction_manager=self.tm2)
self.tm1.begin()
self.s1.add(User(id=1, firstname="udo", lastname="juergens"))
self.tm1.commit()
def test_proxy(self):
config = {
"endpoints": {"https://app.datadoghq.com": ["foo"]},
"proxy_settings": {
"host": "localhost",
"port": PROXY_PORT,
"user": None,
"password": None
}
}
app = Application()
app.skip_ssl_validation = True
app._agentConfig = config
trManager = TransactionManager(MAX_WAIT_FOR_REPLAY, MAX_QUEUE_SIZE, THROTTLING_DELAY)
trManager._flush_without_ioloop = True # Use blocking API to emulate tornado ioloop
CustomAgentTransaction.set_tr_manager(trManager)
app.use_simple_http_client = False # We need proxy capabilities
app.agent_dns_caching = False
# _test is the instance of this class. It is needed to call the method stop() and deal with the asynchronous
# calls as described here : http://www.tornadoweb.org/en/stable/testing.html
CustomAgentTransaction._test = self
CustomAgentTransaction.set_application(app)
CustomAgentTransaction.set_endpoints(config['endpoints'])
CustomAgentTransaction('body', {}, "") # Create and flush the transaction
self.wait()
del CustomAgentTransaction._test
access_log = self.docker_client.exec_start(
self.docker_client.exec_create(CONTAINER_NAME, 'cat /var/log/squid/access.log')['Id'])
self.assertTrue("CONNECT" in access_log) # There should be an entry in the proxy access log
def test_creates_request(self, monkeypatch):
registry = pretend.stub()
pyramid_env = {"request": pretend.stub()}
monkeypatch.setattr(scripting, "prepare", lambda *a, **k: pyramid_env)
obj = tasks.WarehouseTask()
obj.app.pyramid_config = pretend.stub(registry=registry)
request = obj.get_request()
assert obj.request.pyramid_env == pyramid_env
assert request is pyramid_env["request"]
assert isinstance(request.tm, transaction.TransactionManager)
def test_parallelism(self):
step = 4
trManager = TransactionManager(timedelta(seconds=0), MAX_QUEUE_SIZE,
timedelta(seconds=0), max_parallelism=step,
max_endpoint_errors=100)
for i in xrange(step):
trManager.append(SleepingTransaction(trManager))
trManager.flush()
self.assertEqual(trManager._running_flushes, step)
self.assertEqual(trManager._finished_flushes, 0)
# If _trs_to_flush != None, it means that it's still running as it should be
self.assertEqual(trManager._trs_to_flush, [])
time.sleep(1)
# It should be finished
self.assertEqual(trManager._running_flushes, 0)
self.assertEqual(trManager._finished_flushes, step)
self.assertIs(trManager._trs_to_flush, None)
def test_failed_abort_reraises(self):
config = self.config
tm = DummyTransaction(finish_with_exc=ValueError)
config.add_settings({'tm.manager_hook': lambda r: tm})
config.add_settings({'tm.commit_veto': lambda req, resp: True})
config.add_view(lambda r: 'ok', renderer='string')
app = self._makeApp()
self.assertRaises(ValueError, lambda: app.get('/'))
class Dummy(object):
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
class DummyTransaction(TransactionManager):
began = False
committed = False
aborted = False
_resources = []
user = None
def __init__(self, doomed=False, retryable=False, finish_with_exc=None):
self.doomed = doomed
self.began = 0
self.committed = 0
self.aborted = 0
self.retryable = retryable
self.active = False
self.finish_with_exc = finish_with_exc
def _retryable(self, t, v):
def get_request(self):
if not hasattr(self.request, "pyramid_env"):
registry = self.app.pyramid_config.registry
env = pyramid.scripting.prepare(registry=registry)
env["request"].tm = transaction.TransactionManager(explicit=True)
self.request.update(pyramid_env=env)
return self.request.pyramid_env["request"]
def __init__(self, port, agentConfig, watchdog=True):
self._port = int(port)
self._agentConfig = agentConfig
self._metrics = {}
MetricTransaction.set_application(self)
MetricTransaction.set_endpoints()
self._tr_manager = TransactionManager(MAX_WAIT_FOR_REPLAY,
MAX_QUEUE_SIZE, THROTTLING_DELAY)
MetricTransaction.set_tr_manager(self._tr_manager)
self._watchdog = None
if watchdog:
watchdog_timeout = TRANSACTION_FLUSH_INTERVAL * WATCHDOG_INTERVAL_MULTIPLIER
self._watchdog = Watchdog(watchdog_timeout,
max_mem_mb=agentConfig.get('limit_memory_consumption', None))
request = Request.blank(path, base_url=base_url)
request.registry = registry
request.user = None
request.view_name = ''
# This will create request.tm, others
apply_request_extensions(request)
if dbsession:
# Use the provided dbsession for this request
request.dbsession = dbsession
if hasattr(dbsession, "transaction_manager"):
request.tm = request.transaction_manager = dbsession.transaction_manager
else:
# Create a new dbsession and transaction manager for this request
tm = TransactionManager()
dbsession = create_dbsession(request.registry, tm)
request.dbsession = dbsession
request.tm = request.transaction_manager = tm
def terminate_session(request):
# Close db session at the end of the request and return the db connection back to the pool
dbsession.close()
request.add_finished_callback(terminate_session)
return request
def exec_eager(self, *args, **kwargs):
"""Run transaction aware task in eager mode."""
# We are run in a post-commit hook, so there is no transaction manager available
tm = TransactionManager()
# Do not attempt any transaction retries in eager mode
tm.retry_attempt_count = 1
self.request.update(request=self.make_faux_request(tm=tm))
with tm:
# This doesn't do transaction retry attempts, but should be good enough for eager
return self.run(*args, **kwargs)
def exec_eager(self, *args, **kwargs):
"""Run transaction aware task in eager mode."""
# We are run in a post-commit hook, so there is no transaction manager available
tm = TransactionManager()
# Do not attempt any transaction retries in eager mode
tm.retry_attempt_count = 1
self.request.update(request=self.make_faux_request(tm=tm))
return self.run(*args, **kwargs)