| 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 "components/sync/engine/net/http_bridge.h" | 5 #include "components/sync/engine/net/http_bridge.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/bit_cast.h" | 9 #include "base/bit_cast.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 const char kUserAgent[] = "user-agent"; | 32 const char kUserAgent[] = "user-agent"; |
| 33 | 33 |
| 34 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
| 35 #define MAYBE_SyncHttpBridgeTest DISABLED_SyncHttpBridgeTest | 35 #define MAYBE_SyncHttpBridgeTest DISABLED_SyncHttpBridgeTest |
| 36 #else | 36 #else |
| 37 #define MAYBE_SyncHttpBridgeTest SyncHttpBridgeTest | 37 #define MAYBE_SyncHttpBridgeTest SyncHttpBridgeTest |
| 38 #endif // defined(OS_ANDROID) | 38 #endif // defined(OS_ANDROID) |
| 39 class MAYBE_SyncHttpBridgeTest : public testing::Test { | 39 class MAYBE_SyncHttpBridgeTest : public testing::Test { |
| 40 public: | 40 public: |
| 41 MAYBE_SyncHttpBridgeTest() | 41 MAYBE_SyncHttpBridgeTest() |
| 42 : fake_default_request_context_getter_(NULL), | 42 : fake_default_request_context_getter_(nullptr), |
| 43 bridge_for_race_test_(NULL), | 43 bridge_for_race_test_(nullptr), |
| 44 io_thread_("IO thread") { | 44 io_thread_("IO thread") { |
| 45 test_server_.AddDefaultHandlers(base::FilePath(kDocRoot)); | 45 test_server_.AddDefaultHandlers(base::FilePath(kDocRoot)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void SetUp() override { | 48 void SetUp() override { |
| 49 base::Thread::Options options; | 49 base::Thread::Options options; |
| 50 options.message_loop_type = base::MessageLoop::TYPE_IO; | 50 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 51 io_thread_.StartWithOptions(options); | 51 io_thread_.StartWithOptions(options); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void TearDown() override { | 54 void TearDown() override { |
| 55 if (fake_default_request_context_getter_) { | 55 if (fake_default_request_context_getter_) { |
| 56 GetIOThreadLoop()->task_runner()->ReleaseSoon( | 56 GetIOThreadLoop()->task_runner()->ReleaseSoon( |
| 57 FROM_HERE, fake_default_request_context_getter_); | 57 FROM_HERE, fake_default_request_context_getter_); |
| 58 fake_default_request_context_getter_ = NULL; | 58 fake_default_request_context_getter_ = nullptr; |
| 59 } | 59 } |
| 60 io_thread_.Stop(); | 60 io_thread_.Stop(); |
| 61 } | 61 } |
| 62 | 62 |
| 63 HttpBridge* BuildBridge() { | 63 HttpBridge* BuildBridge() { |
| 64 if (!fake_default_request_context_getter_) { | 64 if (!fake_default_request_context_getter_) { |
| 65 fake_default_request_context_getter_ = | 65 fake_default_request_context_getter_ = |
| 66 new net::TestURLRequestContextGetter(io_thread_.task_runner()); | 66 new net::TestURLRequestContextGetter(io_thread_.task_runner()); |
| 67 fake_default_request_context_getter_->AddRef(); | 67 fake_default_request_context_getter_->AddRef(); |
| 68 } | 68 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 153 } |
| 154 | 154 |
| 155 private: | 155 private: |
| 156 ~ShuntedHttpBridge() override {} | 156 ~ShuntedHttpBridge() override {} |
| 157 | 157 |
| 158 void CallOnURLFetchComplete() { | 158 void CallOnURLFetchComplete() { |
| 159 ASSERT_TRUE( | 159 ASSERT_TRUE( |
| 160 test_->GetIOThreadLoop()->task_runner()->BelongsToCurrentThread()); | 160 test_->GetIOThreadLoop()->task_runner()->BelongsToCurrentThread()); |
| 161 // We return a dummy content response. | 161 // We return a dummy content response. |
| 162 std::string response_content = "success!"; | 162 std::string response_content = "success!"; |
| 163 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); | 163 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), nullptr); |
| 164 scoped_refptr<net::HttpResponseHeaders> response_headers( | 164 scoped_refptr<net::HttpResponseHeaders> response_headers( |
| 165 new net::HttpResponseHeaders("")); | 165 new net::HttpResponseHeaders("")); |
| 166 fetcher.set_response_code(200); | 166 fetcher.set_response_code(200); |
| 167 fetcher.SetResponseString(response_content); | 167 fetcher.SetResponseString(response_content); |
| 168 fetcher.set_response_headers(response_headers); | 168 fetcher.set_response_headers(response_headers); |
| 169 OnURLFetchComplete(&fetcher); | 169 OnURLFetchComplete(&fetcher); |
| 170 } | 170 } |
| 171 MAYBE_SyncHttpBridgeTest* test_; | 171 MAYBE_SyncHttpBridgeTest* test_; |
| 172 bool never_finishes_; | 172 bool never_finishes_; |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 void MAYBE_SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( | 175 void MAYBE_SyncHttpBridgeTest::RunSyncThreadBridgeUseTest( |
| 176 base::WaitableEvent* signal_when_created, | 176 base::WaitableEvent* signal_when_created, |
| 177 base::WaitableEvent* signal_when_released) { | 177 base::WaitableEvent* signal_when_released) { |
| 178 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 178 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| 179 new net::TestURLRequestContextGetter(io_thread_.task_runner())); | 179 new net::TestURLRequestContextGetter(io_thread_.task_runner())); |
| 180 { | 180 { |
| 181 scoped_refptr<ShuntedHttpBridge> bridge( | 181 scoped_refptr<ShuntedHttpBridge> bridge( |
| 182 new ShuntedHttpBridge(ctx_getter.get(), this, true)); | 182 new ShuntedHttpBridge(ctx_getter.get(), this, true)); |
| 183 bridge->SetURL("http://www.google.com", 9999); | 183 bridge->SetURL("http://www.google.com", 9999); |
| 184 bridge->SetPostPayload("text/plain", 2, " "); | 184 bridge->SetPostPayload("text/plain", 2, " "); |
| 185 bridge_for_race_test_ = bridge.get(); | 185 bridge_for_race_test_ = bridge.get(); |
| 186 signal_when_created->Signal(); | 186 signal_when_created->Signal(); |
| 187 | 187 |
| 188 int os_error = 0; | 188 int os_error = 0; |
| 189 int response_code = 0; | 189 int response_code = 0; |
| 190 bridge->MakeSynchronousPost(&os_error, &response_code); | 190 bridge->MakeSynchronousPost(&os_error, &response_code); |
| 191 bridge_for_race_test_ = NULL; | 191 bridge_for_race_test_ = nullptr; |
| 192 } | 192 } |
| 193 signal_when_released->Signal(); | 193 signal_when_released->Signal(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 TEST_F(MAYBE_SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { | 196 TEST_F(MAYBE_SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { |
| 197 // Run this test on the IO thread because we can only call | 197 // Run this test on the IO thread because we can only call |
| 198 // URLRequestContextGetter::GetURLRequestContext on the IO thread. | 198 // URLRequestContextGetter::GetURLRequestContext on the IO thread. |
| 199 io_thread()->task_runner()->PostTask( | 199 io_thread()->task_runner()->PostTask( |
| 200 FROM_HERE, | 200 FROM_HERE, |
| 201 base::Bind(&MAYBE_SyncHttpBridgeTest::TestSameHttpNetworkSession, | 201 base::Bind(&MAYBE_SyncHttpBridgeTest::TestSameHttpNetworkSession, |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&io_waiter)))); | 427 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&io_waiter)))); |
| 428 | 428 |
| 429 signal_when_created.Wait(); // Wait till we have a bridge to abort. | 429 signal_when_created.Wait(); // Wait till we have a bridge to abort. |
| 430 ASSERT_TRUE(bridge_for_race_test()); | 430 ASSERT_TRUE(bridge_for_race_test()); |
| 431 | 431 |
| 432 // Schedule the fetch completion callback (but don't run it yet). Don't take | 432 // Schedule the fetch completion callback (but don't run it yet). Don't take |
| 433 // a reference to the bridge to mimic URLFetcher's handling of the delegate. | 433 // a reference to the bridge to mimic URLFetcher's handling of the delegate. |
| 434 net::URLFetcherDelegate* delegate = | 434 net::URLFetcherDelegate* delegate = |
| 435 static_cast<net::URLFetcherDelegate*>(bridge_for_race_test()); | 435 static_cast<net::URLFetcherDelegate*>(bridge_for_race_test()); |
| 436 std::string response_content = "success!"; | 436 std::string response_content = "success!"; |
| 437 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), NULL); | 437 net::TestURLFetcher fetcher(0, GURL("http://www.google.com"), nullptr); |
| 438 fetcher.set_response_code(200); | 438 fetcher.set_response_code(200); |
| 439 fetcher.SetResponseString(response_content); | 439 fetcher.SetResponseString(response_content); |
| 440 ASSERT_TRUE(io_thread()->task_runner()->PostTask( | 440 ASSERT_TRUE(io_thread()->task_runner()->PostTask( |
| 441 FROM_HERE, base::Bind(&net::URLFetcherDelegate::OnURLFetchComplete, | 441 FROM_HERE, base::Bind(&net::URLFetcherDelegate::OnURLFetchComplete, |
| 442 base::Unretained(delegate), &fetcher))); | 442 base::Unretained(delegate), &fetcher))); |
| 443 | 443 |
| 444 // Abort the fetch. This should be smart enough to handle the case where | 444 // Abort the fetch. This should be smart enough to handle the case where |
| 445 // the bridge is destroyed before the callback scheduled above completes. | 445 // the bridge is destroyed before the callback scheduled above completes. |
| 446 bridge_for_race_test()->Abort(); | 446 bridge_for_race_test()->Abort(); |
| 447 | 447 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 signal_wait_start->Signal(); | 486 signal_wait_start->Signal(); |
| 487 wait_done->Wait(); | 487 wait_done->Wait(); |
| 488 } | 488 } |
| 489 | 489 |
| 490 // Tests RequestContextGetter is properly released on IO thread even when | 490 // Tests RequestContextGetter is properly released on IO thread even when |
| 491 // IO thread stops before sync thread. | 491 // IO thread stops before sync thread. |
| 492 TEST_F(MAYBE_SyncHttpBridgeTest, RequestContextGetterReleaseOrder) { | 492 TEST_F(MAYBE_SyncHttpBridgeTest, RequestContextGetterReleaseOrder) { |
| 493 base::Thread sync_thread("SyncThread"); | 493 base::Thread sync_thread("SyncThread"); |
| 494 sync_thread.Start(); | 494 sync_thread.Start(); |
| 495 | 495 |
| 496 HttpPostProviderFactory* factory = NULL; | 496 HttpPostProviderFactory* factory = nullptr; |
| 497 HttpPostProviderInterface* bridge = NULL; | 497 HttpPostProviderInterface* bridge = nullptr; |
| 498 | 498 |
| 499 scoped_refptr<net::URLRequestContextGetter> baseline_context_getter( | 499 scoped_refptr<net::URLRequestContextGetter> baseline_context_getter( |
| 500 new net::TestURLRequestContextGetter(io_thread()->task_runner())); | 500 new net::TestURLRequestContextGetter(io_thread()->task_runner())); |
| 501 | 501 |
| 502 base::WaitableEvent signal_when_created( | 502 base::WaitableEvent signal_when_created( |
| 503 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 503 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 504 base::WaitableEvent::InitialState::NOT_SIGNALED); | 504 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 505 base::WaitableEvent wait_for_shutdown( | 505 base::WaitableEvent wait_for_shutdown( |
| 506 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 506 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 507 base::WaitableEvent::InitialState::NOT_SIGNALED); | 507 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 529 base::WaitableEvent::InitialState::NOT_SIGNALED); | 529 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 530 base::WaitableEvent wait_done( | 530 base::WaitableEvent wait_done( |
| 531 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 531 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 532 base::WaitableEvent::InitialState::NOT_SIGNALED); | 532 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 533 io_thread()->task_runner()->PostTask( | 533 io_thread()->task_runner()->PostTask( |
| 534 FROM_HERE, base::Bind(&WaitOnIOThread, &signal_wait_start, &wait_done)); | 534 FROM_HERE, base::Bind(&WaitOnIOThread, &signal_wait_start, &wait_done)); |
| 535 signal_wait_start.Wait(); | 535 signal_wait_start.Wait(); |
| 536 // |baseline_context_getter| should have only one reference from local | 536 // |baseline_context_getter| should have only one reference from local |
| 537 // variable. | 537 // variable. |
| 538 EXPECT_TRUE(baseline_context_getter->HasOneRef()); | 538 EXPECT_TRUE(baseline_context_getter->HasOneRef()); |
| 539 baseline_context_getter = NULL; | 539 baseline_context_getter = nullptr; |
| 540 | 540 |
| 541 // Unblock and stop IO thread before sync thread. | 541 // Unblock and stop IO thread before sync thread. |
| 542 wait_done.Signal(); | 542 wait_done.Signal(); |
| 543 io_thread()->Stop(); | 543 io_thread()->Stop(); |
| 544 | 544 |
| 545 // Unblock and stop sync thread. | 545 // Unblock and stop sync thread. |
| 546 wait_for_shutdown.Signal(); | 546 wait_for_shutdown.Signal(); |
| 547 sync_thread.Stop(); | 547 sync_thread.Stop(); |
| 548 } | 548 } |
| 549 | 549 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 569 | 569 |
| 570 // Sync thread: Finally run the posted task, only to find that our | 570 // Sync thread: Finally run the posted task, only to find that our |
| 571 // HttpBridgeFactory has been neutered. Should not crash. | 571 // HttpBridgeFactory has been neutered. Should not crash. |
| 572 factory->Init("TestUserAgent", BindToTrackerCallback()); | 572 factory->Init("TestUserAgent", BindToTrackerCallback()); |
| 573 | 573 |
| 574 // At this point, attempting to use the factory would trigger a crash. Both | 574 // At this point, attempting to use the factory would trigger a crash. Both |
| 575 // this test and the real world code should make sure this never happens. | 575 // this test and the real world code should make sure this never happens. |
| 576 } | 576 } |
| 577 | 577 |
| 578 } // namespace syncer | 578 } // namespace syncer |
| OLD | NEW |