Refactor test to avoid random timing-related failures.