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

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

Issue 464084: Cache failed DNS resolutions for 1 second.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Merge changes (to include API change in another file) Created 11 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/host_resolver_impl.cc ('k') | net/base/mock_host_resolver.cc » ('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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <ws2tcpip.h> 8 #include <ws2tcpip.h>
9 #include <wspiapi.h> 9 #include <wspiapi.h>
10 #elif defined(OS_POSIX) 10 #elif defined(OS_POSIX)
(...skipping 12 matching lines...) Expand all
23 #include "net/base/net_errors.h" 23 #include "net/base/net_errors.h"
24 #include "net/base/test_completion_callback.h" 24 #include "net/base/test_completion_callback.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 // TODO(eroman): 27 // TODO(eroman):
28 // - Test mixing async with sync (in particular how does sync update the 28 // - Test mixing async with sync (in particular how does sync update the
29 // cache while an async is already pending). 29 // cache while an async is already pending).
30 30
31 namespace net { 31 namespace net {
32 namespace { 32 namespace {
33 const int kMaxCacheEntries = 100; 33
34 const int kMaxCacheAgeMs = 60000; 34 HostCache* CreateDefaultCache() {
35 return new HostCache(
36 100, // max cache entries.
37 base::TimeDelta::FromMinutes(1),
38 base::TimeDelta::FromSeconds(0));
39 }
35 40
36 // A variant of WaitingHostResolverProc that pushes each host mapped into a 41 // A variant of WaitingHostResolverProc that pushes each host mapped into a
37 // list. 42 // list.
38 // (and uses a manual-reset event rather than auto-reset). 43 // (and uses a manual-reset event rather than auto-reset).
39 class CapturingHostResolverProc : public HostResolverProc { 44 class CapturingHostResolverProc : public HostResolverProc {
40 public: 45 public:
41 explicit CapturingHostResolverProc(HostResolverProc* previous) 46 explicit CapturingHostResolverProc(HostResolverProc* previous)
42 : HostResolverProc(previous), event_(true, false) { 47 : HostResolverProc(previous), event_(true, false) {
43 } 48 }
44 49
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 184
180 TEST_F(HostResolverImplTest, SynchronousLookup) { 185 TEST_F(HostResolverImplTest, SynchronousLookup) {
181 AddressList adrlist; 186 AddressList adrlist;
182 const int kPortnum = 80; 187 const int kPortnum = 80;
183 188
184 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = 189 scoped_refptr<RuleBasedHostResolverProc> resolver_proc =
185 new RuleBasedHostResolverProc(NULL); 190 new RuleBasedHostResolverProc(NULL);
186 resolver_proc->AddRule("just.testing", "192.168.1.42"); 191 resolver_proc->AddRule("just.testing", "192.168.1.42");
187 192
188 scoped_refptr<HostResolver> host_resolver( 193 scoped_refptr<HostResolver> host_resolver(
189 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 194 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
190 195
191 HostResolver::RequestInfo info("just.testing", kPortnum); 196 HostResolver::RequestInfo info("just.testing", kPortnum);
192 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); 197 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
193 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, log); 198 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, log);
194 EXPECT_EQ(OK, err); 199 EXPECT_EQ(OK, err);
195 200
196 EXPECT_EQ(2u, log->events().size()); 201 EXPECT_EQ(2u, log->events().size());
197 ExpectLogContains(log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL, 202 ExpectLogContains(log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL,
198 LoadLog::PHASE_BEGIN); 203 LoadLog::PHASE_BEGIN);
199 ExpectLogContains(log, 1, LoadLog::TYPE_HOST_RESOLVER_IMPL, 204 ExpectLogContains(log, 1, LoadLog::TYPE_HOST_RESOLVER_IMPL,
(...skipping 11 matching lines...) Expand all
211 216
212 TEST_F(HostResolverImplTest, AsynchronousLookup) { 217 TEST_F(HostResolverImplTest, AsynchronousLookup) {
213 AddressList adrlist; 218 AddressList adrlist;
214 const int kPortnum = 80; 219 const int kPortnum = 80;
215 220
216 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = 221 scoped_refptr<RuleBasedHostResolverProc> resolver_proc =
217 new RuleBasedHostResolverProc(NULL); 222 new RuleBasedHostResolverProc(NULL);
218 resolver_proc->AddRule("just.testing", "192.168.1.42"); 223 resolver_proc->AddRule("just.testing", "192.168.1.42");
219 224
220 scoped_refptr<HostResolver> host_resolver( 225 scoped_refptr<HostResolver> host_resolver(
221 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 226 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
222 227
223 HostResolver::RequestInfo info("just.testing", kPortnum); 228 HostResolver::RequestInfo info("just.testing", kPortnum);
224 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); 229 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
225 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, log); 230 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, log);
226 EXPECT_EQ(ERR_IO_PENDING, err); 231 EXPECT_EQ(ERR_IO_PENDING, err);
227 232
228 EXPECT_EQ(1u, log->events().size()); 233 EXPECT_EQ(1u, log->events().size());
229 ExpectLogContains(log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL, 234 ExpectLogContains(log, 0, LoadLog::TYPE_HOST_RESOLVER_IMPL,
230 LoadLog::PHASE_BEGIN); 235 LoadLog::PHASE_BEGIN);
231 236
(...skipping 16 matching lines...) Expand all
248 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); 253 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr);
249 } 254 }
250 255
251 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { 256 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) {
252 scoped_refptr<WaitingHostResolverProc> resolver_proc = 257 scoped_refptr<WaitingHostResolverProc> resolver_proc =
253 new WaitingHostResolverProc(NULL); 258 new WaitingHostResolverProc(NULL);
254 259
255 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); 260 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
256 { 261 {
257 scoped_refptr<HostResolver> host_resolver( 262 scoped_refptr<HostResolver> host_resolver(
258 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 263 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
259 AddressList adrlist; 264 AddressList adrlist;
260 const int kPortnum = 80; 265 const int kPortnum = 80;
261 266
262 HostResolver::RequestInfo info("just.testing", kPortnum); 267 HostResolver::RequestInfo info("just.testing", kPortnum);
263 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, log); 268 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL, log);
264 EXPECT_EQ(ERR_IO_PENDING, err); 269 EXPECT_EQ(ERR_IO_PENDING, err);
265 270
266 // Make sure we will exit the queue even when callback is not called. 271 // Make sure we will exit the queue even when callback is not called.
267 MessageLoop::current()->PostDelayedTask(FROM_HERE, 272 MessageLoop::current()->PostDelayedTask(FROM_HERE,
268 new MessageLoop::QuitTask(), 273 new MessageLoop::QuitTask(),
(...skipping 14 matching lines...) Expand all
283 } 288 }
284 289
285 TEST_F(HostResolverImplTest, NumericIPv4Address) { 290 TEST_F(HostResolverImplTest, NumericIPv4Address) {
286 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. 291 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in.
287 292
288 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = 293 scoped_refptr<RuleBasedHostResolverProc> resolver_proc =
289 new RuleBasedHostResolverProc(NULL); 294 new RuleBasedHostResolverProc(NULL);
290 resolver_proc->AllowDirectLookup("*"); 295 resolver_proc->AllowDirectLookup("*");
291 296
292 scoped_refptr<HostResolver> host_resolver( 297 scoped_refptr<HostResolver> host_resolver(
293 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 298 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
294 AddressList adrlist; 299 AddressList adrlist;
295 const int kPortnum = 5555; 300 const int kPortnum = 5555;
296 HostResolver::RequestInfo info("127.1.2.3", kPortnum); 301 HostResolver::RequestInfo info("127.1.2.3", kPortnum);
297 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, NULL); 302 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, NULL);
298 EXPECT_EQ(OK, err); 303 EXPECT_EQ(OK, err);
299 304
300 const struct addrinfo* ainfo = adrlist.head(); 305 const struct addrinfo* ainfo = adrlist.head();
301 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); 306 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next);
302 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); 307 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen);
303 308
304 const struct sockaddr* sa = ainfo->ai_addr; 309 const struct sockaddr* sa = ainfo->ai_addr;
305 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; 310 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa;
306 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); 311 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port);
307 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr); 312 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr);
308 } 313 }
309 314
310 TEST_F(HostResolverImplTest, NumericIPv6Address) { 315 TEST_F(HostResolverImplTest, NumericIPv6Address) {
311 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = 316 scoped_refptr<RuleBasedHostResolverProc> resolver_proc =
312 new RuleBasedHostResolverProc(NULL); 317 new RuleBasedHostResolverProc(NULL);
313 resolver_proc->AllowDirectLookup("*"); 318 resolver_proc->AllowDirectLookup("*");
314 319
315 // Resolve a plain IPv6 address. Don't worry about [brackets], because 320 // Resolve a plain IPv6 address. Don't worry about [brackets], because
316 // the caller should have removed them. 321 // the caller should have removed them.
317 scoped_refptr<HostResolver> host_resolver( 322 scoped_refptr<HostResolver> host_resolver(
318 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 323 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
319 AddressList adrlist; 324 AddressList adrlist;
320 const int kPortnum = 5555; 325 const int kPortnum = 5555;
321 HostResolver::RequestInfo info("2001:db8::1", kPortnum); 326 HostResolver::RequestInfo info("2001:db8::1", kPortnum);
322 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, NULL); 327 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, NULL);
323 // On computers without IPv6 support, getaddrinfo cannot convert IPv6 328 // On computers without IPv6 support, getaddrinfo cannot convert IPv6
324 // address literals to addresses (getaddrinfo returns EAI_NONAME). So this 329 // address literals to addresses (getaddrinfo returns EAI_NONAME). So this
325 // test has to allow host_resolver->Resolve to fail. 330 // test has to allow host_resolver->Resolve to fail.
326 if (err == ERR_NAME_NOT_RESOLVED) 331 if (err == ERR_NAME_NOT_RESOLVED)
327 return; 332 return;
328 EXPECT_EQ(OK, err); 333 EXPECT_EQ(OK, err);
(...skipping 14 matching lines...) Expand all
343 EXPECT_EQ(expect_addr[i], sa_in6->sin6_addr.s6_addr[i]); 348 EXPECT_EQ(expect_addr[i], sa_in6->sin6_addr.s6_addr[i]);
344 } 349 }
345 } 350 }
346 351
347 TEST_F(HostResolverImplTest, EmptyHost) { 352 TEST_F(HostResolverImplTest, EmptyHost) {
348 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = 353 scoped_refptr<RuleBasedHostResolverProc> resolver_proc =
349 new RuleBasedHostResolverProc(NULL); 354 new RuleBasedHostResolverProc(NULL);
350 resolver_proc->AllowDirectLookup("*"); 355 resolver_proc->AllowDirectLookup("*");
351 356
352 scoped_refptr<HostResolver> host_resolver( 357 scoped_refptr<HostResolver> host_resolver(
353 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 358 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
354 AddressList adrlist; 359 AddressList adrlist;
355 const int kPortnum = 5555; 360 const int kPortnum = 5555;
356 HostResolver::RequestInfo info("", kPortnum); 361 HostResolver::RequestInfo info("", kPortnum);
357 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, NULL); 362 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL, NULL);
358 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err); 363 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, err);
359 } 364 }
360 365
361 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request 366 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request
362 // completion notifications for all the resolves, so it can tally up and 367 // completion notifications for all the resolves, so it can tally up and
363 // determine when we are done. 368 // determine when we are done.
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 DISALLOW_COPY_AND_ASSIGN(DeDupeRequestsVerifier); 409 DISALLOW_COPY_AND_ASSIGN(DeDupeRequestsVerifier);
405 }; 410 };
406 411
407 TEST_F(HostResolverImplTest, DeDupeRequests) { 412 TEST_F(HostResolverImplTest, DeDupeRequests) {
408 // Use a capturing resolver_proc, since the verifier needs to know what calls 413 // Use a capturing resolver_proc, since the verifier needs to know what calls
409 // reached Resolve(). Also, the capturing resolver_proc is initially blocked. 414 // reached Resolve(). Also, the capturing resolver_proc is initially blocked.
410 scoped_refptr<CapturingHostResolverProc> resolver_proc = 415 scoped_refptr<CapturingHostResolverProc> resolver_proc =
411 new CapturingHostResolverProc(NULL); 416 new CapturingHostResolverProc(NULL);
412 417
413 scoped_refptr<HostResolver> host_resolver( 418 scoped_refptr<HostResolver> host_resolver(
414 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 419 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
415 420
416 // The class will receive callbacks for when each resolve completes. It 421 // The class will receive callbacks for when each resolve completes. It
417 // checks that the right things happened. 422 // checks that the right things happened.
418 DeDupeRequestsVerifier verifier(resolver_proc.get()); 423 DeDupeRequestsVerifier verifier(resolver_proc.get());
419 424
420 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is 425 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is
421 // blocked, these should all pile up until we signal it. 426 // blocked, these should all pile up until we signal it.
422 427
423 ResolveRequest req1(host_resolver, "a", 80, &verifier); 428 ResolveRequest req1(host_resolver, "a", 80, &verifier);
424 ResolveRequest req2(host_resolver, "b", 80, &verifier); 429 ResolveRequest req2(host_resolver, "b", 80, &verifier);
(...skipping 30 matching lines...) Expand all
455 }; 460 };
456 461
457 TEST_F(HostResolverImplTest, CancelMultipleRequests) { 462 TEST_F(HostResolverImplTest, CancelMultipleRequests) {
458 // Use a capturing resolver_proc, since the verifier needs to know what calls 463 // Use a capturing resolver_proc, since the verifier needs to know what calls
459 // reached Resolver(). Also, the capturing resolver_proc is initially 464 // reached Resolver(). Also, the capturing resolver_proc is initially
460 // blocked. 465 // blocked.
461 scoped_refptr<CapturingHostResolverProc> resolver_proc = 466 scoped_refptr<CapturingHostResolverProc> resolver_proc =
462 new CapturingHostResolverProc(NULL); 467 new CapturingHostResolverProc(NULL);
463 468
464 scoped_refptr<HostResolver> host_resolver( 469 scoped_refptr<HostResolver> host_resolver(
465 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 470 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
466 471
467 // The class will receive callbacks for when each resolve completes. It 472 // The class will receive callbacks for when each resolve completes. It
468 // checks that the right things happened. 473 // checks that the right things happened.
469 CancelMultipleRequestsVerifier verifier; 474 CancelMultipleRequestsVerifier verifier;
470 475
471 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is 476 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is
472 // blocked, these should all pile up until we signal it. 477 // blocked, these should all pile up until we signal it.
473 478
474 ResolveRequest req1(host_resolver, "a", 80, &verifier); 479 ResolveRequest req1(host_resolver, "a", 80, &verifier);
475 ResolveRequest req2(host_resolver, "b", 80, &verifier); 480 ResolveRequest req2(host_resolver, "b", 80, &verifier);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 }; 547 };
543 548
544 TEST_F(HostResolverImplTest, CancelWithinCallback) { 549 TEST_F(HostResolverImplTest, CancelWithinCallback) {
545 // Use a capturing resolver_proc, since the verifier needs to know what calls 550 // Use a capturing resolver_proc, since the verifier needs to know what calls
546 // reached Resolver(). Also, the capturing resolver_proc is initially 551 // reached Resolver(). Also, the capturing resolver_proc is initially
547 // blocked. 552 // blocked.
548 scoped_refptr<CapturingHostResolverProc> resolver_proc = 553 scoped_refptr<CapturingHostResolverProc> resolver_proc =
549 new CapturingHostResolverProc(NULL); 554 new CapturingHostResolverProc(NULL);
550 555
551 scoped_refptr<HostResolver> host_resolver( 556 scoped_refptr<HostResolver> host_resolver(
552 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); 557 new HostResolverImpl(resolver_proc, CreateDefaultCache()));
553 558
554 // The class will receive callbacks for when each resolve completes. It 559 // The class will receive callbacks for when each resolve completes. It
555 // checks that the right things happened. 560 // checks that the right things happened.
556 CancelWithinCallbackVerifier verifier; 561 CancelWithinCallbackVerifier verifier;
557 562
558 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is 563 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is
559 // blocked, these should all pile up until we signal it. 564 // blocked, these should all pile up until we signal it.
560 565
561 ResolveRequest req1(host_resolver, "a", 80, &verifier); 566 ResolveRequest req1(host_resolver, "a", 80, &verifier);
562 ResolveRequest req2(host_resolver, "a", 81, &verifier); 567 ResolveRequest req2(host_resolver, "a", 81, &verifier);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
603 // Use a capturing resolver_proc, since the verifier needs to know what calls 608 // Use a capturing resolver_proc, since the verifier needs to know what calls
604 // reached Resolver(). Also, the capturing resolver_proc is initially 609 // reached Resolver(). Also, the capturing resolver_proc is initially
605 // blocked. 610 // blocked.
606 scoped_refptr<CapturingHostResolverProc> resolver_proc = 611 scoped_refptr<CapturingHostResolverProc> resolver_proc =
607 new CapturingHostResolverProc(NULL); 612 new CapturingHostResolverProc(NULL);
608 613
609 // The class will receive callbacks for when each resolve completes. It 614 // The class will receive callbacks for when each resolve completes. It
610 // checks that the right things happened. Note that the verifier holds the 615 // checks that the right things happened. Note that the verifier holds the
611 // only reference to |host_resolver|, so it can delete it within callback. 616 // only reference to |host_resolver|, so it can delete it within callback.
612 HostResolver* host_resolver = 617 HostResolver* host_resolver =
613 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs); 618 new HostResolverImpl(resolver_proc, CreateDefaultCache());
614 DeleteWithinCallbackVerifier verifier(host_resolver); 619 DeleteWithinCallbackVerifier verifier(host_resolver);
615 620
616 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is 621 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is
617 // blocked, these should all pile up until we signal it. 622 // blocked, these should all pile up until we signal it.
618 623
619 ResolveRequest req1(host_resolver, "a", 80, &verifier); 624 ResolveRequest req1(host_resolver, "a", 80, &verifier);
620 ResolveRequest req2(host_resolver, "a", 81, &verifier); 625 ResolveRequest req2(host_resolver, "a", 81, &verifier);
621 ResolveRequest req3(host_resolver, "a", 82, &verifier); 626 ResolveRequest req3(host_resolver, "a", 82, &verifier);
622 ResolveRequest req4(host_resolver, "a", 83, &verifier); 627 ResolveRequest req4(host_resolver, "a", 83, &verifier);
623 628
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 661
657 TEST_F(HostResolverImplTest, StartWithinCallback) { 662 TEST_F(HostResolverImplTest, StartWithinCallback) {
658 // Use a capturing resolver_proc, since the verifier needs to know what calls 663 // Use a capturing resolver_proc, since the verifier needs to know what calls
659 // reached Resolver(). Also, the capturing resolver_proc is initially 664 // reached Resolver(). Also, the capturing resolver_proc is initially
660 // blocked. 665 // blocked.
661 scoped_refptr<CapturingHostResolverProc> resolver_proc = 666 scoped_refptr<CapturingHostResolverProc> resolver_proc =
662 new CapturingHostResolverProc(NULL); 667 new CapturingHostResolverProc(NULL);
663 668
664 // Turn off caching for this host resolver. 669 // Turn off caching for this host resolver.
665 scoped_refptr<HostResolver> host_resolver( 670 scoped_refptr<HostResolver> host_resolver(
666 new HostResolverImpl(resolver_proc, 0, 0)); 671 new HostResolverImpl(resolver_proc, NULL));
667 672
668 // The class will receive callbacks for when each resolve completes. It 673 // The class will receive callbacks for when each resolve completes. It
669 // checks that the right things happened. 674 // checks that the right things happened.
670 StartWithinCallbackVerifier verifier; 675 StartWithinCallbackVerifier verifier;
671 676
672 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is 677 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is
673 // blocked, these should all pile up until we signal it. 678 // blocked, these should all pile up until we signal it.
674 679
675 ResolveRequest req1(host_resolver, "a", 80, &verifier); 680 ResolveRequest req1(host_resolver, "a", 80, &verifier);
676 ResolveRequest req2(host_resolver, "a", 81, &verifier); 681 ResolveRequest req2(host_resolver, "a", 81, &verifier);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 } 726 }
722 } 727 }
723 728
724 private: 729 private:
725 scoped_ptr<ResolveRequest> final_request_; 730 scoped_ptr<ResolveRequest> final_request_;
726 DISALLOW_COPY_AND_ASSIGN(BypassCacheVerifier); 731 DISALLOW_COPY_AND_ASSIGN(BypassCacheVerifier);
727 }; 732 };
728 733
729 TEST_F(HostResolverImplTest, BypassCache) { 734 TEST_F(HostResolverImplTest, BypassCache) {
730 scoped_refptr<HostResolver> host_resolver( 735 scoped_refptr<HostResolver> host_resolver(
731 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); 736 new HostResolverImpl(NULL, CreateDefaultCache()));
732 737
733 // The class will receive callbacks for when each resolve completes. It 738 // The class will receive callbacks for when each resolve completes. It
734 // checks that the right things happened. 739 // checks that the right things happened.
735 BypassCacheVerifier verifier; 740 BypassCacheVerifier verifier;
736 741
737 // Start a request. 742 // Start a request.
738 ResolveRequest req1(host_resolver, "a", 80, &verifier); 743 ResolveRequest req1(host_resolver, "a", 80, &verifier);
739 744
740 // |verifier| will send quit message once all the requests have finished. 745 // |verifier| will send quit message once all the requests have finished.
741 MessageLoop::current()->Run(); 746 MessageLoop::current()->Run();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 std::vector<StartOrCancelEntry> start_log; 810 std::vector<StartOrCancelEntry> start_log;
806 std::vector<FinishEntry> finish_log; 811 std::vector<FinishEntry> finish_log;
807 std::vector<StartOrCancelEntry> cancel_log; 812 std::vector<StartOrCancelEntry> cancel_log;
808 }; 813 };
809 814
810 // Test that registering, unregistering, and notifying of observers works. 815 // Test that registering, unregistering, and notifying of observers works.
811 // Does not test the cancellation notification since all resolves are 816 // Does not test the cancellation notification since all resolves are
812 // synchronous. 817 // synchronous.
813 TEST_F(HostResolverImplTest, Observers) { 818 TEST_F(HostResolverImplTest, Observers) {
814 scoped_refptr<HostResolver> host_resolver( 819 scoped_refptr<HostResolver> host_resolver(
815 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); 820 new HostResolverImpl(NULL, CreateDefaultCache()));
816 821
817 CapturingObserver observer; 822 CapturingObserver observer;
818 823
819 host_resolver->AddObserver(&observer); 824 host_resolver->AddObserver(&observer);
820 825
821 AddressList addrlist; 826 AddressList addrlist;
822 827
823 // Resolve "host1". 828 // Resolve "host1".
824 HostResolver::RequestInfo info1("host1", 70); 829 HostResolver::RequestInfo info1("host1", 70);
825 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded)); 830 scoped_refptr<LoadLog> log(new LoadLog(LoadLog::kUnbounded));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 896
892 // Tests that observers are sent OnCancelResolution() whenever a request is 897 // Tests that observers are sent OnCancelResolution() whenever a request is
893 // cancelled. There are two ways to cancel a request: 898 // cancelled. There are two ways to cancel a request:
894 // (1) Delete the HostResolver while job is outstanding. 899 // (1) Delete the HostResolver while job is outstanding.
895 // (2) Call HostResolver::CancelRequest() while a request is outstanding. 900 // (2) Call HostResolver::CancelRequest() while a request is outstanding.
896 TEST_F(HostResolverImplTest, CancellationObserver) { 901 TEST_F(HostResolverImplTest, CancellationObserver) {
897 CapturingObserver observer; 902 CapturingObserver observer;
898 { 903 {
899 // Create a host resolver and attach an observer. 904 // Create a host resolver and attach an observer.
900 scoped_refptr<HostResolver> host_resolver( 905 scoped_refptr<HostResolver> host_resolver(
901 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); 906 new HostResolverImpl(NULL, CreateDefaultCache()));
902 host_resolver->AddObserver(&observer); 907 host_resolver->AddObserver(&observer);
903 908
904 TestCompletionCallback callback; 909 TestCompletionCallback callback;
905 910
906 EXPECT_EQ(0U, observer.start_log.size()); 911 EXPECT_EQ(0U, observer.start_log.size());
907 EXPECT_EQ(0U, observer.finish_log.size()); 912 EXPECT_EQ(0U, observer.finish_log.size());
908 EXPECT_EQ(0U, observer.cancel_log.size()); 913 EXPECT_EQ(0U, observer.cancel_log.size());
909 914
910 // Start an async resolve for (host1:70). 915 // Start an async resolve for (host1:70).
911 HostResolver::RequestInfo info1("host1", 70); 916 HostResolver::RequestInfo info1("host1", 70);
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 EXPECT_EQ(0U, observer.finish_log.size()); 961 EXPECT_EQ(0U, observer.finish_log.size());
957 EXPECT_EQ(2U, observer.cancel_log.size()); 962 EXPECT_EQ(2U, observer.cancel_log.size());
958 963
959 HostResolver::RequestInfo info("host2", 60); 964 HostResolver::RequestInfo info("host2", 60);
960 EXPECT_TRUE(observer.cancel_log[1] == 965 EXPECT_TRUE(observer.cancel_log[1] ==
961 CapturingObserver::StartOrCancelEntry(1, info)); 966 CapturingObserver::StartOrCancelEntry(1, info));
962 } 967 }
963 968
964 } // namespace 969 } // namespace
965 } // namespace net 970 } // namespace net
OLDNEW
« no previous file with comments | « net/base/host_resolver_impl.cc ('k') | net/base/mock_host_resolver.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698