1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/passive_log_collector.h"
#include "base/compiler_specific.h"
#include "base/format_macros.h"
#include "base/string_util.h"
#include "net/url_request/url_request_netlog_params.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
typedef PassiveLogCollector::RequestTracker RequestTracker;
typedef PassiveLogCollector::SourceInfoList SourceInfoList;
typedef PassiveLogCollector::SocketTracker SocketTracker;
using net::NetLog;
const NetLog::SourceType kSourceType = NetLog::SOURCE_NONE;
PassiveLogCollector::Entry MakeStartLogEntryWithURL(int source_id,
const std::string& url) {
return PassiveLogCollector::Entry(
0,
NetLog::TYPE_URL_REQUEST_START_JOB,
base::TimeTicks(),
NetLog::Source(kSourceType, source_id),
NetLog::PHASE_BEGIN,
new URLRequestStartEventParameters(GURL(url), "GET", 0, net::LOW));
}
PassiveLogCollector::Entry MakeStartLogEntry(int source_id) {
return MakeStartLogEntryWithURL(source_id,
StringPrintf("http://req%d", source_id));
}
PassiveLogCollector::Entry MakeEndLogEntry(int source_id) {
return PassiveLogCollector::Entry(
0,
NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
NetLog::Source(kSourceType, source_id),
NetLog::PHASE_END,
NULL);
}
bool OrderBySourceID(const PassiveLogCollector::SourceInfo& a,
const PassiveLogCollector::SourceInfo& b) {
return a.source_id < b.source_id;
}
SourceInfoList GetLiveSources(
const PassiveLogCollector::SourceTracker& tracker) {
SourceInfoList result = tracker.GetAllDeadOrAliveSources(true);
std::sort(result.begin(), result.end(), &OrderBySourceID);
return result;
}
SourceInfoList GetDeadSources(
const PassiveLogCollector::SourceTracker& tracker) {
SourceInfoList result = tracker.GetAllDeadOrAliveSources(false);
std::sort(result.begin(), result.end(), &OrderBySourceID);
return result;
}
static const int kMaxNumLoadLogEntries = 1;
} // namespace
// Test that once the tracker contains a total maximum amount of data
// (graveyard + live requests), it resets itself to avoid growing unbounded.
TEST(RequestTrackerTest, DropsAfterMaximumSize) {
RequestTracker tracker(NULL);
// Fill the source tracker with as many sources as it can hold.
for (size_t i = 0; i < RequestTracker::kMaxNumSources; ++i)
tracker.OnAddEntry(MakeStartLogEntry(i));
EXPECT_EQ(RequestTracker::kMaxNumSources, GetLiveSources(tracker).size());
// Add 1 more -- this should cause it to exceed its expected peak, and
// therefore reset all of its data.
tracker.OnAddEntry(
MakeStartLogEntry(1 + RequestTracker::kMaxNumSources));
EXPECT_EQ(1u, GetLiveSources(tracker).size());
}
TEST(RequestTrackerTest, BasicBounded) {
RequestTracker tracker(NULL);
EXPECT_EQ(0u, GetLiveSources(tracker).size());
EXPECT_EQ(0u, GetDeadSources(tracker).size());
tracker.OnAddEntry(MakeStartLogEntry(1));
tracker.OnAddEntry(MakeStartLogEntry(2));
tracker.OnAddEntry(MakeStartLogEntry(3));
tracker.OnAddEntry(MakeStartLogEntry(4));
tracker.OnAddEntry(MakeStartLogEntry(5));
SourceInfoList live_reqs = GetLiveSources(tracker);
ASSERT_EQ(5u, live_reqs.size());
EXPECT_EQ("http://req1/", live_reqs[0].GetURL());
EXPECT_EQ("http://req2/", live_reqs[1].GetURL());
EXPECT_EQ("http://req3/", live_reqs[2].GetURL());
EXPECT_EQ("http://req4/", live_reqs[3].GetURL());
EXPECT_EQ("http://req5/", live_reqs[4].GetURL());
tracker.OnAddEntry(MakeEndLogEntry(1));
tracker.OnAddEntry(MakeEndLogEntry(5));
tracker.OnAddEntry(MakeEndLogEntry(3));
ASSERT_EQ(3u, GetDeadSources(tracker).size());
live_reqs = GetLiveSources(tracker);
ASSERT_EQ(2u, live_reqs.size());
EXPECT_EQ("http://req2/", live_reqs[0].GetURL());
EXPECT_EQ("http://req4/", live_reqs[1].GetURL());
}
TEST(RequestTrackerTest, GraveyardBounded) {
RequestTracker tracker(NULL);
EXPECT_EQ(0u, GetLiveSources(tracker).size());
EXPECT_EQ(0u, GetDeadSources(tracker).size());
// Add twice as many requests as will fit in the graveyard.
for (size_t i = 0; i < RequestTracker::kMaxGraveyardSize * 2; ++i) {
tracker.OnAddEntry(MakeStartLogEntry(i));
tracker.OnAddEntry(MakeEndLogEntry(i));
}
EXPECT_EQ(0u, GetLiveSources(tracker).size());
// Check that only the last |kMaxGraveyardSize| requests are in-memory.
SourceInfoList recent = GetDeadSources(tracker);
ASSERT_EQ(RequestTracker::kMaxGraveyardSize, recent.size());
for (size_t i = 0; i < RequestTracker::kMaxGraveyardSize; ++i) {
size_t req_number = i + RequestTracker::kMaxGraveyardSize;
std::string url = StringPrintf("http://req%" PRIuS "/", req_number);
EXPECT_EQ(url, recent[i].GetURL());
}
}
// Check that we exclude "chrome://" URLs from being saved into the recent
// requests list (graveyard).
TEST(RequestTrackerTest, GraveyardIsFiltered) {
RequestTracker tracker(NULL);
// This will be excluded.
std::string url1 = "chrome://dontcare/";
tracker.OnAddEntry(MakeStartLogEntryWithURL(1, url1));
tracker.OnAddEntry(MakeEndLogEntry(1));
// This will be be added to graveyard.
std::string url2 = "chrome2://dontcare/";
tracker.OnAddEntry(MakeStartLogEntryWithURL(2, url2));
tracker.OnAddEntry(MakeEndLogEntry(2));
// This will be be added to graveyard.
std::string url3 = "http://foo/";
tracker.OnAddEntry(MakeStartLogEntryWithURL(3, url3));
tracker.OnAddEntry(MakeEndLogEntry(3));
ASSERT_EQ(2u, GetDeadSources(tracker).size());
EXPECT_EQ(url2, GetDeadSources(tracker)[0].GetURL());
EXPECT_EQ(url3, GetDeadSources(tracker)[1].GetURL());
}
TEST(SpdySessionTracker, MovesToGraveyard) {
PassiveLogCollector::SpdySessionTracker tracker;
EXPECT_EQ(0u, GetLiveSources(tracker).size());
EXPECT_EQ(0u, GetDeadSources(tracker).size());
PassiveLogCollector::Entry begin(
0u,
NetLog::TYPE_SPDY_SESSION,
base::TimeTicks(),
NetLog::Source(NetLog::SOURCE_SPDY_SESSION, 1),
NetLog::PHASE_BEGIN,
NULL);
tracker.OnAddEntry(begin);
EXPECT_EQ(1u, GetLiveSources(tracker).size());
EXPECT_EQ(0u, GetDeadSources(tracker).size());
PassiveLogCollector::Entry end(
0u,
NetLog::TYPE_SPDY_SESSION,
base::TimeTicks(),
NetLog::Source(NetLog::SOURCE_SPDY_SESSION, 1),
NetLog::PHASE_END,
NULL);
tracker.OnAddEntry(end);
EXPECT_EQ(0u, GetLiveSources(tracker).size());
EXPECT_EQ(1u, GetDeadSources(tracker).size());
}
// Test that when a SOURCE_SOCKET is connected to a SOURCE_URL_REQUEST
// (via the TYPE_SOCKET_POOL_BOUND_TO_SOCKET event), it holds a reference
// to the SOURCE_SOCKET preventing it from getting deleted as long as the
// SOURCE_URL_REQUEST is still around.
TEST(PassiveLogCollectorTest, HoldReferenceToDependentSource) {
PassiveLogCollector log;
EXPECT_EQ(0u, GetLiveSources(log.url_request_tracker_).size());
EXPECT_EQ(0u, GetLiveSources(log.socket_tracker_).size());
uint32 next_id = 0;
NetLog::Source socket_source(NetLog::SOURCE_SOCKET, next_id++);
NetLog::Source url_request_source(NetLog::SOURCE_URL_REQUEST, next_id++);
// Start a SOURCE_SOCKET.
log.OnAddEntry(NetLog::TYPE_SOCKET_ALIVE,
base::TimeTicks(),
socket_source,
NetLog::PHASE_BEGIN,
NULL);
EXPECT_EQ(0u, GetLiveSources(log.url_request_tracker_).size());
EXPECT_EQ(1u, GetLiveSources(log.socket_tracker_).size());
// Start a SOURCE_URL_REQUEST.
log.OnAddEntry(NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
url_request_source,
NetLog::PHASE_BEGIN,
NULL);
// Check that there is no association between the SOURCE_URL_REQUEST and the
// SOURCE_SOCKET yet.
ASSERT_EQ(1u, GetLiveSources(log.url_request_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetLiveSources(log.url_request_tracker_)[0];
EXPECT_EQ(0, info.reference_count);
EXPECT_EQ(0u, info.dependencies.size());
}
ASSERT_EQ(1u, GetLiveSources(log.socket_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetLiveSources(log.socket_tracker_)[0];
EXPECT_EQ(0, info.reference_count);
EXPECT_EQ(0u, info.dependencies.size());
}
// Associate the SOURCE_SOCKET with the SOURCE_URL_REQUEST.
log.OnAddEntry(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
base::TimeTicks(),
url_request_source,
NetLog::PHASE_NONE,
new net::NetLogSourceParameter("x", socket_source));
// Check that an associate was made -- the SOURCE_URL_REQUEST should have
// added a reference to the SOURCE_SOCKET.
ASSERT_EQ(1u, GetLiveSources(log.url_request_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetLiveSources(log.url_request_tracker_)[0];
EXPECT_EQ(0, info.reference_count);
EXPECT_EQ(1u, info.dependencies.size());
EXPECT_EQ(socket_source.id, info.dependencies[0].id);
}
ASSERT_EQ(1u, GetLiveSources(log.socket_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetLiveSources(log.socket_tracker_)[0];
EXPECT_EQ(1, info.reference_count);
EXPECT_EQ(0u, info.dependencies.size());
}
// Now end both |source_socket| and |source_url_request|. This sends them
// to deletion queue, and they will be deleted once space runs out.
log.OnAddEntry(NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
url_request_source,
NetLog::PHASE_END,
NULL);
log.OnAddEntry(NetLog::TYPE_SOCKET_ALIVE,
base::TimeTicks(),
socket_source,
NetLog::PHASE_END,
NULL);
// Verify that both sources are in fact dead, and that |source_url_request|
// still holds a reference to |source_socket|.
ASSERT_EQ(0u, GetLiveSources(log.url_request_tracker_).size());
ASSERT_EQ(1u, GetDeadSources(log.url_request_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetDeadSources(log.url_request_tracker_)[0];
EXPECT_EQ(0, info.reference_count);
EXPECT_EQ(1u, info.dependencies.size());
EXPECT_EQ(socket_source.id, info.dependencies[0].id);
}
EXPECT_EQ(0u, GetLiveSources(log.socket_tracker_).size());
ASSERT_EQ(1u, GetDeadSources(log.socket_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetDeadSources(log.socket_tracker_)[0];
EXPECT_EQ(1, info.reference_count);
EXPECT_EQ(0u, info.dependencies.size());
}
// Cycle through a bunch of SOURCE_SOCKET -- if it were not referenced, this
// loop will have deleted it.
for (size_t i = 0; i < SocketTracker::kMaxGraveyardSize; ++i) {
log.OnAddEntry(NetLog::TYPE_SOCKET_ALIVE,
base::TimeTicks(),
NetLog::Source(NetLog::SOURCE_SOCKET, next_id++),
NetLog::PHASE_END,
NULL);
}
EXPECT_EQ(0u, GetLiveSources(log.socket_tracker_).size());
ASSERT_EQ(SocketTracker::kMaxGraveyardSize + 1,
GetDeadSources(log.socket_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetDeadSources(log.socket_tracker_)[0];
EXPECT_EQ(socket_source.id, info.source_id);
EXPECT_EQ(1, info.reference_count);
EXPECT_EQ(0u, info.dependencies.size());
}
// Cycle through a bunch of SOURCE_URL_REQUEST -- this will cause
// |source_url_request| to be freed, which in turn should release the final
// reference to |source_socket| cause it to be freed as well.
for (size_t i = 0; i < RequestTracker::kMaxGraveyardSize; ++i) {
log.OnAddEntry(NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
NetLog::Source(NetLog::SOURCE_URL_REQUEST, next_id++),
NetLog::PHASE_END,
NULL);
}
EXPECT_EQ(0u, GetLiveSources(log.url_request_tracker_).size());
EXPECT_EQ(RequestTracker::kMaxGraveyardSize,
GetDeadSources(log.url_request_tracker_).size());
EXPECT_EQ(0u, GetLiveSources(log.socket_tracker_).size());
EXPECT_EQ(SocketTracker::kMaxGraveyardSize,
GetDeadSources(log.socket_tracker_).size());
}
// Have a URL_REQUEST hold a reference to a SOCKET. Then cause the SOCKET to
// get evicted (by exceeding maximum sources limit). Now the URL_REQUEST is
// referencing a non-existant SOCKET. Lastly, evict the URL_REQUEST so it
// tries to drop all of its references. Make sure that in releasing its
// non-existant reference it doesn't trip any DCHECKs.
TEST(PassiveLogCollectorTest, HoldReferenceToDeletedSource) {
PassiveLogCollector log;
EXPECT_EQ(0u, GetLiveSources(log.url_request_tracker_).size());
EXPECT_EQ(0u, GetLiveSources(log.socket_tracker_).size());
uint32 next_id = 0;
NetLog::Source socket_source(NetLog::SOURCE_SOCKET, next_id++);
NetLog::Source url_request_source(NetLog::SOURCE_URL_REQUEST, next_id++);
// Start a SOURCE_SOCKET.
log.OnAddEntry(NetLog::TYPE_SOCKET_ALIVE,
base::TimeTicks(),
socket_source,
NetLog::PHASE_BEGIN,
NULL);
EXPECT_EQ(0u, GetLiveSources(log.url_request_tracker_).size());
EXPECT_EQ(1u, GetLiveSources(log.socket_tracker_).size());
// Start a SOURCE_URL_REQUEST.
log.OnAddEntry(NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
url_request_source,
NetLog::PHASE_BEGIN,
NULL);
// Associate the SOURCE_SOCKET with the SOURCE_URL_REQUEST.
log.OnAddEntry(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
base::TimeTicks(),
url_request_source,
NetLog::PHASE_NONE,
new net::NetLogSourceParameter("x", socket_source));
// Check that an associate was made -- the SOURCE_URL_REQUEST should have
// added a reference to the SOURCE_SOCKET.
ASSERT_EQ(1u, GetLiveSources(log.url_request_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetLiveSources(log.url_request_tracker_)[0];
EXPECT_EQ(0, info.reference_count);
EXPECT_EQ(1u, info.dependencies.size());
EXPECT_EQ(socket_source.id, info.dependencies[0].id);
}
ASSERT_EQ(1u, GetLiveSources(log.socket_tracker_).size());
{
PassiveLogCollector::SourceInfo info =
GetLiveSources(log.socket_tracker_)[0];
EXPECT_EQ(1, info.reference_count);
EXPECT_EQ(0u, info.dependencies.size());
}
// Add lots of sources to the socket tracker. This is just enough to cause
// the tracker to reach its peak, and reset all of its data as a safeguard.
for (size_t i = 0; i < SocketTracker::kMaxNumSources; ++i) {
log.OnAddEntry(NetLog::TYPE_SOCKET_ALIVE,
base::TimeTicks(),
NetLog::Source(NetLog::SOURCE_SOCKET, next_id++),
NetLog::PHASE_BEGIN,
NULL);
}
ASSERT_EQ(1u, GetLiveSources(log.socket_tracker_).size());
// End the original request. Then saturate the graveyard with enough other
// requests to cause it to be deleted. Once that source is deleted, it will
// try to give up its reference to the SOCKET. However that socket_id no
// longer exists -- should not DCHECK().
log.OnAddEntry(NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
url_request_source,
NetLog::PHASE_END,
NULL);
for (size_t i = 0; i < RequestTracker::kMaxGraveyardSize; ++i) {
log.OnAddEntry(NetLog::TYPE_REQUEST_ALIVE,
base::TimeTicks(),
NetLog::Source(NetLog::SOURCE_URL_REQUEST, next_id++),
NetLog::PHASE_END,
NULL);
}
EXPECT_EQ(RequestTracker::kMaxGraveyardSize,
GetDeadSources(log.url_request_tracker_).size());
}
|