Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 1 | #include "aos/configuration.h" |
| 2 | |
| 3 | #include "absl/strings/strip.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame^] | 4 | #include "flatbuffers/reflection.h" |
| 5 | #include "glog/logging.h" |
| 6 | #include "gmock/gmock.h" |
| 7 | #include "gtest/gtest.h" |
| 8 | |
Nathan Leong | 307c969 | 2022-10-08 15:25:03 -0700 | [diff] [blame] | 9 | #include "aos/events/ping_generated.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 10 | #include "aos/json_to_flatbuffer.h" |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 11 | #include "aos/testing/flatbuffer_eq.h" |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 12 | #include "aos/testing/path.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 13 | #include "aos/testing/test_logging.h" |
| 14 | #include "aos/util/file.h" |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 15 | |
| 16 | namespace aos { |
| 17 | namespace configuration { |
| 18 | namespace testing { |
| 19 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 20 | using aos::testing::ArtifactPath; |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 21 | namespace chrono = std::chrono; |
Austin Schuh | 6660213 | 2020-02-28 13:38:37 -0800 | [diff] [blame] | 22 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 23 | class ConfigurationTest : public ::testing::Test { |
| 24 | public: |
| 25 | ConfigurationTest() { ::aos::testing::EnableTestLogging(); } |
| 26 | }; |
| 27 | |
| 28 | typedef ConfigurationTest ConfigurationDeathTest; |
| 29 | |
| 30 | // *the* expected location for all working tests. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 31 | aos::FlatbufferDetachedBuffer<Channel> ExpectedLocation() { |
| 32 | return JsonToFlatbuffer<Channel>( |
| 33 | "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5 }"); |
| 34 | } |
| 35 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 36 | // And for multinode setups |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 37 | aos::FlatbufferDetachedBuffer<Channel> ExpectedMultinodeLocation() { |
| 38 | return JsonToFlatbuffer<Channel>( |
| 39 | "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"max_size\": 5, " |
| 40 | "\"source_node\": \"pi1\" }"); |
| 41 | } |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 42 | |
| 43 | // Tests that we can read and merge a configuration. |
| 44 | TEST_F(ConfigurationTest, ConfigMerge) { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 45 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 46 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 47 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 48 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 49 | EXPECT_EQ(absl::StripSuffix(util::ReadFileToStringOrDie( |
| 50 | ArtifactPath("aos/testdata/expected.json")), |
| 51 | "\n"), |
| 52 | FlatbufferToJson(config, {.multi_line = true})); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 55 | // Tests that we can get back a ChannelIndex. |
| 56 | TEST_F(ConfigurationTest, ChannelIndex) { |
| 57 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 58 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 59 | |
| 60 | EXPECT_EQ( |
| 61 | ChannelIndex(&config.message(), config.message().channels()->Get(1u)), |
| 62 | 1u); |
| 63 | } |
| 64 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 65 | // Tests that we can read and merge a multinode configuration. |
| 66 | TEST_F(ConfigurationTest, ConfigMergeMultinode) { |
| 67 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 68 | ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json")); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 69 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 70 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 71 | EXPECT_EQ(std::string(absl::StripSuffix( |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 72 | util::ReadFileToStringOrDie( |
| 73 | ArtifactPath("aos/testdata/expected_multinode.json")), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 74 | "\n")), |
| 75 | FlatbufferToJson(config, {.multi_line = true})); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 78 | // Tests that we sort the entries in a config so we can look entries up. |
| 79 | TEST_F(ConfigurationTest, UnsortedConfig) { |
| 80 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 81 | ReadConfig(ArtifactPath("aos/testdata/backwards.json")); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 82 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 83 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 85 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/aos/robot_state", |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 86 | "aos.RobotState", "app1", nullptr)), |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 87 | "{ \"name\": \"/aos/robot_state\", \"type\": \"aos.RobotState\", " |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 88 | "\"max_size\": 5 }"); |
| 89 | } |
| 90 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 91 | // Tests that we die when a file is imported twice. |
| 92 | TEST_F(ConfigurationDeathTest, DuplicateFile) { |
| 93 | EXPECT_DEATH( |
| 94 | { |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 95 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 96 | ReadConfig(ArtifactPath("aos/testdata/config1_bad.json")); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 97 | }, |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 98 | "aos/testdata/config1_bad.json"); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 101 | // Tests that we die when we give an invalid path. |
| 102 | TEST_F(ConfigurationDeathTest, NonexistentFile) { |
| 103 | EXPECT_DEATH( |
| 104 | { |
| 105 | FlatbufferDetachedBuffer<Configuration> config = |
| 106 | ReadConfig("nonexistent/config.json"); |
| 107 | }, |
| 108 | "above error"); |
| 109 | } |
| 110 | |
| 111 | // Tests that we return std::nullopt when we give an invalid path. |
| 112 | TEST_F(ConfigurationTest, NonexistentFileOptional) { |
| 113 | std::optional<FlatbufferDetachedBuffer<Configuration>> config = |
| 114 | MaybeReadConfig("nonexistent/config.json"); |
| 115 | EXPECT_FALSE(config.has_value()); |
| 116 | } |
| 117 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 118 | // Tests that we reject invalid channel names. This means any channels with // |
| 119 | // in their name, a trailing /, or regex characters. |
| 120 | TEST_F(ConfigurationDeathTest, InvalidChannelName) { |
| 121 | EXPECT_DEATH( |
| 122 | { |
| 123 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 124 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name1.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 125 | }, |
| 126 | "Channel names can't end with '/'"); |
| 127 | EXPECT_DEATH( |
| 128 | { |
| 129 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 130 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name2.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 131 | }, |
| 132 | "Invalid channel name"); |
| 133 | EXPECT_DEATH( |
| 134 | { |
| 135 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 136 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name3.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 137 | LOG(FATAL) << "Foo"; |
| 138 | }, |
| 139 | "Invalid channel name"); |
Austin Schuh | 47e382e | 2023-05-28 11:20:56 -0700 | [diff] [blame] | 140 | EXPECT_DEATH( |
| 141 | { |
| 142 | FlatbufferDetachedBuffer<Configuration> config = |
| 143 | ReadConfig(ArtifactPath("aos/testdata/invalid_channel_name4.json")); |
| 144 | LOG(FATAL) << "Foo"; |
| 145 | }, |
| 146 | "Channel names must start with '/'"); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 149 | // Tests that we can modify a config with a json snippet. |
| 150 | TEST_F(ConfigurationTest, MergeWithConfig) { |
| 151 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 152 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 153 | LOG(INFO) << "Read: " << FlatbufferToJson(config, {.multi_line = true}); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 154 | |
| 155 | FlatbufferDetachedBuffer<Configuration> updated_config = |
| 156 | MergeWithConfig(&config.message(), |
| 157 | R"channel({ |
| 158 | "channels": [ |
| 159 | { |
| 160 | "name": "/foo", |
| 161 | "type": ".aos.bar", |
| 162 | "max_size": 100 |
| 163 | } |
| 164 | ] |
| 165 | })channel"); |
| 166 | |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 167 | EXPECT_EQ(absl::StripSuffix(util::ReadFileToStringOrDie(ArtifactPath( |
| 168 | "aos/testdata/expected_merge_with.json")), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 169 | "\n"), |
| 170 | FlatbufferToJson(updated_config, {.multi_line = true})); |
Austin Schuh | 8d6cea8 | 2020-02-28 12:17:16 -0800 | [diff] [blame] | 171 | } |
| 172 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 173 | // Tests that we can lookup a location, complete with maps, from a merged |
| 174 | // config. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 175 | TEST_F(ConfigurationTest, GetChannel) { |
| 176 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 177 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 178 | |
| 179 | // Test a basic lookup first. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 180 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", nullptr), |
| 181 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 182 | |
| 183 | // Test that an invalid name results in nullptr back. |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 184 | EXPECT_EQ(GetChannel(config, "/invalid_name", ".aos.bar", "app1", nullptr), |
| 185 | nullptr); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 186 | |
| 187 | // Tests that a root map/rename works. And that they get processed from the |
| 188 | // bottom up. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 189 | EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", nullptr), |
| 190 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 191 | |
| 192 | // And then test that an application specific map/rename works. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 193 | EXPECT_THAT(GetChannel(config, "/bar", ".aos.bar", "app1", nullptr), |
| 194 | aos::testing::FlatbufferEq(ExpectedLocation())); |
| 195 | EXPECT_THAT(GetChannel(config, "/baz", ".aos.bar", "app2", nullptr), |
| 196 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 197 | |
| 198 | // And then test that an invalid application name gets properly ignored. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 199 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app3", nullptr), |
| 200 | aos::testing::FlatbufferEq(ExpectedLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 201 | } |
| 202 | |
James Kuszmaul | c8503f3 | 2022-06-25 16:17:12 -0700 | [diff] [blame] | 203 | // Tests that we can do reverse-lookups of channel names. |
| 204 | TEST_F(ConfigurationTest, GetChannelAliases) { |
| 205 | FlatbufferDetachedBuffer<Configuration> config = |
| 206 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
| 207 | |
| 208 | // Test a basic lookup first. |
| 209 | EXPECT_THAT( |
| 210 | GetChannelAliases(&config.message(), "/foo", ".aos.bar", "app1", nullptr), |
| 211 | ::testing::UnorderedElementsAre("/foo", "/batman", "/bar")); |
| 212 | EXPECT_THAT( |
| 213 | GetChannelAliases(&config.message(), "/bar", ".aos.bar", "app1", nullptr), |
| 214 | ::testing::UnorderedElementsAre("/batman", "/bar")); |
| 215 | EXPECT_THAT(GetChannelAliases(&config.message(), "/batman", ".aos.bar", |
| 216 | "app1", nullptr), |
| 217 | ::testing::UnorderedElementsAre("/batman")); |
| 218 | // /bar (deliberately) does not get included because of the ordering in the |
| 219 | // map. |
| 220 | EXPECT_THAT( |
| 221 | GetChannelAliases(&config.message(), "/foo", ".aos.bar", "", nullptr), |
| 222 | ::testing::UnorderedElementsAre("/foo", "/batman")); |
| 223 | EXPECT_THAT( |
| 224 | GetChannelAliases(&config.message(), "/foo", ".aos.bar", "app2", nullptr), |
| 225 | ::testing::UnorderedElementsAre("/foo", "/batman", "/baz")); |
| 226 | } |
| 227 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 228 | // Tests that we can lookup a location with node specific maps. |
| 229 | TEST_F(ConfigurationTest, GetChannelMultinode) { |
| 230 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 231 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 232 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 233 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 234 | |
| 235 | // Test a basic lookup first. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 236 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", pi1), |
| 237 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
| 238 | EXPECT_THAT(GetChannel(config, "/foo", ".aos.bar", "app1", pi2), |
| 239 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 240 | |
| 241 | // Tests that a root map/rename works with a node specific map. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 242 | EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", pi1), |
| 243 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 244 | |
| 245 | // Tests that a root map/rename fails with a node specific map for the wrong |
| 246 | // node. |
| 247 | EXPECT_EQ(GetChannel(config, "/batman", ".aos.bar", "app1", pi2), nullptr); |
| 248 | |
| 249 | // And then test that an application specific map/rename works. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 250 | EXPECT_THAT(GetChannel(config, "/batman2", ".aos.bar", "app1", pi1), |
| 251 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
| 252 | EXPECT_THAT(GetChannel(config, "/batman3", ".aos.bar", "app1", pi1), |
| 253 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 254 | |
| 255 | // And then that it fails when the node changes. |
| 256 | EXPECT_EQ(GetChannel(config, "/batman3", ".aos.bar", "app1", pi2), nullptr); |
| 257 | } |
| 258 | |
James Kuszmaul | c8503f3 | 2022-06-25 16:17:12 -0700 | [diff] [blame] | 259 | // Tests that reverse channel lookup on a multi-node config (including with |
| 260 | // wildcards) works. |
| 261 | TEST_F(ConfigurationTest, GetChannelAliasesMultinode) { |
| 262 | FlatbufferDetachedBuffer<Configuration> config = |
| 263 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
| 264 | |
| 265 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 266 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 267 | |
| 268 | EXPECT_THAT( |
| 269 | GetChannelAliases(&config.message(), "/foo", ".aos.bar", "app1", pi1), |
| 270 | ::testing::UnorderedElementsAre("/foo", "/batman", "/batman2", "/batman3", |
| 271 | "/magic/string")); |
| 272 | |
| 273 | EXPECT_THAT( |
| 274 | GetChannelAliases(&config.message(), "/foo", ".aos.baz", "app1", pi1), |
| 275 | ::testing::UnorderedElementsAre("/foo", "/batman3", "/magic/string")); |
| 276 | |
| 277 | EXPECT_THAT( |
| 278 | GetChannelAliases(&config.message(), "/foo/testing", ".aos.bar", "", pi1), |
| 279 | ::testing::UnorderedElementsAre("/foo/testing", "/magic/string/testing")); |
| 280 | |
| 281 | EXPECT_THAT( |
| 282 | GetChannelAliases(&config.message(), "/foo/testing", ".aos.bar", "app1", |
| 283 | pi2), |
| 284 | ::testing::UnorderedElementsAre("/foo/testing", "/magic/string/testing")); |
| 285 | } |
| 286 | |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 287 | // Tests that we can lookup a location with type specific maps. |
| 288 | TEST_F(ConfigurationTest, GetChannelTypedMultinode) { |
| 289 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 290 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 291 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 292 | |
| 293 | // Test a basic lookup first. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 294 | EXPECT_THAT(GetChannel(config, "/batman", ".aos.bar", "app1", pi1), |
| 295 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | bca6cf0 | 2019-12-22 17:28:34 -0800 | [diff] [blame] | 296 | |
| 297 | // Now confirm that a second message on the same name doesn't get remapped. |
| 298 | const char *kExpectedBazMultinodeLocation = |
| 299 | "{ \"name\": \"/batman\", \"type\": \".aos.baz\", \"max_size\": 5, " |
| 300 | "\"source_node\": \"pi1\" }"; |
| 301 | EXPECT_EQ( |
| 302 | FlatbufferToJson(GetChannel(config, "/batman", ".aos.baz", "app1", pi1)), |
| 303 | kExpectedBazMultinodeLocation); |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 306 | // Tests that we can lookup a location with a glob |
| 307 | TEST_F(ConfigurationTest, GetChannelGlob) { |
| 308 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 309 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 310 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 311 | |
| 312 | // Confirm that a glob with nothing after it matches. |
Austin Schuh | 1ef01ef | 2021-02-07 20:40:36 -0800 | [diff] [blame] | 313 | EXPECT_THAT(GetChannel(config, "/magic/string", ".aos.bar", "app7", pi1), |
| 314 | aos::testing::FlatbufferEq(ExpectedMultinodeLocation())); |
Austin Schuh | f1fff28 | 2020-03-28 16:57:32 -0700 | [diff] [blame] | 315 | |
| 316 | // Now confirm that glob with something following it matches and renames |
| 317 | // correctly. |
| 318 | const char *kExpectedSubfolderMultinodeLocation = |
| 319 | "{ \"name\": \"/foo/subfolder\", \"type\": \".aos.bar\", \"max_size\": " |
| 320 | "5, \"source_node\": \"pi1\" }"; |
| 321 | EXPECT_EQ(FlatbufferToJson(GetChannel(config, "/magic/string/subfolder", |
| 322 | ".aos.bar", "app7", pi1)), |
| 323 | kExpectedSubfolderMultinodeLocation); |
| 324 | } |
| 325 | |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 326 | // Tests that we reject a configuration which has a nodes list, but has channels |
| 327 | // withoout source_node filled out. |
| 328 | TEST_F(ConfigurationDeathTest, InvalidSourceNode) { |
| 329 | EXPECT_DEATH( |
| 330 | { |
| 331 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 332 | ReadConfig(ArtifactPath("aos/testdata/invalid_nodes.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 333 | }, |
| 334 | "source_node"); |
| 335 | |
| 336 | EXPECT_DEATH( |
| 337 | { |
| 338 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 339 | ReadConfig(ArtifactPath("aos/testdata/invalid_source_node.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 340 | }, |
| 341 | "source_node"); |
| 342 | |
| 343 | EXPECT_DEATH( |
| 344 | { |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 345 | FlatbufferDetachedBuffer<Configuration> config = ReadConfig( |
| 346 | ArtifactPath("aos/testdata/invalid_destination_node.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 347 | }, |
| 348 | "destination_nodes"); |
| 349 | |
| 350 | EXPECT_DEATH( |
| 351 | { |
| 352 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 353 | ReadConfig(ArtifactPath("aos/testdata/self_forward.json")); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 354 | }, |
| 355 | "forwarding data to itself"); |
| 356 | } |
| 357 | |
| 358 | // Tests that our node writeable helpers work as intended. |
| 359 | TEST_F(ConfigurationTest, ChannelIsSendableOnNode) { |
| 360 | FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 361 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 362 | "name": "/test", |
| 363 | "type": "aos.examples.Ping", |
| 364 | "source_node": "foo" |
| 365 | })channel", |
| 366 | Channel::MiniReflectTypeTable())); |
| 367 | |
| 368 | FlatbufferDetachedBuffer<Channel> bad_channel(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 369 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 370 | "name": "/test", |
| 371 | "type": "aos.examples.Ping", |
| 372 | "source_node": "bar" |
| 373 | })channel", |
| 374 | Channel::MiniReflectTypeTable())); |
| 375 | |
| 376 | FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 377 | R"node({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 378 | "name": "foo" |
| 379 | })node", |
| 380 | Node::MiniReflectTypeTable())); |
| 381 | |
| 382 | EXPECT_TRUE( |
| 383 | ChannelIsSendableOnNode(&good_channel.message(), &node.message())); |
| 384 | EXPECT_FALSE( |
| 385 | ChannelIsSendableOnNode(&bad_channel.message(), &node.message())); |
| 386 | } |
| 387 | |
| 388 | // Tests that our node readable and writeable helpers work as intended. |
| 389 | TEST_F(ConfigurationTest, ChannelIsReadableOnNode) { |
| 390 | FlatbufferDetachedBuffer<Channel> good_channel(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 391 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 392 | "name": "/test", |
| 393 | "type": "aos.examples.Ping", |
| 394 | "source_node": "bar", |
| 395 | "destination_nodes": [ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 396 | { |
| 397 | "name": "baz" |
| 398 | }, |
| 399 | { |
| 400 | "name": "foo" |
| 401 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 402 | ] |
| 403 | })channel", |
| 404 | Channel::MiniReflectTypeTable())); |
| 405 | |
| 406 | FlatbufferDetachedBuffer<Channel> bad_channel1(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 407 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 408 | "name": "/test", |
| 409 | "type": "aos.examples.Ping", |
| 410 | "source_node": "bar" |
| 411 | })channel", |
| 412 | Channel::MiniReflectTypeTable())); |
| 413 | |
| 414 | FlatbufferDetachedBuffer<Channel> bad_channel2(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 415 | R"channel({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 416 | "name": "/test", |
| 417 | "type": "aos.examples.Ping", |
| 418 | "source_node": "bar", |
| 419 | "destination_nodes": [ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 420 | { |
| 421 | "name": "baz" |
| 422 | } |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 423 | ] |
| 424 | })channel", |
| 425 | Channel::MiniReflectTypeTable())); |
| 426 | |
| 427 | FlatbufferDetachedBuffer<Node> node(JsonToFlatbuffer( |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 428 | R"node({ |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 429 | "name": "foo" |
| 430 | })node", |
| 431 | Node::MiniReflectTypeTable())); |
| 432 | |
| 433 | EXPECT_TRUE( |
| 434 | ChannelIsReadableOnNode(&good_channel.message(), &node.message())); |
| 435 | EXPECT_FALSE( |
| 436 | ChannelIsReadableOnNode(&bad_channel1.message(), &node.message())); |
| 437 | EXPECT_FALSE( |
| 438 | ChannelIsReadableOnNode(&bad_channel2.message(), &node.message())); |
| 439 | } |
| 440 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 441 | // Tests that our node message is logged helpers work as intended. |
| 442 | TEST_F(ConfigurationTest, ChannelMessageIsLoggedOnNode) { |
| 443 | FlatbufferDetachedBuffer<Channel> logged_on_self_channel(JsonToFlatbuffer( |
| 444 | R"channel({ |
| 445 | "name": "/test", |
| 446 | "type": "aos.examples.Ping", |
| 447 | "source_node": "bar", |
| 448 | "destination_nodes": [ |
| 449 | { |
| 450 | "name": "baz" |
| 451 | } |
| 452 | ] |
| 453 | })channel", |
| 454 | Channel::MiniReflectTypeTable())); |
Austin Schuh | 217a978 | 2019-12-21 23:02:50 -0800 | [diff] [blame] | 455 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 456 | FlatbufferDetachedBuffer<Channel> not_logged_channel(JsonToFlatbuffer( |
| 457 | R"channel({ |
| 458 | "name": "/test", |
| 459 | "type": "aos.examples.Ping", |
| 460 | "source_node": "bar", |
| 461 | "logger": "NOT_LOGGED", |
| 462 | "destination_nodes": [ |
| 463 | { |
| 464 | "name": "baz", |
| 465 | "timestamp_logger": "LOCAL_LOGGER" |
| 466 | } |
| 467 | ] |
| 468 | })channel", |
| 469 | Channel::MiniReflectTypeTable())); |
| 470 | |
| 471 | FlatbufferDetachedBuffer<Channel> logged_on_remote_channel(JsonToFlatbuffer( |
| 472 | R"channel({ |
| 473 | "name": "/test", |
| 474 | "type": "aos.examples.Ping", |
| 475 | "source_node": "bar", |
| 476 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 477 | "logger_nodes": ["baz"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 478 | "destination_nodes": [ |
| 479 | { |
| 480 | "name": "baz" |
| 481 | } |
| 482 | ] |
| 483 | })channel", |
| 484 | Channel::MiniReflectTypeTable())); |
| 485 | |
| 486 | FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel( |
| 487 | JsonToFlatbuffer( |
| 488 | R"channel({ |
| 489 | "name": "/test", |
| 490 | "type": "aos.examples.Ping", |
| 491 | "source_node": "bar", |
| 492 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 493 | "logger_nodes": ["foo"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 494 | "destination_nodes": [ |
| 495 | { |
| 496 | "name": "baz" |
| 497 | } |
| 498 | ] |
| 499 | })channel", |
| 500 | Channel::MiniReflectTypeTable())); |
| 501 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 502 | FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer( |
| 503 | R"channel({ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 504 | "name": "/test", |
| 505 | "type": "aos.examples.Ping", |
| 506 | "source_node": "bar", |
| 507 | "logger": "LOCAL_AND_REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 508 | "logger_nodes": ["baz"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 509 | "destination_nodes": [ |
| 510 | { |
| 511 | "name": "baz" |
| 512 | } |
| 513 | ] |
| 514 | })channel", |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 515 | Channel::MiniReflectTypeTable())); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 516 | |
| 517 | FlatbufferDetachedBuffer<Node> foo_node(JsonToFlatbuffer( |
| 518 | R"node({ |
| 519 | "name": "foo" |
| 520 | })node", |
| 521 | Node::MiniReflectTypeTable())); |
| 522 | |
| 523 | FlatbufferDetachedBuffer<Node> bar_node(JsonToFlatbuffer( |
| 524 | R"node({ |
| 525 | "name": "bar" |
| 526 | })node", |
| 527 | Node::MiniReflectTypeTable())); |
| 528 | |
| 529 | FlatbufferDetachedBuffer<Node> baz_node(JsonToFlatbuffer( |
| 530 | R"node({ |
| 531 | "name": "baz" |
| 532 | })node", |
| 533 | Node::MiniReflectTypeTable())); |
| 534 | |
| 535 | // Local logger. |
| 536 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), |
| 537 | &foo_node.message())); |
| 538 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), |
| 539 | &bar_node.message())); |
| 540 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), |
| 541 | &baz_node.message())); |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 542 | EXPECT_TRUE( |
| 543 | ChannelMessageIsLoggedOnNode(&logged_on_self_channel.message(), nullptr)); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 544 | |
| 545 | // No logger. |
| 546 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), |
| 547 | &foo_node.message())); |
| 548 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 549 | &bar_node.message())); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 550 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), |
| 551 | &baz_node.message())); |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 552 | EXPECT_FALSE( |
| 553 | ChannelMessageIsLoggedOnNode(¬_logged_channel.message(), nullptr)); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 554 | |
| 555 | // Remote logger. |
| 556 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(), |
| 557 | &foo_node.message())); |
| 558 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(), |
| 559 | &bar_node.message())); |
| 560 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_remote_channel.message(), |
| 561 | &baz_node.message())); |
| 562 | |
| 563 | // Separate logger. |
| 564 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode( |
| 565 | &logged_on_separate_logger_node_channel.message(), &foo_node.message())); |
| 566 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode( |
| 567 | &logged_on_separate_logger_node_channel.message(), &bar_node.message())); |
| 568 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode( |
| 569 | &logged_on_separate_logger_node_channel.message(), &baz_node.message())); |
| 570 | |
| 571 | // Logged in multiple places. |
| 572 | EXPECT_FALSE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 573 | &foo_node.message())); |
| 574 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 575 | &bar_node.message())); |
| 576 | EXPECT_TRUE(ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 577 | &baz_node.message())); |
| 578 | } |
| 579 | |
Austin Schuh | 48e9450 | 2021-06-18 18:35:53 -0700 | [diff] [blame] | 580 | // Tests that our node message is logged helpers work as intended. |
| 581 | TEST_F(ConfigurationDeathTest, ChannelMessageIsLoggedOnNode) { |
| 582 | FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer( |
| 583 | R"channel({ |
| 584 | "name": "/test", |
| 585 | "type": "aos.examples.Ping", |
| 586 | "source_node": "bar", |
| 587 | "logger": "LOCAL_AND_REMOTE_LOGGER", |
| 588 | "logger_nodes": ["baz"], |
| 589 | "destination_nodes": [ |
| 590 | { |
| 591 | "name": "baz" |
| 592 | } |
| 593 | ] |
| 594 | })channel", |
| 595 | Channel::MiniReflectTypeTable())); |
| 596 | |
| 597 | FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel( |
| 598 | JsonToFlatbuffer( |
| 599 | R"channel({ |
| 600 | "name": "/test", |
| 601 | "type": "aos.examples.Ping", |
| 602 | "source_node": "bar", |
| 603 | "logger": "REMOTE_LOGGER", |
| 604 | "logger_nodes": ["foo"], |
| 605 | "destination_nodes": [ |
| 606 | { |
| 607 | "name": "baz" |
| 608 | } |
| 609 | ] |
| 610 | })channel", |
| 611 | Channel::MiniReflectTypeTable())); |
| 612 | |
| 613 | EXPECT_DEATH( |
| 614 | { |
| 615 | ChannelMessageIsLoggedOnNode(&logged_on_both_channel.message(), |
| 616 | nullptr); |
| 617 | }, |
| 618 | "Unsupported logging configuration in a single node world"); |
| 619 | EXPECT_DEATH( |
| 620 | { |
| 621 | ChannelMessageIsLoggedOnNode( |
| 622 | &logged_on_separate_logger_node_channel.message(), nullptr); |
| 623 | }, |
| 624 | "Unsupported logging configuration in a single node world"); |
| 625 | } |
| 626 | |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 627 | // Tests that our forwarding timestamps are logged helpers work as intended. |
| 628 | TEST_F(ConfigurationTest, ConnectionDeliveryTimeIsLoggedOnNode) { |
| 629 | FlatbufferDetachedBuffer<Channel> logged_on_self_channel(JsonToFlatbuffer( |
| 630 | R"channel({ |
| 631 | "name": "/test", |
| 632 | "type": "aos.examples.Ping", |
| 633 | "source_node": "bar", |
| 634 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 635 | "logger_nodes": ["baz"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 636 | "destination_nodes": [ |
| 637 | { |
| 638 | "name": "baz" |
| 639 | } |
| 640 | ] |
| 641 | })channel", |
| 642 | Channel::MiniReflectTypeTable())); |
| 643 | |
| 644 | FlatbufferDetachedBuffer<Channel> not_logged_channel(JsonToFlatbuffer( |
| 645 | R"channel({ |
| 646 | "name": "/test", |
| 647 | "type": "aos.examples.Ping", |
| 648 | "source_node": "bar", |
| 649 | "logger": "NOT_LOGGED", |
| 650 | "destination_nodes": [ |
| 651 | { |
| 652 | "name": "baz", |
| 653 | "timestamp_logger": "NOT_LOGGED" |
| 654 | } |
| 655 | ] |
| 656 | })channel", |
| 657 | Channel::MiniReflectTypeTable())); |
| 658 | |
| 659 | FlatbufferDetachedBuffer<Channel> logged_on_remote_channel(JsonToFlatbuffer( |
| 660 | R"channel({ |
| 661 | "name": "/test", |
| 662 | "type": "aos.examples.Ping", |
| 663 | "source_node": "bar", |
| 664 | "destination_nodes": [ |
| 665 | { |
| 666 | "name": "baz", |
| 667 | "timestamp_logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 668 | "timestamp_logger_nodes": ["bar"] |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 669 | } |
| 670 | ] |
| 671 | })channel", |
| 672 | Channel::MiniReflectTypeTable())); |
| 673 | |
| 674 | FlatbufferDetachedBuffer<Channel> logged_on_separate_logger_node_channel( |
| 675 | JsonToFlatbuffer( |
| 676 | R"channel({ |
| 677 | "name": "/test", |
| 678 | "type": "aos.examples.Ping", |
| 679 | "source_node": "bar", |
| 680 | "logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 681 | "logger_nodes": ["foo"], |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 682 | "destination_nodes": [ |
| 683 | { |
| 684 | "name": "baz", |
| 685 | "timestamp_logger": "REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 686 | "timestamp_logger_nodes": ["foo"] |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 687 | } |
| 688 | ] |
| 689 | })channel", |
| 690 | Channel::MiniReflectTypeTable())); |
| 691 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 692 | FlatbufferDetachedBuffer<Channel> logged_on_both_channel(JsonToFlatbuffer( |
| 693 | R"channel({ |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 694 | "name": "/test", |
| 695 | "type": "aos.examples.Ping", |
| 696 | "source_node": "bar", |
| 697 | "destination_nodes": [ |
| 698 | { |
| 699 | "name": "baz", |
| 700 | "timestamp_logger": "LOCAL_AND_REMOTE_LOGGER", |
Austin Schuh | da40e47 | 2020-03-28 15:15:29 -0700 | [diff] [blame] | 701 | "timestamp_logger_nodes": ["bar"] |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 702 | } |
| 703 | ] |
| 704 | })channel", |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 705 | Channel::MiniReflectTypeTable())); |
Austin Schuh | 719946b | 2019-12-28 14:51:01 -0800 | [diff] [blame] | 706 | |
| 707 | FlatbufferDetachedBuffer<Node> foo_node(JsonToFlatbuffer( |
| 708 | R"node({ |
| 709 | "name": "foo" |
| 710 | })node", |
| 711 | Node::MiniReflectTypeTable())); |
| 712 | |
| 713 | FlatbufferDetachedBuffer<Node> bar_node(JsonToFlatbuffer( |
| 714 | R"node({ |
| 715 | "name": "bar" |
| 716 | })node", |
| 717 | Node::MiniReflectTypeTable())); |
| 718 | |
| 719 | FlatbufferDetachedBuffer<Node> baz_node(JsonToFlatbuffer( |
| 720 | R"node({ |
| 721 | "name": "baz" |
| 722 | })node", |
| 723 | Node::MiniReflectTypeTable())); |
| 724 | |
| 725 | // Local logger. |
| 726 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 727 | &logged_on_self_channel.message(), &baz_node.message(), |
| 728 | &foo_node.message())); |
| 729 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 730 | &logged_on_self_channel.message(), &baz_node.message(), |
| 731 | &bar_node.message())); |
| 732 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 733 | &logged_on_self_channel.message(), &baz_node.message(), |
| 734 | &baz_node.message())); |
| 735 | |
| 736 | // No logger means. |
| 737 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 738 | ¬_logged_channel.message(), &baz_node.message(), &foo_node.message())); |
| 739 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 740 | ¬_logged_channel.message(), &baz_node.message(), &bar_node.message())); |
| 741 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 742 | ¬_logged_channel.message(), &baz_node.message(), &baz_node.message())); |
| 743 | |
| 744 | // Remote logger. |
| 745 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 746 | &logged_on_remote_channel.message(), &baz_node.message(), |
| 747 | &foo_node.message())); |
| 748 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 749 | &logged_on_remote_channel.message(), &baz_node.message(), |
| 750 | &bar_node.message())); |
| 751 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 752 | &logged_on_remote_channel.message(), &baz_node.message(), |
| 753 | &baz_node.message())); |
| 754 | |
| 755 | // Separate logger. |
| 756 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 757 | &logged_on_separate_logger_node_channel.message(), &baz_node.message(), |
| 758 | &foo_node.message())); |
| 759 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 760 | &logged_on_separate_logger_node_channel.message(), &baz_node.message(), |
| 761 | &bar_node.message())); |
| 762 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 763 | &logged_on_separate_logger_node_channel.message(), &baz_node.message(), |
| 764 | &baz_node.message())); |
| 765 | |
| 766 | // Logged on both the node and a remote node. |
| 767 | EXPECT_FALSE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 768 | &logged_on_both_channel.message(), &baz_node.message(), |
| 769 | &foo_node.message())); |
| 770 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 771 | &logged_on_both_channel.message(), &baz_node.message(), |
| 772 | &bar_node.message())); |
| 773 | EXPECT_TRUE(ConnectionDeliveryTimeIsLoggedOnNode( |
| 774 | &logged_on_both_channel.message(), &baz_node.message(), |
| 775 | &baz_node.message())); |
| 776 | } |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 777 | |
| 778 | // Tests that we can deduce source nodes from a multinode config. |
| 779 | TEST_F(ConfigurationTest, SourceNodeNames) { |
| 780 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 781 | ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json")); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 782 | |
| 783 | // This is a bit simplistic in that it doesn't test deduplication, but it does |
| 784 | // exercise a lot of the logic. |
| 785 | EXPECT_THAT( |
| 786 | SourceNodeNames(&config.message(), config.message().nodes()->Get(0)), |
| 787 | ::testing::ElementsAreArray({"pi2"})); |
| 788 | EXPECT_THAT( |
| 789 | SourceNodeNames(&config.message(), config.message().nodes()->Get(1)), |
| 790 | ::testing::ElementsAreArray({"pi1"})); |
| 791 | } |
| 792 | |
| 793 | // Tests that we can deduce destination nodes from a multinode config. |
| 794 | TEST_F(ConfigurationTest, DestinationNodeNames) { |
| 795 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 796 | ReadConfig(ArtifactPath("aos/testdata/config1_multinode.json")); |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 797 | |
| 798 | // This is a bit simplistic in that it doesn't test deduplication, but it does |
| 799 | // exercise a lot of the logic. |
| 800 | EXPECT_THAT( |
| 801 | DestinationNodeNames(&config.message(), config.message().nodes()->Get(0)), |
| 802 | ::testing::ElementsAreArray({"pi2"})); |
| 803 | EXPECT_THAT( |
| 804 | DestinationNodeNames(&config.message(), config.message().nodes()->Get(1)), |
| 805 | ::testing::ElementsAreArray({"pi1"})); |
| 806 | } |
| 807 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 808 | // Tests that we can pull out all the nodes. |
| 809 | TEST_F(ConfigurationTest, GetNodes) { |
| 810 | { |
| 811 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 812 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 813 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 814 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 815 | |
| 816 | EXPECT_THAT(GetNodes(&config.message()), ::testing::ElementsAre(pi1, pi2)); |
| 817 | } |
| 818 | |
| 819 | { |
| 820 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 821 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 822 | EXPECT_THAT(GetNodes(&config.message()), ::testing::ElementsAre(nullptr)); |
| 823 | } |
| 824 | } |
| 825 | |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 826 | // Tests that we can pull out all the nodes with a tag. |
| 827 | TEST_F(ConfigurationTest, GetNodesWithTag) { |
| 828 | { |
| 829 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 830 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 831 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 832 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 833 | |
| 834 | EXPECT_THAT(GetNodesWithTag(&config.message(), "a"), |
| 835 | ::testing::ElementsAre(pi1)); |
| 836 | EXPECT_THAT(GetNodesWithTag(&config.message(), "b"), |
| 837 | ::testing::ElementsAre(pi2)); |
| 838 | EXPECT_THAT(GetNodesWithTag(&config.message(), "c"), |
| 839 | ::testing::ElementsAre(pi1, pi2)); |
| 840 | } |
| 841 | |
| 842 | { |
| 843 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 844 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | 6546533 | 2020-11-05 17:36:53 -0800 | [diff] [blame] | 845 | EXPECT_THAT(GetNodesWithTag(&config.message(), "arglfish"), |
| 846 | ::testing::ElementsAre(nullptr)); |
| 847 | } |
| 848 | } |
| 849 | |
Brian Silverman | 631b626 | 2021-11-10 12:25:08 -0800 | [diff] [blame] | 850 | // Tests that we can check if a node has a tag. |
| 851 | TEST_F(ConfigurationTest, NodeHasTag) { |
| 852 | { |
| 853 | FlatbufferDetachedBuffer<Configuration> config = |
| 854 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
| 855 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 856 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 857 | |
| 858 | EXPECT_TRUE(NodeHasTag(pi1, "a")); |
| 859 | EXPECT_FALSE(NodeHasTag(pi2, "a")); |
| 860 | EXPECT_FALSE(NodeHasTag(pi1, "b")); |
| 861 | EXPECT_TRUE(NodeHasTag(pi2, "b")); |
| 862 | EXPECT_TRUE(NodeHasTag(pi1, "c")); |
| 863 | EXPECT_TRUE(NodeHasTag(pi2, "c")); |
| 864 | EXPECT_FALSE(NodeHasTag(pi1, "nope")); |
| 865 | EXPECT_FALSE(NodeHasTag(pi2, "nope")); |
| 866 | } |
| 867 | |
| 868 | EXPECT_TRUE(NodeHasTag(nullptr, "arglfish")); |
| 869 | } |
| 870 | |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 871 | // Tests that we can extract a node index from a config. |
| 872 | TEST_F(ConfigurationTest, GetNodeIndex) { |
| 873 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 874 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 875 | FlatbufferDetachedBuffer<Configuration> config2 = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 876 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 877 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 878 | const Node *pi2 = GetNode(&config.message(), "pi2"); |
| 879 | |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 880 | // Try the normal case. |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 881 | EXPECT_EQ(GetNodeIndex(&config.message(), pi1), 0); |
| 882 | EXPECT_EQ(GetNodeIndex(&config.message(), pi2), 1); |
Austin Schuh | 04408fc | 2020-02-16 21:48:54 -0800 | [diff] [blame] | 883 | |
| 884 | // Now try if we have node pointers from a different message. |
| 885 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi1), 0); |
| 886 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi2), 1); |
| 887 | |
| 888 | // And now try string names. |
| 889 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi1->name()->string_view()), 0); |
| 890 | EXPECT_EQ(GetNodeIndex(&config2.message(), pi2->name()->string_view()), 1); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 891 | } |
| 892 | |
| 893 | // Tests that GetNodeOrDie handles both single and multi-node worlds and returns |
| 894 | // valid nodes. |
| 895 | TEST_F(ConfigurationDeathTest, GetNodeOrDie) { |
| 896 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 897 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 898 | FlatbufferDetachedBuffer<Configuration> config2 = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 899 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 900 | { |
| 901 | // Simple case, nullptr -> nullptr |
| 902 | FlatbufferDetachedBuffer<Configuration> single_node_config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 903 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
Austin Schuh | c9e10ec | 2020-01-26 16:08:28 -0800 | [diff] [blame] | 904 | EXPECT_EQ(nullptr, GetNodeOrDie(&single_node_config.message(), nullptr)); |
| 905 | |
| 906 | // Confirm that we die when a node is passed in. |
| 907 | EXPECT_DEATH( |
| 908 | { |
| 909 | GetNodeOrDie(&single_node_config.message(), |
| 910 | config.message().nodes()->Get(0)); |
| 911 | }, |
| 912 | "Provided a node in a single node world."); |
| 913 | } |
| 914 | |
| 915 | const Node *pi1 = GetNode(&config.message(), "pi1"); |
| 916 | // Now try a lookup using a node from a different instance of the config. |
| 917 | EXPECT_EQ(pi1, |
| 918 | GetNodeOrDie(&config.message(), config2.message().nodes()->Get(0))); |
| 919 | } |
| 920 | |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 921 | TEST_F(ConfigurationTest, GetNodeFromHostname) { |
| 922 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 923 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 924 | EXPECT_EQ("pi1", |
| 925 | CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "raspberrypi")) |
| 926 | ->name() |
| 927 | ->string_view()); |
| 928 | EXPECT_EQ("pi2", CHECK_NOTNULL( |
| 929 | GetNodeFromHostname(&config.message(), "raspberrypi2")) |
| 930 | ->name() |
| 931 | ->string_view()); |
| 932 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "raspberrypi3")); |
| 933 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "localhost")); |
| 934 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "3")); |
| 935 | } |
| 936 | |
| 937 | TEST_F(ConfigurationTest, GetNodeFromHostnames) { |
| 938 | FlatbufferDetachedBuffer<Configuration> config = |
Austin Schuh | 373f176 | 2021-06-02 21:07:09 -0700 | [diff] [blame] | 939 | ReadConfig(ArtifactPath("aos/testdata/good_multinode_hostnames.json")); |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 940 | EXPECT_EQ("pi1", |
| 941 | CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "raspberrypi")) |
| 942 | ->name() |
| 943 | ->string_view()); |
| 944 | EXPECT_EQ("pi2", CHECK_NOTNULL( |
| 945 | GetNodeFromHostname(&config.message(), "raspberrypi2")) |
| 946 | ->name() |
| 947 | ->string_view()); |
| 948 | EXPECT_EQ("pi2", CHECK_NOTNULL( |
| 949 | GetNodeFromHostname(&config.message(), "raspberrypi3")) |
| 950 | ->name() |
| 951 | ->string_view()); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 952 | EXPECT_EQ("pi2", |
| 953 | CHECK_NOTNULL(GetNodeFromHostname(&config.message(), "other")) |
| 954 | ->name() |
| 955 | ->string_view()); |
Brian Silverman | aa2633f | 2020-02-17 21:04:14 -0800 | [diff] [blame] | 956 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "raspberrypi4")); |
| 957 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "localhost")); |
| 958 | EXPECT_EQ(nullptr, GetNodeFromHostname(&config.message(), "3")); |
| 959 | } |
| 960 | |
Austin Schuh | fc7b6a0 | 2021-07-12 21:19:07 -0700 | [diff] [blame] | 961 | // Tests that SourceNodeIndex reasonably handles a multi-node log file. |
| 962 | TEST_F(ConfigurationTest, SourceNodeIndex) { |
| 963 | FlatbufferDetachedBuffer<Configuration> config = |
| 964 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
| 965 | std::vector<size_t> result = SourceNodeIndex(&config.message()); |
| 966 | |
| 967 | EXPECT_THAT(result, ::testing::ElementsAreArray({0, 1, 0, 0})); |
| 968 | } |
| 969 | |
Austin Schuh | 5e95bd6 | 2021-10-11 18:40:22 -0700 | [diff] [blame] | 970 | // Tests that we reject invalid logging configurations. |
| 971 | TEST_F(ConfigurationDeathTest, InvalidLoggerConfig) { |
| 972 | EXPECT_DEATH( |
| 973 | { |
Milind Upadhyay | 17098ba | 2022-04-15 22:18:50 -0700 | [diff] [blame] | 974 | FlatbufferDetachedBuffer<Configuration> config = ReadConfig( |
| 975 | ArtifactPath("aos/testdata/invalid_logging_configuration.json")); |
Austin Schuh | 5e95bd6 | 2021-10-11 18:40:22 -0700 | [diff] [blame] | 976 | }, |
| 977 | "Logging timestamps without data"); |
| 978 | } |
| 979 | |
Austin Schuh | a156fb2 | 2021-10-11 19:23:21 -0700 | [diff] [blame] | 980 | // Tests that we reject duplicate timestamp destination node configurations. |
| 981 | TEST_F(ConfigurationDeathTest, DuplicateTimestampDestinationNodes) { |
| 982 | EXPECT_DEATH( |
| 983 | { |
| 984 | FlatbufferDetachedBuffer<Configuration> config = ReadConfig( |
| 985 | ArtifactPath("aos/testdata/duplicate_destination_nodes.json")); |
| 986 | }, |
| 987 | "Found duplicate timestamp_logger_nodes in"); |
| 988 | } |
| 989 | |
| 990 | // Tests that we reject duplicate logger node configurations for a channel's |
| 991 | // data. |
| 992 | TEST_F(ConfigurationDeathTest, DuplicateLoggerNodes) { |
| 993 | EXPECT_DEATH( |
| 994 | { |
| 995 | FlatbufferDetachedBuffer<Configuration> config = ReadConfig( |
| 996 | ArtifactPath("aos/testdata/duplicate_logger_nodes.json")); |
| 997 | }, |
| 998 | "Found duplicate logger_nodes in"); |
| 999 | } |
| 1000 | |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 1001 | // Tests that we properly compute the queue size for the provided duration. |
| 1002 | TEST_F(ConfigurationTest, QueueSize) { |
| 1003 | EXPECT_EQ(QueueSize(100, chrono::seconds(2)), 200); |
| 1004 | EXPECT_EQ(QueueSize(200, chrono::seconds(2)), 400); |
| 1005 | EXPECT_EQ(QueueSize(100, chrono::seconds(6)), 600); |
| 1006 | EXPECT_EQ(QueueSize(100, chrono::milliseconds(10)), 1); |
| 1007 | EXPECT_EQ(QueueSize(100, chrono::milliseconds(10) - chrono::nanoseconds(1)), |
| 1008 | 1); |
| 1009 | EXPECT_EQ(QueueSize(100, chrono::milliseconds(10) - chrono::nanoseconds(2)), |
| 1010 | 1); |
| 1011 | } |
| 1012 | |
| 1013 | // Tests that we compute scratch buffer size correctly too. |
| 1014 | TEST_F(ConfigurationTest, QueueScratchBufferSize) { |
| 1015 | const aos::FlatbufferDetachedBuffer<Channel> channel = |
| 1016 | JsonToFlatbuffer<Channel>( |
| 1017 | "{ \"name\": \"/foo\", \"type\": \".aos.bar\", \"num_readers\": 5, " |
| 1018 | "\"num_senders\": 10 }"); |
Austin Schuh | fb37c61 | 2022-08-11 15:24:51 -0700 | [diff] [blame] | 1019 | EXPECT_EQ(QueueScratchBufferSize(&channel.message()), 15); |
| 1020 | } |
| 1021 | |
Nathan Leong | 307c969 | 2022-10-08 15:25:03 -0700 | [diff] [blame] | 1022 | // Tests that GetSchema returns schema of specified type |
| 1023 | TEST_F(ConfigurationTest, GetSchema) { |
| 1024 | FlatbufferDetachedBuffer<Configuration> config = |
| 1025 | ReadConfig(ArtifactPath("aos/events/pingpong_config.json")); |
| 1026 | FlatbufferVector<reflection::Schema> expected_schema = |
| 1027 | FileToFlatbuffer<reflection::Schema>( |
| 1028 | ArtifactPath("aos/events/ping.bfbs")); |
| 1029 | EXPECT_EQ(FlatbufferToJson(GetSchema(&config.message(), "aos.examples.Ping")), |
| 1030 | FlatbufferToJson(expected_schema)); |
| 1031 | EXPECT_EQ(GetSchema(&config.message(), "invalid_name"), nullptr); |
| 1032 | } |
| 1033 | |
| 1034 | // Tests that GetSchema template returns schema of specified type |
| 1035 | TEST_F(ConfigurationTest, GetSchemaTemplate) { |
| 1036 | FlatbufferDetachedBuffer<Configuration> config = |
| 1037 | ReadConfig(ArtifactPath("aos/events/pingpong_config.json")); |
| 1038 | FlatbufferVector<reflection::Schema> expected_schema = |
| 1039 | FileToFlatbuffer<reflection::Schema>( |
| 1040 | ArtifactPath("aos/events/ping.bfbs")); |
| 1041 | EXPECT_EQ(FlatbufferToJson(GetSchema<aos::examples::Ping>(&config.message())), |
| 1042 | FlatbufferToJson(expected_schema)); |
| 1043 | } |
| 1044 | |
| 1045 | // Tests that GetSchemaDetachedBuffer returns detached buffer of specified type |
| 1046 | TEST_F(ConfigurationTest, GetSchemaDetachedBuffer) { |
| 1047 | FlatbufferDetachedBuffer<Configuration> config = |
| 1048 | ReadConfig(ArtifactPath("aos/events/pingpong_config.json")); |
| 1049 | FlatbufferVector<reflection::Schema> expected_schema = |
| 1050 | FileToFlatbuffer<reflection::Schema>( |
| 1051 | ArtifactPath("aos/events/ping.bfbs")); |
| 1052 | EXPECT_EQ(FlatbufferToJson( |
| 1053 | GetSchemaDetachedBuffer(&config.message(), "aos.examples.Ping") |
| 1054 | .value()), |
| 1055 | FlatbufferToJson(expected_schema)); |
| 1056 | EXPECT_EQ(GetSchemaDetachedBuffer(&config.message(), "invalid_name"), |
| 1057 | std::nullopt); |
| 1058 | } |
| 1059 | |
James Kuszmaul | 741a4d0 | 2023-01-05 14:59:21 -0800 | [diff] [blame] | 1060 | // Tests that we can use a utility to add individual channels to a single-node |
| 1061 | // config. |
| 1062 | TEST_F(ConfigurationTest, AddChannelToConfigSingleNode) { |
| 1063 | FlatbufferDetachedBuffer<Configuration> base_config = |
| 1064 | ReadConfig(ArtifactPath("aos/testdata/config1.json")); |
| 1065 | |
| 1066 | FlatbufferVector<reflection::Schema> schema = |
| 1067 | FileToFlatbuffer<reflection::Schema>( |
| 1068 | ArtifactPath("aos/events/ping.bfbs")); |
| 1069 | |
| 1070 | FlatbufferDetachedBuffer<Configuration> new_config = |
| 1071 | AddChannelToConfiguration(&base_config.message(), "/new", schema); |
| 1072 | |
| 1073 | ASSERT_EQ(new_config.message().channels()->size(), |
| 1074 | base_config.message().channels()->size() + 1); |
| 1075 | |
| 1076 | const Channel *channel = |
| 1077 | GetChannel(new_config, "/new", "aos.examples.Ping", "", nullptr); |
| 1078 | ASSERT_TRUE(channel != nullptr); |
| 1079 | ASSERT_TRUE(channel->has_schema()); |
| 1080 | // Check that we don't populate channel settings that we don't override the |
| 1081 | // defaults of. |
| 1082 | ASSERT_FALSE(channel->has_frequency()); |
| 1083 | } |
| 1084 | |
| 1085 | // Tests that we can use a utility to add individual channels to a multi-node |
| 1086 | // config. |
| 1087 | TEST_F(ConfigurationTest, AddChannelToConfigMultiNode) { |
| 1088 | FlatbufferDetachedBuffer<Configuration> base_config = |
| 1089 | ReadConfig(ArtifactPath("aos/testdata/good_multinode.json")); |
| 1090 | |
| 1091 | FlatbufferVector<reflection::Schema> schema = |
| 1092 | FileToFlatbuffer<reflection::Schema>( |
| 1093 | ArtifactPath("aos/events/ping.bfbs")); |
| 1094 | |
| 1095 | aos::ChannelT channel_overrides; |
| 1096 | channel_overrides.frequency = 971; |
| 1097 | FlatbufferDetachedBuffer<Configuration> new_config = |
| 1098 | AddChannelToConfiguration(&base_config.message(), "/new", schema, |
| 1099 | GetNode(&base_config.message(), "pi1"), |
| 1100 | channel_overrides); |
| 1101 | |
| 1102 | ASSERT_EQ(new_config.message().channels()->size(), |
| 1103 | base_config.message().channels()->size() + 1); |
| 1104 | |
| 1105 | const Channel *channel = |
| 1106 | GetChannel(new_config, "/new", "aos.examples.Ping", "", nullptr); |
| 1107 | ASSERT_TRUE(channel != nullptr); |
| 1108 | ASSERT_TRUE(channel->has_schema()); |
| 1109 | ASSERT_TRUE(channel->has_source_node()); |
| 1110 | ASSERT_EQ("pi1", channel->source_node()->string_view()); |
| 1111 | ASSERT_EQ(971, channel->frequency()); |
| 1112 | } |
| 1113 | |
Austin Schuh | cb10841 | 2019-10-13 16:09:54 -0700 | [diff] [blame] | 1114 | } // namespace testing |
| 1115 | } // namespace configuration |
| 1116 | } // namespace aos |