1 // Copyright 2023 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 QUICHE_QUIC_TOOLS_QUIC_EVENT_LOOP_TOOLS_H_ 6 #define QUICHE_QUIC_TOOLS_QUIC_EVENT_LOOP_TOOLS_H_ 7 8 #include "absl/base/attributes.h" 9 #include "quiche/quic/core/io/quic_event_loop.h" 10 #include "quiche/quic/core/quic_clock.h" 11 #include "quiche/quic/core/quic_time.h" 12 #include "quiche/common/quiche_callbacks.h" 13 14 namespace quic { 15 16 inline constexpr QuicTimeDelta kDefaultTimeoutForTools = 17 QuicTimeDelta::FromSeconds(3); 18 inline constexpr QuicTimeDelta kDefaultEventLoopTimeoutForTools = 19 QuicTimeDelta::FromMilliseconds(50); 20 21 // Runs the event loop until the specified callback returns true, or until the 22 // timeout occurs. Returns true if callback returned true at least once. 23 ABSL_MUST_USE_RESULT inline bool ProcessEventsUntil( 24 QuicEventLoop* event_loop, quiche::UnretainedCallback<bool()> callback, 25 QuicTimeDelta timeout = kDefaultTimeoutForTools) { 26 const QuicClock* clock = event_loop->GetClock(); 27 QuicTime start = clock->Now(); 28 while (!callback()) { 29 event_loop->RunEventLoopOnce(kDefaultEventLoopTimeoutForTools); 30 QuicTimeDelta elapsed = clock->Now() - start; 31 if (elapsed >= timeout) { 32 return false; 33 } 34 } 35 return true; 36 } 37 38 } // namespace quic 39 40 #endif // QUICHE_QUIC_TOOLS_QUIC_EVENT_LOOP_TOOLS_H_ 41