James Kuszmaul | 0963242 | 2022-05-25 15:56:19 -0700 | [diff] [blame^] | 1 | #include "aos/events/logging/log_reader.h" |
| 2 | #include "aos/events/logging/log_writer.h" |
| 3 | #include "aos/events/ping_lib.h" |
| 4 | #include "aos/events/shm_event_loop.h" |
| 5 | #include "aos/json_to_flatbuffer.h" |
| 6 | #include "aos/testing/path.h" |
| 7 | #include "aos/testing/tmpdir.h" |
| 8 | #include "gtest/gtest.h" |
| 9 | |
| 10 | namespace aos::logger::testing { |
| 11 | |
| 12 | class RealtimeLoggerTest : public ::testing::Test { |
| 13 | protected: |
| 14 | RealtimeLoggerTest() |
| 15 | : shm_dir_(aos::testing::TestTmpDir() + "/aos"), |
| 16 | config_file_( |
| 17 | aos::testing::ArtifactPath("aos/events/pingpong_config.json")), |
| 18 | config_(aos::configuration::ReadConfig(config_file_)), |
| 19 | event_loop_factory_(&config_.message()), |
| 20 | ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")), |
| 21 | ping_(ping_event_loop_.get()) { |
| 22 | FLAGS_shm_base = shm_dir_; |
| 23 | |
| 24 | // Nuke the shm dir, to ensure we aren't being affected by any preexisting |
| 25 | // tests. |
| 26 | aos::util::UnlinkRecursive(shm_dir_); |
| 27 | } |
| 28 | |
| 29 | gflags::FlagSaver flag_saver_; |
| 30 | std::string shm_dir_; |
| 31 | |
| 32 | const std::string config_file_; |
| 33 | const aos::FlatbufferDetachedBuffer<aos::Configuration> config_; |
| 34 | |
| 35 | // Factory and Ping class to generate a test logfile. |
| 36 | SimulatedEventLoopFactory event_loop_factory_; |
| 37 | std::unique_ptr<EventLoop> ping_event_loop_; |
| 38 | Ping ping_; |
| 39 | }; |
| 40 | |
| 41 | TEST_F(RealtimeLoggerTest, RealtimeReplay) { |
| 42 | const std::string tmpdir = aos::testing::TestTmpDir(); |
| 43 | const std::string base_name = tmpdir + "/logfile/"; |
| 44 | aos::util::UnlinkRecursive(base_name); |
| 45 | { |
| 46 | std::unique_ptr<EventLoop> logger_event_loop = |
| 47 | event_loop_factory_.MakeEventLoop("logger"); |
| 48 | |
| 49 | event_loop_factory_.RunFor(std::chrono::milliseconds(95)); |
| 50 | |
| 51 | Logger logger(logger_event_loop.get()); |
| 52 | logger.set_separate_config(false); |
| 53 | logger.set_polling_period(std::chrono::milliseconds(100)); |
| 54 | logger.StartLoggingLocalNamerOnRun(base_name); |
| 55 | event_loop_factory_.RunFor(std::chrono::milliseconds(2000)); |
| 56 | } |
| 57 | |
| 58 | LogReader reader(logger::SortParts(logger::FindLogs(base_name))); |
| 59 | ShmEventLoop shm_event_loop(reader.configuration()); |
| 60 | reader.Register(&shm_event_loop); |
| 61 | reader.OnEnd(shm_event_loop.node(), |
| 62 | [&shm_event_loop]() { shm_event_loop.Exit(); }); |
| 63 | |
| 64 | Fetcher<examples::Ping> ping_fetcher = |
| 65 | shm_event_loop.MakeFetcher<examples::Ping>("/test"); |
| 66 | |
| 67 | shm_event_loop.AddTimer([]() { LOG(INFO) << "Hello, World!"; }) |
| 68 | ->Setup(shm_event_loop.monotonic_now(), std::chrono::seconds(1)); |
| 69 | |
| 70 | shm_event_loop.Run(); |
| 71 | reader.Deregister(); |
| 72 | |
| 73 | ASSERT_TRUE(ping_fetcher.Fetch()); |
| 74 | ASSERT_EQ(ping_fetcher->value(), 210); |
| 75 | } |
| 76 | } // namespace aos::logger::testing |