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