Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 1 | #include "aos/events/logging/logfile_utils.h" |
| 2 | |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 3 | #include <chrono> |
| 4 | #include <string> |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 5 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 6 | #include "aos/events/logging/logfile_sorting.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 7 | #include "aos/events/logging/test_message_generated.h" |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 8 | #include "aos/flatbuffer_merge.h" |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 9 | #include "aos/flatbuffers.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 10 | #include "aos/json_to_flatbuffer.h" |
| 11 | #include "aos/testing/tmpdir.h" |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
| 15 | namespace logger { |
| 16 | namespace testing { |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 17 | namespace chrono = std::chrono; |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 18 | |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 19 | // Creates a size prefixed flatbuffer from json. |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 20 | template <typename T> |
| 21 | SizePrefixedFlatbufferDetachedBuffer<T> JsonToSizedFlatbuffer( |
| 22 | const std::string_view data) { |
| 23 | flatbuffers::FlatBufferBuilder fbb; |
| 24 | fbb.ForceDefaults(true); |
| 25 | fbb.FinishSizePrefixed(JsonToFlatbuffer<T>(data, &fbb)); |
| 26 | return fbb.Release(); |
| 27 | } |
| 28 | |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 29 | // Tests that we can write and read 2 flatbuffers to file. |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 30 | TEST(SpanReaderTest, ReadWrite) { |
| 31 | const std::string logfile = aos::testing::TestTmpDir() + "/log.bfbs"; |
| 32 | unlink(logfile.c_str()); |
| 33 | |
| 34 | const aos::SizePrefixedFlatbufferDetachedBuffer<TestMessage> m1 = |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 35 | JsonToSizedFlatbuffer<TestMessage>(R"({ "value": 1 })"); |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 36 | const aos::SizePrefixedFlatbufferDetachedBuffer<TestMessage> m2 = |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 37 | JsonToSizedFlatbuffer<TestMessage>(R"({ "value": 2 })"); |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 38 | |
| 39 | { |
| 40 | DetachedBufferWriter writer(logfile, std::make_unique<DummyEncoder>()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 41 | writer.QueueSpan(m1.span()); |
| 42 | writer.QueueSpan(m2.span()); |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | SpanReader reader(logfile); |
| 46 | |
| 47 | EXPECT_EQ(reader.filename(), logfile); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 48 | EXPECT_EQ(reader.ReadMessage(), m1.span()); |
| 49 | EXPECT_EQ(reader.ReadMessage(), m2.span()); |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 50 | EXPECT_EQ(reader.ReadMessage(), absl::Span<const uint8_t>()); |
| 51 | } |
| 52 | |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 53 | // Tests that we can actually parse the resulting messages at a basic level |
| 54 | // through MessageReader. |
| 55 | TEST(MessageReaderTest, ReadWrite) { |
| 56 | const std::string logfile = aos::testing::TestTmpDir() + "/log.bfbs"; |
| 57 | unlink(logfile.c_str()); |
| 58 | |
| 59 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config = |
| 60 | JsonToSizedFlatbuffer<LogFileHeader>( |
| 61 | R"({ "max_out_of_order_duration": 100000000 })"); |
| 62 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m1 = |
| 63 | JsonToSizedFlatbuffer<MessageHeader>( |
| 64 | R"({ "channel_index": 0, "monotonic_sent_time": 1 })"); |
| 65 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m2 = |
| 66 | JsonToSizedFlatbuffer<MessageHeader>( |
| 67 | R"({ "channel_index": 0, "monotonic_sent_time": 2 })"); |
| 68 | |
| 69 | { |
| 70 | DetachedBufferWriter writer(logfile, std::make_unique<DummyEncoder>()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 71 | writer.QueueSpan(config.span()); |
| 72 | writer.QueueSpan(m1.span()); |
| 73 | writer.QueueSpan(m2.span()); |
Austin Schuh | e243aaf | 2020-10-11 15:46:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | MessageReader reader(logfile); |
| 77 | |
| 78 | EXPECT_EQ(reader.filename(), logfile); |
| 79 | |
| 80 | EXPECT_EQ( |
| 81 | reader.max_out_of_order_duration(), |
| 82 | std::chrono::nanoseconds(config.message().max_out_of_order_duration())); |
| 83 | EXPECT_EQ(reader.newest_timestamp(), monotonic_clock::min_time); |
| 84 | EXPECT_TRUE(reader.ReadMessage()); |
| 85 | EXPECT_EQ(reader.newest_timestamp(), |
| 86 | monotonic_clock::time_point(chrono::nanoseconds(1))); |
| 87 | EXPECT_TRUE(reader.ReadMessage()); |
| 88 | EXPECT_EQ(reader.newest_timestamp(), |
| 89 | monotonic_clock::time_point(chrono::nanoseconds(2))); |
| 90 | EXPECT_FALSE(reader.ReadMessage()); |
| 91 | } |
| 92 | |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 93 | // Tests that we explode when messages are too far out of order. |
| 94 | TEST(PartsMessageReaderDeathTest, TooFarOutOfOrder) { |
| 95 | const std::string logfile0 = aos::testing::TestTmpDir() + "/log0.bfbs"; |
| 96 | unlink(logfile0.c_str()); |
| 97 | |
| 98 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config0 = |
| 99 | JsonToSizedFlatbuffer<LogFileHeader>( |
| 100 | R"({ |
| 101 | "max_out_of_order_duration": 100000000, |
| 102 | "log_event_uuid": "30ef1283-81d7-4004-8c36-1c162dbcb2b2", |
| 103 | "parts_uuid": "2a05d725-5d5c-4c0b-af42-88de2f3c3876", |
| 104 | "parts_index": 0 |
| 105 | })"); |
| 106 | |
| 107 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m1 = |
| 108 | JsonToSizedFlatbuffer<MessageHeader>( |
| 109 | R"({ "channel_index": 0, "monotonic_sent_time": 100000000 })"); |
| 110 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m2 = |
| 111 | JsonToSizedFlatbuffer<MessageHeader>( |
| 112 | R"({ "channel_index": 0, "monotonic_sent_time": 0 })"); |
| 113 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m3 = |
| 114 | JsonToSizedFlatbuffer<MessageHeader>( |
| 115 | R"({ "channel_index": 0, "monotonic_sent_time": -1 })"); |
| 116 | |
| 117 | { |
| 118 | DetachedBufferWriter writer(logfile0, std::make_unique<DummyEncoder>()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 119 | writer.QueueSpan(config0.span()); |
| 120 | writer.QueueSpan(m1.span()); |
| 121 | writer.QueueSpan(m2.span()); |
| 122 | writer.QueueSpan(m3.span()); |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | const std::vector<LogFile> parts = SortParts({logfile0}); |
| 126 | |
| 127 | PartsMessageReader reader(parts[0].parts[0]); |
| 128 | |
| 129 | EXPECT_TRUE(reader.ReadMessage()); |
| 130 | EXPECT_TRUE(reader.ReadMessage()); |
| 131 | EXPECT_DEATH({ reader.ReadMessage(); }, "-0.000000001sec vs. 0.000000000sec"); |
| 132 | } |
| 133 | |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 134 | // Tests that we can transparently re-assemble part files with a |
| 135 | // PartsMessageReader. |
| 136 | TEST(PartsMessageReaderTest, ReadWrite) { |
| 137 | const std::string logfile0 = aos::testing::TestTmpDir() + "/log0.bfbs"; |
| 138 | const std::string logfile1 = aos::testing::TestTmpDir() + "/log1.bfbs"; |
| 139 | unlink(logfile0.c_str()); |
| 140 | unlink(logfile1.c_str()); |
| 141 | |
| 142 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config0 = |
| 143 | JsonToSizedFlatbuffer<LogFileHeader>( |
| 144 | R"({ |
| 145 | "max_out_of_order_duration": 100000000, |
| 146 | "log_event_uuid": "30ef1283-81d7-4004-8c36-1c162dbcb2b2", |
| 147 | "parts_uuid": "2a05d725-5d5c-4c0b-af42-88de2f3c3876", |
| 148 | "parts_index": 0 |
| 149 | })"); |
| 150 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config1 = |
| 151 | JsonToSizedFlatbuffer<LogFileHeader>( |
| 152 | R"({ |
| 153 | "max_out_of_order_duration": 200000000, |
| 154 | "monotonic_start_time": 0, |
| 155 | "realtime_start_time": 0, |
| 156 | "log_event_uuid": "30ef1283-81d7-4004-8c36-1c162dbcb2b2", |
| 157 | "parts_uuid": "2a05d725-5d5c-4c0b-af42-88de2f3c3876", |
| 158 | "parts_index": 1 |
| 159 | })"); |
| 160 | |
| 161 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m1 = |
| 162 | JsonToSizedFlatbuffer<MessageHeader>( |
| 163 | R"({ "channel_index": 0, "monotonic_sent_time": 1 })"); |
| 164 | const aos::SizePrefixedFlatbufferDetachedBuffer<MessageHeader> m2 = |
| 165 | JsonToSizedFlatbuffer<MessageHeader>( |
| 166 | R"({ "channel_index": 0, "monotonic_sent_time": 2 })"); |
| 167 | |
| 168 | { |
| 169 | DetachedBufferWriter writer(logfile0, std::make_unique<DummyEncoder>()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 170 | writer.QueueSpan(config0.span()); |
| 171 | writer.QueueSpan(m1.span()); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 172 | } |
| 173 | { |
| 174 | DetachedBufferWriter writer(logfile1, std::make_unique<DummyEncoder>()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 175 | writer.QueueSpan(config1.span()); |
| 176 | writer.QueueSpan(m2.span()); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | const std::vector<LogFile> parts = SortParts({logfile0, logfile1}); |
| 180 | |
| 181 | PartsMessageReader reader(parts[0].parts[0]); |
| 182 | |
| 183 | EXPECT_EQ(reader.filename(), logfile0); |
| 184 | |
| 185 | // Confirm that the timestamps track, and the filename also updates. |
| 186 | // Read the first message. |
| 187 | EXPECT_EQ(reader.newest_timestamp(), monotonic_clock::min_time); |
| 188 | EXPECT_EQ( |
| 189 | reader.max_out_of_order_duration(), |
| 190 | std::chrono::nanoseconds(config0.message().max_out_of_order_duration())); |
| 191 | EXPECT_TRUE(reader.ReadMessage()); |
| 192 | EXPECT_EQ(reader.filename(), logfile0); |
| 193 | EXPECT_EQ(reader.newest_timestamp(), |
| 194 | monotonic_clock::time_point(chrono::nanoseconds(1))); |
| 195 | EXPECT_EQ( |
| 196 | reader.max_out_of_order_duration(), |
| 197 | std::chrono::nanoseconds(config0.message().max_out_of_order_duration())); |
| 198 | |
| 199 | // Read the second message. |
| 200 | EXPECT_TRUE(reader.ReadMessage()); |
| 201 | EXPECT_EQ(reader.filename(), logfile1); |
| 202 | EXPECT_EQ(reader.newest_timestamp(), |
| 203 | monotonic_clock::time_point(chrono::nanoseconds(2))); |
| 204 | EXPECT_EQ( |
| 205 | reader.max_out_of_order_duration(), |
| 206 | std::chrono::nanoseconds(config1.message().max_out_of_order_duration())); |
| 207 | |
| 208 | // And then confirm that reading again returns no message. |
| 209 | EXPECT_FALSE(reader.ReadMessage()); |
| 210 | EXPECT_EQ(reader.filename(), logfile1); |
| 211 | EXPECT_EQ( |
| 212 | reader.max_out_of_order_duration(), |
| 213 | std::chrono::nanoseconds(config1.message().max_out_of_order_duration())); |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 214 | EXPECT_EQ(reader.newest_timestamp(), monotonic_clock::max_time); |
Austin Schuh | c41603c | 2020-10-11 16:17:37 -0700 | [diff] [blame] | 215 | } |
Austin Schuh | 32f6849 | 2020-11-08 21:45:51 -0800 | [diff] [blame] | 216 | |
Austin Schuh | 1be0ce4 | 2020-11-29 22:43:26 -0800 | [diff] [blame] | 217 | // Tests that Message's operator < works as expected. |
| 218 | TEST(MessageTest, Sorting) { |
| 219 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 220 | |
| 221 | Message m1{.channel_index = 0, |
| 222 | .queue_index = 0, |
| 223 | .timestamp = e + chrono::milliseconds(1), |
| 224 | .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()}; |
| 225 | Message m2{.channel_index = 0, |
| 226 | .queue_index = 0, |
| 227 | .timestamp = e + chrono::milliseconds(2), |
| 228 | .data = SizePrefixedFlatbufferVector<MessageHeader>::Empty()}; |
| 229 | |
| 230 | EXPECT_LT(m1, m2); |
| 231 | EXPECT_GE(m2, m1); |
| 232 | |
| 233 | m1.timestamp = e; |
| 234 | m2.timestamp = e; |
| 235 | |
| 236 | m1.channel_index = 1; |
| 237 | m2.channel_index = 2; |
| 238 | |
| 239 | EXPECT_LT(m1, m2); |
| 240 | EXPECT_GE(m2, m1); |
| 241 | |
| 242 | m1.channel_index = 0; |
| 243 | m2.channel_index = 0; |
| 244 | m1.queue_index = 0; |
| 245 | m2.queue_index = 1; |
| 246 | |
| 247 | EXPECT_LT(m1, m2); |
| 248 | EXPECT_GE(m2, m1); |
| 249 | } |
| 250 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 251 | aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> MakeHeader( |
| 252 | const aos::FlatbufferDetachedBuffer<Configuration> &config, |
| 253 | const std::string_view json) { |
| 254 | flatbuffers::FlatBufferBuilder fbb; |
| 255 | flatbuffers::Offset<Configuration> config_offset = |
| 256 | aos::CopyFlatBuffer(config, &fbb); |
| 257 | LogFileHeader::Builder header_builder(fbb); |
| 258 | header_builder.add_configuration(config_offset); |
| 259 | fbb.Finish(header_builder.Finish()); |
| 260 | aos::FlatbufferDetachedBuffer<LogFileHeader> config_header(fbb.Release()); |
| 261 | |
| 262 | aos::FlatbufferDetachedBuffer<LogFileHeader> header_updates( |
| 263 | JsonToFlatbuffer<LogFileHeader>(json)); |
| 264 | CHECK(header_updates.Verify()); |
| 265 | flatbuffers::FlatBufferBuilder fbb2; |
| 266 | fbb2.FinishSizePrefixed( |
| 267 | aos::MergeFlatBuffers(config_header, header_updates, &fbb2)); |
| 268 | return fbb2.Release(); |
| 269 | } |
| 270 | |
| 271 | class SortingElementTest : public ::testing::Test { |
| 272 | public: |
| 273 | SortingElementTest() |
| 274 | : config_(JsonToFlatbuffer<Configuration>( |
| 275 | R"({ |
| 276 | "channels": [ |
| 277 | { |
| 278 | "name": "/a", |
| 279 | "type": "aos.logger.testing.TestMessage", |
| 280 | "source_node": "pi1", |
| 281 | "destination_nodes": [ |
| 282 | { |
| 283 | "name": "pi2" |
| 284 | }, |
| 285 | { |
| 286 | "name": "pi3" |
| 287 | } |
| 288 | ] |
| 289 | }, |
| 290 | { |
| 291 | "name": "/b", |
| 292 | "type": "aos.logger.testing.TestMessage", |
| 293 | "source_node": "pi1" |
| 294 | }, |
| 295 | { |
| 296 | "name": "/c", |
| 297 | "type": "aos.logger.testing.TestMessage", |
| 298 | "source_node": "pi1" |
| 299 | } |
| 300 | ], |
| 301 | "nodes": [ |
| 302 | { |
| 303 | "name": "pi1" |
| 304 | }, |
| 305 | { |
| 306 | "name": "pi2" |
| 307 | }, |
| 308 | { |
| 309 | "name": "pi3" |
| 310 | } |
| 311 | ] |
| 312 | } |
| 313 | )")), |
| 314 | config0_(MakeHeader(config_, R"({ |
| 315 | /* 100ms */ |
| 316 | "max_out_of_order_duration": 100000000, |
| 317 | "node": { |
| 318 | "name": "pi1" |
| 319 | }, |
| 320 | "logger_node": { |
| 321 | "name": "pi1" |
| 322 | }, |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 323 | "monotonic_start_time": 1000000, |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 324 | "realtime_start_time": 1000000000000, |
| 325 | "log_event_uuid": "30ef1283-81d7-4004-8c36-1c162dbcb2b2", |
| 326 | "parts_uuid": "2a05d725-5d5c-4c0b-af42-88de2f3c3876", |
| 327 | "parts_index": 0 |
| 328 | })")), |
| 329 | config1_(MakeHeader(config_, |
| 330 | R"({ |
| 331 | /* 100ms */ |
| 332 | "max_out_of_order_duration": 100000000, |
| 333 | "node": { |
| 334 | "name": "pi1" |
| 335 | }, |
| 336 | "logger_node": { |
| 337 | "name": "pi1" |
| 338 | }, |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 339 | "monotonic_start_time": 1000000, |
| 340 | "realtime_start_time": 1000000000000, |
| 341 | "log_event_uuid": "30ef1283-81d7-4004-8c36-1c162dbcb2b2", |
| 342 | "parts_uuid": "bafe9f8e-7dea-4bd9-95f5-3d8390e49208", |
| 343 | "parts_index": 0 |
| 344 | })")), |
| 345 | config2_(MakeHeader(config_, |
| 346 | R"({ |
| 347 | /* 100ms */ |
| 348 | "max_out_of_order_duration": 100000000, |
| 349 | "node": { |
| 350 | "name": "pi2" |
| 351 | }, |
| 352 | "logger_node": { |
| 353 | "name": "pi2" |
| 354 | }, |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 355 | "monotonic_start_time": 0, |
| 356 | "realtime_start_time": 1000000000000, |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 357 | "log_event_uuid": "cb89a1ce-c4b6-4747-a647-051f09ac888c", |
| 358 | "parts_uuid": "e6bff6c6-757f-4675-90d8-3bfb642870e6", |
| 359 | "parts_index": 0 |
| 360 | })")), |
| 361 | config3_(MakeHeader(config_, |
| 362 | R"({ |
| 363 | /* 100ms */ |
| 364 | "max_out_of_order_duration": 100000000, |
| 365 | "node": { |
| 366 | "name": "pi1" |
| 367 | }, |
| 368 | "logger_node": { |
| 369 | "name": "pi1" |
| 370 | }, |
| 371 | "monotonic_start_time": 2000000, |
| 372 | "realtime_start_time": 1000000000, |
| 373 | "log_event_uuid": "cb26b86a-473e-4f74-8403-50eb92ed60ad", |
| 374 | "parts_uuid": "1f098701-949f-4392-81f9-be463e2d7bd4", |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 375 | "parts_index": 0 |
| 376 | })")) { |
| 377 | unlink(logfile0_.c_str()); |
| 378 | unlink(logfile1_.c_str()); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 379 | unlink(logfile2_.c_str()); |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 380 | queue_index_.resize(kChannels); |
| 381 | } |
| 382 | |
| 383 | protected: |
| 384 | static constexpr size_t kChannels = 3u; |
| 385 | |
| 386 | flatbuffers::DetachedBuffer MakeLogMessage( |
| 387 | const aos::monotonic_clock::time_point monotonic_now, int channel_index, |
| 388 | int value) { |
| 389 | flatbuffers::FlatBufferBuilder message_fbb; |
| 390 | message_fbb.ForceDefaults(true); |
| 391 | TestMessage::Builder test_message_builder(message_fbb); |
| 392 | test_message_builder.add_value(value); |
| 393 | message_fbb.Finish(test_message_builder.Finish()); |
| 394 | |
| 395 | aos::Context context; |
| 396 | context.monotonic_event_time = monotonic_now; |
| 397 | context.realtime_event_time = aos::realtime_clock::epoch() + |
| 398 | chrono::seconds(1000) + |
| 399 | monotonic_now.time_since_epoch(); |
| 400 | context.queue_index = queue_index_[channel_index]; |
| 401 | context.size = message_fbb.GetSize(); |
| 402 | context.data = message_fbb.GetBufferPointer(); |
| 403 | |
| 404 | ++queue_index_[channel_index]; |
| 405 | |
| 406 | flatbuffers::FlatBufferBuilder fbb; |
| 407 | fbb.FinishSizePrefixed( |
| 408 | PackMessage(&fbb, context, channel_index, LogType::kLogMessage)); |
| 409 | |
| 410 | return fbb.Release(); |
| 411 | } |
| 412 | |
| 413 | flatbuffers::DetachedBuffer MakeTimestampMessage( |
| 414 | const aos::monotonic_clock::time_point sender_monotonic_now, |
| 415 | int channel_index, chrono::nanoseconds receiver_monotonic_offset) { |
| 416 | aos::Context context; |
| 417 | context.monotonic_remote_time = sender_monotonic_now; |
| 418 | context.realtime_remote_time = aos::realtime_clock::epoch() + |
| 419 | chrono::seconds(1000) + |
| 420 | sender_monotonic_now.time_since_epoch(); |
| 421 | context.remote_queue_index = queue_index_[channel_index] - 1; |
| 422 | context.monotonic_event_time = |
| 423 | sender_monotonic_now + receiver_monotonic_offset; |
| 424 | context.realtime_event_time = |
| 425 | aos::realtime_clock::epoch() + chrono::seconds(1000) + |
| 426 | context.monotonic_event_time.time_since_epoch(); |
| 427 | context.queue_index = queue_index_[channel_index] - 1 + 100; |
| 428 | context.size = 0; |
| 429 | context.data = nullptr; |
| 430 | |
| 431 | flatbuffers::FlatBufferBuilder fbb; |
| 432 | fbb.FinishSizePrefixed(PackMessage(&fbb, context, channel_index, |
| 433 | LogType::kLogDeliveryTimeOnly)); |
| 434 | LOG(INFO) << aos::FlatbufferToJson( |
| 435 | aos::SizePrefixedFlatbufferSpan<MessageHeader>( |
| 436 | absl::Span<uint8_t>(fbb.GetBufferPointer(), fbb.GetSize()))); |
| 437 | |
| 438 | return fbb.Release(); |
| 439 | } |
| 440 | |
| 441 | const std::string logfile0_ = aos::testing::TestTmpDir() + "/log0.bfbs"; |
| 442 | const std::string logfile1_ = aos::testing::TestTmpDir() + "/log1.bfbs"; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 443 | const std::string logfile2_ = aos::testing::TestTmpDir() + "/log2.bfbs"; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 444 | |
| 445 | const aos::FlatbufferDetachedBuffer<Configuration> config_; |
| 446 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config0_; |
| 447 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config1_; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 448 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config2_; |
| 449 | const aos::SizePrefixedFlatbufferDetachedBuffer<LogFileHeader> config3_; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 450 | |
| 451 | std::vector<uint32_t> queue_index_; |
| 452 | }; |
| 453 | |
| 454 | using LogPartsSorterTest = SortingElementTest; |
| 455 | using LogPartsSorterDeathTest = LogPartsSorterTest; |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 456 | using NodeMergerTest = SortingElementTest; |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 457 | using TimestampMapperTest = SortingElementTest; |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 458 | |
| 459 | // Tests that we can pull messages out of a log sorted in order. |
| 460 | TEST_F(LogPartsSorterTest, Pull) { |
| 461 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 462 | { |
| 463 | DetachedBufferWriter writer(logfile0_, std::make_unique<DummyEncoder>()); |
| 464 | writer.QueueSpan(config0_.span()); |
| 465 | writer.QueueSizedFlatbuffer( |
| 466 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 467 | writer.QueueSizedFlatbuffer( |
| 468 | MakeLogMessage(e + chrono::milliseconds(1000), 1, 0x105)); |
| 469 | writer.QueueSizedFlatbuffer( |
| 470 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 471 | writer.QueueSizedFlatbuffer( |
| 472 | MakeLogMessage(e + chrono::milliseconds(1901), 1, 0x107)); |
| 473 | } |
| 474 | |
| 475 | const std::vector<LogFile> parts = SortParts({logfile0_}); |
| 476 | |
| 477 | LogPartsSorter parts_sorter(parts[0].parts[0]); |
| 478 | |
| 479 | // Confirm we aren't sorted until any time until the message is popped. |
| 480 | // Peeking shouldn't change the sorted until time. |
| 481 | EXPECT_EQ(parts_sorter.sorted_until(), monotonic_clock::min_time); |
| 482 | |
| 483 | std::deque<Message> output; |
| 484 | |
| 485 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 486 | output.emplace_back(std::move(*parts_sorter.Front())); |
| 487 | parts_sorter.PopFront(); |
| 488 | EXPECT_EQ(parts_sorter.sorted_until(), e + chrono::milliseconds(1900)); |
| 489 | |
| 490 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 491 | output.emplace_back(std::move(*parts_sorter.Front())); |
| 492 | parts_sorter.PopFront(); |
| 493 | EXPECT_EQ(parts_sorter.sorted_until(), e + chrono::milliseconds(1900)); |
| 494 | |
| 495 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 496 | output.emplace_back(std::move(*parts_sorter.Front())); |
| 497 | parts_sorter.PopFront(); |
| 498 | EXPECT_EQ(parts_sorter.sorted_until(), monotonic_clock::max_time); |
| 499 | |
| 500 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 501 | output.emplace_back(std::move(*parts_sorter.Front())); |
| 502 | parts_sorter.PopFront(); |
| 503 | EXPECT_EQ(parts_sorter.sorted_until(), monotonic_clock::max_time); |
| 504 | |
| 505 | ASSERT_TRUE(parts_sorter.Front() == nullptr); |
| 506 | |
| 507 | EXPECT_EQ(output[0].timestamp, e + chrono::milliseconds(1000)); |
| 508 | EXPECT_EQ(output[1].timestamp, e + chrono::milliseconds(1000)); |
| 509 | EXPECT_EQ(output[2].timestamp, e + chrono::milliseconds(1901)); |
| 510 | EXPECT_EQ(output[3].timestamp, e + chrono::milliseconds(2000)); |
| 511 | } |
| 512 | |
| 513 | // Tests that messages too far out of order trigger death. |
| 514 | TEST_F(LogPartsSorterDeathTest, Pull) { |
| 515 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 516 | { |
| 517 | DetachedBufferWriter writer(logfile0_, std::make_unique<DummyEncoder>()); |
| 518 | writer.QueueSpan(config0_.span()); |
| 519 | writer.QueueSizedFlatbuffer( |
| 520 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 521 | writer.QueueSizedFlatbuffer( |
| 522 | MakeLogMessage(e + chrono::milliseconds(1000), 1, 0x105)); |
| 523 | writer.QueueSizedFlatbuffer( |
| 524 | MakeLogMessage(e + chrono::milliseconds(2001), 0, 0x006)); |
| 525 | // The following message is too far out of order and will trigger the CHECK. |
| 526 | writer.QueueSizedFlatbuffer( |
| 527 | MakeLogMessage(e + chrono::milliseconds(1900), 1, 0x107)); |
| 528 | } |
| 529 | |
| 530 | const std::vector<LogFile> parts = SortParts({logfile0_}); |
| 531 | |
| 532 | LogPartsSorter parts_sorter(parts[0].parts[0]); |
| 533 | |
| 534 | // Confirm we aren't sorted until any time until the message is popped. |
| 535 | // Peeking shouldn't change the sorted until time. |
| 536 | EXPECT_EQ(parts_sorter.sorted_until(), monotonic_clock::min_time); |
| 537 | std::deque<Message> output; |
| 538 | |
| 539 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 540 | parts_sorter.PopFront(); |
| 541 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 542 | ASSERT_TRUE(parts_sorter.Front() != nullptr); |
| 543 | parts_sorter.PopFront(); |
| 544 | |
| 545 | EXPECT_DEATH({ parts_sorter.Front(); }, "Max out of order exceeded."); |
| 546 | } |
| 547 | |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 548 | // Tests that we can merge data from 2 separate files, including duplicate data. |
| 549 | TEST_F(NodeMergerTest, TwoFileMerger) { |
| 550 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 551 | { |
| 552 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 553 | writer0.QueueSpan(config0_.span()); |
| 554 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 555 | writer1.QueueSpan(config1_.span()); |
| 556 | |
| 557 | writer0.QueueSizedFlatbuffer( |
| 558 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 559 | writer1.QueueSizedFlatbuffer( |
| 560 | MakeLogMessage(e + chrono::milliseconds(1001), 1, 0x105)); |
| 561 | |
| 562 | writer0.QueueSizedFlatbuffer( |
| 563 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 564 | writer1.QueueSizedFlatbuffer( |
| 565 | MakeLogMessage(e + chrono::milliseconds(1002), 1, 0x106)); |
| 566 | |
| 567 | // Make a duplicate! |
| 568 | SizePrefixedFlatbufferDetachedBuffer<MessageHeader> msg( |
| 569 | MakeLogMessage(e + chrono::milliseconds(3000), 0, 0x007)); |
| 570 | writer0.QueueSpan(msg.span()); |
| 571 | writer1.QueueSpan(msg.span()); |
| 572 | |
| 573 | writer1.QueueSizedFlatbuffer( |
| 574 | MakeLogMessage(e + chrono::milliseconds(3002), 1, 0x107)); |
| 575 | } |
| 576 | |
| 577 | const std::vector<LogFile> parts = SortParts({logfile0_, logfile1_}); |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 578 | ASSERT_EQ(parts.size(), 1u); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 579 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 580 | NodeMerger merger(FilterPartsForNode(parts, "pi1")); |
Austin Schuh | 8f52ed5 | 2020-11-30 23:12:39 -0800 | [diff] [blame] | 581 | |
| 582 | EXPECT_EQ(merger.sorted_until(), monotonic_clock::min_time); |
| 583 | |
| 584 | std::deque<Message> output; |
| 585 | |
| 586 | EXPECT_EQ(merger.sorted_until(), monotonic_clock::min_time); |
| 587 | ASSERT_TRUE(merger.Front() != nullptr); |
| 588 | EXPECT_EQ(merger.sorted_until(), e + chrono::milliseconds(1900)); |
| 589 | |
| 590 | output.emplace_back(std::move(*merger.Front())); |
| 591 | merger.PopFront(); |
| 592 | EXPECT_EQ(merger.sorted_until(), e + chrono::milliseconds(1900)); |
| 593 | |
| 594 | ASSERT_TRUE(merger.Front() != nullptr); |
| 595 | output.emplace_back(std::move(*merger.Front())); |
| 596 | merger.PopFront(); |
| 597 | EXPECT_EQ(merger.sorted_until(), e + chrono::milliseconds(2900)); |
| 598 | |
| 599 | ASSERT_TRUE(merger.Front() != nullptr); |
| 600 | output.emplace_back(std::move(*merger.Front())); |
| 601 | merger.PopFront(); |
| 602 | EXPECT_EQ(merger.sorted_until(), e + chrono::milliseconds(2900)); |
| 603 | |
| 604 | ASSERT_TRUE(merger.Front() != nullptr); |
| 605 | output.emplace_back(std::move(*merger.Front())); |
| 606 | merger.PopFront(); |
| 607 | EXPECT_EQ(merger.sorted_until(), e + chrono::milliseconds(2900)); |
| 608 | |
| 609 | ASSERT_TRUE(merger.Front() != nullptr); |
| 610 | output.emplace_back(std::move(*merger.Front())); |
| 611 | merger.PopFront(); |
| 612 | EXPECT_EQ(merger.sorted_until(), monotonic_clock::max_time); |
| 613 | |
| 614 | ASSERT_TRUE(merger.Front() != nullptr); |
| 615 | output.emplace_back(std::move(*merger.Front())); |
| 616 | merger.PopFront(); |
| 617 | EXPECT_EQ(merger.sorted_until(), monotonic_clock::max_time); |
| 618 | |
| 619 | ASSERT_TRUE(merger.Front() == nullptr); |
| 620 | |
| 621 | EXPECT_EQ(output[0].timestamp, e + chrono::milliseconds(1000)); |
| 622 | EXPECT_EQ(output[1].timestamp, e + chrono::milliseconds(1001)); |
| 623 | EXPECT_EQ(output[2].timestamp, e + chrono::milliseconds(1002)); |
| 624 | EXPECT_EQ(output[3].timestamp, e + chrono::milliseconds(2000)); |
| 625 | EXPECT_EQ(output[4].timestamp, e + chrono::milliseconds(3000)); |
| 626 | EXPECT_EQ(output[5].timestamp, e + chrono::milliseconds(3002)); |
| 627 | } |
| 628 | |
Austin Schuh | d2f9610 | 2020-12-01 20:27:29 -0800 | [diff] [blame^] | 629 | // Tests that we can match timestamps on delivered messages. |
| 630 | TEST_F(TimestampMapperTest, ReadNode0First) { |
| 631 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 632 | { |
| 633 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 634 | writer0.QueueSpan(config0_.span()); |
| 635 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 636 | writer1.QueueSpan(config2_.span()); |
| 637 | |
| 638 | writer0.QueueSizedFlatbuffer( |
| 639 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 640 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 641 | e + chrono::milliseconds(1000), 0, chrono::seconds(100))); |
| 642 | |
| 643 | writer0.QueueSizedFlatbuffer( |
| 644 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 645 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 646 | e + chrono::milliseconds(2000), 0, chrono::seconds(100))); |
| 647 | |
| 648 | writer0.QueueSizedFlatbuffer( |
| 649 | MakeLogMessage(e + chrono::milliseconds(3000), 0, 0x007)); |
| 650 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 651 | e + chrono::milliseconds(3000), 0, chrono::seconds(100))); |
| 652 | } |
| 653 | |
| 654 | const std::vector<LogFile> parts = SortParts({logfile0_, logfile1_}); |
| 655 | |
| 656 | ASSERT_EQ(parts[0].logger_node, "pi1"); |
| 657 | ASSERT_EQ(parts[1].logger_node, "pi2"); |
| 658 | |
| 659 | TimestampMapper mapper0(FilterPartsForNode(parts, "pi1")); |
| 660 | TimestampMapper mapper1(FilterPartsForNode(parts, "pi2")); |
| 661 | |
| 662 | mapper0.AddPeer(&mapper1); |
| 663 | mapper1.AddPeer(&mapper0); |
| 664 | |
| 665 | { |
| 666 | std::deque<TimestampedMessage> output0; |
| 667 | |
| 668 | ASSERT_TRUE(mapper0.Front() != nullptr); |
| 669 | output0.emplace_back(std::move(*mapper0.Front())); |
| 670 | mapper0.PopFront(); |
| 671 | EXPECT_EQ(mapper0.sorted_until(), e + chrono::milliseconds(1900)); |
| 672 | |
| 673 | ASSERT_TRUE(mapper0.Front() != nullptr); |
| 674 | output0.emplace_back(std::move(*mapper0.Front())); |
| 675 | mapper0.PopFront(); |
| 676 | EXPECT_EQ(mapper0.sorted_until(), e + chrono::milliseconds(2900)); |
| 677 | |
| 678 | ASSERT_TRUE(mapper0.Front() != nullptr); |
| 679 | output0.emplace_back(std::move(*mapper0.Front())); |
| 680 | mapper0.PopFront(); |
| 681 | EXPECT_EQ(mapper0.sorted_until(), monotonic_clock::max_time); |
| 682 | |
| 683 | ASSERT_TRUE(mapper0.Front() == nullptr); |
| 684 | |
| 685 | EXPECT_EQ(output0[0].monotonic_event_time, e + chrono::milliseconds(1000)); |
| 686 | EXPECT_TRUE(output0[0].data.Verify()); |
| 687 | EXPECT_EQ(output0[1].monotonic_event_time, e + chrono::milliseconds(2000)); |
| 688 | EXPECT_TRUE(output0[1].data.Verify()); |
| 689 | EXPECT_EQ(output0[2].monotonic_event_time, e + chrono::milliseconds(3000)); |
| 690 | EXPECT_TRUE(output0[2].data.Verify()); |
| 691 | } |
| 692 | |
| 693 | { |
| 694 | SCOPED_TRACE("Trying node1 now"); |
| 695 | std::deque<TimestampedMessage> output1; |
| 696 | |
| 697 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 698 | output1.emplace_back(std::move(*mapper1.Front())); |
| 699 | mapper1.PopFront(); |
| 700 | EXPECT_EQ(mapper1.sorted_until(), |
| 701 | e + chrono::seconds(100) + chrono::milliseconds(1900)); |
| 702 | |
| 703 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 704 | output1.emplace_back(std::move(*mapper1.Front())); |
| 705 | mapper1.PopFront(); |
| 706 | EXPECT_EQ(mapper1.sorted_until(), |
| 707 | e + chrono::seconds(100) + chrono::milliseconds(2900)); |
| 708 | |
| 709 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 710 | output1.emplace_back(std::move(*mapper1.Front())); |
| 711 | mapper1.PopFront(); |
| 712 | EXPECT_EQ(mapper1.sorted_until(), monotonic_clock::max_time); |
| 713 | |
| 714 | ASSERT_TRUE(mapper1.Front() == nullptr); |
| 715 | |
| 716 | EXPECT_EQ(output1[0].monotonic_event_time, |
| 717 | e + chrono::seconds(100) + chrono::milliseconds(1000)); |
| 718 | EXPECT_TRUE(output1[0].data.Verify()); |
| 719 | EXPECT_EQ(output1[1].monotonic_event_time, |
| 720 | e + chrono::seconds(100) + chrono::milliseconds(2000)); |
| 721 | EXPECT_TRUE(output1[1].data.Verify()); |
| 722 | EXPECT_EQ(output1[2].monotonic_event_time, |
| 723 | e + chrono::seconds(100) + chrono::milliseconds(3000)); |
| 724 | EXPECT_TRUE(output1[2].data.Verify()); |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | // Tests that we can match timestamps on delivered messages. By doing this in |
| 729 | // the reverse order, the second node needs to queue data up from the first node |
| 730 | // to find the matching timestamp. |
| 731 | TEST_F(TimestampMapperTest, ReadNode1First) { |
| 732 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 733 | { |
| 734 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 735 | writer0.QueueSpan(config0_.span()); |
| 736 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 737 | writer1.QueueSpan(config2_.span()); |
| 738 | |
| 739 | writer0.QueueSizedFlatbuffer( |
| 740 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 741 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 742 | e + chrono::milliseconds(1000), 0, chrono::seconds(100))); |
| 743 | |
| 744 | writer0.QueueSizedFlatbuffer( |
| 745 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 746 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 747 | e + chrono::milliseconds(2000), 0, chrono::seconds(100))); |
| 748 | |
| 749 | writer0.QueueSizedFlatbuffer( |
| 750 | MakeLogMessage(e + chrono::milliseconds(3000), 0, 0x007)); |
| 751 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 752 | e + chrono::milliseconds(3000), 0, chrono::seconds(100))); |
| 753 | } |
| 754 | |
| 755 | const std::vector<LogFile> parts = SortParts({logfile0_, logfile1_}); |
| 756 | |
| 757 | ASSERT_EQ(parts[0].logger_node, "pi1"); |
| 758 | ASSERT_EQ(parts[1].logger_node, "pi2"); |
| 759 | |
| 760 | TimestampMapper mapper0(FilterPartsForNode(parts, "pi1")); |
| 761 | TimestampMapper mapper1(FilterPartsForNode(parts, "pi2")); |
| 762 | |
| 763 | mapper0.AddPeer(&mapper1); |
| 764 | mapper1.AddPeer(&mapper0); |
| 765 | |
| 766 | { |
| 767 | SCOPED_TRACE("Trying node1 now"); |
| 768 | std::deque<TimestampedMessage> output1; |
| 769 | |
| 770 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 771 | output1.emplace_back(std::move(*mapper1.Front())); |
| 772 | mapper1.PopFront(); |
| 773 | EXPECT_EQ(mapper1.sorted_until(), |
| 774 | e + chrono::seconds(100) + chrono::milliseconds(1900)); |
| 775 | |
| 776 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 777 | output1.emplace_back(std::move(*mapper1.Front())); |
| 778 | mapper1.PopFront(); |
| 779 | EXPECT_EQ(mapper1.sorted_until(), |
| 780 | e + chrono::seconds(100) + chrono::milliseconds(2900)); |
| 781 | |
| 782 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 783 | output1.emplace_back(std::move(*mapper1.Front())); |
| 784 | mapper1.PopFront(); |
| 785 | EXPECT_EQ(mapper1.sorted_until(), monotonic_clock::max_time); |
| 786 | |
| 787 | ASSERT_TRUE(mapper1.Front() == nullptr); |
| 788 | |
| 789 | EXPECT_EQ(output1[0].monotonic_event_time, |
| 790 | e + chrono::seconds(100) + chrono::milliseconds(1000)); |
| 791 | EXPECT_TRUE(output1[0].data.Verify()); |
| 792 | EXPECT_EQ(output1[1].monotonic_event_time, |
| 793 | e + chrono::seconds(100) + chrono::milliseconds(2000)); |
| 794 | EXPECT_TRUE(output1[1].data.Verify()); |
| 795 | EXPECT_EQ(output1[2].monotonic_event_time, |
| 796 | e + chrono::seconds(100) + chrono::milliseconds(3000)); |
| 797 | EXPECT_TRUE(output1[2].data.Verify()); |
| 798 | } |
| 799 | |
| 800 | { |
| 801 | std::deque<TimestampedMessage> output0; |
| 802 | |
| 803 | ASSERT_TRUE(mapper0.Front() != nullptr); |
| 804 | output0.emplace_back(std::move(*mapper0.Front())); |
| 805 | mapper0.PopFront(); |
| 806 | EXPECT_EQ(mapper0.sorted_until(), monotonic_clock::max_time); |
| 807 | |
| 808 | ASSERT_TRUE(mapper0.Front() != nullptr); |
| 809 | output0.emplace_back(std::move(*mapper0.Front())); |
| 810 | mapper0.PopFront(); |
| 811 | EXPECT_EQ(mapper0.sorted_until(), monotonic_clock::max_time); |
| 812 | |
| 813 | ASSERT_TRUE(mapper0.Front() != nullptr); |
| 814 | output0.emplace_back(std::move(*mapper0.Front())); |
| 815 | mapper0.PopFront(); |
| 816 | EXPECT_EQ(mapper0.sorted_until(), monotonic_clock::max_time); |
| 817 | |
| 818 | ASSERT_TRUE(mapper0.Front() == nullptr); |
| 819 | |
| 820 | EXPECT_EQ(output0[0].monotonic_event_time, e + chrono::milliseconds(1000)); |
| 821 | EXPECT_TRUE(output0[0].data.Verify()); |
| 822 | EXPECT_EQ(output0[1].monotonic_event_time, e + chrono::milliseconds(2000)); |
| 823 | EXPECT_TRUE(output0[1].data.Verify()); |
| 824 | EXPECT_EQ(output0[2].monotonic_event_time, e + chrono::milliseconds(3000)); |
| 825 | EXPECT_TRUE(output0[2].data.Verify()); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | // Tests that we return just the timestamps if we couldn't find the data and the |
| 830 | // missing data was at the beginning of the file. |
| 831 | TEST_F(TimestampMapperTest, ReadMissingDataBefore) { |
| 832 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 833 | { |
| 834 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 835 | writer0.QueueSpan(config0_.span()); |
| 836 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 837 | writer1.QueueSpan(config2_.span()); |
| 838 | |
| 839 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005); |
| 840 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 841 | e + chrono::milliseconds(1000), 0, chrono::seconds(100))); |
| 842 | |
| 843 | writer0.QueueSizedFlatbuffer( |
| 844 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 845 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 846 | e + chrono::milliseconds(2000), 0, chrono::seconds(100))); |
| 847 | |
| 848 | writer0.QueueSizedFlatbuffer( |
| 849 | MakeLogMessage(e + chrono::milliseconds(3000), 0, 0x007)); |
| 850 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 851 | e + chrono::milliseconds(3000), 0, chrono::seconds(100))); |
| 852 | } |
| 853 | |
| 854 | const std::vector<LogFile> parts = SortParts({logfile0_, logfile1_}); |
| 855 | |
| 856 | ASSERT_EQ(parts[0].logger_node, "pi1"); |
| 857 | ASSERT_EQ(parts[1].logger_node, "pi2"); |
| 858 | |
| 859 | TimestampMapper mapper0(FilterPartsForNode(parts, "pi1")); |
| 860 | TimestampMapper mapper1(FilterPartsForNode(parts, "pi2")); |
| 861 | |
| 862 | mapper0.AddPeer(&mapper1); |
| 863 | mapper1.AddPeer(&mapper0); |
| 864 | |
| 865 | { |
| 866 | SCOPED_TRACE("Trying node1 now"); |
| 867 | std::deque<TimestampedMessage> output1; |
| 868 | |
| 869 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 870 | output1.emplace_back(std::move(*mapper1.Front())); |
| 871 | mapper1.PopFront(); |
| 872 | EXPECT_EQ(mapper1.sorted_until(), |
| 873 | e + chrono::seconds(100) + chrono::milliseconds(1900)); |
| 874 | |
| 875 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 876 | output1.emplace_back(std::move(*mapper1.Front())); |
| 877 | mapper1.PopFront(); |
| 878 | EXPECT_EQ(mapper1.sorted_until(), |
| 879 | e + chrono::seconds(100) + chrono::milliseconds(2900)); |
| 880 | |
| 881 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 882 | output1.emplace_back(std::move(*mapper1.Front())); |
| 883 | mapper1.PopFront(); |
| 884 | EXPECT_EQ(mapper1.sorted_until(), monotonic_clock::max_time); |
| 885 | |
| 886 | ASSERT_TRUE(mapper1.Front() == nullptr); |
| 887 | |
| 888 | EXPECT_EQ(output1[0].monotonic_event_time, |
| 889 | e + chrono::seconds(100) + chrono::milliseconds(1000)); |
| 890 | EXPECT_FALSE(output1[0].data.Verify()); |
| 891 | EXPECT_EQ(output1[1].monotonic_event_time, |
| 892 | e + chrono::seconds(100) + chrono::milliseconds(2000)); |
| 893 | EXPECT_TRUE(output1[1].data.Verify()); |
| 894 | EXPECT_EQ(output1[2].monotonic_event_time, |
| 895 | e + chrono::seconds(100) + chrono::milliseconds(3000)); |
| 896 | EXPECT_TRUE(output1[2].data.Verify()); |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | // Tests that we return just the timestamps if we couldn't find the data and the |
| 901 | // missing data was at the end of the file. |
| 902 | TEST_F(TimestampMapperTest, ReadMissingDataAfter) { |
| 903 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 904 | { |
| 905 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 906 | writer0.QueueSpan(config0_.span()); |
| 907 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 908 | writer1.QueueSpan(config2_.span()); |
| 909 | |
| 910 | writer0.QueueSizedFlatbuffer( |
| 911 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 912 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 913 | e + chrono::milliseconds(1000), 0, chrono::seconds(100))); |
| 914 | |
| 915 | writer0.QueueSizedFlatbuffer( |
| 916 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 917 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 918 | e + chrono::milliseconds(2000), 0, chrono::seconds(100))); |
| 919 | |
| 920 | MakeLogMessage(e + chrono::milliseconds(3000), 0, 0x007); |
| 921 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 922 | e + chrono::milliseconds(3000), 0, chrono::seconds(100))); |
| 923 | } |
| 924 | |
| 925 | const std::vector<LogFile> parts = SortParts({logfile0_, logfile1_}); |
| 926 | |
| 927 | ASSERT_EQ(parts[0].logger_node, "pi1"); |
| 928 | ASSERT_EQ(parts[1].logger_node, "pi2"); |
| 929 | |
| 930 | TimestampMapper mapper0(FilterPartsForNode(parts, "pi1")); |
| 931 | TimestampMapper mapper1(FilterPartsForNode(parts, "pi2")); |
| 932 | |
| 933 | mapper0.AddPeer(&mapper1); |
| 934 | mapper1.AddPeer(&mapper0); |
| 935 | |
| 936 | { |
| 937 | SCOPED_TRACE("Trying node1 now"); |
| 938 | std::deque<TimestampedMessage> output1; |
| 939 | |
| 940 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 941 | output1.emplace_back(std::move(*mapper1.Front())); |
| 942 | mapper1.PopFront(); |
| 943 | EXPECT_EQ(mapper1.sorted_until(), |
| 944 | e + chrono::seconds(100) + chrono::milliseconds(1900)); |
| 945 | |
| 946 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 947 | output1.emplace_back(std::move(*mapper1.Front())); |
| 948 | mapper1.PopFront(); |
| 949 | EXPECT_EQ(mapper1.sorted_until(), |
| 950 | e + chrono::seconds(100) + chrono::milliseconds(2900)); |
| 951 | |
| 952 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 953 | output1.emplace_back(std::move(*mapper1.Front())); |
| 954 | mapper1.PopFront(); |
| 955 | EXPECT_EQ(mapper1.sorted_until(), monotonic_clock::max_time); |
| 956 | |
| 957 | ASSERT_TRUE(mapper1.Front() == nullptr); |
| 958 | |
| 959 | EXPECT_EQ(output1[0].monotonic_event_time, |
| 960 | e + chrono::seconds(100) + chrono::milliseconds(1000)); |
| 961 | EXPECT_TRUE(output1[0].data.Verify()); |
| 962 | EXPECT_EQ(output1[1].monotonic_event_time, |
| 963 | e + chrono::seconds(100) + chrono::milliseconds(2000)); |
| 964 | EXPECT_TRUE(output1[1].data.Verify()); |
| 965 | EXPECT_EQ(output1[2].monotonic_event_time, |
| 966 | e + chrono::seconds(100) + chrono::milliseconds(3000)); |
| 967 | EXPECT_FALSE(output1[2].data.Verify()); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | // Tests that we properly sort log files with duplicate timestamps. |
| 972 | TEST_F(TimestampMapperTest, ReadSameTimestamp) { |
| 973 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 974 | { |
| 975 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 976 | writer0.QueueSpan(config0_.span()); |
| 977 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 978 | writer1.QueueSpan(config2_.span()); |
| 979 | |
| 980 | writer0.QueueSizedFlatbuffer( |
| 981 | MakeLogMessage(e + chrono::milliseconds(1000), 0, 0x005)); |
| 982 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 983 | e + chrono::milliseconds(1000), 0, chrono::seconds(100))); |
| 984 | |
| 985 | writer0.QueueSizedFlatbuffer( |
| 986 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x006)); |
| 987 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 988 | e + chrono::milliseconds(2000), 0, chrono::seconds(100))); |
| 989 | |
| 990 | writer0.QueueSizedFlatbuffer( |
| 991 | MakeLogMessage(e + chrono::milliseconds(2000), 0, 0x007)); |
| 992 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 993 | e + chrono::milliseconds(2000), 0, chrono::seconds(100))); |
| 994 | |
| 995 | writer0.QueueSizedFlatbuffer( |
| 996 | MakeLogMessage(e + chrono::milliseconds(3000), 0, 0x008)); |
| 997 | writer1.QueueSizedFlatbuffer(MakeTimestampMessage( |
| 998 | e + chrono::milliseconds(3000), 0, chrono::seconds(100))); |
| 999 | } |
| 1000 | |
| 1001 | const std::vector<LogFile> parts = SortParts({logfile0_, logfile1_}); |
| 1002 | |
| 1003 | ASSERT_EQ(parts[0].logger_node, "pi1"); |
| 1004 | ASSERT_EQ(parts[1].logger_node, "pi2"); |
| 1005 | |
| 1006 | TimestampMapper mapper0(FilterPartsForNode(parts, "pi1")); |
| 1007 | TimestampMapper mapper1(FilterPartsForNode(parts, "pi2")); |
| 1008 | |
| 1009 | mapper0.AddPeer(&mapper1); |
| 1010 | mapper1.AddPeer(&mapper0); |
| 1011 | |
| 1012 | { |
| 1013 | SCOPED_TRACE("Trying node1 now"); |
| 1014 | std::deque<TimestampedMessage> output1; |
| 1015 | |
| 1016 | for (int i = 0; i < 4; ++i) { |
| 1017 | ASSERT_TRUE(mapper1.Front() != nullptr); |
| 1018 | output1.emplace_back(std::move(*mapper1.Front())); |
| 1019 | mapper1.PopFront(); |
| 1020 | } |
| 1021 | ASSERT_TRUE(mapper1.Front() == nullptr); |
| 1022 | |
| 1023 | EXPECT_EQ(output1[0].monotonic_event_time, |
| 1024 | e + chrono::seconds(100) + chrono::milliseconds(1000)); |
| 1025 | EXPECT_TRUE(output1[0].data.Verify()); |
| 1026 | EXPECT_EQ(output1[1].monotonic_event_time, |
| 1027 | e + chrono::seconds(100) + chrono::milliseconds(2000)); |
| 1028 | EXPECT_TRUE(output1[1].data.Verify()); |
| 1029 | EXPECT_EQ(output1[2].monotonic_event_time, |
| 1030 | e + chrono::seconds(100) + chrono::milliseconds(2000)); |
| 1031 | EXPECT_TRUE(output1[2].data.Verify()); |
| 1032 | EXPECT_EQ(output1[3].monotonic_event_time, |
| 1033 | e + chrono::seconds(100) + chrono::milliseconds(3000)); |
| 1034 | EXPECT_TRUE(output1[3].data.Verify()); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | // Tests that we properly sort log files with duplicate timestamps. |
| 1039 | TEST_F(TimestampMapperTest, StartTime) { |
| 1040 | const aos::monotonic_clock::time_point e = monotonic_clock::epoch(); |
| 1041 | { |
| 1042 | DetachedBufferWriter writer0(logfile0_, std::make_unique<DummyEncoder>()); |
| 1043 | writer0.QueueSpan(config0_.span()); |
| 1044 | DetachedBufferWriter writer1(logfile1_, std::make_unique<DummyEncoder>()); |
| 1045 | writer1.QueueSpan(config1_.span()); |
| 1046 | DetachedBufferWriter writer2(logfile2_, std::make_unique<DummyEncoder>()); |
| 1047 | writer2.QueueSpan(config3_.span()); |
| 1048 | } |
| 1049 | |
| 1050 | const std::vector<LogFile> parts = |
| 1051 | SortParts({logfile0_, logfile1_, logfile2_}); |
| 1052 | |
| 1053 | TimestampMapper mapper0(FilterPartsForNode(parts, "pi1")); |
| 1054 | |
| 1055 | EXPECT_EQ(mapper0.monotonic_start_time(), e + chrono::milliseconds(1)); |
| 1056 | EXPECT_EQ(mapper0.realtime_start_time(), |
| 1057 | realtime_clock::time_point(chrono::seconds(1000))); |
| 1058 | } |
| 1059 | |
Austin Schuh | c243b42 | 2020-10-11 15:35:08 -0700 | [diff] [blame] | 1060 | } // namespace testing |
| 1061 | } // namespace logger |
| 1062 | } // namespace aos |