blob: 98f6a7434b838b904d419275486071477fa13a25 [file] [log] [blame]
James Kuszmaul38735e82019-12-07 16:42:06 -08001#include "aos/events/logging/logger.h"
Austin Schuhe309d2a2019-11-29 13:25:21 -08002
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
10namespace aos {
11namespace logger {
12namespace testing {
13
14namespace chrono = std::chrono;
15
16class 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 Schuh5f1cc5c2019-12-01 18:01:11 -080022 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080023 ping_(ping_event_loop_.get()),
Austin Schuh5f1cc5c2019-12-01 18:01:11 -080024 pong_event_loop_(event_loop_factory_.MakeEventLoop("pong")),
Austin Schuhe309d2a2019-11-29 13:25:21 -080025 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.
42TEST_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 Schuh5f1cc5c2019-12-01 18:01:11 -080053 event_loop_factory_.MakeEventLoop("logger");
Austin Schuhe309d2a2019-11-29 13:25:21 -080054
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 Kuszmaulc7bbb3e2020-01-03 20:01:00 -080062 // 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 Schuhe309d2a2019-11-29 13:25:21 -080065
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080066 // Confirm that we can remap logged channels to point to new buses.
67 reader.RemapLoggedChannel<aos::examples::Ping>("/test", "/original");
Austin Schuhe309d2a2019-11-29 13:25:21 -080068
Austin Schuh15649d62019-12-28 16:36:38 -080069 // This sends out the fetched messages and advances time to the start of the
70 // log file.
James Kuszmaul84ff3e52020-01-03 19:48:53 -080071 reader.Register();
Austin Schuhe309d2a2019-11-29 13:25:21 -080072
James Kuszmaul84ff3e52020-01-03 19:48:53 -080073 EXPECT_EQ(reader.node(), nullptr);
74
75 EXPECT_EQ(reader.event_loop_factory()->node(), nullptr);
Austin Schuh0bf5b9e2019-12-30 18:16:09 -080076
Austin Schuhe309d2a2019-11-29 13:25:21 -080077 std::unique_ptr<EventLoop> test_event_loop =
James Kuszmaul84ff3e52020-01-03 19:48:53 -080078 reader.event_loop_factory()->MakeEventLoop("log_reader");
Austin Schuhe309d2a2019-11-29 13:25:21 -080079
80 int ping_count = 10;
81 int pong_count = 10;
82
James Kuszmaulc7bbb3e2020-01-03 20:01:00 -080083 // Confirm that the ping value matches in the remapped channel location.
84 test_event_loop->MakeWatcher("/original/test",
Austin Schuhe309d2a2019-11-29 13:25:21 -080085 [&ping_count](const examples::Ping &ping) {
86 EXPECT_EQ(ping.value(), ping_count + 1);
87 ++ping_count;
88 });
89 // Confirm that the ping and pong counts both match, and the value also
90 // matches.
91 test_event_loop->MakeWatcher(
92 "/test", [&pong_count, &ping_count](const examples::Pong &pong) {
93 EXPECT_EQ(pong.value(), pong_count + 1);
94 ++pong_count;
95 EXPECT_EQ(ping_count, pong_count);
96 });
97
James Kuszmaul84ff3e52020-01-03 19:48:53 -080098 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
Austin Schuhe309d2a2019-11-29 13:25:21 -080099 EXPECT_EQ(ping_count, 2010);
Austin Schuhe309d2a2019-11-29 13:25:21 -0800100}
101
Austin Schuh4c4e0092019-12-22 16:18:03 -0800102// Tests that a large number of messages per second doesn't overwhelm writev.
103TEST_F(LoggerTest, ManyMessages) {
104 const ::std::string tmpdir(getenv("TEST_TMPDIR"));
105 const ::std::string logfile = tmpdir + "/logfile.bfbs";
106 // Remove the log file.
107 unlink(logfile.c_str());
108
109 LOG(INFO) << "Logging data to " << logfile;
110
111 {
112 DetachedBufferWriter writer(logfile);
113 std::unique_ptr<EventLoop> logger_event_loop =
114 event_loop_factory_.MakeEventLoop("logger");
115
116 std::unique_ptr<EventLoop> ping_spammer_event_loop =
117 event_loop_factory_.MakeEventLoop("ping_spammer");
118 aos::Sender<examples::Ping> ping_sender =
119 ping_spammer_event_loop->MakeSender<examples::Ping>("/test");
120
121 aos::TimerHandler *timer_handler =
122 ping_spammer_event_loop->AddTimer([&ping_sender]() {
123 aos::Sender<examples::Ping>::Builder builder =
124 ping_sender.MakeBuilder();
125 examples::Ping::Builder ping_builder =
126 builder.MakeBuilder<examples::Ping>();
127 CHECK(builder.Send(ping_builder.Finish()));
128 });
129
130 // 100 ms / 0.05 ms -> 2000 messages. Should be enough to crash it.
131 ping_spammer_event_loop->OnRun([&ping_spammer_event_loop, timer_handler]() {
132 timer_handler->Setup(ping_spammer_event_loop->monotonic_now(),
133 chrono::microseconds(50));
134 });
135
136 Logger logger(&writer, logger_event_loop.get(),
137 std::chrono::milliseconds(100));
138
139 event_loop_factory_.RunFor(chrono::milliseconds(1000));
140 }
141}
142
Austin Schuh15649d62019-12-28 16:36:38 -0800143class MultinodeLoggerTest : public ::testing::Test {
144 public:
145 MultinodeLoggerTest()
146 : config_(aos::configuration::ReadConfig(
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800147 "aos/events/logging/multinode_pingpong_config.json")),
Austin Schuh15649d62019-12-28 16:36:38 -0800148 event_loop_factory_(&config_.message(), "pi1"),
149 ping_event_loop_(event_loop_factory_.MakeEventLoop("ping")),
150 ping_(ping_event_loop_.get()) {}
151
152 // Config and factory.
153 aos::FlatbufferDetachedBuffer<aos::Configuration> config_;
154 SimulatedEventLoopFactory event_loop_factory_;
155
156 // Event loop and app for Ping
157 std::unique_ptr<EventLoop> ping_event_loop_;
158 Ping ping_;
159};
160
161// Tests that we can startup at all in a multinode configuration.
162TEST_F(MultinodeLoggerTest, MultiNode) {
163 constexpr chrono::seconds kTimeOffset = chrono::seconds(10000);
164 constexpr uint32_t kQueueIndexOffset = 1024;
165 const ::std::string tmpdir(getenv("TEST_TMPDIR"));
166 const ::std::string logfile = tmpdir + "/multi_logfile.bfbs";
167 // Remove it.
168 unlink(logfile.c_str());
169
170 LOG(INFO) << "Logging data to " << logfile;
171
172 {
173 std::unique_ptr<EventLoop> pong_event_loop =
174 event_loop_factory_.MakeEventLoop("pong");
175
176 std::unique_ptr<aos::RawSender> pong_sender(
177 pong_event_loop->MakeRawSender(aos::configuration::GetChannel(
178 pong_event_loop->configuration(), "/test", "aos.examples.Pong",
179 pong_event_loop->name(), pong_event_loop->node())));
180
181 // Ok, let's fake a remote node. We use the fancy raw sender Send
182 // method that message_gateway will use to do that.
183 int pong_count = 0;
184 pong_event_loop->MakeWatcher(
185 "/test", [&pong_event_loop, &pong_count, &pong_sender,
186 kTimeOffset](const examples::Ping &ping) {
187 flatbuffers::FlatBufferBuilder fbb;
188 examples::Pong::Builder pong_builder(fbb);
189 pong_builder.add_value(ping.value());
190 pong_builder.add_initial_send_time(ping.send_time());
191 fbb.Finish(pong_builder.Finish());
192
193 pong_sender->Send(fbb.GetBufferPointer(), fbb.GetSize(),
194 pong_event_loop->monotonic_now() + kTimeOffset,
195 pong_event_loop->realtime_now() + kTimeOffset,
196 kQueueIndexOffset + pong_count);
197 ++pong_count;
198 });
199
200 DetachedBufferWriter writer(logfile);
201 std::unique_ptr<EventLoop> logger_event_loop =
202 event_loop_factory_.MakeEventLoop("logger");
203
204 event_loop_factory_.RunFor(chrono::milliseconds(95));
205
206 Logger logger(&writer, logger_event_loop.get(),
207 std::chrono::milliseconds(100));
208 event_loop_factory_.RunFor(chrono::milliseconds(20000));
209 }
210
211 LogReader reader(logfile);
212
213 // TODO(austin): Also replay as pi2 or pi3 and make sure we see the pong
214 // messages. This won't work today yet until the log reading code gets
215 // significantly better.
Austin Schuh15649d62019-12-28 16:36:38 -0800216
217 // This sends out the fetched messages and advances time to the start of the
218 // log file.
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800219 reader.Register();
220
221 ASSERT_NE(reader.node(), nullptr);
222 EXPECT_EQ(reader.node()->name()->string_view(), "pi1");
223
224 reader.event_loop_factory()->set_send_delay(chrono::microseconds(0));
Austin Schuh15649d62019-12-28 16:36:38 -0800225
226 std::unique_ptr<EventLoop> test_event_loop =
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800227 reader.event_loop_factory()->MakeEventLoop("test");
Austin Schuh15649d62019-12-28 16:36:38 -0800228
229 int ping_count = 10;
230 int pong_count = 10;
231
232 // Confirm that the ping value matches.
233 test_event_loop->MakeWatcher("/test",
234 [&ping_count](const examples::Ping &ping) {
235 EXPECT_EQ(ping.value(), ping_count + 1);
236 ++ping_count;
237 });
238 // Confirm that the ping and pong counts both match, and the value also
239 // matches.
240 test_event_loop->MakeWatcher(
241 "/test", [&test_event_loop, &ping_count, &pong_count,
242 kTimeOffset](const examples::Pong &pong) {
243 EXPECT_EQ(test_event_loop->context().remote_queue_index,
244 pong_count + kQueueIndexOffset);
245 EXPECT_EQ(test_event_loop->context().monotonic_remote_time,
246 test_event_loop->monotonic_now() + kTimeOffset);
247 EXPECT_EQ(test_event_loop->context().realtime_remote_time,
248 test_event_loop->realtime_now() + kTimeOffset);
249
250 EXPECT_EQ(pong.value(), pong_count + 1);
251 ++pong_count;
252 EXPECT_EQ(ping_count, pong_count);
253 });
254
James Kuszmaul84ff3e52020-01-03 19:48:53 -0800255 reader.event_loop_factory()->RunFor(std::chrono::seconds(100));
Austin Schuh15649d62019-12-28 16:36:38 -0800256 EXPECT_EQ(ping_count, 2010);
257 EXPECT_EQ(pong_count, 2010);
Austin Schuh15649d62019-12-28 16:36:38 -0800258}
259
Austin Schuhe309d2a2019-11-29 13:25:21 -0800260} // namespace testing
261} // namespace logger
262} // namespace aos