Austin Schuh | 60e7794 | 2022-05-16 17:48:24 -0700 | [diff] [blame] | 1 | #include "aos/logging/dynamic_logging.h" |
| 2 | |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 3 | #include <chrono> |
| 4 | #include <memory> |
| 5 | #include <ostream> |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 6 | |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 7 | #include "flatbuffers/buffer.h" |
| 8 | #include "flatbuffers/flatbuffer_builder.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 9 | #include "glog/logging.h" |
| 10 | #include "gtest/gtest.h" |
| 11 | |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 12 | #include "aos/configuration.h" |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 13 | #include "aos/events/event_loop.h" |
| 14 | #include "aos/events/simulated_event_loop.h" |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 15 | #include "aos/flatbuffers.h" |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 16 | #include "aos/testing/path.h" |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 17 | |
| 18 | using aos::testing::ArtifactPath; |
| 19 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 20 | namespace aos::logging::testing { |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 21 | |
| 22 | namespace chrono = std::chrono; |
| 23 | |
| 24 | class DynamicLoggingTest : public ::testing::Test { |
| 25 | public: |
| 26 | DynamicLoggingTest() |
| 27 | : config_(aos::configuration::ReadConfig( |
| 28 | ArtifactPath("aos/events/pingpong_config.json"))), |
| 29 | event_loop_factory_(&config_.message()), |
| 30 | event_loop_send_(event_loop_factory_.MakeEventLoop("send")), |
| 31 | event_loop_main_(event_loop_factory_.MakeEventLoop("main")) {} |
| 32 | |
| 33 | aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 34 | SimulatedEventLoopFactory event_loop_factory_; |
| 35 | |
| 36 | std::unique_ptr<EventLoop> event_loop_send_; |
| 37 | std::unique_ptr<EventLoop> event_loop_main_; |
| 38 | }; |
| 39 | |
| 40 | TEST_F(DynamicLoggingTest, TestVLog) { |
| 41 | aos::Sender<DynamicLogCommand> dynamic_log_command_sender = |
| 42 | event_loop_send_->MakeSender<DynamicLogCommand>("/aos"); |
| 43 | |
| 44 | // Set VLOG level to 1 at t=50us and then back to 0 at t=150us. |
| 45 | int log_level = 1; |
| 46 | aos::TimerHandler *timer_handler = event_loop_send_->AddTimer( |
| 47 | [this, &dynamic_log_command_sender, &log_level, &timer_handler]() { |
| 48 | aos::Sender<DynamicLogCommand>::Builder message = |
| 49 | dynamic_log_command_sender.MakeBuilder(); |
| 50 | const auto name_str = message.fbb()->CreateString("main"); |
| 51 | |
| 52 | DynamicLogCommand::Builder builder = |
| 53 | message.MakeBuilder<DynamicLogCommand>(); |
| 54 | builder.add_name(name_str); |
| 55 | builder.add_vlog_level(log_level); |
| 56 | CHECK_EQ(message.Send(builder.Finish()), RawSender::Error::kOk); |
| 57 | --log_level; |
| 58 | if (log_level >= 0) { |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 59 | timer_handler->Schedule(event_loop_send_->monotonic_now() + |
| 60 | chrono::microseconds(100)); |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 61 | } |
| 62 | }); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 63 | timer_handler->Schedule(event_loop_send_->monotonic_now() + |
| 64 | chrono::microseconds(50)); |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 65 | |
| 66 | // VLOG(1) at t=0us, t=100us, t=200us |
| 67 | aos::TimerHandler *vlog_timer_handler = |
| 68 | event_loop_main_->AddTimer([]() { VLOG(1) << "VLOG 1"; }); |
Philipp Schrader | a671252 | 2023-07-05 20:25:11 -0700 | [diff] [blame] | 69 | vlog_timer_handler->Schedule(event_loop_main_->monotonic_now(), |
| 70 | chrono::microseconds(100)); |
Sarah Newman | a7e8793 | 2022-04-11 15:00:03 -0700 | [diff] [blame] | 71 | |
| 72 | DynamicLogging dynamic_logging(event_loop_main_.get()); |
| 73 | |
| 74 | { |
| 75 | // Validate no log message in first 50us. |
| 76 | ::testing::internal::CaptureStderr(); |
| 77 | event_loop_factory_.RunFor(chrono::microseconds(50)); |
| 78 | std::string output = ::testing::internal::GetCapturedStderr(); |
| 79 | EXPECT_EQ(output.find("VLOG 1"), std::string::npos); |
| 80 | } |
| 81 | { |
| 82 | // Validate 1 log message between 50us to 150us |
| 83 | ::testing::internal::CaptureStderr(); |
| 84 | event_loop_factory_.RunFor(chrono::microseconds(100)); |
| 85 | std::string output = ::testing::internal::GetCapturedStderr(); |
| 86 | EXPECT_NE(output.find("VLOG 1"), std::string::npos); |
| 87 | } |
| 88 | { |
| 89 | // Validate no log message between 150us to 250us |
| 90 | ::testing::internal::CaptureStderr(); |
| 91 | event_loop_factory_.RunFor(chrono::microseconds(100)); |
| 92 | std::string output = ::testing::internal::GetCapturedStderr(); |
| 93 | EXPECT_EQ(output.find("VLOG 1"), std::string::npos); |
| 94 | } |
| 95 | } |
| 96 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 97 | } // namespace aos::logging::testing |