event_waiter.h 996 B

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