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

Side by Side Diff: components/cronet/stale_host_resolver_unittest.cc

Issue 2692043002: Allow HostCache to handle network changes itself (Closed)
Patch Set: Created 3 years, 10 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 | « components/cronet/android/BUILD.gn ('k') | net/dns/host_resolver_impl.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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/cronet/stale_host_resolver.h" 5 #include "components/cronet/stale_host_resolver.h"
6 6
7 #include "base/callback_helpers.h" 7 #include "base/callback_helpers.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/memory/ptr_util.h" 9 #include "base/memory/ptr_util.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/test/test_timeouts.h" 13 #include "base/test/test_timeouts.h"
14 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "components/cronet/url_request_context_config.h" 16 #include "components/cronet/url_request_context_config.h"
17 #include "net/android/network_change_notifier_factory_android.h"
17 #include "net/base/net_errors.h" 18 #include "net/base/net_errors.h"
19 #include "net/base/network_change_notifier.h"
18 #include "net/cert/cert_verifier.h" 20 #include "net/cert/cert_verifier.h"
19 #include "net/dns/host_resolver_proc.h" 21 #include "net/dns/host_resolver_proc.h"
20 #include "net/http/http_network_session.h" 22 #include "net/http/http_network_session.h"
21 #include "net/log/net_log.h" 23 #include "net/log/net_log.h"
22 #include "net/log/net_log_with_source.h" 24 #include "net/log/net_log_with_source.h"
23 #include "net/proxy/proxy_config.h" 25 #include "net/proxy/proxy_config.h"
24 #include "net/proxy/proxy_config_service_fixed.h" 26 #include "net/proxy/proxy_config_service_fixed.h"
25 #include "net/url_request/url_request_context.h" 27 #include "net/url_request/url_request_context.h"
26 #include "net/url_request/url_request_context_builder.h" 28 #include "net/url_request/url_request_context_builder.h"
27 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 resolver_ = resolver; 130 resolver_ = resolver;
129 } 131 }
130 132
131 void ClearResolver() { 133 void ClearResolver() {
132 DCHECK(resolver_); 134 DCHECK(resolver_);
133 DCHECK(!stale_resolver_); 135 DCHECK(!stale_resolver_);
134 136
135 resolver_ = nullptr; 137 resolver_ = nullptr;
136 } 138 }
137 139
140 void CreateNetworkChangeNotifier() {
141 net::NetworkChangeNotifier::SetFactory(
142 new net::NetworkChangeNotifierFactoryAndroid());
143 net::NetworkChangeNotifier::Create();
144 }
145
138 // Creates a cache entry for |kHostname| that is |age_sec| seconds old. 146 // Creates a cache entry for |kHostname| that is |age_sec| seconds old.
139 void CreateCacheEntry(int age_sec) { 147 void CreateCacheEntry(int age_sec) {
140 DCHECK(resolver_); 148 DCHECK(resolver_);
141 DCHECK(resolver_->GetHostCache()); 149 DCHECK(resolver_->GetHostCache());
142 150
143 base::TimeDelta ttl(base::TimeDelta::FromSeconds(kCacheEntryTTLSec)); 151 base::TimeDelta ttl(base::TimeDelta::FromSeconds(kCacheEntryTTLSec));
144 net::HostCache::Key key(kHostname, net::ADDRESS_FAMILY_IPV4, 0); 152 net::HostCache::Key key(kHostname, net::ADDRESS_FAMILY_IPV4, 0);
145 net::HostCache::Entry entry(net::OK, MakeAddressList(kCacheAddress), ttl); 153 net::HostCache::Entry entry(net::OK, MakeAddressList(kCacheAddress), ttl);
146 base::TimeDelta age = base::TimeDelta::FromSeconds(age_sec); 154 base::TimeDelta age = base::TimeDelta::FromSeconds(age_sec);
147 base::TimeTicks then = base::TimeTicks::Now() - age; 155 base::TimeTicks then = base::TimeTicks::Now() - age;
148 resolver_->GetHostCache()->Set(key, entry, then, ttl); 156 resolver_->GetHostCache()->Set(key, entry, then, ttl);
149 } 157 }
150 158
151 void OnNetworkChange() { 159 void OnNetworkChange() {
152 DCHECK(resolver_); 160 net::NetworkChangeNotifier::NotifyObserversOfIPAddressChangeForTests();
153 DCHECK(resolver_->GetHostCache()); 161 base::RunLoop().RunUntilIdle(); // Wait for notification.
154
155 resolver_->GetHostCache()->OnNetworkChange();
156 } 162 }
157 163
158 void LookupStale() { 164 void LookupStale() {
159 DCHECK(resolver_); 165 DCHECK(resolver_);
160 DCHECK(resolver_->GetHostCache()); 166 DCHECK(resolver_->GetHostCache());
161 167
162 net::HostCache::Key key(kHostname, net::ADDRESS_FAMILY_IPV4, 0); 168 net::HostCache::Key key(kHostname, net::ADDRESS_FAMILY_IPV4, 0);
163 base::TimeTicks now = base::TimeTicks::Now(); 169 base::TimeTicks now = base::TimeTicks::Now();
164 const net::HostCache::Entry* entry; 170 const net::HostCache::Entry* entry;
165 net::HostCache::EntryStaleness stale; 171 net::HostCache::EntryStaleness stale;
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 {2, 0, true, 1, 1, 1, true}, 399 {2, 0, true, 1, 1, 1, true},
394 {2, 0, true, 1, 1, 9, true}, 400 {2, 0, true, 1, 1, 9, true},
395 401
396 // Disallowing other networks only accepts zero network changes. 402 // Disallowing other networks only accepts zero network changes.
397 {2, 0, false, 1, 1, 0, true}, 403 {2, 0, false, 1, 1, 0, true},
398 {2, 0, false, 1, 1, 1, false}, 404 {2, 0, false, 1, 1, 1, false},
399 {2, 0, false, 1, 1, 9, false}, 405 {2, 0, false, 1, 1, 9, false},
400 }; 406 };
401 407
402 SetStaleDelay(kNoStaleDelaySec); 408 SetStaleDelay(kNoStaleDelaySec);
409 CreateNetworkChangeNotifier();
403 410
404 for (size_t i = 0; i < arraysize(kUsabilityTestCases); ++i) { 411 for (size_t i = 0; i < arraysize(kUsabilityTestCases); ++i) {
405 const auto& test_case = kUsabilityTestCases[i]; 412 const auto& test_case = kUsabilityTestCases[i];
406 413
407 SetStaleUsability(test_case.max_expired_time_sec, test_case.max_stale_uses, 414 SetStaleUsability(test_case.max_expired_time_sec, test_case.max_stale_uses,
408 test_case.allow_other_network); 415 test_case.allow_other_network);
409 CreateResolver(); 416 CreateResolver();
410 CreateCacheEntry(kCacheEntryTTLSec + test_case.age_sec); 417 CreateCacheEntry(kCacheEntryTTLSec + test_case.age_sec);
411 for (int j = 0; j < test_case.network_changes; ++j) 418 for (int j = 0; j < test_case.network_changes; ++j)
412 OnNetworkChange(); 419 OnNetworkChange();
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 498
492 EXPECT_TRUE(resolve_complete()); 499 EXPECT_TRUE(resolve_complete());
493 EXPECT_EQ(net::OK, resolve_error()); 500 EXPECT_EQ(net::OK, resolve_error());
494 EXPECT_EQ(1u, resolve_addresses().size()); 501 EXPECT_EQ(1u, resolve_addresses().size());
495 EXPECT_EQ(kCacheAddress, resolve_addresses()[0].ToStringWithoutPort()); 502 EXPECT_EQ(kCacheAddress, resolve_addresses()[0].ToStringWithoutPort());
496 } 503 }
497 504
498 } // namespace 505 } // namespace
499 506
500 } // namespace cronet 507 } // namespace cronet
OLDNEW
« no previous file with comments | « components/cronet/android/BUILD.gn ('k') | net/dns/host_resolver_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698