Fix potential race condition in test