def test_set_context(self): # the Context can be set in the current Thread ctx = Context() local = ThreadLocalContext() ok_(local.get() is not ctx) local.set(ctx) ok_(local.get() is ctx)
def test_set_context(self): # the Context can be set in the current Thread ctx = Context() local = ThreadLocalContext() assert local.get() is not ctx local.set(ctx) assert local.get() is ctx
def test_multiple_threads_multiple_context(self): # each thread should have it's own Context l_ctx = ThreadLocalContext() def _fill_ctx(): ctx = l_ctx.get() span = Span(tracer=None, name='fake_span') ctx.add_span(span) assert 1 == len(ctx._trace) threads = [threading.Thread(target=_fill_ctx) for _ in range(100)] for t in threads: t.daemon = True t.start() for t in threads: t.join() # the main instance should have an empty Context # because it has not been used in this thread ctx = l_ctx.get() assert 0 == len(ctx._trace)
def test_multiple_threads_multiple_context(self): # each thread should have it's own Context l_ctx = ThreadLocalContext() def _fill_ctx(): ctx = l_ctx.get() span = Span(tracer=None, name='fake_span') ctx.add_span(span) eq_(1, len(ctx._trace)) threads = [threading.Thread(target=_fill_ctx) for _ in range(100)] for t in threads: t.daemon = True t.start() for t in threads: t.join() # the main instance should have an empty Context # because it has not been used in this thread ctx = l_ctx.get() eq_(0, len(ctx._trace))
def test_get_or_create(self): # asking the Context multiple times should return # always the same instance l_ctx = ThreadLocalContext() assert l_ctx.get() == l_ctx.get()
def test_get_or_create(self): # asking the Context multiple times should return # always the same instance l_ctx = ThreadLocalContext() eq_(l_ctx.get(), l_ctx.get())