Introduce a new doAfterPendingMessages helper to wait until we have no more pending messages before doing an action.