aboutsummaryrefslogtreecommitdiff
path: root/pw_async/public/pw_async/fake_dispatcher.h
blob: 6a7e5ab31874f0b918b5b94b1deb3d95f4719f03 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2022 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once

#include "pw_async/dispatcher.h"
#include "pw_async_backend/fake_dispatcher.h"  // nogncheck

namespace pw::async::test {

// FakeDispatcher is a facade for an implementation of Dispatcher that is used
// in unit tests. FakeDispatcher uses simulated time. RunUntil() and RunFor()
// advance time immediately, and now() returns the current simulated time.
//
// To support various Task backends, FakeDispatcher wraps a
// backend::NativeFakeDispatcher that implements standard FakeDispatcher
// behavior using backend::NativeTask objects.
class FakeDispatcher final : public Dispatcher {
 public:
  FakeDispatcher() : native_dispatcher_(*this) {}

  // Stop processing tasks. After calling RequestStop(), the next time the
  // Dispatcher is run, all waiting Tasks will be dequeued and their
  // TaskFunctions called with a PW_STATUS_CANCELLED status.
  void RequestStop() override { native_dispatcher_.RequestStop(); }

  // Post caller owned |task|.
  void Post(Task& task) override { native_dispatcher_.Post(task); }

  // Post caller owned |task| to be run after |delay|.
  void PostAfter(Task& task, chrono::SystemClock::duration delay) override {
    native_dispatcher_.PostAfter(task, delay);
  }

  // Post caller owned |task| to be run at |time|.
  void PostAt(Task& task, chrono::SystemClock::time_point time) override {
    native_dispatcher_.PostAt(task, time);
  }

  // Post caller owned |task| to be run immediately then rerun at a regular
  // |interval|.
  void PostPeriodic(Task& task,
                    chrono::SystemClock::duration interval) override {
    native_dispatcher_.PostPeriodic(task, interval);
  }
  // Post caller owned |task| to be run at |start_time| then rerun at a regular
  // |interval|.
  void PostPeriodicAt(Task& task,
                      chrono::SystemClock::duration interval,
                      chrono::SystemClock::time_point start_time) override {
    native_dispatcher_.PostPeriodicAt(task, interval, start_time);
  }

  // Returns true if |task| is succesfully canceled.
  // If cancelation fails, the task may be running or completed.
  // Periodic tasks may run once more after they are canceled.
  bool Cancel(Task& task) override { return native_dispatcher_.Cancel(task); }

  // Execute all runnable tasks and return without advancing simulated time.
  void RunUntilIdle() override { native_dispatcher_.RunUntilIdle(); }

  // Run the Dispatcher until Now() has reached `end_time`, executing all tasks
  // that come due before then.
  void RunUntil(chrono::SystemClock::time_point end_time) override {
    native_dispatcher_.RunUntil(end_time);
  }

  // Run the Dispatcher until `duration` has elapsed, executing all tasks that
  // come due in that period.
  void RunFor(chrono::SystemClock::duration duration) override {
    native_dispatcher_.RunFor(duration);
  }

  // VirtualSystemClock overrides:

  chrono::SystemClock::time_point now() override {
    return native_dispatcher_.now();
  }

  // Returns the inner NativeFakeDispatcher containing backend-specific
  // state/logic. Only non-portable code should call these methods!
  backend::NativeFakeDispatcher& native_type() { return native_dispatcher_; }
  const backend::NativeFakeDispatcher& native_type() const {
    return native_dispatcher_;
  }

 private:
  backend::NativeFakeDispatcher native_dispatcher_;
};

}  // namespace pw::async::test