Fix sometimes failing tests (fixes #1407).