Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 1 | #include "aos/events/event_scheduler.h" |
| 2 | |
| 3 | #include <chrono> |
| 4 | |
| 5 | #include "gtest/gtest.h" |
| 6 | |
| 7 | namespace aos { |
| 8 | |
| 9 | namespace chrono = std::chrono; |
| 10 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 11 | // Legacy time converter for keeping old tests working. Has numerical precision |
| 12 | // problems. |
| 13 | class 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 Schuh | b7c8d2a | 2021-07-19 19:22:12 -0700 | [diff] [blame^] | 44 | void ObserveTimePassed(distributed_clock::time_point /*time*/) override {} |
| 45 | |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 46 | 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 Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 53 | // Tests that the default parameters (slope of 1, offest of 0) behave as |
| 54 | // an identity. |
| 55 | TEST(EventSchedulerTest, IdentityTimeConversion) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 56 | SlopeOffsetTimeConverter time(1); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 57 | EventScheduler s; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 58 | s.SetTimeConverter(0u, &time); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 59 | 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 Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 69 | EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(1)), |
| 70 | distributed_clock::epoch() + chrono::seconds(1)); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Tests that a non-unity slope is computed correctly. |
| 74 | TEST(EventSchedulerTest, DoubleTimeConversion) { |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 75 | SlopeOffsetTimeConverter time(1); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 76 | EventScheduler s; |
Austin Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 77 | s.SetTimeConverter(0u, &time); |
| 78 | time.SetDistributedOffset(0u, std::chrono::seconds(7), 2.0); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 79 | |
| 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 Schuh | 87dd383 | 2021-01-01 23:07:31 -0800 | [diff] [blame] | 90 | EXPECT_EQ(s.ToDistributedClock(monotonic_clock::epoch() + chrono::seconds(9)), |
| 91 | distributed_clock::epoch() + chrono::seconds(1)); |
Austin Schuh | be69cf3 | 2020-08-27 11:38:33 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | } // namespace aos |