blob: 768d0a29be533d9a5f1e72c36296d3921295220e [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080013namespace aos::message_bridge {
Austin Schuha9abc032021-01-01 16:46:19 -080014
15// Simple class to which uses InterpolatedTimeConverter to produce an
16// interpolated timeline. Should only be used for testing.
17class TestingTimeConverter final : public InterpolatedTimeConverter {
18 public:
19 TestingTimeConverter(size_t node_count);
20
21 virtual ~TestingTimeConverter();
22
23 // Starts all nodes equal to the distributed clock.
24 void StartEqual();
25
26 // Elapses each node's clock by the provided duration, and returns the
27 // duration that the distributed clock elapsed by.
28 std::chrono::nanoseconds AddMonotonic(
29 std::vector<monotonic_clock::duration> times);
30
31 // Sets time on each node's clock to the provided times, and returns the
32 // duration that the distributed clock elapsed by. Note: time must always go
33 // forwards.
34 std::chrono::nanoseconds AddMonotonic(
Austin Schuh66168842021-08-17 19:42:21 -070035 std::vector<logger::BootTimestamp> times);
Austin Schuha9abc032021-01-01 16:46:19 -080036
Austin Schuh58646e22021-08-23 23:51:46 -070037 void RebootAt(size_t node_index, distributed_clock::time_point t);
38
Austin Schuha9abc032021-01-01 16:46:19 -080039 // Adds a distributed to monotonic clock mapping to the queue.
40 void AddNextTimestamp(distributed_clock::time_point time,
Austin Schuh66168842021-08-17 19:42:21 -070041 std::vector<logger::BootTimestamp> times);
Austin Schuha9abc032021-01-01 16:46:19 -080042
James Kuszmaulbeaa3c82023-09-07 11:11:27 -070043 std::optional<std::optional<std::tuple<distributed_clock::time_point,
44 std::vector<logger::BootTimestamp>>>>
Austin Schuha9abc032021-01-01 16:46:19 -080045 NextTimestamp() override;
46
Austin Schuh58646e22021-08-23 23:51:46 -070047 void set_boot_uuid(size_t node_index, size_t boot_count, UUID uuid) {
48 CHECK(boot_uuids_
49 .emplace(std::make_pair(node_index, boot_count), std ::move(uuid))
50 .second)
51 << ": Duplicate boot";
52 }
53
54 UUID boot_uuid(size_t node_index, size_t boot_count) override {
55 auto it = boot_uuids_.find(std::make_pair(node_index, boot_count));
56 if (it != boot_uuids_.end()) return it->second;
57
58 auto new_it = boot_uuids_.emplace(std::make_pair(node_index, boot_count),
59 UUID::Random());
60 CHECK(new_it.second);
61 return new_it.first->second;
62 }
63
Austin Schuha9abc032021-01-01 16:46:19 -080064 private:
65 // List of timestamps.
66 std::deque<std::tuple<distributed_clock::time_point,
Austin Schuh66168842021-08-17 19:42:21 -070067 std::vector<logger::BootTimestamp>>>
Austin Schuha9abc032021-01-01 16:46:19 -080068 ts_;
69
70 // True if there is no time queued.
71 bool first_ = true;
72 // The last times returned on all clocks.
73 distributed_clock::time_point last_distributed_ = distributed_clock::epoch();
Austin Schuh66168842021-08-17 19:42:21 -070074 std::vector<logger::BootTimestamp> last_monotonic_;
Austin Schuh58646e22021-08-23 23:51:46 -070075
76 std::map<std::pair<size_t, size_t>, UUID> boot_uuids_;
Austin Schuha9abc032021-01-01 16:46:19 -080077};
78
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080079} // namespace aos::message_bridge
Austin Schuha9abc032021-01-01 16:46:19 -080080
81#endif // AOS_NETWORK_TESTING_TIME_CONVERTER_H_