COR-299: Add a test to cleanup the thread local only when it is needed