Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| 22 // Local test infrastructure | |
| 23 // * URLRequestSpecifiedResponseJob: A URLRequestJob that returns | |
| 24 // a different but derivable response for each URL (used for all | |
| 25 // url requests in this file). The class provides interfaces to | |
| 26 // signal whenever the total number of jobs transitions to zero. | |
| 27 // * SdchDictionaryFetcherTest: Registers a callback with the above | |
| 28 // class, and provides blocking interfaces for a transition to zero jobs. | |
| 29 // Contains an SdchDictionaryFetcher, and tracks fetcher dictionary | |
| 30 // addition callbacks. | |
|
mmenke
2015/01/21 21:33:57
Class level are generally be put just above the cl
Randy Smith (Not in Mondays)
2015/01/22 19:04:40
Hmmm. I can do that, and if you really want me to
mmenke
2015/01/22 19:58:33
Rather than tell you twice, which is something I p
Randy Smith (Not in Mondays)
2015/01/24 23:21:01
Thinking about it, the thing I care the most about
mmenke
2015/01/26 16:06:06
Works for me.
| |
| 31 // Most tests schedule a dictionary fetch, wait for no jobs outstanding, | |
| 32 // then test that the fetch results are as expected. | |
| 33 | |
| 21 namespace net { | 34 namespace net { |
| 22 | 35 |
| 23 static const char* kSampleBufferContext = "This is a sample buffer."; | 36 static const char* kSampleBufferContext = "This is a sample buffer."; |
| 24 static const char* kTestDomain = "top.domain.test"; | 37 static const char* kTestDomain = "top.domain.test"; |
|
mmenke
2015/01/22 19:58:33
nit: While you're here, "static const char kBlah[
Randy Smith (Not in Mondays)
2015/01/24 23:21:01
Done.
| |
| 25 | 38 |
| 26 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { | 39 class URLRequestSpecifiedResponseJob : public URLRequestSimpleJob { |
|
mmenke
2015/01/21 21:33:57
This should be in an anonymous namespace, and the
Randy Smith (Not in Mondays)
2015/01/22 19:04:40
Done, but I'd love to get a bit more guidance or p
mmenke
2015/01/22 19:58:32
You don't need namespace qualifiers with an anonym
Randy Smith (Not in Mondays)
2015/01/24 23:21:01
Sounds good. I'll adopt the whole file double nes
| |
| 27 public: | 40 public: |
| 28 URLRequestSpecifiedResponseJob(URLRequest* request, | 41 URLRequestSpecifiedResponseJob(URLRequest* request, |
| 29 NetworkDelegate* network_delegate) | 42 NetworkDelegate* network_delegate) |
| 30 : URLRequestSimpleJob(request, network_delegate) {} | 43 : URLRequestSimpleJob(request, network_delegate) { |
| 44 ++jobs_outstanding_; | |
| 45 } | |
| 31 | 46 |
| 32 static void AddUrlHandler() { | 47 static void AddUrlHandler() { |
| 33 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 48 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 34 jobs_requested_ = 0; | 49 jobs_requested_ = 0; |
| 35 filter->AddHostnameHandler( | 50 filter->AddHostnameHandler( |
| 36 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory); | 51 "http", kTestDomain, &URLRequestSpecifiedResponseJob::Factory); |
| 37 } | 52 } |
| 38 | 53 |
| 39 static void RemoveUrlHandler() { | 54 static void RemoveUrlHandler() { |
| 40 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 55 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 51 | 66 |
| 52 static std::string ExpectedResponseForURL(const GURL& url) { | 67 static std::string ExpectedResponseForURL(const GURL& url) { |
| 53 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", | 68 return base::StringPrintf("Response for %s\n%s\nEnd Response for %s\n", |
| 54 url.spec().c_str(), | 69 url.spec().c_str(), |
| 55 kSampleBufferContext, | 70 kSampleBufferContext, |
| 56 url.spec().c_str()); | 71 url.spec().c_str()); |
| 57 } | 72 } |
| 58 | 73 |
| 59 static int jobs_requested() { return jobs_requested_; } | 74 static int jobs_requested() { return jobs_requested_; } |
| 60 | 75 |
| 76 // |*callback| will be invoked when the total number of outstanding | |
| 77 // jobs drops to zero. | |
| 78 // Ownership of |*callback| remains with caller; |*callback| | |
| 79 // must be kept alive as long as this variable has a pointer to | |
| 80 // it. | |
| 81 static void SetSignalOnNoJobs(base::Closure* callback) { | |
| 82 signal_on_no_jobs_ = callback; | |
| 83 } | |
| 84 | |
| 85 static int jobs_outstanding() { return jobs_outstanding_; } | |
| 86 | |
| 61 private: | 87 private: |
| 62 ~URLRequestSpecifiedResponseJob() override{}; | 88 ~URLRequestSpecifiedResponseJob() override { |
| 89 --jobs_outstanding_; | |
| 90 if (jobs_outstanding_ == 0 && signal_on_no_jobs_) | |
| 91 signal_on_no_jobs_->Run(); | |
| 92 } | |
| 93 | |
| 63 int GetData(std::string* mime_type, | 94 int GetData(std::string* mime_type, |
| 64 std::string* charset, | 95 std::string* charset, |
| 65 std::string* data, | 96 std::string* data, |
| 66 const CompletionCallback& callback) const override { | 97 const CompletionCallback& callback) const override { |
| 67 GURL url(request_->url()); | 98 GURL url(request_->url()); |
| 68 *data = ExpectedResponseForURL(url); | 99 *data = ExpectedResponseForURL(url); |
| 69 return OK; | 100 return OK; |
| 70 } | 101 } |
| 71 | 102 |
| 103 static base::Closure* signal_on_no_jobs_; | |
| 72 static int jobs_requested_; | 104 static int jobs_requested_; |
| 105 static int jobs_outstanding_; | |
|
mmenke
2015/01/21 21:33:57
I'd advise against using non-const static variable
Randy Smith (Not in Mondays)
2015/01/22 19:04:39
Good point, and maybe I'm just having a braino, bu
mmenke
2015/01/22 19:58:33
I'm fine with avoiding RunUntilIdle, like this (I'
mmenke
2015/01/24 17:01:24
And a quick followup... If keeping around an unow
Randy Smith (Not in Mondays)
2015/01/24 23:21:01
I'm good with your original suggestion, and it did
| |
| 73 }; | 106 }; |
| 74 | 107 |
| 108 base::Closure* URLRequestSpecifiedResponseJob::signal_on_no_jobs_(nullptr); | |
| 109 | |
| 75 int URLRequestSpecifiedResponseJob::jobs_requested_(0); | 110 int URLRequestSpecifiedResponseJob::jobs_requested_(0); |
| 76 | 111 |
| 112 int URLRequestSpecifiedResponseJob::jobs_outstanding_(0); | |
| 113 | |
| 77 class SdchDictionaryFetcherTest : public ::testing::Test { | 114 class SdchDictionaryFetcherTest : public ::testing::Test { |
| 78 public: | 115 public: |
| 79 struct DictionaryAdditions { | 116 struct DictionaryAdditions { |
| 80 DictionaryAdditions(const std::string& dictionary_text, | 117 DictionaryAdditions(const std::string& dictionary_text, |
| 81 const GURL& dictionary_url) | 118 const GURL& dictionary_url) |
| 82 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} | 119 : dictionary_text(dictionary_text), dictionary_url(dictionary_url) {} |
| 83 | 120 |
| 84 std::string dictionary_text; | 121 std::string dictionary_text; |
| 85 GURL dictionary_url; | 122 GURL dictionary_url; |
| 86 }; | 123 }; |
| 87 | 124 |
| 88 SdchDictionaryFetcherTest() { | 125 SdchDictionaryFetcherTest() |
| 126 : waiting_for_no_jobs_(false), | |
| 127 run_loop_(nullptr), | |
| 128 no_jobs_seen_callback_( | |
| 129 base::Bind(&SdchDictionaryFetcherTest::NotifyNoJobs, | |
| 130 base::Unretained(this))) { | |
| 89 URLRequestSpecifiedResponseJob::AddUrlHandler(); | 131 URLRequestSpecifiedResponseJob::AddUrlHandler(); |
| 90 context_.reset(new TestURLRequestContext); | 132 context_.reset(new TestURLRequestContext); |
| 91 fetcher_.reset(new SdchDictionaryFetcher( | 133 fetcher_.reset(new SdchDictionaryFetcher( |
| 92 context_.get(), | 134 context_.get(), |
| 93 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, | 135 base::Bind(&SdchDictionaryFetcherTest::OnDictionaryFetched, |
| 94 base::Unretained(this)))); | 136 base::Unretained(this)))); |
| 137 URLRequestSpecifiedResponseJob::SetSignalOnNoJobs( | |
| 138 // Callback will be reset before destruction completion. | |
| 139 &no_jobs_seen_callback_); | |
| 95 } | 140 } |
| 96 | 141 |
| 97 ~SdchDictionaryFetcherTest() override { | 142 ~SdchDictionaryFetcherTest() override { |
| 98 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); | 143 URLRequestSpecifiedResponseJob::RemoveUrlHandler(); |
| 144 URLRequestSpecifiedResponseJob::SetSignalOnNoJobs(nullptr); | |
| 99 } | 145 } |
| 100 | 146 |
| 101 void OnDictionaryFetched(const std::string& dictionary_text, | 147 void OnDictionaryFetched(const std::string& dictionary_text, |
| 102 const GURL& dictionary_url, | 148 const GURL& dictionary_url, |
| 103 const BoundNetLog& net_log) { | 149 const BoundNetLog& net_log) { |
| 104 dictionary_additions.push_back( | 150 dictionary_additions.push_back( |
| 105 DictionaryAdditions(dictionary_text, dictionary_url)); | 151 DictionaryAdditions(dictionary_text, dictionary_url)); |
| 106 } | 152 } |
| 107 | 153 |
| 108 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { | 154 void GetDictionaryAdditions(std::vector<DictionaryAdditions>* out) { |
| 109 out->swap(dictionary_additions); | 155 out->swap(dictionary_additions); |
| 110 dictionary_additions.clear(); | 156 dictionary_additions.clear(); |
| 111 } | 157 } |
| 112 | 158 |
| 113 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } | 159 SdchDictionaryFetcher* fetcher() { return fetcher_.get(); } |
| 114 | 160 |
| 115 // May not be called outside the SetUp()/TearDown() interval. | 161 // May not be called outside the SetUp()/TearDown() interval. |
| 116 int JobsRequested() { | 162 int JobsRequested() { |
| 117 return URLRequestSpecifiedResponseJob::jobs_requested(); | 163 return URLRequestSpecifiedResponseJob::jobs_requested(); |
| 118 } | 164 } |
| 119 | 165 |
| 120 GURL PathToGurl(const char* path) { | 166 GURL PathToGurl(const char* path) { |
| 121 std::string gurl_string("http://"); | 167 std::string gurl_string("http://"); |
| 122 gurl_string += kTestDomain; | 168 gurl_string += kTestDomain; |
| 123 gurl_string += "/"; | 169 gurl_string += "/"; |
| 124 gurl_string += path; | 170 gurl_string += path; |
| 125 return GURL(gurl_string); | 171 return GURL(gurl_string); |
| 126 } | 172 } |
| 127 | 173 |
| 174 // Block until there are no outstanding URLRequestSpecifiedResponseJobs. | |
| 175 void WaitForNoJobs() { | |
| 176 if (URLRequestSpecifiedResponseJob::jobs_outstanding() == 0) | |
| 177 return; | |
| 178 | |
| 179 base::RunLoop run_loop; | |
| 180 run_loop_ = &run_loop; | |
|
mmenke
2015/01/21 21:33:57
Suggest just a scoped_ptr<RunLoop> in the class -
Randy Smith (Not in Mondays)
2015/01/22 19:04:40
Done.
| |
| 181 DCHECK(!waiting_for_no_jobs_); | |
| 182 waiting_for_no_jobs_ = true; | |
| 183 run_loop.Run(); | |
| 184 run_loop_ = nullptr; | |
| 185 waiting_for_no_jobs_ = false; | |
| 186 } | |
| 187 | |
| 128 private: | 188 private: |
| 189 void NotifyNoJobs() { | |
| 190 if (waiting_for_no_jobs_) | |
|
mmenke
2015/01/21 21:33:57
Any case where this is not the same as run_loop_ !
Randy Smith (Not in Mondays)
2015/01/22 19:04:40
CL history I should have cleaned up--I used to hav
| |
| 191 run_loop_->Quit(); | |
| 192 } | |
| 193 | |
| 194 bool waiting_for_no_jobs_; | |
| 195 base::RunLoop* run_loop_; | |
| 196 | |
| 129 scoped_ptr<TestURLRequestContext> context_; | 197 scoped_ptr<TestURLRequestContext> context_; |
| 130 scoped_ptr<SdchDictionaryFetcher> fetcher_; | 198 scoped_ptr<SdchDictionaryFetcher> fetcher_; |
| 131 std::vector<DictionaryAdditions> dictionary_additions; | 199 std::vector<DictionaryAdditions> dictionary_additions; |
| 200 base::Closure no_jobs_seen_callback_; | |
| 132 }; | 201 }; |
| 133 | 202 |
| 134 // Schedule a fetch and make sure it happens. | 203 // Schedule a fetch and make sure it happens. |
| 135 TEST_F(SdchDictionaryFetcherTest, Basic) { | 204 TEST_F(SdchDictionaryFetcherTest, Basic) { |
| 136 GURL dictionary_url(PathToGurl("dictionary")); | 205 GURL dictionary_url(PathToGurl("dictionary")); |
| 137 fetcher()->Schedule(dictionary_url); | 206 fetcher()->Schedule(dictionary_url); |
| 207 WaitForNoJobs(); | |
| 138 | 208 |
| 139 base::RunLoop().RunUntilIdle(); | |
| 140 EXPECT_EQ(1, JobsRequested()); | 209 EXPECT_EQ(1, JobsRequested()); |
| 141 std::vector<DictionaryAdditions> additions; | 210 std::vector<DictionaryAdditions> additions; |
| 142 GetDictionaryAdditions(&additions); | 211 GetDictionaryAdditions(&additions); |
| 143 ASSERT_EQ(1u, additions.size()); | 212 ASSERT_EQ(1u, additions.size()); |
| 144 EXPECT_EQ( | 213 EXPECT_EQ( |
| 145 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 214 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 146 additions[0].dictionary_text); | 215 additions[0].dictionary_text); |
| 147 } | 216 } |
| 148 | 217 |
| 149 // Multiple fetches of the same URL should result in only one request. | 218 // Multiple fetches of the same URL should result in only one request. |
| 150 TEST_F(SdchDictionaryFetcherTest, Multiple) { | 219 TEST_F(SdchDictionaryFetcherTest, Multiple) { |
| 151 GURL dictionary_url(PathToGurl("dictionary")); | 220 GURL dictionary_url(PathToGurl("dictionary")); |
| 152 fetcher()->Schedule(dictionary_url); | 221 fetcher()->Schedule(dictionary_url); |
| 153 fetcher()->Schedule(dictionary_url); | 222 fetcher()->Schedule(dictionary_url); |
| 154 fetcher()->Schedule(dictionary_url); | 223 fetcher()->Schedule(dictionary_url); |
| 155 base::RunLoop().RunUntilIdle(); | 224 WaitForNoJobs(); |
| 156 | 225 |
| 157 EXPECT_EQ(1, JobsRequested()); | 226 EXPECT_EQ(1, JobsRequested()); |
| 158 std::vector<DictionaryAdditions> additions; | 227 std::vector<DictionaryAdditions> additions; |
| 159 GetDictionaryAdditions(&additions); | 228 GetDictionaryAdditions(&additions); |
| 160 ASSERT_EQ(1u, additions.size()); | 229 ASSERT_EQ(1u, additions.size()); |
| 161 EXPECT_EQ( | 230 EXPECT_EQ( |
| 162 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | 231 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), |
| 163 additions[0].dictionary_text); | 232 additions[0].dictionary_text); |
| 164 } | 233 } |
| 165 | 234 |
| 166 // A cancel should result in no actual requests being generated. | 235 // A cancel should result in no actual requests being generated. |
| 167 TEST_F(SdchDictionaryFetcherTest, Cancel) { | 236 TEST_F(SdchDictionaryFetcherTest, Cancel) { |
| 168 GURL dictionary_url_1(PathToGurl("dictionary_1")); | 237 GURL dictionary_url_1(PathToGurl("dictionary_1")); |
| 169 GURL dictionary_url_2(PathToGurl("dictionary_2")); | 238 GURL dictionary_url_2(PathToGurl("dictionary_2")); |
| 170 GURL dictionary_url_3(PathToGurl("dictionary_3")); | 239 GURL dictionary_url_3(PathToGurl("dictionary_3")); |
| 171 | 240 |
| 172 fetcher()->Schedule(dictionary_url_1); | 241 fetcher()->Schedule(dictionary_url_1); |
| 173 fetcher()->Schedule(dictionary_url_2); | 242 fetcher()->Schedule(dictionary_url_2); |
| 174 fetcher()->Schedule(dictionary_url_3); | 243 fetcher()->Schedule(dictionary_url_3); |
| 175 fetcher()->Cancel(); | 244 fetcher()->Cancel(); |
| 176 base::RunLoop().RunUntilIdle(); | 245 WaitForNoJobs(); |
| 177 | 246 |
| 178 // Synchronous execution may have resulted in a single job being scheduled. | 247 // Synchronous execution may have resulted in a single job being scheduled. |
| 179 EXPECT_GE(1, JobsRequested()); | 248 EXPECT_GE(1, JobsRequested()); |
| 180 } | 249 } |
| 250 | |
| 251 // Attempt to confuse the fetcher loop processing by scheduling a | |
| 252 // dictionary addition while another fetch is in process. | |
| 253 TEST_F(SdchDictionaryFetcherTest, LoopRace) { | |
| 254 GURL dictionary_url(PathToGurl("dictionary")); | |
| 255 GURL dictionary1_url(PathToGurl("dictionary1")); | |
| 256 fetcher()->Schedule(dictionary_url); | |
| 257 fetcher()->Schedule(dictionary1_url); | |
| 258 WaitForNoJobs(); | |
| 259 | |
| 260 EXPECT_EQ(2, JobsRequested()); | |
| 261 std::vector<DictionaryAdditions> additions; | |
| 262 GetDictionaryAdditions(&additions); | |
| 263 ASSERT_EQ(2u, additions.size()); | |
| 264 EXPECT_EQ( | |
| 265 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary_url), | |
| 266 additions[0].dictionary_text); | |
| 267 EXPECT_EQ( | |
| 268 URLRequestSpecifiedResponseJob::ExpectedResponseForURL(dictionary1_url), | |
| 269 additions[1].dictionary_text); | |
| 181 } | 270 } |
| 271 } | |
|
mmenke
2015/01/21 21:33:57
// net (And there should be a linebreak before the
Randy Smith (Not in Mondays)
2015/01/22 19:04:39
Whoops. Done.
| |
| OLD | NEW |