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 | |
| 62 | LogReader reader(logfile); |
| 63 | |
| 64 | LOG(INFO) << "Config " << FlatbufferToJson(reader.configuration()); |
| 65 | |
| 66 | // TODO(austin): Figure out what the API needs to look like. How do we replay |
| 67 | // the data that was fetched before it all starts? How do we set the starting |
| 68 | // time from the log file? Probably need to let the reader do more if it |
| 69 | // knows about the factory. |
| 70 | SimulatedEventLoopFactory log_reader_factory(reader.configuration()); |
| 71 | std::unique_ptr<EventLoop> reader_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 72 | log_reader_factory.MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 73 | |
| 74 | reader.Register(reader_event_loop.get()); |
| 75 | |
| 76 | // Capture monotonic start time in OnRun and offset from there? Let the user |
| 77 | // configure the factory if they want time to match? |
| 78 | /* |
| 79 | log_reader_factory.InitializeTime(log_reader.monotonic_start_time(), |
| 80 | log_reader.realtime_start_time()); |
| 81 | */ |
| 82 | std::unique_ptr<EventLoop> test_event_loop = |
Austin Schuh | 5f1cc5c | 2019-12-01 18:01:11 -0800 | [diff] [blame] | 83 | log_reader_factory.MakeEventLoop("log_reader"); |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 84 | |
| 85 | int ping_count = 10; |
| 86 | int pong_count = 10; |
| 87 | |
| 88 | // Confirm that the ping value matches. |
| 89 | test_event_loop->MakeWatcher("/test", |
| 90 | [&ping_count](const examples::Ping &ping) { |
| 91 | EXPECT_EQ(ping.value(), ping_count + 1); |
| 92 | ++ping_count; |
| 93 | }); |
| 94 | // Confirm that the ping and pong counts both match, and the value also |
| 95 | // matches. |
| 96 | test_event_loop->MakeWatcher( |
| 97 | "/test", [&pong_count, &ping_count](const examples::Pong &pong) { |
| 98 | EXPECT_EQ(pong.value(), pong_count + 1); |
| 99 | ++pong_count; |
| 100 | EXPECT_EQ(ping_count, pong_count); |
| 101 | }); |
| 102 | |
| 103 | log_reader_factory.RunFor(std::chrono::seconds(100)); |
| 104 | EXPECT_EQ(ping_count, 2010); |
| 105 | |
| 106 | reader.Deregister(); |
| 107 | } |
| 108 | |
| 109 | } // namespace testing |
| 110 | } // namespace logger |
| 111 | } // namespace aos |