Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(189)

Side by Side Diff: util/thread/thread_log_messages_test.cc

Issue 1041643003: Add ThreadLogMessages and its test (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Update mini_chromium to 91ea4908ffd7 Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « util/thread/thread_log_messages.cc ('k') | util/util.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/thread/thread_log_messages.h"
16
17 #include <string.h>
18
19 #include "base/logging.h"
20 #include "base/strings/stringprintf.h"
21 #include "gtest/gtest.h"
22 #include "util/test/thread.h"
23
24 namespace crashpad {
25 namespace test {
26 namespace {
27
28 TEST(ThreadLogMessages, Empty) {
29 ThreadLogMessages thread_log_messages;
30
31 const std::vector<std::string>& log_messages =
32 thread_log_messages.log_messages();
33
34 EXPECT_TRUE(log_messages.empty());
35 }
36
37 // For a message formatted like "[preamble] message\n", returns just "message".
38 // If the message is not formatted as expected, a gtest expectation failure will
39 // be recorded and this function will return an empty string.
40 std::string MessageString(const std::string& log_message) {
41 if (log_message.size() < 1) {
42 EXPECT_GE(log_message.size(), 1u);
43 return std::string();
44 }
45
46 const char kStartChar = '[';
47 if (log_message[0] != kStartChar) {
48 EXPECT_EQ(kStartChar, log_message[0]);
49 return std::string();
50 }
51
52 const char kFindString[] = "] ";
53 size_t pos = log_message.find(kFindString);
54 if (pos == std::string::npos) {
55 EXPECT_NE(std::string::npos, pos);
56 return std::string();
57 }
58
59 std::string message_string = log_message.substr(pos + strlen(kFindString));
60 if (message_string.size() < 1) {
61 EXPECT_GE(message_string.size(), 1u);
62 return std::string();
63 }
64
65 const char kEndChar = '\n';
66 if (message_string[message_string.size() - 1] != kEndChar) {
67 EXPECT_NE(message_string[message_string.size() - 1], kEndChar);
68 return std::string();
69 }
70
71 message_string.resize(message_string.size() - 1);
72 return message_string;
73 }
74
75 TEST(ThreadLogMessages, Basic) {
76 // Logging must be enabled at least at this level for this test to work.
77 ASSERT_TRUE(LOG_IS_ON(INFO));
78
79 {
80 const char* const kMessages[] = {
81 "An info message",
82 "A warning message",
83 "An error message",
84 };
85
86 ThreadLogMessages thread_log_messages;
87
88 LOG(INFO) << kMessages[0];
89 LOG(WARNING) << kMessages[1];
90 LOG(ERROR) << kMessages[2];
91
92 const std::vector<std::string>& log_messages =
93 thread_log_messages.log_messages();
94
95 EXPECT_EQ(arraysize(kMessages), log_messages.size());
96 for (size_t index = 0; index < arraysize(kMessages); ++index) {
97 EXPECT_EQ(kMessages[index], MessageString(log_messages[index]))
98 << "index " << index;
99 }
100 }
101
102 {
103 const char kMessage[] = "Sample error message";
104
105 ThreadLogMessages thread_log_messages;
106
107 LOG(ERROR) << kMessage;
108
109 const std::vector<std::string>& log_messages =
110 thread_log_messages.log_messages();
111
112 EXPECT_EQ(1u, log_messages.size());
113 EXPECT_EQ(kMessage, MessageString(log_messages[0]));
114 }
115
116 {
117 ThreadLogMessages thread_log_messages;
118
119 LOG(INFO) << "I can't believe I " << "streamed" << " the whole thing.";
120
121 const std::vector<std::string>& log_messages =
122 thread_log_messages.log_messages();
123
124 EXPECT_EQ(1u, log_messages.size());
125 EXPECT_EQ("I can't believe I streamed the whole thing.",
126 MessageString(log_messages[0]));
127 }
128 }
129
130 class LoggingTestThread : public Thread {
131 public:
132 LoggingTestThread() : thread_number_(0), start_(0), count_(0) {}
133 ~LoggingTestThread() override {}
134
135 void Initialize(size_t thread_number, int start, int count) {
136 thread_number_ = thread_number;
137 start_ = start;
138 count_ = count;
139 }
140
141 private:
142 void ThreadMain() override {
143 ThreadLogMessages thread_log_messages;
144
145 std::vector<std::string> expected_messages;
146 for (int index = start_; index <= start_ + count_; ++index) {
147 std::string message = base::StringPrintf("message %d", index);
148 expected_messages.push_back(message);
149 LOG(WARNING) << message;
150 }
151
152 const std::vector<std::string>& log_messages =
153 thread_log_messages.log_messages();
154
155 ASSERT_EQ(expected_messages.size(), log_messages.size());
156 for (size_t index = 0; index < log_messages.size(); ++index) {
157 EXPECT_EQ(expected_messages[index], MessageString(log_messages[index]))
158 << "thread_number_ " << thread_number_ << ", index " << index;
159 }
160 }
161
162 size_t thread_number_;
163 int start_;
164 int count_;
165
166 DISALLOW_COPY_AND_ASSIGN(LoggingTestThread);
167 };
168
169 TEST(ThreadLogMessages, Multithreaded) {
170 // Logging must be enabled at least at this level for this test to work.
171 ASSERT_TRUE(LOG_IS_ON(WARNING));
172
173 LoggingTestThread threads[20];
174 int start = 0;
175 for (size_t index = 0; index < arraysize(threads); ++index) {
176 threads[index].Initialize(
177 index, static_cast<int>(start), static_cast<int>(index));
178 start += static_cast<int>(index);
179
180 ASSERT_NO_FATAL_FAILURE(threads[index].Start());
181 }
182
183 for (LoggingTestThread& thread : threads) {
184 thread.Join();
185 }
186 }
187
188 } // namespace
189 } // namespace test
190 } // namespace crashpad
OLDNEW
« no previous file with comments | « util/thread/thread_log_messages.cc ('k') | util/util.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698