| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/bit_cast.h" | 8 #include "base/bit_cast.h" |
| 9 #include "base/run_loop.h" |
| 9 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 11 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 14 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
| 15 #include "net/test/embedded_test_server/embedded_test_server.h" | 16 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 16 #include "net/url_request/test_url_fetcher_factory.h" | 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 17 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 48 } | 49 } |
| 49 | 50 |
| 50 void SetUp() override { | 51 void SetUp() override { |
| 51 base::Thread::Options options; | 52 base::Thread::Options options; |
| 52 options.message_loop_type = base::MessageLoop::TYPE_IO; | 53 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 53 io_thread_.StartWithOptions(options); | 54 io_thread_.StartWithOptions(options); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void TearDown() override { | 57 void TearDown() override { |
| 57 if (fake_default_request_context_getter_) { | 58 if (fake_default_request_context_getter_) { |
| 58 GetIOThreadLoop()->ReleaseSoon(FROM_HERE, | 59 GetIOThreadLoop()->task_runner()->ReleaseSoon( |
| 59 fake_default_request_context_getter_); | 60 FROM_HERE, fake_default_request_context_getter_); |
| 60 fake_default_request_context_getter_ = NULL; | 61 fake_default_request_context_getter_ = NULL; |
| 61 } | 62 } |
| 62 io_thread_.Stop(); | 63 io_thread_.Stop(); |
| 63 } | 64 } |
| 64 | 65 |
| 65 HttpBridge* BuildBridge() { | 66 HttpBridge* BuildBridge() { |
| 66 if (!fake_default_request_context_getter_) { | 67 if (!fake_default_request_context_getter_) { |
| 67 fake_default_request_context_getter_ = | 68 fake_default_request_context_getter_ = |
| 68 new net::TestURLRequestContextGetter(io_thread_.task_runner()); | 69 new net::TestURLRequestContextGetter(io_thread_.task_runner()); |
| 69 fake_default_request_context_getter_->AddRef(); | 70 fake_default_request_context_getter_->AddRef(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 87 MAYBE_SyncHttpBridgeTest* test) { | 88 MAYBE_SyncHttpBridgeTest* test) { |
| 88 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); | 89 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); |
| 89 EXPECT_TRUE(test->GetTestRequestContextGetter()); | 90 EXPECT_TRUE(test->GetTestRequestContextGetter()); |
| 90 net::HttpNetworkSession* test_session = | 91 net::HttpNetworkSession* test_session = |
| 91 test->GetTestRequestContextGetter()->GetURLRequestContext()-> | 92 test->GetTestRequestContextGetter()->GetURLRequestContext()-> |
| 92 http_transaction_factory()->GetSession(); | 93 http_transaction_factory()->GetSession(); |
| 93 EXPECT_EQ(test_session, | 94 EXPECT_EQ(test_session, |
| 94 http_bridge->GetRequestContextGetterForTest()-> | 95 http_bridge->GetRequestContextGetterForTest()-> |
| 95 GetURLRequestContext()-> | 96 GetURLRequestContext()-> |
| 96 http_transaction_factory()->GetSession()); | 97 http_transaction_factory()->GetSession()); |
| 97 main_message_loop->PostTask(FROM_HERE, | 98 main_message_loop->task_runner()->PostTask( |
| 98 base::MessageLoop::QuitWhenIdleClosure()); | 99 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure()); |
| 99 } | 100 } |
| 100 | 101 |
| 101 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } | 102 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } |
| 102 | 103 |
| 103 // Note this is lazy created, so don't call this before your bridge. | 104 // Note this is lazy created, so don't call this before your bridge. |
| 104 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { | 105 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { |
| 105 return fake_default_request_context_getter_; | 106 return fake_default_request_context_getter_; |
| 106 } | 107 } |
| 107 | 108 |
| 108 net::EmbeddedTestServer test_server_; | 109 net::EmbeddedTestServer test_server_; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 144 |
| 144 protected: | 145 protected: |
| 145 void MakeAsynchronousPost() override { | 146 void MakeAsynchronousPost() override { |
| 146 ASSERT_TRUE( | 147 ASSERT_TRUE( |
| 147 test_->GetIOThreadLoop()->task_runner()->BelongsToCurrentThread()); | 148 test_->GetIOThreadLoop()->task_runner()->BelongsToCurrentThread()); |
| 148 if (never_finishes_) | 149 if (never_finishes_) |
| 149 return; | 150 return; |
| 150 | 151 |
| 151 // We don't actually want to make a request for this test, so just callback | 152 // We don't actually want to make a request for this test, so just callback |
| 152 // as if it completed. | 153 // as if it completed. |
| 153 test_->GetIOThreadLoop()->PostTask(FROM_HERE, | 154 test_->GetIOThreadLoop()->task_runner()->PostTask( |
| 155 FROM_HERE, |
| 154 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); | 156 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); |
| 155 } | 157 } |
| 156 | 158 |
| 157 private: | 159 private: |
| 158 ~ShuntedHttpBridge() override {} | 160 ~ShuntedHttpBridge() override {} |
| 159 | 161 |
| 160 void CallOnURLFetchComplete() { | 162 void CallOnURLFetchComplete() { |
| 161 ASSERT_TRUE( | 163 ASSERT_TRUE( |
| 162 test_->GetIOThreadLoop()->task_runner()->BelongsToCurrentThread()); | 164 test_->GetIOThreadLoop()->task_runner()->BelongsToCurrentThread()); |
| 163 // We return a dummy content response. | 165 // We return a dummy content response. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 191 int response_code = 0; | 193 int response_code = 0; |
| 192 bridge->MakeSynchronousPost(&os_error, &response_code); | 194 bridge->MakeSynchronousPost(&os_error, &response_code); |
| 193 bridge_for_race_test_ = NULL; | 195 bridge_for_race_test_ = NULL; |
| 194 } | 196 } |
| 195 signal_when_released->Signal(); | 197 signal_when_released->Signal(); |
| 196 } | 198 } |
| 197 | 199 |
| 198 TEST_F(MAYBE_SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { | 200 TEST_F(MAYBE_SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { |
| 199 // Run this test on the IO thread because we can only call | 201 // Run this test on the IO thread because we can only call |
| 200 // URLRequestContextGetter::GetURLRequestContext on the IO thread. | 202 // URLRequestContextGetter::GetURLRequestContext on the IO thread. |
| 201 io_thread()->message_loop()->PostTask( | 203 io_thread()->task_runner()->PostTask( |
| 202 FROM_HERE, | 204 FROM_HERE, |
| 203 base::Bind(&MAYBE_SyncHttpBridgeTest::TestSameHttpNetworkSession, | 205 base::Bind(&MAYBE_SyncHttpBridgeTest::TestSameHttpNetworkSession, |
| 204 base::MessageLoop::current(), this)); | 206 base::MessageLoop::current(), this)); |
| 205 base::MessageLoop::current()->Run(); | 207 base::RunLoop().Run(); |
| 206 } | 208 } |
| 207 | 209 |
| 208 // Test the HttpBridge without actually making any network requests. | 210 // Test the HttpBridge without actually making any network requests. |
| 209 TEST_F(MAYBE_SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { | 211 TEST_F(MAYBE_SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { |
| 210 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 212 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
| 211 new net::TestURLRequestContextGetter(io_thread()->task_runner())); | 213 new net::TestURLRequestContextGetter(io_thread()->task_runner())); |
| 212 scoped_refptr<HttpBridge> http_bridge( | 214 scoped_refptr<HttpBridge> http_bridge( |
| 213 new ShuntedHttpBridge(ctx_getter.get(), this, false)); | 215 new ShuntedHttpBridge(ctx_getter.get(), this, false)); |
| 214 http_bridge->SetURL("http://www.google.com", 9999); | 216 http_bridge->SetURL("http://www.google.com", 9999); |
| 215 http_bridge->SetPostPayload("text/plain", 2, " "); | 217 http_bridge->SetPostPayload("text/plain", 2, " "); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 base::Thread sync_thread("SyncThread"); | 409 base::Thread sync_thread("SyncThread"); |
| 408 sync_thread.Start(); | 410 sync_thread.Start(); |
| 409 | 411 |
| 410 // First, block the sync thread on the post. | 412 // First, block the sync thread on the post. |
| 411 base::WaitableEvent signal_when_created( | 413 base::WaitableEvent signal_when_created( |
| 412 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 414 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 413 base::WaitableEvent::InitialState::NOT_SIGNALED); | 415 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 414 base::WaitableEvent signal_when_released( | 416 base::WaitableEvent signal_when_released( |
| 415 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 417 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 416 base::WaitableEvent::InitialState::NOT_SIGNALED); | 418 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 417 sync_thread.message_loop()->PostTask(FROM_HERE, | 419 sync_thread.task_runner()->PostTask( |
| 420 FROM_HERE, |
| 418 base::Bind(&MAYBE_SyncHttpBridgeTest::RunSyncThreadBridgeUseTest, | 421 base::Bind(&MAYBE_SyncHttpBridgeTest::RunSyncThreadBridgeUseTest, |
| 419 base::Unretained(this), | 422 base::Unretained(this), &signal_when_created, |
| 420 &signal_when_created, | |
| 421 &signal_when_released)); | 423 &signal_when_released)); |
| 422 | 424 |
| 423 // Stop IO so we can control order of operations. | 425 // Stop IO so we can control order of operations. |
| 424 base::WaitableEvent io_waiter( | 426 base::WaitableEvent io_waiter( |
| 425 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 427 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 426 base::WaitableEvent::InitialState::NOT_SIGNALED); | 428 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 427 ASSERT_TRUE(io_thread()->task_runner()->PostTask( | 429 ASSERT_TRUE(io_thread()->task_runner()->PostTask( |
| 428 FROM_HERE, | 430 FROM_HERE, |
| 429 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&io_waiter)))); | 431 base::Bind(&base::WaitableEvent::Wait, base::Unretained(&io_waiter)))); |
| 430 | 432 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 507 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 509 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 508 base::WaitableEvent::InitialState::NOT_SIGNALED); | 510 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 509 base::WaitableEvent wait_for_shutdown( | 511 base::WaitableEvent wait_for_shutdown( |
| 510 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 512 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 511 base::WaitableEvent::InitialState::NOT_SIGNALED); | 513 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 512 | 514 |
| 513 CancelationSignal release_request_context_signal; | 515 CancelationSignal release_request_context_signal; |
| 514 | 516 |
| 515 // Create bridge factory and factory on sync thread and wait for the creation | 517 // Create bridge factory and factory on sync thread and wait for the creation |
| 516 // to finish. | 518 // to finish. |
| 517 sync_thread.message_loop()->PostTask( | 519 sync_thread.task_runner()->PostTask( |
| 518 FROM_HERE, base::Bind(&HttpBridgeRunOnSyncThread, | 520 FROM_HERE, base::Bind(&HttpBridgeRunOnSyncThread, |
| 519 base::Unretained(baseline_context_getter.get()), | 521 base::Unretained(baseline_context_getter.get()), |
| 520 &release_request_context_signal, &factory, &bridge, | 522 &release_request_context_signal, &factory, &bridge, |
| 521 &signal_when_created, &wait_for_shutdown)); | 523 &signal_when_created, &wait_for_shutdown)); |
| 522 signal_when_created.Wait(); | 524 signal_when_created.Wait(); |
| 523 | 525 |
| 524 // Simulate sync shutdown by aborting bridge and shutting down factory on | 526 // Simulate sync shutdown by aborting bridge and shutting down factory on |
| 525 // frontend. | 527 // frontend. |
| 526 bridge->Abort(); | 528 bridge->Abort(); |
| 527 release_request_context_signal.Signal(); | 529 release_request_context_signal.Signal(); |
| 528 | 530 |
| 529 // Wait for sync's RequestContextGetter to be cleared on IO thread and | 531 // Wait for sync's RequestContextGetter to be cleared on IO thread and |
| 530 // check for reference count. | 532 // check for reference count. |
| 531 base::WaitableEvent signal_wait_start( | 533 base::WaitableEvent signal_wait_start( |
| 532 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 534 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 533 base::WaitableEvent::InitialState::NOT_SIGNALED); | 535 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 534 base::WaitableEvent wait_done( | 536 base::WaitableEvent wait_done( |
| 535 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 537 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 536 base::WaitableEvent::InitialState::NOT_SIGNALED); | 538 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 537 io_thread()->message_loop()->PostTask( | 539 io_thread()->task_runner()->PostTask( |
| 538 FROM_HERE, | 540 FROM_HERE, base::Bind(&WaitOnIOThread, &signal_wait_start, &wait_done)); |
| 539 base::Bind(&WaitOnIOThread, &signal_wait_start, &wait_done)); | |
| 540 signal_wait_start.Wait(); | 541 signal_wait_start.Wait(); |
| 541 // |baseline_context_getter| should have only one reference from local | 542 // |baseline_context_getter| should have only one reference from local |
| 542 // variable. | 543 // variable. |
| 543 EXPECT_TRUE(baseline_context_getter->HasOneRef()); | 544 EXPECT_TRUE(baseline_context_getter->HasOneRef()); |
| 544 baseline_context_getter = NULL; | 545 baseline_context_getter = NULL; |
| 545 | 546 |
| 546 // Unblock and stop IO thread before sync thread. | 547 // Unblock and stop IO thread before sync thread. |
| 547 wait_done.Signal(); | 548 wait_done.Signal(); |
| 548 io_thread()->Stop(); | 549 io_thread()->Stop(); |
| 549 | 550 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 574 | 575 |
| 575 // Sync thread: Finally run the posted task, only to find that our | 576 // Sync thread: Finally run the posted task, only to find that our |
| 576 // HttpBridgeFactory has been neutered. Should not crash. | 577 // HttpBridgeFactory has been neutered. Should not crash. |
| 577 factory->Init("TestUserAgent", BindToTrackerCallback()); | 578 factory->Init("TestUserAgent", BindToTrackerCallback()); |
| 578 | 579 |
| 579 // At this point, attempting to use the factory would trigger a crash. Both | 580 // At this point, attempting to use the factory would trigger a crash. Both |
| 580 // this test and the real world code should make sure this never happens. | 581 // this test and the real world code should make sure this never happens. |
| 581 } | 582 } |
| 582 | 583 |
| 583 } // namespace syncer | 584 } // namespace syncer |
| OLD | NEW |