Factor out test code into wait_for_message