fake_metronome.h (1948B)
1 /* 2 * Copyright (c) 2022 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_METRONOME_TEST_FAKE_METRONOME_H_ 12 #define API_METRONOME_TEST_FAKE_METRONOME_H_ 13 14 #include <cstddef> 15 #include <vector> 16 17 #include "absl/functional/any_invocable.h" 18 #include "api/metronome/metronome.h" 19 #include "api/units/time_delta.h" 20 21 namespace webrtc::test { 22 23 // ForcedTickMetronome is a Metronome that ticks when `Tick()` is invoked. 24 // The constructor argument `tick_period` returned in `TickPeriod()`. 25 class ForcedTickMetronome : public Metronome { 26 public: 27 explicit ForcedTickMetronome(TimeDelta tick_period); 28 29 // Forces all TickListeners to run `OnTick`. 30 void Tick(); 31 size_t NumListeners(); 32 33 // Metronome implementation. 34 void RequestCallOnNextTick(absl::AnyInvocable<void() &&> callback) override; 35 TimeDelta TickPeriod() const override; 36 37 private: 38 const TimeDelta tick_period_; 39 std::vector<absl::AnyInvocable<void() &&>> callbacks_; 40 }; 41 42 // FakeMetronome is a metronome that ticks based on a repeating task at the 43 // `tick_period` provided in the constructor. It is designed for use with 44 // simulated task queues for unit tests. 45 class FakeMetronome : public Metronome { 46 public: 47 explicit FakeMetronome(TimeDelta tick_period); 48 49 void SetTickPeriod(TimeDelta tick_period); 50 51 // Metronome implementation. 52 void RequestCallOnNextTick(absl::AnyInvocable<void() &&> callback) override; 53 TimeDelta TickPeriod() const override; 54 55 private: 56 TimeDelta tick_period_; 57 std::vector<absl::AnyInvocable<void() &&>> callbacks_; 58 }; 59 60 } // namespace webrtc::test 61 62 #endif // API_METRONOME_TEST_FAKE_METRONOME_H_