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

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

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

Powered by Google App Engine
This is Rietveld 408576698