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

Side by Side Diff: net/url_request/sdch_dictionary_fetcher_unittest.cc

Issue 864923002: In SdchDictionaryFetcher, don't set state when scheduling if loop is active. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync'd to p313032. Created 5 years, 11 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 | « net/url_request/sdch_dictionary_fetcher.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/url_request/sdch_dictionary_fetcher.h" 5 #include "net/url_request/sdch_dictionary_fetcher.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/message_loop/message_loop_proxy.h" 10 #include "base/callback.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/thread_task_runner_handle.h" 13 #include "base/thread_task_runner_handle.h"
14 #include "net/base/sdch_manager.h" 14 #include "net/base/sdch_manager.h"
15 #include "net/url_request/url_request_data_job.h" 15 #include "net/url_request/url_request_data_job.h"
16 #include "net/url_request/url_request_filter.h" 16 #include "net/url_request/url_request_filter.h"
17 #include "net/url_request/url_request_interceptor.h" 17 #include "net/url_request/url_request_interceptor.h"
18 #include "net/url_request/url_request_test_util.h" 18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 20 #include "url/gurl.h"
21 21
22 namespace net { 22 namespace net {
23 23
24 static const char kSampleBufferContext[] = "This is a sample buffer."; 24 namespace {
25 static const char kTestDomain[] = "top.domain.test"; 25
26 const char kSampleBufferContext[] = "This is a sample buffer.";
27 const char kTestDomain[] = "top.domain.test";
26 28
27 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { 29 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob {
28 public: 30 public:
29 URLRequestSpecifiedResponseJob(URLRequest* request, 31 URLRequestSpecifiedResponseJob(URLRequest* request,
30 NetworkDelegate* network_delegate) 32 NetworkDelegate* network_delegate,
31 : URLRequestSimpleJob(request, network_delegate) {} 33 const base::Closure& destruction_callback)
32 34 : URLRequestSimpleJob(request, network_delegate),
33 static void AddUrlHandler() { 35 destruction_callback_(destruction_callback) {}
34 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
35 jobs_requested_ = 0;
36 filter->AddHostnameHandler(
37 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory);
38 }
39
40 static void RemoveUrlHandler() {
41 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
42 filter->RemoveHostnameHandler("http", kTestDomain);
43 jobs_requested_ = 0;
44 }
45
46 static URLRequestJob* Factory(URLRequest* request,
47 net::NetworkDelegate* network_delegate,
48 const std::string& scheme) {
49 ++jobs_requested_;
50 return new URLRequestSpecifiedResponseJob(request, network_delegate);
51 }
52 36
53 static std::string ExpectedResponseForURL(const GURL& url) { 37 static std::string ExpectedResponseForURL(const GURL& url) {
54 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", 38 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n",
55 url.spec().c_str(), 39 url.spec().c_str(),
56 kSampleBufferContext, 40 kSampleBufferContext,
57 url.spec().c_str()); 41 url.spec().c_str());
58 } 42 }
59 43
60 static int jobs_requested() { return jobs_requested_; }
61
62 private: 44 private:
63 ~URLRequestSpecifiedResponseJob() override{}; 45 ~URLRequestSpecifiedResponseJob() override {
46 if (!destruction_callback_.is_null())
mmenke 2015/01/26 16:06:06 Since you're never passing in a null callback, sho
Randy Smith (Not in Mondays) 2015/01/26 19:21:17 Done.
47 destruction_callback_.Run();
48 }
64 49
65 // URLRequestSimpleJob implementation: 50 // URLRequestSimpleJob implementation:
66
67 int GetData(std::string* mime_type, 51 int GetData(std::string* mime_type,
68 std::string* charset, 52 std::string* charset,
69 std::string* data, 53 std::string* data,
70 const CompletionCallback& callback) const override { 54 const CompletionCallback& callback) const override {
71 GURL url(request_->url()); 55 GURL url(request_->url());
72 *data = ExpectedResponseForURL(url); 56 *data = ExpectedResponseForURL(url);
73 return OK; 57 return OK;
74 } 58 }
75 59
76 base::TaskRunner* GetTaskRunner() const override { 60 base::Closure destruction_callback_;
mmenke 2015/01/26 16:06:06 optional: Could make this const.
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Done.
77 return base::MessageLoopProxy::current().get(); 61 };
62
63 class SpecifiedResponseJobInterceptor : public URLRequestInterceptor {
64 public:
65 typedef base::Callback<void(int outstanding_job_delta)> LifecycleCallback;
mmenke 2015/01/26 16:06:06 Think this could use a comment, what it doesn't wh
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Done.
66
67 SpecifiedResponseJobInterceptor(const LifecycleCallback& lifecycle_callback)
mmenke 2015/01/26 16:06:06 nit: explicit
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Done.
68 : lifecycle_callback_(lifecycle_callback), factory_(this) {}
69 ~SpecifiedResponseJobInterceptor() override {}
70
71 URLRequestJob* MaybeInterceptRequest(
72 URLRequest* request,
73 NetworkDelegate* network_delegate) const override {
74 if (!lifecycle_callback_.is_null())
75 lifecycle_callback_.Run(1);
76
77 return new URLRequestSpecifiedResponseJob(
78 request, network_delegate,
79 base::Bind(
80 &SpecifiedResponseJobInterceptor::OnSpecfiedResponseJobDestruction,
81 factory_.GetWeakPtr()));
mmenke 2015/01/26 16:06:06 Don't need two weak pointers here (The test fixtur
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Good point. I'm including the DCHECKs for interfa
78 } 82 }
79 83
80 static int jobs_requested_; 84 // The caller must ensure that the callback is valid to call for the
85 // lifetime of the SpecifiedResponseJobInterceptor (i.e. until
86 // Unregister() is called).
87 static void RegisterWithFilter(const LifecycleCallback& lifecycle_callback) {
88 scoped_ptr<SpecifiedResponseJobInterceptor> interceptor(
89 new SpecifiedResponseJobInterceptor(lifecycle_callback));
90
91 net::URLRequestFilter::GetInstance()->AddHostnameInterceptor(
92 "http", kTestDomain, interceptor.Pass());
93 }
94
95 static void Unregister() {
96 net::URLRequestFilter::GetInstance()->RemoveHostnameHandler("http",
97 kTestDomain);
98 }
99
100 private:
101 void OnSpecfiedResponseJobDestruction() const {
102 if (!lifecycle_callback_.is_null())
103 lifecycle_callback_.Run(-1);
104 }
105
106 LifecycleCallback lifecycle_callback_;
107 mutable base::WeakPtrFactory<SpecifiedResponseJobInterceptor> factory_;
81 }; 108 };
82 109
83 int URLRequestSpecifiedResponseJob::jobs_requested_(0); 110 // Local test infrastructure
111 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns
112 // a different but derivable response for each URL (used for all
113 // url requests in this file). The class provides interfaces to
114 // signal whenever the total number of jobs transitions to zero.
115 // * SdchDictionaryFetcherTest: Registers a callback with the above
116 // class, and provides blocking interfaces for a transition to zero jobs.
117 // Contains an SdchDictionaryFetcher, and tracks fetcher dictionary
118 // addition callbacks.
119 // Most tests schedule a dictionary fetch, wait for no jobs outstanding,
120 // then test that the fetch results are as expected.
84 121
85 class SdchDictionaryFetcherTest : public ::testing::Test { 122 class SdchDictionaryFetcherTest : public ::testing::Test {
86 public: 123 public:
87 struct DictionaryAdditions { 124 struct DictionaryAdditions {
88 DictionaryAdditions(const std::string& dictionary_text, 125 DictionaryAdditions(const std::string& dictionary_text,
89 const GURL& dictionary_url) 126 const GURL& dictionary_url)
90 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} 127 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {}
91 128
92 std::string dictionary_text; 129 std::string dictionary_text;
93 GURL dictionary_url; 130 GURL dictionary_url;
94 }; 131 };
95 132
96 SdchDictionaryFetcherTest() { 133 SdchDictionaryFetcherTest()
97 URLRequestSpecifiedResponseJob::AddUrlHandler(); 134 : jobs_requested_(0),
98 context_.reset(new TestURLRequestContext); 135 jobs_outstanding_(0),
99 fetcher_.reset(new SdchDictionaryFetcher( 136 context_(new TestURLRequestContext),
100 context_.get(), 137 fetcher_(new SdchDictionaryFetcher(
101 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, 138 context_.get(),
102 base::Unretained(this)))); 139 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched,
140 base::Unretained(this)))),
141 factory_(this) {
142 SpecifiedResponseJobInterceptor::RegisterWithFilter(
143 base::Bind(&SdchDictionaryFetcherTest::OnNumberJobsChanged,
144 factory_.GetWeakPtr()));
103 } 145 }
104 146
105 ~SdchDictionaryFetcherTest() override { 147 ~SdchDictionaryFetcherTest() override {
106 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); 148 SpecifiedResponseJobInterceptor::Unregister();
107 } 149 }
108 150
109 void OnDictionaryFetched(const std::string& dictionary_text, 151 void OnDictionaryFetched(const std::string& dictionary_text,
110 const GURL& dictionary_url, 152 const GURL& dictionary_url,
111 const BoundNetLog& net_log) { 153 const BoundNetLog& net_log) {
112 dictionary_additions.push_back( 154 dictionary_additions.push_back(
113 DictionaryAdditions(dictionary_text, dictionary_url)); 155 DictionaryAdditions(dictionary_text, dictionary_url));
114 } 156 }
115 157
116 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { 158 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) {
mmenke 2015/01/26 16:06:06 Suggest comments here. The clear behavior, in par
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Done.
117 out->swap(dictionary_additions); 159 out->swap(dictionary_additions);
118 dictionary_additions.clear(); 160 dictionary_additions.clear();
119 } 161 }
120 162
121 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } 163 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); }
122 164
123 // May not be called outside the SetUp()/TearDown() interval. 165 // May not be called outside the SetUp()/TearDown() interval.
124 int JobsRequested() { 166 int jobs_requested() { return jobs_requested_; }
mmenke 2015/01/26 16:06:06 nit: const
Randy Smith (Not in Mondays) 2015/01/26 19:21:17 Done.
125 return URLRequestSpecifiedResponseJob::jobs_requested();
126 }
127 167
128 GURL PathToGurl(const char* path) { 168 GURL PathToGurl(const char* path) {
129 std::string gurl_string("http://"); 169 std::string gurl_string("http://");
130 gurl_string += kTestDomain; 170 gurl_string += kTestDomain;
131 gurl_string += "/"; 171 gurl_string += "/";
132 gurl_string += path; 172 gurl_string += path;
133 return GURL(gurl_string); 173 return GURL(gurl_string);
134 } 174 }
135 175
176 // Block until there are no outstanding URLRequestSpecifiedResponseJobs.
177 void WaitForNoJobs() {
178 if (jobs_outstanding_ == 0)
179 return;
180
181 run_loop_.reset(new base::RunLoop);
182 run_loop_->Run();
183 run_loop_.reset();
184 }
185
136 private: 186 private:
187 void OnNumberJobsChanged(int outstanding_jobs_delta) {
188 if (outstanding_jobs_delta > 0)
189 jobs_requested_ += outstanding_jobs_delta;
190 jobs_outstanding_ += outstanding_jobs_delta;
191 if (jobs_outstanding_ == 0 && run_loop_.get())
mmenke 2015/01/26 16:06:06 nit: get() isn't necessary when coercing to bool
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Done.
192 run_loop_->Quit();
193 }
194
195 int jobs_requested_;
196 int jobs_outstanding_;
197 scoped_ptr<base::RunLoop> run_loop_;
137 scoped_ptr<TestURLRequestContext> context_; 198 scoped_ptr<TestURLRequestContext> context_;
138 scoped_ptr<SdchDictionaryFetcher> fetcher_; 199 scoped_ptr<SdchDictionaryFetcher> fetcher_;
139 std::vector<DictionaryAdditions> dictionary_additions; 200 std::vector<DictionaryAdditions> dictionary_additions;
201 base::WeakPtrFactory<SdchDictionaryFetcherTest> factory_;
140 }; 202 };
141 203
142 // Schedule a fetch and make sure it happens. 204 // Schedule a fetch and make sure it happens.
143 TEST_F(SdchDictionaryFetcherTest, Basic) { 205 TEST_F(SdchDictionaryFetcherTest, Basic) {
144 GURL dictionary_url(PathToGurl("dictionary")); 206 GURL dictionary_url(PathToGurl("dictionary"));
145 fetcher()->Schedule(dictionary_url); 207 fetcher()->Schedule(dictionary_url);
208 WaitForNoJobs();
146 209
147 base::RunLoop().RunUntilIdle(); 210 EXPECT_EQ(1, jobs_requested());
148 EXPECT_EQ(1, JobsRequested());
149 std::vector<DictionaryAdditions> additions; 211 std::vector<DictionaryAdditions> additions;
150 GetDictionaryAdditions(&additions); 212 GetDictionaryAdditions(&additions);
151 ASSERT_EQ(1u, additions.size()); 213 ASSERT_EQ(1u, additions.size());
152 EXPECT_EQ( 214 EXPECT_EQ(
153 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), 215 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
154 additions[0].dictionary_text); 216 additions[0].dictionary_text);
155 } 217 }
156 218
157 // Multiple fetches of the same URL should result in only one request. 219 // Multiple fetches of the same URL should result in only one request.
158 TEST_F(SdchDictionaryFetcherTest, Multiple) { 220 TEST_F(SdchDictionaryFetcherTest, Multiple) {
159 GURL dictionary_url(PathToGurl("dictionary")); 221 GURL dictionary_url(PathToGurl("dictionary"));
160 fetcher()->Schedule(dictionary_url); 222 fetcher()->Schedule(dictionary_url);
161 fetcher()->Schedule(dictionary_url); 223 fetcher()->Schedule(dictionary_url);
162 fetcher()->Schedule(dictionary_url); 224 fetcher()->Schedule(dictionary_url);
163 base::RunLoop().RunUntilIdle(); 225 WaitForNoJobs();
164 226
165 EXPECT_EQ(1, JobsRequested()); 227 EXPECT_EQ(1, jobs_requested());
166 std::vector<DictionaryAdditions> additions; 228 std::vector<DictionaryAdditions> additions;
167 GetDictionaryAdditions(&additions); 229 GetDictionaryAdditions(&additions);
168 ASSERT_EQ(1u, additions.size()); 230 ASSERT_EQ(1u, additions.size());
169 EXPECT_EQ( 231 EXPECT_EQ(
170 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), 232 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
171 additions[0].dictionary_text); 233 additions[0].dictionary_text);
172 } 234 }
173 235
174 // A cancel should result in no actual requests being generated. 236 // A cancel should result in no actual requests being generated.
175 TEST_F(SdchDictionaryFetcherTest, Cancel) { 237 TEST_F(SdchDictionaryFetcherTest, Cancel) {
176 GURL dictionary_url_1(PathToGurl("dictionary_1")); 238 GURL dictionary_url_1(PathToGurl("dictionary_1"));
177 GURL dictionary_url_2(PathToGurl("dictionary_2")); 239 GURL dictionary_url_2(PathToGurl("dictionary_2"));
178 GURL dictionary_url_3(PathToGurl("dictionary_3")); 240 GURL dictionary_url_3(PathToGurl("dictionary_3"));
179 241
180 fetcher()->Schedule(dictionary_url_1); 242 fetcher()->Schedule(dictionary_url_1);
181 fetcher()->Schedule(dictionary_url_2); 243 fetcher()->Schedule(dictionary_url_2);
182 fetcher()->Schedule(dictionary_url_3); 244 fetcher()->Schedule(dictionary_url_3);
183 fetcher()->Cancel(); 245 fetcher()->Cancel();
184 base::RunLoop().RunUntilIdle(); 246 WaitForNoJobs();
185 247
186 // Synchronous execution may have resulted in a single job being scheduled. 248 // Synchronous execution may have resulted in a single job being scheduled.
187 EXPECT_GE(1, JobsRequested()); 249 EXPECT_GE(1, jobs_requested());
188 } 250 }
251
252 // Attempt to confuse the fetcher loop processing by scheduling a
253 // dictionary addition while another fetch is in process.
254 TEST_F(SdchDictionaryFetcherTest, LoopRace) {
255 GURL dictionary_url(PathToGurl("dictionary"));
mmenke 2015/01/26 16:06:06 Suggest replacing "dictionary" with "dictionary0".
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Probably--I don't remember :-}. Done.
256 GURL dictionary1_url(PathToGurl("dictionary1"));
257 fetcher()->Schedule(dictionary_url);
258 fetcher()->Schedule(dictionary1_url);
259 WaitForNoJobs();
260
261 EXPECT_EQ(2, jobs_requested());
mmenke 2015/01/26 16:06:06 This should be an assert, I think.
Randy Smith (Not in Mondays) 2015/01/26 19:21:18 Agreed; done.
262 std::vector<DictionaryAdditions> additions;
263 GetDictionaryAdditions(&additions);
264 ASSERT_EQ(2u, additions.size());
265 EXPECT_EQ(
266 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url),
267 additions[0].dictionary_text);
268 EXPECT_EQ(
269 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url),
270 additions[1].dictionary_text);
189 } 271 }
272
273 } // namespace
274
275 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/sdch_dictionary_fetcher.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698