Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 1 | /* |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 17 | #include "LogBufferTest.h" |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 18 | |
| 19 | #include <unistd.h> |
| 20 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 21 | #include <limits> |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 22 | #include <memory> |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 23 | #include <regex> |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/stringprintf.h> |
| 27 | #include <android-base/strings.h> |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 28 | |
Tom Cherry | 8319bb4 | 2020-06-11 14:06:58 -0700 | [diff] [blame] | 29 | #include "LogBuffer.h" |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 30 | #include "LogReaderThread.h" |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 31 | #include "LogWriter.h" |
| 32 | |
| 33 | using android::base::Join; |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 34 | using android::base::Split; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 35 | using android::base::StringPrintf; |
| 36 | |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 37 | char* android::uidToName(uid_t) { |
| 38 | return nullptr; |
| 39 | } |
| 40 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 41 | static std::vector<std::string> CompareLoggerEntries(const logger_entry& expected, |
| 42 | const logger_entry& result, bool ignore_len) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 43 | std::vector<std::string> errors; |
| 44 | if (!ignore_len && expected.len != result.len) { |
| 45 | errors.emplace_back( |
| 46 | StringPrintf("len: expected %" PRIu16 " vs %" PRIu16, expected.len, result.len)); |
| 47 | } |
| 48 | if (expected.hdr_size != result.hdr_size) { |
| 49 | errors.emplace_back(StringPrintf("hdr_size: %" PRIu16 " vs %" PRIu16, expected.hdr_size, |
| 50 | result.hdr_size)); |
| 51 | } |
| 52 | if (expected.pid != result.pid) { |
| 53 | errors.emplace_back( |
| 54 | StringPrintf("pid: expected %" PRIi32 " vs %" PRIi32, expected.pid, result.pid)); |
| 55 | } |
| 56 | if (expected.tid != result.tid) { |
| 57 | errors.emplace_back( |
| 58 | StringPrintf("tid: expected %" PRIu32 " vs %" PRIu32, expected.tid, result.tid)); |
| 59 | } |
| 60 | if (expected.sec != result.sec) { |
| 61 | errors.emplace_back( |
| 62 | StringPrintf("sec: expected %" PRIu32 " vs %" PRIu32, expected.sec, result.sec)); |
| 63 | } |
| 64 | if (expected.nsec != result.nsec) { |
| 65 | errors.emplace_back( |
| 66 | StringPrintf("nsec: expected %" PRIu32 " vs %" PRIu32, expected.nsec, result.nsec)); |
| 67 | } |
| 68 | if (expected.lid != result.lid) { |
| 69 | errors.emplace_back( |
| 70 | StringPrintf("lid: expected %" PRIu32 " vs %" PRIu32, expected.lid, result.lid)); |
| 71 | } |
| 72 | if (expected.uid != result.uid) { |
| 73 | errors.emplace_back( |
| 74 | StringPrintf("uid: expected %" PRIu32 " vs %" PRIu32, expected.uid, result.uid)); |
| 75 | } |
| 76 | return errors; |
| 77 | } |
| 78 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 79 | static std::string MakePrintable(std::string in) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 80 | if (in.size() > 80) { |
| 81 | in = in.substr(0, 80) + "..."; |
| 82 | } |
| 83 | std::string result; |
| 84 | for (const char c : in) { |
| 85 | if (isprint(c)) { |
| 86 | result.push_back(c); |
| 87 | } else { |
| 88 | result.append(StringPrintf("\\%02x", static_cast<int>(c) & 0xFF)); |
| 89 | } |
| 90 | } |
| 91 | return result; |
| 92 | } |
| 93 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 94 | static std::string CompareMessages(const std::string& expected, const std::string& result) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 95 | if (expected == result) { |
| 96 | return {}; |
| 97 | } |
| 98 | size_t diff_index = 0; |
| 99 | for (; diff_index < std::min(expected.size(), result.size()); ++diff_index) { |
| 100 | if (expected[diff_index] != result[diff_index]) { |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
Tom Cherry | b3e1633 | 2020-05-28 20:02:42 -0700 | [diff] [blame] | 105 | if (diff_index < 80) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 106 | auto expected_short = MakePrintable(expected); |
| 107 | auto result_short = MakePrintable(result); |
| 108 | return StringPrintf("msg: expected '%s' vs '%s'", expected_short.c_str(), |
| 109 | result_short.c_str()); |
| 110 | } |
| 111 | |
| 112 | auto expected_short = MakePrintable(expected.substr(diff_index)); |
| 113 | auto result_short = MakePrintable(result.substr(diff_index)); |
| 114 | return StringPrintf("msg: index %zu: expected '%s' vs '%s'", diff_index, expected_short.c_str(), |
| 115 | result_short.c_str()); |
| 116 | } |
| 117 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 118 | static std::string CompareRegexMessages(const std::string& expected, const std::string& result) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 119 | auto expected_pieces = Split(expected, std::string("\0", 1)); |
| 120 | auto result_pieces = Split(result, std::string("\0", 1)); |
| 121 | |
| 122 | if (expected_pieces.size() != 3 || result_pieces.size() != 3) { |
| 123 | return StringPrintf( |
| 124 | "msg: should have 3 null delimited strings found %d in expected, %d in result: " |
| 125 | "'%s' vs '%s'", |
| 126 | static_cast<int>(expected_pieces.size()), static_cast<int>(result_pieces.size()), |
| 127 | MakePrintable(expected).c_str(), MakePrintable(result).c_str()); |
| 128 | } |
| 129 | if (expected_pieces[0] != result_pieces[0]) { |
| 130 | return StringPrintf("msg: tag/priority mismatch expected '%s' vs '%s'", |
| 131 | MakePrintable(expected_pieces[0]).c_str(), |
| 132 | MakePrintable(result_pieces[0]).c_str()); |
| 133 | } |
| 134 | std::regex expected_tag_regex(expected_pieces[1]); |
| 135 | if (!std::regex_search(result_pieces[1], expected_tag_regex)) { |
| 136 | return StringPrintf("msg: message regex mismatch expected '%s' vs '%s'", |
| 137 | MakePrintable(expected_pieces[1]).c_str(), |
| 138 | MakePrintable(result_pieces[1]).c_str()); |
| 139 | } |
| 140 | if (expected_pieces[2] != result_pieces[2]) { |
| 141 | return StringPrintf("msg: nothing expected after final null character '%s' vs '%s'", |
| 142 | MakePrintable(expected_pieces[2]).c_str(), |
| 143 | MakePrintable(result_pieces[2]).c_str()); |
| 144 | } |
| 145 | return {}; |
| 146 | } |
| 147 | |
| 148 | void CompareLogMessages(const std::vector<LogMessage>& expected, |
| 149 | const std::vector<LogMessage>& result) { |
| 150 | EXPECT_EQ(expected.size(), result.size()); |
| 151 | size_t end = std::min(expected.size(), result.size()); |
| 152 | size_t num_errors = 0; |
| 153 | for (size_t i = 0; i < end; ++i) { |
| 154 | auto errors = |
| 155 | CompareLoggerEntries(expected[i].entry, result[i].entry, expected[i].regex_compare); |
| 156 | auto msg_error = expected[i].regex_compare |
| 157 | ? CompareRegexMessages(expected[i].message, result[i].message) |
| 158 | : CompareMessages(expected[i].message, result[i].message); |
| 159 | if (!msg_error.empty()) { |
| 160 | errors.emplace_back(msg_error); |
| 161 | } |
| 162 | if (!errors.empty()) { |
| 163 | GTEST_LOG_(ERROR) << "Mismatch log message " << i << "\n" << Join(errors, "\n"); |
| 164 | ++num_errors; |
| 165 | } |
| 166 | } |
| 167 | EXPECT_EQ(0U, num_errors); |
| 168 | } |
| 169 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 170 | void FixupMessages(std::vector<LogMessage>* messages) { |
| 171 | for (auto& [entry, message, _] : *messages) { |
| 172 | entry.hdr_size = sizeof(logger_entry); |
| 173 | entry.len = message.size(); |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 174 | } |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 175 | } |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 176 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 177 | TEST_P(LogBufferTest, smoke) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 178 | std::vector<LogMessage> log_messages = { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 179 | {{ |
| 180 | .pid = 1, |
| 181 | .tid = 1, |
| 182 | .sec = 1234, |
| 183 | .nsec = 323001, |
| 184 | .lid = LOG_ID_MAIN, |
| 185 | .uid = 0, |
| 186 | }, |
| 187 | "smoke test"}, |
| 188 | }; |
| 189 | FixupMessages(&log_messages); |
| 190 | LogMessages(log_messages); |
| 191 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 192 | std::vector<LogMessage> read_log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 193 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr)); |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 194 | std::unique_ptr<FlushToState> flush_to_state = log_buffer_->CreateFlushToState(1, kLogMaskAll); |
| 195 | EXPECT_TRUE(log_buffer_->FlushTo(test_writer.get(), *flush_to_state, nullptr)); |
| 196 | EXPECT_EQ(2ULL, flush_to_state->start()); |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 197 | CompareLogMessages(log_messages, read_log_messages); |
| 198 | } |
| 199 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 200 | TEST_P(LogBufferTest, smoke_with_reader_thread) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 201 | std::vector<LogMessage> log_messages = { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 202 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0}, |
| 203 | "first"}, |
| 204 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0}, |
| 205 | "second"}, |
| 206 | {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_KERNEL, .uid = 0}, |
| 207 | "third"}, |
| 208 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20004, .lid = LOG_ID_MAIN, .uid = 0}, |
| 209 | "fourth"}, |
| 210 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20005, .lid = LOG_ID_RADIO, .uid = 0}, |
| 211 | "fifth"}, |
| 212 | {{.pid = 2, .tid = 2, .sec = 10000, .nsec = 20006, .lid = LOG_ID_RADIO, .uid = 0}, |
| 213 | "sixth"}, |
| 214 | {{.pid = 3, .tid = 2, .sec = 10000, .nsec = 20007, .lid = LOG_ID_RADIO, .uid = 0}, |
| 215 | "seventh"}, |
| 216 | {{.pid = 4, .tid = 2, .sec = 10000, .nsec = 20008, .lid = LOG_ID_MAIN, .uid = 0}, |
| 217 | "eighth"}, |
| 218 | {{.pid = 5, .tid = 2, .sec = 10000, .nsec = 20009, .lid = LOG_ID_CRASH, .uid = 0}, |
| 219 | "nineth"}, |
| 220 | {{.pid = 6, .tid = 2, .sec = 10000, .nsec = 20011, .lid = LOG_ID_MAIN, .uid = 0}, |
| 221 | "tenth"}, |
| 222 | }; |
| 223 | FixupMessages(&log_messages); |
| 224 | LogMessages(log_messages); |
| 225 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 226 | std::vector<LogMessage> read_log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 227 | bool released = false; |
| 228 | |
| 229 | { |
| 230 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 231 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 232 | std::unique_ptr<LogReaderThread> log_reader( |
| 233 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true, |
Tom Cherry | 8319bb4 | 2020-06-11 14:06:58 -0700 | [diff] [blame] | 234 | 0, kLogMaskAll, 0, {}, 1, {})); |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 235 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 236 | } |
| 237 | |
| 238 | while (!released) { |
| 239 | usleep(5000); |
| 240 | } |
| 241 | { |
| 242 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 243 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 244 | } |
| 245 | CompareLogMessages(log_messages, read_log_messages); |
| 246 | } |
| 247 | |
| 248 | // Generate random messages, set the 'sec' parameter explicit though, to be able to track the |
| 249 | // expected order of messages. |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 250 | LogMessage GenerateRandomLogMessage(uint32_t sec) { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 251 | auto rand_uint32 = [](int max) -> uint32_t { return rand() % max; }; |
| 252 | logger_entry entry = { |
| 253 | .hdr_size = sizeof(logger_entry), |
| 254 | .pid = rand() % 5000, |
| 255 | .tid = rand_uint32(5000), |
| 256 | .sec = sec, |
| 257 | .nsec = rand_uint32(NS_PER_SEC), |
| 258 | .lid = rand_uint32(LOG_ID_STATS), |
| 259 | .uid = rand_uint32(100000), |
| 260 | }; |
| 261 | |
| 262 | // See comment in ChattyLogBuffer::Log() for why this is disallowed. |
| 263 | if (entry.nsec % 1000 == 0) { |
| 264 | ++entry.nsec; |
| 265 | } |
| 266 | |
| 267 | if (entry.lid == LOG_ID_EVENTS) { |
| 268 | entry.lid = LOG_ID_KERNEL; |
| 269 | } |
| 270 | |
| 271 | std::string message; |
| 272 | char priority = ANDROID_LOG_INFO + rand() % 2; |
| 273 | message.push_back(priority); |
| 274 | |
| 275 | int tag_length = 2 + rand() % 10; |
| 276 | for (int i = 0; i < tag_length; ++i) { |
| 277 | message.push_back('a' + rand() % 26); |
| 278 | } |
| 279 | message.push_back('\0'); |
| 280 | |
| 281 | int msg_length = 2 + rand() % 1000; |
| 282 | for (int i = 0; i < msg_length; ++i) { |
| 283 | message.push_back('a' + rand() % 26); |
| 284 | } |
| 285 | message.push_back('\0'); |
| 286 | |
| 287 | entry.len = message.size(); |
| 288 | |
| 289 | return {entry, message}; |
| 290 | } |
| 291 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 292 | TEST_P(LogBufferTest, random_messages) { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 293 | srand(1); |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 294 | std::vector<LogMessage> log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 295 | for (size_t i = 0; i < 1000; ++i) { |
| 296 | log_messages.emplace_back(GenerateRandomLogMessage(i)); |
| 297 | } |
| 298 | LogMessages(log_messages); |
| 299 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 300 | std::vector<LogMessage> read_log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 301 | bool released = false; |
| 302 | |
| 303 | { |
| 304 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 305 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 306 | std::unique_ptr<LogReaderThread> log_reader( |
| 307 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true, |
Tom Cherry | 8319bb4 | 2020-06-11 14:06:58 -0700 | [diff] [blame] | 308 | 0, kLogMaskAll, 0, {}, 1, {})); |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 309 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 310 | } |
| 311 | |
| 312 | while (!released) { |
| 313 | usleep(5000); |
| 314 | } |
| 315 | { |
| 316 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 317 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 318 | } |
| 319 | CompareLogMessages(log_messages, read_log_messages); |
| 320 | } |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 321 | |
Tom Cherry | 90e9ce0 | 2020-06-01 13:43:50 -0700 | [diff] [blame] | 322 | TEST_P(LogBufferTest, read_last_sequence) { |
| 323 | std::vector<LogMessage> log_messages = { |
| 324 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0}, |
| 325 | "first"}, |
| 326 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0}, |
| 327 | "second"}, |
| 328 | {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_MAIN, .uid = 0}, |
| 329 | "third"}, |
| 330 | }; |
| 331 | FixupMessages(&log_messages); |
| 332 | LogMessages(log_messages); |
| 333 | |
| 334 | std::vector<LogMessage> read_log_messages; |
| 335 | bool released = false; |
| 336 | |
| 337 | { |
| 338 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 339 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 340 | std::unique_ptr<LogReaderThread> log_reader( |
| 341 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true, |
Tom Cherry | 8319bb4 | 2020-06-11 14:06:58 -0700 | [diff] [blame] | 342 | 0, kLogMaskAll, 0, {}, 3, {})); |
Tom Cherry | 90e9ce0 | 2020-06-01 13:43:50 -0700 | [diff] [blame] | 343 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 344 | } |
| 345 | |
| 346 | while (!released) { |
| 347 | usleep(5000); |
| 348 | } |
| 349 | { |
| 350 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 351 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 352 | } |
| 353 | std::vector<LogMessage> expected_log_messages = {log_messages.back()}; |
| 354 | CompareLogMessages(expected_log_messages, read_log_messages); |
| 355 | } |
| 356 | |
Tom Cherry | 8319bb4 | 2020-06-11 14:06:58 -0700 | [diff] [blame] | 357 | TEST_P(LogBufferTest, clear_logs) { |
| 358 | // Log 3 initial logs. |
| 359 | std::vector<LogMessage> log_messages = { |
| 360 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0}, |
| 361 | "first"}, |
| 362 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0}, |
| 363 | "second"}, |
| 364 | {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_MAIN, .uid = 0}, |
| 365 | "third"}, |
| 366 | }; |
| 367 | FixupMessages(&log_messages); |
| 368 | LogMessages(log_messages); |
| 369 | |
| 370 | std::vector<LogMessage> read_log_messages; |
| 371 | bool released = false; |
| 372 | |
| 373 | // Connect a blocking reader. |
| 374 | { |
| 375 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 376 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 377 | std::unique_ptr<LogReaderThread> log_reader( |
| 378 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), false, |
| 379 | 0, kLogMaskAll, 0, {}, 1, {})); |
| 380 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 381 | } |
| 382 | |
| 383 | // Wait up to 250ms for the reader to read the first 3 logs. |
| 384 | constexpr int kMaxRetryCount = 50; |
| 385 | int count = 0; |
| 386 | for (; count < kMaxRetryCount; ++count) { |
| 387 | usleep(5000); |
| 388 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 389 | if (reader_list_.reader_threads().back()->start() == 4) { |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | ASSERT_LT(count, kMaxRetryCount); |
| 394 | |
| 395 | // Clear the log buffer. |
| 396 | log_buffer_->Clear(LOG_ID_MAIN, 0); |
| 397 | |
| 398 | // Log 3 more logs. |
| 399 | std::vector<LogMessage> after_clear_messages = { |
| 400 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0}, |
| 401 | "4th"}, |
| 402 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0}, |
| 403 | "5th"}, |
| 404 | {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_MAIN, .uid = 0}, |
| 405 | "6th"}, |
| 406 | }; |
| 407 | FixupMessages(&after_clear_messages); |
| 408 | LogMessages(after_clear_messages); |
| 409 | |
| 410 | // Wait up to 250ms for the reader to read the 3 additional logs. |
| 411 | for (count = 0; count < kMaxRetryCount; ++count) { |
| 412 | usleep(5000); |
| 413 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 414 | if (reader_list_.reader_threads().back()->start() == 7) { |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | ASSERT_LT(count, kMaxRetryCount); |
| 419 | |
| 420 | // Release the reader, wait for it to get the signal then check that it has been deleted. |
| 421 | { |
| 422 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 423 | reader_list_.reader_threads().back()->release_Locked(); |
| 424 | } |
| 425 | while (!released) { |
| 426 | usleep(5000); |
| 427 | } |
| 428 | { |
| 429 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 430 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 431 | } |
| 432 | |
| 433 | // Check that we have read all 6 messages. |
| 434 | std::vector<LogMessage> expected_log_messages = log_messages; |
| 435 | expected_log_messages.insert(expected_log_messages.end(), after_clear_messages.begin(), |
| 436 | after_clear_messages.end()); |
| 437 | CompareLogMessages(expected_log_messages, read_log_messages); |
| 438 | |
| 439 | // Finally, call FlushTo and ensure that only the 3 logs after the clear remain in the buffer. |
| 440 | std::vector<LogMessage> read_log_messages_after_clear; |
| 441 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages_after_clear, nullptr)); |
| 442 | std::unique_ptr<FlushToState> flush_to_state = log_buffer_->CreateFlushToState(1, kLogMaskAll); |
| 443 | EXPECT_TRUE(log_buffer_->FlushTo(test_writer.get(), *flush_to_state, nullptr)); |
| 444 | EXPECT_EQ(7ULL, flush_to_state->start()); |
| 445 | CompareLogMessages(after_clear_messages, read_log_messages_after_clear); |
| 446 | } |
| 447 | |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 448 | INSTANTIATE_TEST_CASE_P(LogBufferTests, LogBufferTest, |
| 449 | testing::Values("chatty", "serialized", "simple")); |