File: fake_task_runner.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (74 lines) | stat: -rw-r--r-- 2,136 bytes parent folder | download | duplicates (5)
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
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019 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_TASK_RUNNER_H_
#define PLATFORM_TEST_FAKE_TASK_RUNNER_H_

#include <map>
#include <vector>

#include "platform/api/task_runner.h"
#include "platform/api/time.h"
#include "platform/test/fake_clock.h"

namespace openscreen {

// Usage:
//
//   #include ".../gtest.h"
//
//   class FooTest : public testing::Test {
//    public:
//     FakeClock* clock() { return &clock_; }
//     FakeTaskRunner* task_runner() { return &task_runner_; }
//
//    private:
//     FakeClock clock_{Clock::now()};
//     FakeTaskRunner task_runner_{&clock_};
//   };
//
//   TEST_F(FooTest, RunsTask) {
//     Foo foo(task_runner());
//     foo.DoSomethingToPostATask();
//     task_runner()->RunTasksUntilIdle();
//     // Alternatively: clock()->Advance(std::chrono::seconds(0));
//   }
//
//   TEST_F(FooTest, RunsDelayedTask) {
//     Foo foo(task_runner());
//     foo.DoSomethingInOneSecond();  // Schedules 1-second delayed task.
//     clock()->Advance(std::chrono::seconds(3));  // Delayed Task runs here!
//   }
class FakeTaskRunner : public TaskRunner {
 public:
  using Task = TaskRunner::Task;

  explicit FakeTaskRunner(FakeClock* clock);
  ~FakeTaskRunner() override;

  // Runs all ready-to-run tasks.
  void RunTasksUntilIdle();

  // TaskRunner implementation.
  void PostPackagedTask(Task task) override;
  void PostPackagedTaskWithDelay(Task task, Clock::duration delay) override;
  bool IsRunningOnTaskRunner() override;

  int ready_task_count() const { return ready_to_run_tasks_.size(); }
  int delayed_task_count() const { return delayed_tasks_.size(); }

  // Returns the time at which the next task is scheduled to run, or
  // Clock::time_point::max() if there is none scheduled.
  Clock::time_point GetResumeTime() const;

 private:
  FakeClock* const clock_;

  std::vector<Task> ready_to_run_tasks_;
  std::multimap<Clock::time_point, Task> delayed_tasks_;
};

}  // namespace openscreen

#endif  // PLATFORM_TEST_FAKE_TASK_RUNNER_H_