aboutsummaryrefslogtreecommitdiff
path: root/platform/test/fake_clock.h
blob: 8847a6874bb341a64df7c05a1203acf7b7da6918 (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
41
42
// Copyright 2018 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 PLATFORM_TEST_FAKE_CLOCK_H_
#define PLATFORM_TEST_FAKE_CLOCK_H_

#include <atomic>
#include <vector>

#include "platform/api/time.h"

namespace openscreen {
namespace platform {

class FakeTaskRunner;

class FakeClock {
 public:
  explicit FakeClock(Clock::time_point start_time);
  ~FakeClock();

  void Advance(Clock::duration delta);

  static Clock::time_point now() noexcept;

 protected:
  friend class FakeTaskRunner;

  void SubscribeToTimeChanges(FakeTaskRunner* task_runner);
  void UnsubscribeFromTimeChanges(FakeTaskRunner* task_runner);

 private:
  std::vector<FakeTaskRunner*> task_runners_;

  static std::atomic<Clock::time_point> now_;
};

}  // namespace platform
}  // namespace openscreen

#endif  // PLATFORM_TEST_FAKE_CLOCK_H_