OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "base/callback.h" |
5 #include "base/metrics/field_trial.h" | 6 #include "base/metrics/field_trial.h" |
6 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
7 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
8 #include "base/test/mock_entropy_provider.h" | 9 #include "base/test/mock_entropy_provider.h" |
9 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
10 #include "net/http/http_response_headers.h" | 11 #include "net/http/http_response_headers.h" |
11 #include "net/test/spawned_test_server/spawned_test_server.h" | 12 #include "net/test/spawned_test_server/spawned_test_server.h" |
12 #include "net/url_request/test_url_fetcher_factory.h" | 13 #include "net/url_request/test_url_fetcher_factory.h" |
13 #include "net/url_request/url_fetcher_delegate.h" | 14 #include "net/url_request/url_fetcher_delegate.h" |
14 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" |
15 #include "sync/internal_api/public/base/cancelation_signal.h" | 16 #include "sync/internal_api/public/base/cancelation_signal.h" |
16 #include "sync/internal_api/public/http_bridge.h" | 17 #include "sync/internal_api/public/http_bridge.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include "third_party/zlib/zlib.h" | 19 #include "third_party/zlib/zlib.h" |
19 | 20 |
| 21 namespace net { |
| 22 class URLFetcher; |
| 23 } |
| 24 |
20 namespace syncer { | 25 namespace syncer { |
21 | 26 |
22 namespace { | 27 namespace { |
23 | 28 |
24 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. | 29 // TODO(timsteele): Should use PathService here. See Chromium Issue 3113. |
25 const base::FilePath::CharType kDocRoot[] = | 30 const base::FilePath::CharType kDocRoot[] = |
26 FILE_PATH_LITERAL("chrome/test/data"); | 31 FILE_PATH_LITERAL("chrome/test/data"); |
27 | 32 |
28 // ----------------------------------------------------------------------------- | 33 // ----------------------------------------------------------------------------- |
29 // The rest of the code in the anon namespace is copied from | 34 // The rest of the code in the anon namespace is copied from |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 } | 141 } |
137 io_thread_.Stop(); | 142 io_thread_.Stop(); |
138 } | 143 } |
139 | 144 |
140 HttpBridge* BuildBridge() { | 145 HttpBridge* BuildBridge() { |
141 if (!fake_default_request_context_getter_) { | 146 if (!fake_default_request_context_getter_) { |
142 fake_default_request_context_getter_ = | 147 fake_default_request_context_getter_ = |
143 new net::TestURLRequestContextGetter(io_thread_.task_runner()); | 148 new net::TestURLRequestContextGetter(io_thread_.task_runner()); |
144 fake_default_request_context_getter_->AddRef(); | 149 fake_default_request_context_getter_->AddRef(); |
145 } | 150 } |
146 HttpBridge* bridge = | 151 HttpBridge* bridge = new HttpBridge( |
147 new HttpBridge(kUserAgent, | 152 kUserAgent, fake_default_request_context_getter_, |
148 fake_default_request_context_getter_, | 153 NetworkTimeUpdateCallback(), base::Callback<void(net::URLFetcher*)>()); |
149 NetworkTimeUpdateCallback()); | |
150 return bridge; | 154 return bridge; |
151 } | 155 } |
152 | 156 |
153 static void Abort(HttpBridge* bridge) { | 157 static void Abort(HttpBridge* bridge) { |
154 bridge->Abort(); | 158 bridge->Abort(); |
155 } | 159 } |
156 | 160 |
157 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race | 161 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race |
158 // condition. | 162 // condition. |
159 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, | 163 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 204 |
201 // An HttpBridge that doesn't actually make network requests and just calls | 205 // An HttpBridge that doesn't actually make network requests and just calls |
202 // back with dummy response info. | 206 // back with dummy response info. |
203 // TODO(tim): Instead of inheriting here we should inject a component | 207 // TODO(tim): Instead of inheriting here we should inject a component |
204 // responsible for the MakeAsynchronousPost bit. | 208 // responsible for the MakeAsynchronousPost bit. |
205 class ShuntedHttpBridge : public HttpBridge { | 209 class ShuntedHttpBridge : public HttpBridge { |
206 public: | 210 public: |
207 // If |never_finishes| is true, the simulated request never actually | 211 // If |never_finishes| is true, the simulated request never actually |
208 // returns. | 212 // returns. |
209 ShuntedHttpBridge(net::URLRequestContextGetter* baseline_context_getter, | 213 ShuntedHttpBridge(net::URLRequestContextGetter* baseline_context_getter, |
210 MAYBE_SyncHttpBridgeTest* test, bool never_finishes) | 214 MAYBE_SyncHttpBridgeTest* test, |
| 215 bool never_finishes) |
211 : HttpBridge(kUserAgent, | 216 : HttpBridge(kUserAgent, |
212 baseline_context_getter, | 217 baseline_context_getter, |
213 NetworkTimeUpdateCallback()), | 218 NetworkTimeUpdateCallback(), |
214 test_(test), never_finishes_(never_finishes) { } | 219 base::Callback<void(net::URLFetcher*)>()), |
| 220 test_(test), |
| 221 never_finishes_(never_finishes) {} |
| 222 |
215 protected: | 223 protected: |
216 void MakeAsynchronousPost() override { | 224 void MakeAsynchronousPost() override { |
217 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); | 225 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); |
218 if (never_finishes_) | 226 if (never_finishes_) |
219 return; | 227 return; |
220 | 228 |
221 // We don't actually want to make a request for this test, so just callback | 229 // We don't actually want to make a request for this test, so just callback |
222 // as if it completed. | 230 // as if it completed. |
223 test_->GetIOThreadLoop()->PostTask(FROM_HERE, | 231 test_->GetIOThreadLoop()->PostTask(FROM_HERE, |
224 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); | 232 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); |
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 net::URLRequestContextGetter* baseline_context_getter, | 580 net::URLRequestContextGetter* baseline_context_getter, |
573 CancelationSignal* factory_cancelation_signal, | 581 CancelationSignal* factory_cancelation_signal, |
574 syncer::HttpPostProviderFactory** bridge_factory_out, | 582 syncer::HttpPostProviderFactory** bridge_factory_out, |
575 syncer::HttpPostProviderInterface** bridge_out, | 583 syncer::HttpPostProviderInterface** bridge_out, |
576 base::WaitableEvent* signal_when_created, | 584 base::WaitableEvent* signal_when_created, |
577 base::WaitableEvent* wait_for_shutdown) { | 585 base::WaitableEvent* wait_for_shutdown) { |
578 scoped_ptr<syncer::HttpBridgeFactory> bridge_factory( | 586 scoped_ptr<syncer::HttpBridgeFactory> bridge_factory( |
579 new syncer::HttpBridgeFactory(baseline_context_getter, | 587 new syncer::HttpBridgeFactory(baseline_context_getter, |
580 NetworkTimeUpdateCallback(), | 588 NetworkTimeUpdateCallback(), |
581 factory_cancelation_signal)); | 589 factory_cancelation_signal)); |
582 bridge_factory->Init("test"); | 590 bridge_factory->Init("test", base::Callback<void(net::URLFetcher*)>()); |
583 *bridge_factory_out = bridge_factory.get(); | 591 *bridge_factory_out = bridge_factory.get(); |
584 | 592 |
585 HttpPostProviderInterface* bridge = bridge_factory->Create(); | 593 HttpPostProviderInterface* bridge = bridge_factory->Create(); |
586 *bridge_out = bridge; | 594 *bridge_out = bridge; |
587 | 595 |
588 signal_when_created->Signal(); | 596 signal_when_created->Signal(); |
589 wait_for_shutdown->Wait(); | 597 wait_for_shutdown->Wait(); |
590 | 598 |
591 bridge_factory->Destroy(bridge); | 599 bridge_factory->Destroy(bridge); |
592 } | 600 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 new HttpBridgeFactory(baseline_context_getter.get(), | 674 new HttpBridgeFactory(baseline_context_getter.get(), |
667 NetworkTimeUpdateCallback(), | 675 NetworkTimeUpdateCallback(), |
668 &release_request_context_signal)); | 676 &release_request_context_signal)); |
669 | 677 |
670 // UI Thread: A very early shutdown request arrives and executes on the UI | 678 // UI Thread: A very early shutdown request arrives and executes on the UI |
671 // thread before the posted sync thread task is run. | 679 // thread before the posted sync thread task is run. |
672 release_request_context_signal.Signal(); | 680 release_request_context_signal.Signal(); |
673 | 681 |
674 // Sync thread: Finally run the posted task, only to find that our | 682 // Sync thread: Finally run the posted task, only to find that our |
675 // HttpBridgeFactory has been neutered. Should not crash. | 683 // HttpBridgeFactory has been neutered. Should not crash. |
676 factory->Init("TestUserAgent"); | 684 factory->Init("TestUserAgent", base::Callback<void(net::URLFetcher*)>()); |
677 | 685 |
678 // At this point, attempting to use the factory would trigger a crash. Both | 686 // At this point, attempting to use the factory would trigger a crash. Both |
679 // this test and the real world code should make sure this never happens. | 687 // this test and the real world code should make sure this never happens. |
680 }; | 688 }; |
681 | 689 |
682 } // namespace syncer | 690 } // namespace syncer |
OLD | NEW |