def foo(): nose.tools.eq_( len(gcm.stack), 1, "Context stack length is wrong (got {}, should be 1)".format(len(gcm.stack)) ) nose.tools.eq_(str(gcm.get_context().tracer_id), tid1, "Tracer id does not match") # Wrap execution of bar inside the second context: gcm.run_with_new_context(bar, tid2)
def test_concurrent_threads(self): """ Each thread should have its own context stack. """ tid1 = "d551573a-01dc-41b2-b197-ea8afb7fbac1".replace("-", "") tid2 = "6c006821-0cb1-42e8-8ab1-fb6e059cabab".replace("-", "") stacks = [] tids = [] def append_to_context(): # print('@gcs=0x{:0X} @gcs.tracer_id=0x{:0X}'.format(id(gcs), id(gcs.tracer_id))) stacks.append(id(gcm.stack)) tids.append(gcm.get_context().tracer_id) gcm.run_with_new_context(append_to_context, tracer_id=tid1) p = partial(gcm.run_with_new_context, append_to_context, tracer_id=tid2) t = threading.Thread(target=p) t.start() t.join() nose.tools.assert_not_equal(stacks[0], stacks[1], "Both threads are sharing the same stack") nose.tools.assert_not_equal(tids[0], tids[1], "Tracer ids are the same")