Refactor tests to make them less likely to have timing issues

Change-Id: Idf24ab4f7c2c48f8246f0c38906d2b8a46f46587