blob: ae85ceea0e1d512b18ab804252f131490b055e6f [file] [log] [blame]
Austin Schuha9abc032021-01-01 16:46:19 -08001#ifndef AOS_NETWORK_TESTING_TIME_CONVERTER_H_
2#define AOS_NETWORK_TESTING_TIME_CONVERTER_H_
3
4#include <deque>
5#include <optional>
6#include <tuple>
7
8#include "aos/events/event_scheduler.h"
Austin Schuh58646e22021-08-23 23:51:46 -07009#include "aos/events/logging/boot_timestamp.h"
Austin Schuha9abc032021-01-01 16:46:19 -080010#include "aos/network/multinode_timestamp_filter.h"
11#include "aos/time/time.h"
12
13namespace aos {
14namespace message_bridge {
15
16// Simple class to which uses InterpolatedTimeConverter to produce an
17// interpolated timeline. Should only be used for testing.
18class TestingTimeConverter final : public InterpolatedTimeConverter {
19 public:
20 TestingTimeConverter(size_t node_count);
21
22 virtual ~TestingTimeConverter();
23
24 // Starts all nodes equal to the distributed clock.
25 void StartEqual();
26
27 // Elapses each node's clock by the provided duration, and returns the
28 // duration that the distributed clock elapsed by.
29 std::chrono::nanoseconds AddMonotonic(
30 std::vector<monotonic_clock::duration> times);
31
32 // Sets time on each node's clock to the provided times, and returns the
33 // duration that the distributed clock elapsed by. Note: time must always go
34 // forwards.
35 std::chrono::nanoseconds AddMonotonic(
Austin Schuh66168842021-08-17 19:42:21 -070036 std::vector<logger::BootTimestamp> times);
Austin Schuha9abc032021-01-01 16:46:19 -080037
Austin Schuh58646e22021-08-23 23:51:46 -070038 void RebootAt(size_t node_index, distributed_clock::time_point t);
39
Austin Schuha9abc032021-01-01 16:46:19 -080040 // Adds a distributed to monotonic clock mapping to the queue.
41 void AddNextTimestamp(distributed_clock::time_point time,
Austin Schuh66168842021-08-17 19:42:21 -070042 std::vector<logger::BootTimestamp> times);
Austin Schuha9abc032021-01-01 16:46:19 -080043
James Kuszmaulbeaa3c82023-09-07 11:11:27 -070044 std::optional<std::optional<std::tuple<distributed_clock::time_point,
45 std::vector<logger::BootTimestamp>>>>
Austin Schuha9abc032021-01-01 16:46:19 -080046 NextTimestamp() override;
47
Austin Schuh58646e22021-08-23 23:51:46 -070048 void set_boot_uuid(size_t node_index, size_t boot_count, UUID uuid) {
49 CHECK(boot_uuids_
50 .emplace(std::make_pair(node_index, boot_count), std ::move(uuid))
51 .second)
52 << ": Duplicate boot";
53 }
54
55 UUID boot_uuid(size_t node_index, size_t boot_count) override {
56 auto it = boot_uuids_.find(std::make_pair(node_index, boot_count));
57 if (it != boot_uuids_.end()) return it->second;
58
59 auto new_it = boot_uuids_.emplace(std::make_pair(node_index, boot_count),
60 UUID::Random());
61 CHECK(new_it.second);
62 return new_it.first->second;
63 }
64
Austin Schuha9abc032021-01-01 16:46:19 -080065 private:
66 // List of timestamps.
67 std::deque<std::tuple<distributed_clock::time_point,
Austin Schuh66168842021-08-17 19:42:21 -070068 std::vector<logger::BootTimestamp>>>
Austin Schuha9abc032021-01-01 16:46:19 -080069 ts_;
70
71 // True if there is no time queued.
72 bool first_ = true;
73 // The last times returned on all clocks.
74 distributed_clock::time_point last_distributed_ = distributed_clock::epoch();
Austin Schuh66168842021-08-17 19:42:21 -070075 std::vector<logger::BootTimestamp> last_monotonic_;
Austin Schuh58646e22021-08-23 23:51:46 -070076
77 std::map<std::pair<size_t, size_t>, UUID> boot_uuids_;
Austin Schuha9abc032021-01-01 16:46:19 -080078};
79
80} // namespace message_bridge
81} // namespace aos
82
83#endif // AOS_NETWORK_TESTING_TIME_CONVERTER_H_