aboutsummaryrefslogtreecommitdiff
path: root/include/weave/provider/test/fake_task_runner.h
blob: 9a06a8e129e09f5616a1f9cefd5405461c44f999 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2015 The Weave 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 LIBWEAVE_INCLUDE_WEAVE_PROVIDER_TEST_FAKE_TASK_RUNNER_H_
#define LIBWEAVE_INCLUDE_WEAVE_PROVIDER_TEST_FAKE_TASK_RUNNER_H_

#include <weave/provider/task_runner.h>

#include <algorithm>
#include <queue>
#include <memory>
#include <utility>
#include <vector>

#include <base/time/clock.h>

namespace weave {
namespace provider {
namespace test {

class FakeTaskRunner : public TaskRunner {
 public:
  FakeTaskRunner();
  ~FakeTaskRunner() override;

  void PostDelayedTask(const tracked_objects::Location& from_here,
                       const base::Closure& task,
                       base::TimeDelta delay) override;

  bool RunOnce();
  void Run(size_t number_of_iterations = 1000);
  void Break();
  base::Clock* GetClock();
  size_t GetTaskQueueSize() const;

 private:
  using QueueItem = std::pair<std::pair<base::Time, size_t>, base::Closure>;

  struct Greater {
    bool operator()(const QueueItem& a, const QueueItem& b) const {
      return a.first > b.first;
    }
  };

  bool break_{false};
  size_t counter_{0};  // Keeps order of tasks with the same time.

  class TestClock;
  std::unique_ptr<TestClock> test_clock_;

  std::priority_queue<QueueItem,
                      std::vector<QueueItem>,
                      FakeTaskRunner::Greater>
      queue_;
};

}  // namespace test
}  // namespace provider
}  // namespace weave

#endif  // LIBWEAVE_INCLUDE_WEAVE_PROVIDER_TEST_FAKE_TASK_RUNNER_H_