blob: a217a74479d328bdfd07b275cf602d584b2620de [file] [log] [blame]
James Kuszmaul839c8aa2023-01-10 15:27:57 -08001#include "aos/network/timestamp_channel.h"
2
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "gtest/gtest.h"
4
James Kuszmaul839c8aa2023-01-10 15:27:57 -08005#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 Kuszmaul839c8aa2023-01-10 15:27:57 -080011
12DECLARE_string(override_hostname);
13
14namespace aos::message_bridge::testing {
15class 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.
28TEST_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
34class 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.
41TEST_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
61std::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
66INSTANTIATE_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