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