Fix race conditions in tests