James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 1 | #include "aos/network/timestamp_channel.h" |
| 2 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 3 | #include "absl/flags/declare.h" |
| 4 | #include "absl/flags/flag.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
| 6 | |
James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 7 | #include "aos/configuration.h" |
| 8 | #include "aos/events/ping_generated.h" |
| 9 | #include "aos/events/shm_event_loop.h" |
| 10 | #include "aos/events/simulated_event_loop.h" |
| 11 | #include "aos/testing/path.h" |
| 12 | #include "aos/testing/tmpdir.h" |
James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 13 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 14 | ABSL_DECLARE_FLAG(std::string, override_hostname); |
James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 15 | |
| 16 | namespace aos::message_bridge::testing { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 17 | |
James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 18 | class TimestampChannelTest : public ::testing::Test { |
| 19 | protected: |
| 20 | TimestampChannelTest() |
| 21 | : config_(aos::configuration::ReadConfig(aos::testing::ArtifactPath( |
| 22 | "aos/network/timestamp_channel_test_config.json"))) { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 23 | absl::SetFlag(&FLAGS_override_hostname, "pi1"); |
James Kuszmaul | 839c8aa | 2023-01-10 15:27:57 -0800 | [diff] [blame] | 24 | } |
| 25 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 26 | }; |
| 27 | |
| 28 | // Tests that creating a SimulatedEventLoopFactory with invalid remote timestamp |
| 29 | // channel frequencies fails. |
| 30 | TEST_F(TimestampChannelTest, SimulatedNetworkBridgeFrequencyMismatch) { |
| 31 | SimulatedEventLoopFactory factory(&config_.message()); |
| 32 | EXPECT_DEATH(factory.RunFor(std::chrono::seconds(1)), |
| 33 | "rate is lower than the source channel"); |
| 34 | } |
| 35 | |
| 36 | class TimestampChannelParamTest |
| 37 | : public TimestampChannelTest, |
| 38 | public ::testing::WithParamInterface< |
| 39 | std::tuple<std::string, std::optional<std::string>>> {}; |
| 40 | |
| 41 | // Tests whether we can or can't retrieve a timestamp channel depending on |
| 42 | // whether it has a valid max frequency configured. |
| 43 | TEST_P(TimestampChannelParamTest, ChannelFrequency) { |
| 44 | aos::ShmEventLoop event_loop(&config_.message()); |
| 45 | ChannelTimestampSender timestamp_sender(&event_loop); |
| 46 | const aos::Channel *channel = |
| 47 | event_loop.GetChannel<aos::examples::Ping>(std::get<0>(GetParam())); |
| 48 | const std::optional<std::string> error_message = std::get<1>(GetParam()); |
| 49 | if (error_message.has_value()) { |
| 50 | EXPECT_DEATH(timestamp_sender.SenderForChannel( |
| 51 | channel, channel->destination_nodes()->Get(0)), |
| 52 | error_message.value()); |
| 53 | } else { |
| 54 | aos::Sender<RemoteMessage> *sender = timestamp_sender.SenderForChannel( |
| 55 | channel, channel->destination_nodes()->Get(0)); |
| 56 | ASSERT_TRUE(sender != nullptr); |
| 57 | EXPECT_EQ(absl::StrCat("/pi1/aos/remote_timestamps/pi2", |
| 58 | std::get<0>(GetParam()), "/aos-examples-Ping"), |
| 59 | sender->channel()->name()->string_view()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | std::tuple<std::string, std::optional<std::string>> MakeParams( |
| 64 | std::string channel, std::optional<std::string> error) { |
| 65 | return std::make_tuple(channel, error); |
| 66 | } |
| 67 | |
| 68 | INSTANTIATE_TEST_SUITE_P( |
| 69 | ChannelFrequencyTest, TimestampChannelParamTest, |
| 70 | ::testing::Values(MakeParams("/nominal", std::nullopt), |
| 71 | MakeParams("/timestamps_too_fast", std::nullopt), |
| 72 | MakeParams("/timestamps_too_slow", |
| 73 | "rate is lower than the source channel"))); |
| 74 | } // namespace aos::message_bridge::testing |