xref: /aosp_15_r20/external/cronet/net/test/event_waiter.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2015 The Chromium Authors
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 NET_TEST_EVENT_WAITER_H_
6 #define NET_TEST_EVENT_WAITER_H_
7 
8 #include "base/run_loop.h"
9 
10 namespace net {
11 
12 // Helper class to run a RunLoop until an expected event is reported.
13 template <typename Event>
14 class EventWaiter {
15  public:
16   // Runs a RunLoop until NotifyEvent() is called with |event|.
WaitForEvent(Event event)17   void WaitForEvent(Event event) {
18     expected_event_ = event;
19     base::RunLoop run_loop;
20     quit_closure_ = run_loop.QuitClosure();
21     run_loop.Run();
22   }
23 
24   // Unblocks a WaitForEvent() call if it was called with |event|. Otherwise,
25   // has no effect.
NotifyEvent(Event event)26   void NotifyEvent(Event event) {
27     if (!quit_closure_.is_null() && event == expected_event_)
28       std::move(quit_closure_).Run();
29   }
30 
31  private:
32   Event expected_event_;
33   base::OnceClosure quit_closure_;
34 };
35 
36 }  // namespace net
37 
38 #endif  // NET_TEST_EVENT_WAITER_H_
39