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