def test_context_gevent(request): try: import gevent except ImportError: pytest.skip('gevent must be installed') request.addfinalizer(enable_gevent_context()) def f1(): assert Context.logging.flat == {} Context.logging.push({'f1': 1}) Context.track('gevent', 1.0) assert Context.logging.flat == {'f1': 1} assert Context.current().tracking['gevent'].count == 1 gevent.sleep(0.2) # yield to let f2 run assert Context.logging.flat == {'f1': 1} assert Context.current().tracking['gevent'].count == 1 def f2(): assert Context.logging.flat == {} Context.logging.push({'f2': 2}) Context.track('gevent', 1.0) assert Context.current().tracking['gevent'].count == 1 assert Context.logging.flat == {'f2': 2} g1 = gevent.spawn(f1) g2 = gevent.spawn(f2) gevent.joinall([g1, g2], timeout=2) # Resetting Context Context.clear() Context.current().request_id = None Context.current().tracking = {}
def test_context_api(): Context.logging.push(a=1) Context.request_id = 'id' Context.track('test', 1.0) assert Context.current.logging.flat == {'a': 1} assert Context.current.request_id == 'id' assert Context.current.tracking['test'].count == 1 assert Context.current.tracking['test'].time == 1.0 Context.clear() assert Context.current.logging.flat == {} assert Context.current.request_id is None assert Context.current.tracking == {}
def clear_context(): """Helper to clear any thread local contexts.""" import talisker.sentry Context.clear() talisker.sentry.clear()