1 // Copyright 2020 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TESTING_UTIL_TASK_UTIL_H_ 6 #define TESTING_UTIL_TASK_UTIL_H_ 7 8 #include <thread> 9 10 #include "gtest/gtest.h" 11 #include "platform/api/time.h" 12 #include "util/osp_logging.h" 13 14 namespace openscreen { 15 16 template <typename Cond> 17 void WaitForCondition(Cond condition, 18 Clock::duration delay = std::chrono::milliseconds(250), 19 int max_attempts = 8) { 20 int attempts = 1; 21 do { 22 OSP_LOG_INFO << "--- Checking condition, attempt " << attempts << "/" 23 << max_attempts; 24 if (condition()) { 25 break; 26 } 27 std::this_thread::sleep_for(delay); 28 } while (attempts++ < max_attempts); 29 ASSERT_TRUE(condition()); 30 } 31 32 } // namespace openscreen 33 34 #endif // TESTING_UTIL_TASK_UTIL_H_ 35