Improve test to avoid spurious failures