blob: 9c0f57ce28e09fceee867588c3f7a5d4090af891 [file] [log] [blame]
James Kuszmaul839c8aa2023-01-10 15:27:57 -08001#include "aos/network/timestamp_channel.h"
2
Austin Schuh99f7c6a2024-06-25 22:07:44 -07003#include "absl/flags/declare.h"
4#include "absl/flags/flag.h"
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "gtest/gtest.h"
6
James Kuszmaul839c8aa2023-01-10 15:27:57 -08007#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 Kuszmaul839c8aa2023-01-10 15:27:57 -080013
Austin Schuh99f7c6a2024-06-25 22:07:44 -070014ABSL_DECLARE_FLAG(std::string, override_hostname);
James Kuszmaul839c8aa2023-01-10 15:27:57 -080015
16namespace aos::message_bridge::testing {
Austin Schuh99f7c6a2024-06-25 22:07:44 -070017
James Kuszmaul839c8aa2023-01-10 15:27:57 -080018class 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 Schuh99f7c6a2024-06-25 22:07:44 -070023 absl::SetFlag(&FLAGS_override_hostname, "pi1");
James Kuszmaul839c8aa2023-01-10 15:27:57 -080024 }
25 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
26};
27
28// Tests that creating a SimulatedEventLoopFactory with invalid remote timestamp
29// channel frequencies fails.
30TEST_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
36class 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.
43TEST_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
63std::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
68INSTANTIATE_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