blob: 1ad0a84e699795d88c13f44e1b39a4b8eaf6df52 [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
44 private:
45 // Offset to the distributed clock.
46 // distributed = monotonic + offset;
47 std::vector<std::chrono::nanoseconds> distributed_offset_;
48 std::vector<double> distributed_slope_;
49};
50
Austin Schuhbe69cf32020-08-27 11:38:33 -070051// Tests that the default parameters (slope of 1, offest of 0) behave as
52// an identity.
53TEST(EventSchedulerTest, IdentityTimeConversion) {
Austin Schuh87dd3832021-01-01 23:07:31 -080054 SlopeOffsetTimeConverter time(1);
Austin Schuhbe69cf32020-08-27 11:38:33 -070055 EventScheduler s;
Austin Schuh87dd3832021-01-01 23:07:31 -080056 s.SetTimeConverter(0u, &time);
Austin Schuhbe69cf32020-08-27 11:38:33 -070057 EXPECT_EQ(s.FromDistributedClock(distributed_clock::epoch()),
58 monotonic_clock::epoch());
59
60 EXPECT_EQ(
61 s.FromDistributedClock(distributed_clock::epoch() + chrono::seconds(1)),
62 monotonic_clock::epoch() + chrono::seconds(1));
63
64 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch()),
65 distributed_clock::epoch());
66
Austin Schuh87dd3832021-01-01 23:07:31 -080067 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(1)),
68 distributed_clock::epoch() + chrono::seconds(1));
Austin Schuhbe69cf32020-08-27 11:38:33 -070069}
70
71// Tests that a non-unity slope is computed correctly.
72TEST(EventSchedulerTest, DoubleTimeConversion) {
Austin Schuh87dd3832021-01-01 23:07:31 -080073 SlopeOffsetTimeConverter time(1);
Austin Schuhbe69cf32020-08-27 11:38:33 -070074 EventScheduler s;
Austin Schuh87dd3832021-01-01 23:07:31 -080075 s.SetTimeConverter(0u, &time);
76 time.SetDistributedOffset(0u, std::chrono::seconds(7), 2.0);
Austin Schuhbe69cf32020-08-27 11:38:33 -070077
78 EXPECT_EQ(s.FromDistributedClock(distributed_clock::epoch()),
79 monotonic_clock::epoch() + chrono::seconds(7));
80
81 EXPECT_EQ(
82 s.FromDistributedClock(distributed_clock::epoch() + chrono::seconds(1)),
83 monotonic_clock::epoch() + chrono::seconds(9));
84
85 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(7)),
86 distributed_clock::epoch());
87
Austin Schuh87dd3832021-01-01 23:07:31 -080088 EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(9)),
89 distributed_clock::epoch() + chrono::seconds(1));
Austin Schuhbe69cf32020-08-27 11:38:33 -070090}
91
92} // namespace aos