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

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

Issue 4118004: Update NetLog to be thread safe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Final sync with trunk Created 10 years 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 | Annotate | Revision Log
« no previous file with comments | « net/base/forwarding_net_log_unittest.cc ('k') | net/base/net_log.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/base/host_resolver_impl.h" 5 #include "net/base/host_resolver_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 resolver_proc->AddRule("just.testing", "192.168.1.42"); 264 resolver_proc->AddRule("just.testing", "192.168.1.42");
265 265
266 scoped_ptr<HostResolver> host_resolver( 266 scoped_ptr<HostResolver> host_resolver(
267 CreateHostResolverImpl(resolver_proc)); 267 CreateHostResolverImpl(resolver_proc));
268 268
269 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 269 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
270 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 270 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
271 int err = host_resolver->Resolve(info, &addrlist, NULL, NULL, log.bound()); 271 int err = host_resolver->Resolve(info, &addrlist, NULL, NULL, log.bound());
272 EXPECT_EQ(OK, err); 272 EXPECT_EQ(OK, err);
273 273
274 EXPECT_EQ(2u, log.entries().size()); 274 net::CapturingNetLog::EntryList entries;
275 log.GetEntries(&entries);
276
277 EXPECT_EQ(2u, entries.size());
275 EXPECT_TRUE(LogContainsBeginEvent( 278 EXPECT_TRUE(LogContainsBeginEvent(
276 log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); 279 entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL));
277 EXPECT_TRUE(LogContainsEndEvent( 280 EXPECT_TRUE(LogContainsEndEvent(
278 log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); 281 entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL));
279 282
280 const struct addrinfo* ainfo = addrlist.head(); 283 const struct addrinfo* ainfo = addrlist.head();
281 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 284 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
282 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); 285 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
283 286
284 const struct sockaddr* sa = ainfo->ai_addr; 287 const struct sockaddr* sa = ainfo->ai_addr;
285 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; 288 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
286 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); 289 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
287 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); 290 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr);
288 } 291 }
289 292
290 TEST_F(HostResolverImplTest, AsynchronousLookup) { 293 TEST_F(HostResolverImplTest, AsynchronousLookup) {
291 AddressList addrlist; 294 AddressList addrlist;
292 const int kPortnum = 80; 295 const int kPortnum = 80;
293 296
294 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 297 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
295 new RuleBasedHostResolverProc(NULL)); 298 new RuleBasedHostResolverProc(NULL));
296 resolver_proc->AddRule("just.testing", "192.168.1.42"); 299 resolver_proc->AddRule("just.testing", "192.168.1.42");
297 300
298 scoped_ptr<HostResolver> host_resolver( 301 scoped_ptr<HostResolver> host_resolver(
299 CreateHostResolverImpl(resolver_proc)); 302 CreateHostResolverImpl(resolver_proc));
300 303
301 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum)); 304 HostResolver::RequestInfo info(HostPortPair("just.testing", kPortnum));
302 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 305 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
303 int err = host_resolver->Resolve(info, &addrlist, &callback_, NULL, 306 int err = host_resolver->Resolve(info, &addrlist, &callback_, NULL,
304 log.bound()); 307 log.bound());
305 EXPECT_EQ(ERR_IO_PENDING, err); 308 EXPECT_EQ(ERR_IO_PENDING, err);
306 309
307 EXPECT_EQ(1u, log.entries().size()); 310 net::CapturingNetLog::EntryList entries;
311 log.GetEntries(&entries);
312
313 EXPECT_EQ(1u, entries.size());
308 EXPECT_TRUE(LogContainsBeginEvent( 314 EXPECT_TRUE(LogContainsBeginEvent(
309 log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); 315 entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL));
310 316
311 MessageLoop::current()->Run(); 317 MessageLoop::current()->Run();
312 318
313 ASSERT_TRUE(callback_called_); 319 ASSERT_TRUE(callback_called_);
314 ASSERT_EQ(OK, callback_result_); 320 ASSERT_EQ(OK, callback_result_);
315 321
316 EXPECT_EQ(2u, log.entries().size()); 322 log.GetEntries(&entries);
323
324 EXPECT_EQ(2u, entries.size());
317 EXPECT_TRUE(LogContainsEndEvent( 325 EXPECT_TRUE(LogContainsEndEvent(
318 log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); 326 entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL));
319 327
320 const struct addrinfo* ainfo = addrlist.head(); 328 const struct addrinfo* ainfo = addrlist.head();
321 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 329 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
322 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); 330 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
323 331
324 const struct sockaddr* sa = ainfo->ai_addr; 332 const struct sockaddr* sa = ainfo->ai_addr;
325 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; 333 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
326 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); 334 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
327 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); 335 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr);
328 } 336 }
(...skipping 20 matching lines...) Expand all
349 357
350 // Make sure we will exit the queue even when callback is not called. 358 // Make sure we will exit the queue even when callback is not called.
351 MessageLoop::current()->PostDelayedTask(FROM_HERE, 359 MessageLoop::current()->PostDelayedTask(FROM_HERE,
352 new MessageLoop::QuitTask(), 360 new MessageLoop::QuitTask(),
353 1000); 361 1000);
354 MessageLoop::current()->Run(); 362 MessageLoop::current()->Run();
355 } 363 }
356 364
357 resolver_proc->Signal(); 365 resolver_proc->Signal();
358 366
359 EXPECT_EQ(2u, log.entries().size()); 367 net::CapturingNetLog::EntryList entries;
368 log.GetEntries(&entries);
369
370 EXPECT_EQ(2u, entries.size());
360 EXPECT_TRUE(LogContainsBeginEvent( 371 EXPECT_TRUE(LogContainsBeginEvent(
361 log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); 372 entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL));
362 EXPECT_TRUE(LogContainsEndEvent( 373 EXPECT_TRUE(LogContainsEndEvent(
363 log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); 374 entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL));
364 375
365 int pos = net::ExpectLogContainsSomewhereAfter(net_log.entries(), 0, 376 net::CapturingNetLog::EntryList net_log_entries;
377 net_log.GetEntries(&net_log_entries);
378
379 int pos = net::ExpectLogContainsSomewhereAfter(net_log_entries, 0,
366 net::NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, 380 net::NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
367 net::NetLog::PHASE_BEGIN); 381 net::NetLog::PHASE_BEGIN);
368 pos = net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, 382 pos = net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
369 net::NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, 383 net::NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
370 net::NetLog::PHASE_BEGIN); 384 net::NetLog::PHASE_BEGIN);
371 // Both Job and Request need to be cancelled. 385 // Both Job and Request need to be cancelled.
372 pos = net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, 386 pos = net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
373 net::NetLog::TYPE_CANCELLED, 387 net::NetLog::TYPE_CANCELLED,
374 net::NetLog::PHASE_NONE); 388 net::NetLog::PHASE_NONE);
375 // Don't care about order in which they end, or when the other one is 389 // Don't care about order in which they end, or when the other one is
376 // cancelled. 390 // cancelled.
377 net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, 391 net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
378 net::NetLog::TYPE_CANCELLED, 392 net::NetLog::TYPE_CANCELLED,
379 net::NetLog::PHASE_NONE); 393 net::NetLog::PHASE_NONE);
380 net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, 394 net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
381 net::NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST, 395 net::NetLog::TYPE_HOST_RESOLVER_IMPL_REQUEST,
382 net::NetLog::PHASE_END); 396 net::NetLog::PHASE_END);
383 net::ExpectLogContainsSomewhereAfter(net_log.entries(), pos + 1, 397 net::ExpectLogContainsSomewhereAfter(net_log_entries, pos + 1,
384 net::NetLog::TYPE_HOST_RESOLVER_IMPL_JOB, 398 net::NetLog::TYPE_HOST_RESOLVER_IMPL_JOB,
385 net::NetLog::PHASE_END); 399 net::NetLog::PHASE_END);
386 400
387 EXPECT_FALSE(callback_called_); 401 EXPECT_FALSE(callback_called_);
388 } 402 }
389 403
390 TEST_F(HostResolverImplTest, NumericIPv4Address) { 404 TEST_F(HostResolverImplTest, NumericIPv4Address) {
391 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. 405 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in.
392 406
393 scoped_refptr<RuleBasedHostResolverProc> resolver_proc( 407 scoped_refptr<RuleBasedHostResolverProc> resolver_proc(
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
936 host_resolver->AddObserver(&observer); 950 host_resolver->AddObserver(&observer);
937 951
938 AddressList addrlist; 952 AddressList addrlist;
939 953
940 // Resolve "host1". 954 // Resolve "host1".
941 HostResolver::RequestInfo info1(HostPortPair("host1", 70)); 955 HostResolver::RequestInfo info1(HostPortPair("host1", 70));
942 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); 956 CapturingBoundNetLog log(CapturingNetLog::kUnbounded);
943 int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL, log.bound()); 957 int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL, log.bound());
944 EXPECT_EQ(OK, rv); 958 EXPECT_EQ(OK, rv);
945 959
946 EXPECT_EQ(2u, log.entries().size()); 960 net::CapturingNetLog::EntryList entries;
961 log.GetEntries(&entries);
962
963 EXPECT_EQ(2u, entries.size());
947 EXPECT_TRUE(LogContainsBeginEvent( 964 EXPECT_TRUE(LogContainsBeginEvent(
948 log.entries(), 0, NetLog::TYPE_HOST_RESOLVER_IMPL)); 965 entries, 0, NetLog::TYPE_HOST_RESOLVER_IMPL));
949 EXPECT_TRUE(LogContainsEndEvent( 966 EXPECT_TRUE(LogContainsEndEvent(
950 log.entries(), 1, NetLog::TYPE_HOST_RESOLVER_IMPL)); 967 entries, 1, NetLog::TYPE_HOST_RESOLVER_IMPL));
951 968
952 EXPECT_EQ(1U, observer.start_log.size()); 969 EXPECT_EQ(1U, observer.start_log.size());
953 EXPECT_EQ(1U, observer.finish_log.size()); 970 EXPECT_EQ(1U, observer.finish_log.size());
954 EXPECT_EQ(0U, observer.cancel_log.size()); 971 EXPECT_EQ(0U, observer.cancel_log.size());
955 EXPECT_TRUE(observer.start_log[0] == 972 EXPECT_TRUE(observer.start_log[0] ==
956 CapturingObserver::StartOrCancelEntry(0, info1)); 973 CapturingObserver::StartOrCancelEntry(0, info1));
957 EXPECT_TRUE(observer.finish_log[0] == 974 EXPECT_TRUE(observer.finish_log[0] ==
958 CapturingObserver::FinishEntry(0, true, info1)); 975 CapturingObserver::FinishEntry(0, true, info1));
959 976
960 // Resolve "host1" again -- this time it will be served from cache, but it 977 // Resolve "host1" again -- this time it will be served from cache, but it
(...skipping 677 matching lines...) Expand 10 before | Expand all | Expand 10 after
1638 EXPECT_EQ("192.1.98.2", NetAddressToString(addrlist[1].head())); 1655 EXPECT_EQ("192.1.98.2", NetAddressToString(addrlist[1].head()));
1639 EXPECT_EQ("192.1.98.1", NetAddressToString(addrlist[2].head())); 1656 EXPECT_EQ("192.1.98.1", NetAddressToString(addrlist[2].head()));
1640 EXPECT_EQ("192.1.98.1", NetAddressToString(addrlist[3].head())); 1657 EXPECT_EQ("192.1.98.1", NetAddressToString(addrlist[3].head()));
1641 } 1658 }
1642 1659
1643 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags. 1660 // TODO(cbentzel): Test a mix of requests with different HostResolverFlags.
1644 1661
1645 } // namespace 1662 } // namespace
1646 1663
1647 } // namespace net 1664 } // namespace net
OLDNEW
« no previous file with comments | « net/base/forwarding_net_log_unittest.cc ('k') | net/base/net_log.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698