summaryrefslogtreecommitdiffstats
path: root/net/test/event_waiter.h
blob: 682e6f5872287e97c5567dce0d73ac57b3f5fd93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_TEST_EVENT_WAITER_H_
#define NET_TEST_EVENT_WAITER_H_

#include "base/run_loop.h"

namespace net {

// Helper class to run a RunLoop until an expected event is reported.
template <typename Event>
class EventWaiter {
 public:
  // Runs a RunLoop until NotifyEvent() is called with |event|.
  void WaitForEvent(Event event) {
    expected_event_ = event;
    base::RunLoop run_loop;
    quit_closure_ = run_loop.QuitClosure();
    run_loop.Run();
  }

  // Unblocks a WaitForEvent() call if it was called with |event|. Otherwise,
  // has no effect.
  void NotifyEvent(Event event) {
    if (!quit_closure_.is_null() && event == expected_event_) {
      quit_closure_.Run();
      quit_closure_.Reset();
    }
  }

 private:
  Event expected_event_;
  base::Closure quit_closure_;
};

}  // namespace net

#endif  // NET_TEST_EVENT_WAITER_H_