blob: 3bcf6ca3dd15bd92057609b5be2f114479079c53 [file] [log] [blame]
Austin Schuhbe69cf32020-08-27 11:38:33 -07001#include "aos/events/event_scheduler.h"
2
3#include <chrono>
4
5#include "gtest/gtest.h"
6
7namespace aos {
8
9namespace chrono = std::chrono;
10
Austin Schuh87dd3832021-01-01 23:07:31 -080011// Legacy time converter for keeping old tests working. Has numerical precision
12// problems.
13class SlopeOffsetTimeConverter final : public TimeConverter {
14 public:
15 SlopeOffsetTimeConverter(size_t nodes_count)
16 : distributed_offset_(nodes_count, std::chrono::seconds(0)),
17 distributed_slope_(nodes_count, 1.0) {}
18
19 // Sets the offset between the distributed and monotonic clock.
20 // monotonic = distributed * slope + offset;
21 void SetDistributedOffset(size_t node_index,
22 std::chrono::nanoseconds distributed_offset,
23 double distributed_slope) {
24 distributed_offset_[node_index] = distributed_offset;
25 distributed_slope_[node_index] = distributed_slope;
26 }
27
28 distributed_clock::time_point ToDistributedClock(
29 size_t node_index, monotonic_clock::time_point time) override {
30 return distributed_clock::epoch() +
31 std::chrono::duration_cast<std::chrono::nanoseconds>(
32 (time.time_since_epoch() - distributed_offset_[node_index]) /
33 distributed_slope_[node_index]);
34 }
35
36 monotonic_clock::time_point FromDistributedClock(
37 size_t node_index, distributed_clock::time_point time) override {
38 return monotonic_clock::epoch() +
39 std::chrono::duration_cast<std::chrono::nanoseconds>(
40 time.time_since_epoch() * distributed_slope_[node_index]) +
41 distributed_offset_[node_index];
42 }
43
Austin Schuhb7c8d2a2021-07-19 19:22:12 -070044 void ObserveTimePassed(distributed_clock::time_point /*time*/) override {}
45
Austin Schuh87dd3832021-01-01 23:07:31 -080046 private:
47 // Offset to the distributed clock.
48 // distributed = monotonic + offset;
49 std::vector<std::chrono::nanoseconds> distributed_offset_;
50 std::vector<double> distributed_slope_;
51};
52
Austin Schuhbe69cf32020-08-27 11:38:33 -070053// Tests that the default parameters (slope of 1, offest of 0) behave as
54// an identity.
55TEST(EventSchedulerTest, IdentityTimeConversion) {
Austin Schuh87dd3832021-01-01 23:07:31 -080056 SlopeOffsetTimeConverter time(1);
Austin Schuhbe69cf32020-08-27 11:38:33 -070057 EventScheduler s;
Austin Schuh87dd3832021-01-01 23:07:31 -080058 s.SetTimeConverter(0u, &time);
Austin Schuhbe69cf32020-08-27 11:38:33 -070059 EXPECT_EQ(s.FromDistributedClock(distributed_clock::epoch()),
60 monotonic_clock::epoch());
61
62 EXPECT_EQ(
63 s.FromDistributedClock(distributed_clock::epoch() + chrono::seconds(1)),
64 monotonic_clock::epoch() + chrono::seconds(1));
65
66 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch()),
67 distributed_clock::epoch());
68
Austin Schuh87dd3832021-01-01 23:07:31 -080069 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(1)),
70 distributed_clock::epoch() + chrono::seconds(1));
Austin Schuhbe69cf32020-08-27 11:38:33 -070071}
72
73// Tests that a non-unity slope is computed correctly.
74TEST(EventSchedulerTest, DoubleTimeConversion) {
Austin Schuh87dd3832021-01-01 23:07:31 -080075 SlopeOffsetTimeConverter time(1);
Austin Schuhbe69cf32020-08-27 11:38:33 -070076 EventScheduler s;
Austin Schuh87dd3832021-01-01 23:07:31 -080077 s.SetTimeConverter(0u, &time);
78 time.SetDistributedOffset(0u, std::chrono::seconds(7), 2.0);
Austin Schuhbe69cf32020-08-27 11:38:33 -070079
80 EXPECT_EQ(s.FromDistributedClock(distributed_clock::epoch()),
81 monotonic_clock::epoch() + chrono::seconds(7));
82
83 EXPECT_EQ(
84 s.FromDistributedClock(distributed_clock::epoch() + chrono::seconds(1)),
85 monotonic_clock::epoch() + chrono::seconds(9));
86
87 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(7)),
88 distributed_clock::epoch());
89
Austin Schuh87dd3832021-01-01 23:07:31 -080090 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(9)),
91 distributed_clock::epoch() + chrono::seconds(1));
Austin Schuhbe69cf32020-08-27 11:38:33 -070092}
93
94} // namespace aos