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

Side by Side Diff: net/base/trace_net_log_observer_unittest.cc

Issue 468083004: Use NESTABLE_ASYNC APIs to get NetLog data into Tracing (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added comments and cleaned up tests Created 6 years, 3 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
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/base/trace_net_log_observer.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/debug/trace_event.h"
11 #include "base/debug/trace_event_impl.h"
12 #include "base/json/json_reader.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/ref_counted_memory.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/values.h"
19 #include "net/base/capturing_net_log.h"
20 #include "net/base/net_log.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace net {
24
25 namespace {
26
27 using base::debug::TraceLog;
28
29 struct TraceEntryInfo {
30 std::string category;
31 std::string id;
32 std::string phase;
33 std::string source_type;
34 };
35
36 TraceEntryInfo GetTraceEntryInfoFromValue(const base::DictionaryValue *value) {
37 std::string category;
38 std::string id;
39 std::string phase;
40 std::string source_type;
41 value->GetString("cat", &category);
42 value->GetString("id", &id);
43 value->GetString("ph", &phase);
44 value->GetString("args.source_type", &source_type);
45
46 return TraceEntryInfo{category, id, phase, source_type};
47 }
48
49 bool TraceEntryInfoEqual(const TraceEntryInfo& a, const TraceEntryInfo& b) {
50 return a.category == b.category &&
51 a.id == b.id &&
52 a.phase == b.phase &&
53 a.source_type == b.source_type;
54 }
55
56 class TraceNetLogObserverTest : public testing::Test {
57 public:
58 TraceNetLogObserverTest() {
59 TraceLog::DeleteForTesting();
60 TraceLog* tracelog = TraceLog::GetInstance();
61 DCHECK(tracelog);
62 DCHECK(!tracelog->IsEnabled());
63 trace_buffer_.SetOutputCallback(json_output_.GetCallback());
64 net_log_.reset(new CapturingNetLog);
65 trace_net_log_observer_.reset(new TraceNetLogObserver());
66 // TODO(xunjieli): Remove this once the Trace bug (397022) is fixed.
67 TraceLog::GetInstance()->SetCurrentThreadBlocksMessageLoop();
68 }
69
70 virtual ~TraceNetLogObserverTest() {
71 DCHECK(!TraceLog::GetInstance()->IsEnabled());
72 TraceLog::DeleteForTesting();
73 }
74
75 void OnTraceDataCollected(
76 const scoped_refptr<base::RefCountedString>& events_str,
77 bool has_more_events) {
78 DCHECK(trace_events_.empty());
79 trace_buffer_.Start();
80 trace_buffer_.AddFragment(events_str->data());
81 trace_buffer_.Finish();
82
83 scoped_ptr<base::Value> trace_value;
84 trace_value.reset(base::JSONReader::Read(
85 json_output_.json_output,
86 base::JSON_PARSE_RFC | base::JSON_DETACHABLE_CHILDREN));
87
88 ASSERT_TRUE(trace_value) << json_output_.json_output;
89 base::ListValue* trace_events = NULL;
90 ASSERT_TRUE(trace_value);
91 ASSERT_TRUE(trace_value->GetAsList(&trace_events));
92 trace_events_.Swap(trace_events);
93 }
94
95 static void EnableTraceLog() {
96 TraceLog::GetInstance()->SetEnabled(
97 base::debug::CategoryFilter("netlog"),
98 TraceLog::RECORDING_MODE,
99 base::debug::TraceOptions());
100 }
101
102 static void DisableTraceLog() {
103 TraceLog::GetInstance()->SetDisabled();
104 }
105
106 void EndTraceAndFlush() {
107 TraceLog::GetInstance()->SetDisabled();
108 TraceLog::GetInstance()->Flush(
109 Bind(&TraceNetLogObserverTest::OnTraceDataCollected,
110 base::Unretained(this)));
111 }
112
113 void set_trace_net_log_observer(TraceNetLogObserver* trace_net_log_observer) {
114 trace_net_log_observer_.reset(trace_net_log_observer);
115 }
116
117 const base::DictionaryValue* FindTraceEntry(NetLog::EventType event_type,
118 size_t pos) {
119 std::string type(NetLog::EventTypeToString(event_type));
120 const base::Value* value = NULL;
121 trace_events_.Get(pos, &value);
122 if (value) {
123 const base::DictionaryValue* dict = NULL;
124 if (value->GetAsDictionary(&dict)) {
125 std::string actual_type;
126 if (dict->GetString("name", &actual_type)) {
127 if (actual_type == type) {
128 return dict;
129 }
130 }
131 }
132 }
133 return NULL;
134 }
135
136 std::vector<const base::DictionaryValue*> FindTraceEntries(
137 NetLog::EventType event_type) {
138 std::vector<const base::DictionaryValue*> hits;
139 std::string type(NetLog::EventTypeToString(event_type));
140 size_t trace_events_count = trace_events_.GetSize();
141 for (size_t i = 0; i < trace_events_count; i++) {
142 const base::Value* value = NULL;
143 trace_events_.Get(i, &value);
144 const base::DictionaryValue* dict = NULL;
145 if (!value->GetAsDictionary(&dict))
146 continue;
147 std::string actual_type;
148 if (dict->GetString("name", &actual_type)) {
149 if (actual_type == type)
150 hits.push_back(dict);
151 }
152 }
153 return hits;
154 }
155
156 CapturingNetLog* net_log() const {
157 return net_log_.get();
158 }
159
160 TraceNetLogObserver* trace_net_log_observer() const {
161 return trace_net_log_observer_.get();
162 }
163
164 private:
165 base::ListValue trace_events_;
166 base::debug::TraceResultBuffer trace_buffer_;
167 base::debug::TraceResultBuffer::SimpleOutput json_output_;
168 scoped_ptr<CapturingNetLog> net_log_;
169 scoped_ptr<TraceNetLogObserver> trace_net_log_observer_;
170 };
171
172 TEST_F(TraceNetLogObserverTest, TracingNotEnabled) {
173 trace_net_log_observer()->WatchForTraceStart(net_log());
174 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE);
175
176 EndTraceAndFlush();
177 trace_net_log_observer()->StopWatchForTraceStart();
178
179 const base::DictionaryValue* item = FindTraceEntry(
180 NetLog::TYPE_REQUEST_ALIVE, 0);
181 EXPECT_FALSE(item);
182 }
183
184 TEST_F(TraceNetLogObserverTest, TraceEventCaptured) {
185 CapturingNetLog::CapturedEntryList entries;
186 net_log()->GetEntries(&entries);
187 EXPECT_TRUE(entries.empty());
188
189 trace_net_log_observer()->WatchForTraceStart(net_log());
190 EnableTraceLog();
191 BoundNetLog bound_net_log = BoundNetLog::Make(net_log(),
192 net::NetLog::SOURCE_NONE);
193 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED);
194 bound_net_log.BeginEvent(NetLog::TYPE_URL_REQUEST_START_JOB);
195 bound_net_log.EndEvent(NetLog::TYPE_REQUEST_ALIVE);
196
197 net_log()->GetEntries(&entries);
198 ASSERT_EQ(3u, entries.size());
199
200 EndTraceAndFlush();
201 trace_net_log_observer()->StopWatchForTraceStart();
202
203 const base::DictionaryValue* item1 = FindTraceEntry(
204 NetLog::TYPE_CANCELLED, 0);
205 const base::DictionaryValue* item2 = FindTraceEntry(
206 NetLog::TYPE_URL_REQUEST_START_JOB, 1);
207 const base::DictionaryValue* item3 = FindTraceEntry(
208 NetLog::TYPE_REQUEST_ALIVE, 2);
209
210 TraceEntryInfo expected_item1 = TraceEntryInfo{
211 "netlog",
212 base::StringPrintf("0x%d", entries[0].source.id),
213 std::string(1, TRACE_EVENT_PHASE_NET_LOG_INSTANT),
214 NetLog::SourceTypeToString(entries[0].source.type)};
215 TraceEntryInfo expected_item2 = TraceEntryInfo{
216 "netlog",
217 base::StringPrintf("0x%d", entries[1].source.id),
218 std::string(1, TRACE_EVENT_PHASE_NET_LOG_BEGIN),
219 NetLog::SourceTypeToString(entries[1].source.type)};
220 TraceEntryInfo expected_item3 = TraceEntryInfo{
221 "netlog",
222 base::StringPrintf("0x%d", entries[2].source.id),
223 std::string(1, TRACE_EVENT_PHASE_NET_LOG_END),
224 NetLog::SourceTypeToString(entries[2].source.type)};
225
226 EXPECT_TRUE(TraceEntryInfoEqual(expected_item1,
227 GetTraceEntryInfoFromValue(item1)));
228 EXPECT_TRUE(TraceEntryInfoEqual(expected_item2,
229 GetTraceEntryInfoFromValue(item2)));
230 EXPECT_TRUE(TraceEntryInfoEqual(expected_item3,
231 GetTraceEntryInfoFromValue(item3)));
232 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_CANCELLED).size());
233 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_URL_REQUEST_START_JOB).size());
234 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_REQUEST_ALIVE).size());
235 }
236
237 TEST_F(TraceNetLogObserverTest, EnableAndDisableTracing) {
238 trace_net_log_observer()->WatchForTraceStart(net_log());
239 EnableTraceLog();
240 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED);
241 DisableTraceLog();
242 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE);
243 EnableTraceLog();
244 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB);
245
246 EndTraceAndFlush();
247 trace_net_log_observer()->StopWatchForTraceStart();
248
249 const base::DictionaryValue* item1 = FindTraceEntry(
250 NetLog::TYPE_CANCELLED, 0);
251 const base::DictionaryValue* item2 = FindTraceEntry(
252 NetLog::TYPE_REQUEST_ALIVE, 1);
253 const base::DictionaryValue* item3 = FindTraceEntry(
254 NetLog::TYPE_URL_REQUEST_START_JOB, 2);
255 EXPECT_TRUE(item1);
256 EXPECT_FALSE(item2);
257 EXPECT_TRUE(item3);
258
259 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_CANCELLED).size());
260 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_URL_REQUEST_START_JOB).size());
261 }
262
263 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileTracing) {
264 trace_net_log_observer()->WatchForTraceStart(net_log());
265 EnableTraceLog();
266 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED);
267 trace_net_log_observer()->StopWatchForTraceStart();
268 set_trace_net_log_observer(NULL);
269 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE);
270
271 EndTraceAndFlush();
272
273 const base::DictionaryValue* item1 = FindTraceEntry(
274 NetLog::TYPE_CANCELLED, 0);
275 const base::DictionaryValue* item2 = FindTraceEntry(
276 NetLog::TYPE_REQUEST_ALIVE, 1);
277 EXPECT_TRUE(item1);
278 EXPECT_FALSE(item2);
279
280 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_CANCELLED).size());
281 }
282
283 TEST_F(TraceNetLogObserverTest, DestroyObserverWhileNotTracing) {
284 trace_net_log_observer()->WatchForTraceStart(net_log());
285 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED);
286 trace_net_log_observer()->StopWatchForTraceStart();
287 set_trace_net_log_observer(NULL);
288 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE);
289 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB);
290
291 EndTraceAndFlush();
292
293 const base::DictionaryValue* item1 = FindTraceEntry(
294 NetLog::TYPE_CANCELLED, 0);
295 const base::DictionaryValue* item2 = FindTraceEntry(
296 NetLog::TYPE_REQUEST_ALIVE, 1);
297 const base::DictionaryValue* item3 = FindTraceEntry(
298 NetLog::TYPE_URL_REQUEST_START_JOB, 2);
299 EXPECT_FALSE(item1);
300 EXPECT_FALSE(item2);
301 EXPECT_FALSE(item3);
302 }
303
304 TEST_F(TraceNetLogObserverTest, CreateObserverAfterTracingStarts) {
305 set_trace_net_log_observer(NULL);
306 EnableTraceLog();
307 set_trace_net_log_observer(new TraceNetLogObserver());
308 trace_net_log_observer()->WatchForTraceStart(net_log());
309 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED);
310 trace_net_log_observer()->StopWatchForTraceStart();
311 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE);
312 net_log()->AddGlobalEntry(NetLog::TYPE_URL_REQUEST_START_JOB);
313
314 EndTraceAndFlush();
315
316 const base::DictionaryValue* item1 = FindTraceEntry(
317 NetLog::TYPE_CANCELLED, 0);
318 const base::DictionaryValue* item2 = FindTraceEntry(
319 NetLog::TYPE_REQUEST_ALIVE, 1);
320 const base::DictionaryValue* item3 = FindTraceEntry(
321 NetLog::TYPE_URL_REQUEST_START_JOB, 2);
322 EXPECT_FALSE(item1);
323 EXPECT_FALSE(item2);
324 EXPECT_FALSE(item3);
325 }
326
327 TEST_F(TraceNetLogObserverTest, EventsWithAndWithoutParameters) {
328 trace_net_log_observer()->WatchForTraceStart(net_log());
329 EnableTraceLog();
330 NetLog::ParametersCallback net_log_callback;
331 std::string param = "bar";
332 net_log_callback = NetLog::StringCallback("foo",
333 &param);
334
335 net_log()->AddGlobalEntry(NetLog::TYPE_CANCELLED, net_log_callback);
336 net_log()->AddGlobalEntry(NetLog::TYPE_REQUEST_ALIVE);
337
338 EndTraceAndFlush();
339 trace_net_log_observer()->StopWatchForTraceStart();
340
341 const base::DictionaryValue* item1 = FindTraceEntry(
342 NetLog::TYPE_CANCELLED, 0);
343 const base::DictionaryValue* item2 = FindTraceEntry(
344 NetLog::TYPE_REQUEST_ALIVE, 1);
345
346 std::string item1_params;
347 std::string item2_params;
348 EXPECT_TRUE(item1 && item1->GetString("args.params.foo", &item1_params));
349 EXPECT_EQ("bar", item1_params);
350
351 EXPECT_TRUE(item2 && item2->GetString("args.params", &item2_params));
352 EXPECT_TRUE(item2_params.empty());
353
354 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_CANCELLED).size());
355 EXPECT_EQ(1u, FindTraceEntries(NetLog::TYPE_REQUEST_ALIVE).size());
356 }
357
358 } // namespace
359
360 } // namespace net
OLDNEW
« base/debug/trace_event.h ('K') | « net/base/trace_net_log_observer.cc ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698