Avoiding race conditions in tests