James Kuszmaul | 38735e8 | 2019-12-07 16:42:06 -0800 | [diff] [blame] | 1 | #include "aos/events/logging/logger.h" |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 2 | |
| 3 | #include "aos/events/event_loop.h" |
| 4 | #include "aos/events/ping_lib.h" |
| 5 | #include "aos/events/pong_lib.h" |
| 6 | #include "aos/events/simulated_event_loop.h" |
| 7 | #include "glog/logging.h" |
| 8 | #include "gtest/gtest.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | namespace logger { |
| 12 | namespace testing { |
| 13 | |
| 14 | namespace chrono = std::chrono; |
| 15 | |
| 16 | class LoggerTest : public ::testing::Test { |
| 17 | public: |
| 18 | LoggerTest() |
| 19 | : config_( |
| 20 | aos::configuration::ReadConfig("aos/events/pingpong_config.json")), |
| 21 | event_loop_factory_(&config_.message()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 22 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 23 | ping_(ping_event_loop_.get()), |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 24 | pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")), |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 25 | pong_(pong_event_loop_.get()) {} |
| 26 | |
| 27 | // Config and factory. |
| 28 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 29 | SimulatedEventLoopFactory event_loop_factory_; |
| 30 | |
| 31 | // Event loop and app for Ping |
| 32 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 33 | Ping ping_; |
| 34 | |
| 35 | // Event loop and app for Pong |
| 36 | std::unique_ptr<EventLoop> pong_event_loop_; |
| 37 | Pong pong_; |
| 38 | }; |
| 39 | |
| 40 | // Tests that we can startup at all. This confirms that the channels are all in |
| 41 | // the config. |
| 42 | TEST_F(LoggerTest, Starts) { |
| 43 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 44 | const ::std::string logfile = tmpdir + "/logfile.bfbs"; |
| 45 | // Remove it. |
| 46 | unlink(logfile.c_str()); |
| 47 | |
| 48 | LOG(INFO) << "Logging data to " << logfile; |
| 49 | |
| 50 | { |
| 51 | DetachedBufferWriter writer(logfile); |
| 52 | std::unique_ptr<EventLoop> logger_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 53 | event_loop_factory_.MakeEventLoop("logger"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 54 | |
| 55 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 56 | |
| 57 | Logger logger(&writer, logger_event_loop.get(), |
| 58 | std::chrono::milliseconds(100)); |
| 59 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 60 | } |
| 61 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 62 | // Even though it doesn't make any difference here, exercise the logic for |
| 63 | // passing in a separate config. |
| 64 | LogReader reader(logfile, &config_.message()); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 65 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 66 | // Confirm that we can remap logged channels to point to new buses. |
| 67 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 69 | // This sends out the fetched messages and advances time to the start of the |
| 70 | // log file. |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 71 | reader.Register(); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 72 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 73 | EXPECT_EQ(reader.node(), nullptr); |
| 74 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 75 | std::unique_ptr<EventLoop> test_event_loop = |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 76 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 77 | |
| 78 | int ping_count = 10; |
| 79 | int pong_count = 10; |
| 80 | |
James Kuszmaul | c7bbb3e | 2020-01-03 20:01:00 -0800 | [diff] [blame] | 81 | // Confirm that the ping value matches in the remapped channel location. |
| 82 | test_event_loop->MakeWatcher("/original/test", |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 83 | [&ping_count](const examples::Ping &ping) { |
| 84 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 85 | ++ping_count; |
| 86 | }); |
| 87 | // Confirm that the ping and pong counts both match, and the value also |
| 88 | // matches. |
| 89 | test_event_loop->MakeWatcher( |
| 90 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 91 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 92 | ++pong_count; |
| 93 | EXPECT_EQ(ping_count, pong_count); |
| 94 | }); |
| 95 | |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 96 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 97 | EXPECT_EQ(ping_count, 2010); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Austin Schuh | fa89589 | 2020-01-07 20:07:41 -0800 | [diff] [blame^] | 100 | // Tests that we can read and write rotated log files. |
| 101 | TEST_F(LoggerTest, RotatedLogFile) { |
| 102 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 103 | const ::std::string logfile0 = tmpdir + "/logfile0.bfbs"; |
| 104 | const ::std::string logfile1 = tmpdir + "/logfile1.bfbs"; |
| 105 | // Remove it. |
| 106 | unlink(logfile0.c_str()); |
| 107 | unlink(logfile1.c_str()); |
| 108 | |
| 109 | LOG(INFO) << "Logging data to " << logfile0 << " and " << logfile1; |
| 110 | |
| 111 | { |
| 112 | DetachedBufferWriter writer0(logfile0); |
| 113 | DetachedBufferWriter writer1(logfile1); |
| 114 | std::unique_ptr<EventLoop> logger_event_loop = |
| 115 | event_loop_factory_.MakeEventLoop("logger"); |
| 116 | |
| 117 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 118 | |
| 119 | Logger logger(&writer0, logger_event_loop.get(), |
| 120 | std::chrono::milliseconds(100)); |
| 121 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 122 | logger.Rotate(&writer1); |
| 123 | event_loop_factory_.RunFor(chrono::milliseconds(10000)); |
| 124 | } |
| 125 | |
| 126 | // Even though it doesn't make any difference here, exercise the logic for |
| 127 | // passing in a separate config. |
| 128 | LogReader reader(std::vector<std::string>{logfile0, logfile1}, |
| 129 | &config_.message()); |
| 130 | |
| 131 | // Confirm that we can remap logged channels to point to new buses. |
| 132 | reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original"); |
| 133 | |
| 134 | // This sends out the fetched messages and advances time to the start of the |
| 135 | // log file. |
| 136 | reader.Register(); |
| 137 | |
| 138 | EXPECT_EQ(reader.node(), nullptr); |
| 139 | |
| 140 | std::unique_ptr<EventLoop> test_event_loop = |
| 141 | reader.event_loop_factory()->MakeEventLoop("log_reader"); |
| 142 | |
| 143 | int ping_count = 10; |
| 144 | int pong_count = 10; |
| 145 | |
| 146 | // Confirm that the ping value matches in the remapped channel location. |
| 147 | test_event_loop->MakeWatcher("/original/test", |
| 148 | [&ping_count](const examples::Ping &ping) { |
| 149 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 150 | ++ping_count; |
| 151 | }); |
| 152 | // Confirm that the ping and pong counts both match, and the value also |
| 153 | // matches. |
| 154 | test_event_loop->MakeWatcher( |
| 155 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 156 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 157 | ++pong_count; |
| 158 | EXPECT_EQ(ping_count, pong_count); |
| 159 | }); |
| 160 | |
| 161 | reader.event_loop_factory()->RunFor(std::chrono::seconds(100)); |
| 162 | EXPECT_EQ(ping_count, 2010); |
| 163 | } |
| 164 | |
Austin Schuh | 4c4e009 | 2019-12-22 16:18:03 -0800 | [diff] [blame] | 165 | // Tests that a large number of messages per second doesn't overwhelm writev. |
| 166 | TEST_F(LoggerTest, ManyMessages) { |
| 167 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 168 | const ::std::string logfile = tmpdir + "/logfile.bfbs"; |
| 169 | // Remove the log file. |
| 170 | unlink(logfile.c_str()); |
| 171 | |
| 172 | LOG(INFO) << "Logging data to " << logfile; |
| 173 | |
| 174 | { |
| 175 | DetachedBufferWriter writer(logfile); |
| 176 | std::unique_ptr<EventLoop> logger_event_loop = |
| 177 | event_loop_factory_.MakeEventLoop("logger"); |
| 178 | |
| 179 | std::unique_ptr<EventLoop> ping_spammer_event_loop = |
| 180 | event_loop_factory_.MakeEventLoop("ping_spammer"); |
| 181 | aos::Sender<examples::Ping> ping_sender = |
| 182 | ping_spammer_event_loop->MakeSender<examples::Ping>("/test"); |
| 183 | |
| 184 | aos::TimerHandler *timer_handler = |
| 185 | ping_spammer_event_loop->AddTimer([&ping_sender]() { |
| 186 | aos::Sender<examples::Ping>::Builder builder = |
| 187 | ping_sender.MakeBuilder(); |
| 188 | examples::Ping::Builder ping_builder = |
| 189 | builder.MakeBuilder<examples::Ping>(); |
| 190 | CHECK(builder.Send(ping_builder.Finish())); |
| 191 | }); |
| 192 | |
| 193 | // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it. |
| 194 | ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() { |
| 195 | timer_handler->Setup(ping_spammer_event_loop->monotonic_now(), |
| 196 | chrono::microseconds(50)); |
| 197 | }); |
| 198 | |
| 199 | Logger logger(&writer, logger_event_loop.get(), |
| 200 | std::chrono::milliseconds(100)); |
| 201 | |
| 202 | event_loop_factory_.RunFor(chrono::milliseconds(1000)); |
| 203 | } |
| 204 | } |
| 205 | |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 206 | class MultinodeLoggerTest : public ::testing::Test { |
| 207 | public: |
| 208 | MultinodeLoggerTest() |
| 209 | : config_(aos::configuration::ReadConfig( |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 210 | "aos/events/logging/multinode_pingpong_config.json")), |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 211 | event_loop_factory_(&config_.message(), "pi1"), |
| 212 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
| 213 | ping_(ping_event_loop_.get()) {} |
| 214 | |
| 215 | // Config and factory. |
| 216 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 217 | SimulatedEventLoopFactory event_loop_factory_; |
| 218 | |
| 219 | // Event loop and app for Ping |
| 220 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 221 | Ping ping_; |
| 222 | }; |
| 223 | |
| 224 | // Tests that we can startup at all in a multinode configuration. |
| 225 | TEST_F(MultinodeLoggerTest, MultiNode) { |
| 226 | constexpr chrono::seconds kTimeOffset = chrono::seconds(10000); |
| 227 | constexpr uint32_t kQueueIndexOffset = 1024; |
| 228 | const ::std::string tmpdir(getenv("TEST_TMPDIR")); |
| 229 | const ::std::string logfile = tmpdir + "/multi_logfile.bfbs"; |
| 230 | // Remove it. |
| 231 | unlink(logfile.c_str()); |
| 232 | |
| 233 | LOG(INFO) << "Logging data to " << logfile; |
| 234 | |
| 235 | { |
| 236 | std::unique_ptr<EventLoop> pong_event_loop = |
| 237 | event_loop_factory_.MakeEventLoop("pong"); |
| 238 | |
| 239 | std::unique_ptr<aos::RawSender> pong_sender( |
| 240 | pong_event_loop->MakeRawSender(aos::configuration::GetChannel( |
| 241 | pong_event_loop->configuration(), "/test", "aos.examples.Pong", |
| 242 | pong_event_loop->name(), pong_event_loop->node()))); |
| 243 | |
| 244 | // Ok, let's fake a remote node. We use the fancy raw sender Send |
| 245 | // method that message_gateway will use to do that. |
| 246 | int pong_count = 0; |
| 247 | pong_event_loop->MakeWatcher( |
| 248 | "/test", [&pong_event_loop, &pong_count, &pong_sender, |
| 249 | kTimeOffset](const examples::Ping &ping) { |
| 250 | flatbuffers::FlatBufferBuilder fbb; |
| 251 | examples::Pong::Builder pong_builder(fbb); |
| 252 | pong_builder.add_value(ping.value()); |
| 253 | pong_builder.add_initial_send_time(ping.send_time()); |
| 254 | fbb.Finish(pong_builder.Finish()); |
| 255 | |
| 256 | pong_sender->Send(fbb.GetBufferPointer(), fbb.GetSize(), |
| 257 | pong_event_loop->monotonic_now() + kTimeOffset, |
| 258 | pong_event_loop->realtime_now() + kTimeOffset, |
| 259 | kQueueIndexOffset + pong_count); |
| 260 | ++pong_count; |
| 261 | }); |
| 262 | |
| 263 | DetachedBufferWriter writer(logfile); |
| 264 | std::unique_ptr<EventLoop> logger_event_loop = |
| 265 | event_loop_factory_.MakeEventLoop("logger"); |
| 266 | |
| 267 | event_loop_factory_.RunFor(chrono::milliseconds(95)); |
| 268 | |
| 269 | Logger logger(&writer, logger_event_loop.get(), |
| 270 | std::chrono::milliseconds(100)); |
| 271 | event_loop_factory_.RunFor(chrono::milliseconds(20000)); |
| 272 | } |
| 273 | |
| 274 | LogReader reader(logfile); |
| 275 | |
| 276 | // TODO(austin): Also replay as pi2 or pi3 and make sure we see the pong |
| 277 | // messages. This won't work today yet until the log reading code gets |
| 278 | // significantly better. |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 279 | SimulatedEventLoopFactory log_reader_factory(reader.logged_configuration(), reader.node()); |
| 280 | log_reader_factory.set_send_delay(chrono::microseconds(0)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 281 | |
| 282 | // This sends out the fetched messages and advances time to the start of the |
| 283 | // log file. |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 284 | reader.Register(&log_reader_factory); |
James Kuszmaul | 84ff3e5 | 2020-01-03 19:48:53 -0800 | [diff] [blame] | 285 | |
| 286 | ASSERT_NE(reader.node(), nullptr); |
| 287 | EXPECT_EQ(reader.node()->name()->string_view(), "pi1"); |
| 288 | |
| 289 | reader.event_loop_factory()->set_send_delay(chrono::microseconds(0)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 290 | |
| 291 | std::unique_ptr<EventLoop> test_event_loop = |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 292 | log_reader_factory.MakeEventLoop("test"); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 293 | |
| 294 | int ping_count = 10; |
| 295 | int pong_count = 10; |
| 296 | |
| 297 | // Confirm that the ping value matches. |
| 298 | test_event_loop->MakeWatcher("/test", |
| 299 | [&ping_count](const examples::Ping &ping) { |
| 300 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 301 | ++ping_count; |
| 302 | }); |
| 303 | // Confirm that the ping and pong counts both match, and the value also |
| 304 | // matches. |
| 305 | test_event_loop->MakeWatcher( |
| 306 | "/test", [&test_event_loop, &ping_count, &pong_count, |
| 307 | kTimeOffset](const examples::Pong &pong) { |
| 308 | EXPECT_EQ(test_event_loop->context().remote_queue_index, |
| 309 | pong_count + kQueueIndexOffset); |
| 310 | EXPECT_EQ(test_event_loop->context().monotonic_remote_time, |
| 311 | test_event_loop->monotonic_now() + kTimeOffset); |
| 312 | EXPECT_EQ(test_event_loop->context().realtime_remote_time, |
| 313 | test_event_loop->realtime_now() + kTimeOffset); |
| 314 | |
| 315 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 316 | ++pong_count; |
| 317 | EXPECT_EQ(ping_count, pong_count); |
| 318 | }); |
| 319 | |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 320 | log_reader_factory.RunFor(std::chrono::seconds(100)); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 321 | EXPECT_EQ(ping_count, 2010); |
| 322 | EXPECT_EQ(pong_count, 2010); |
Austin Schuh | 6331ef9 | 2020-01-07 18:28:09 -0800 | [diff] [blame] | 323 | |
| 324 | reader.Deregister(); |
Austin Schuh | 15649d6 | 2019-12-28 16:36:38 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 327 | } // namespace testing |
| 328 | } // namespace logger |
| 329 | } // namespace aos |