Rewrite test to avoid race condition.