Fix race condition when tearing down test case