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/message_loop_proxy.h" | 5 #include "base/message_loop_proxy.h" |
6 #include "base/synchronization/waitable_event.h" | 6 #include "base/synchronization/waitable_event.h" |
7 #include "base/threading/thread.h" | 7 #include "base/threading/thread.h" |
8 #include "net/test/spawned_test_server.h" | 8 #include "net/test/spawned_test_server.h" |
9 #include "net/url_request/test_url_fetcher_factory.h" | 9 #include "net/url_request/test_url_fetcher_factory.h" |
10 #include "net/url_request/url_fetcher_delegate.h" | 10 #include "net/url_request/url_fetcher_delegate.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 : test_server_(net::SpawnedTestServer::TYPE_HTTP, | 26 : test_server_(net::SpawnedTestServer::TYPE_HTTP, |
27 net::SpawnedTestServer::kLocalhost, | 27 net::SpawnedTestServer::kLocalhost, |
28 base::FilePath(kDocRoot)), | 28 base::FilePath(kDocRoot)), |
29 fake_default_request_context_getter_(NULL), | 29 fake_default_request_context_getter_(NULL), |
30 bridge_for_race_test_(NULL), | 30 bridge_for_race_test_(NULL), |
31 io_thread_("IO thread") { | 31 io_thread_("IO thread") { |
32 } | 32 } |
33 | 33 |
34 virtual void SetUp() { | 34 virtual void SetUp() { |
35 base::Thread::Options options; | 35 base::Thread::Options options; |
36 options.message_loop_type = MessageLoop::TYPE_IO; | 36 options.message_loop_type = base::MessageLoop::TYPE_IO; |
37 io_thread_.StartWithOptions(options); | 37 io_thread_.StartWithOptions(options); |
38 } | 38 } |
39 | 39 |
40 virtual void TearDown() { | 40 virtual void TearDown() { |
41 if (fake_default_request_context_getter_) { | 41 if (fake_default_request_context_getter_) { |
42 GetIOThreadLoop()->ReleaseSoon(FROM_HERE, | 42 GetIOThreadLoop()->ReleaseSoon(FROM_HERE, |
43 fake_default_request_context_getter_); | 43 fake_default_request_context_getter_); |
44 fake_default_request_context_getter_ = NULL; | 44 fake_default_request_context_getter_ = NULL; |
45 } | 45 } |
46 io_thread_.Stop(); | 46 io_thread_.Stop(); |
(...skipping 15 matching lines...) Expand all Loading... |
62 | 62 |
63 static void Abort(HttpBridge* bridge) { | 63 static void Abort(HttpBridge* bridge) { |
64 bridge->Abort(); | 64 bridge->Abort(); |
65 } | 65 } |
66 | 66 |
67 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race | 67 // Used by AbortAndReleaseBeforeFetchCompletes to test an interesting race |
68 // condition. | 68 // condition. |
69 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, | 69 void RunSyncThreadBridgeUseTest(base::WaitableEvent* signal_when_created, |
70 base::WaitableEvent* signal_when_released); | 70 base::WaitableEvent* signal_when_released); |
71 | 71 |
72 static void TestSameHttpNetworkSession(MessageLoop* main_message_loop, | 72 static void TestSameHttpNetworkSession(base::MessageLoop* main_message_loop, |
73 SyncHttpBridgeTest* test) { | 73 SyncHttpBridgeTest* test) { |
74 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); | 74 scoped_refptr<HttpBridge> http_bridge(test->BuildBridge()); |
75 EXPECT_TRUE(test->GetTestRequestContextGetter()); | 75 EXPECT_TRUE(test->GetTestRequestContextGetter()); |
76 net::HttpNetworkSession* test_session = | 76 net::HttpNetworkSession* test_session = |
77 test->GetTestRequestContextGetter()->GetURLRequestContext()-> | 77 test->GetTestRequestContextGetter()->GetURLRequestContext()-> |
78 http_transaction_factory()->GetSession(); | 78 http_transaction_factory()->GetSession(); |
79 EXPECT_EQ(test_session, | 79 EXPECT_EQ(test_session, |
80 http_bridge->GetRequestContextGetterForTest()-> | 80 http_bridge->GetRequestContextGetterForTest()-> |
81 GetURLRequestContext()-> | 81 GetURLRequestContext()-> |
82 http_transaction_factory()->GetSession()); | 82 http_transaction_factory()->GetSession()); |
83 main_message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 83 main_message_loop->PostTask(FROM_HERE, base::MessageLoop::QuitClosure()); |
84 } | 84 } |
85 | 85 |
86 MessageLoop* GetIOThreadLoop() { | 86 base::MessageLoop* GetIOThreadLoop() { return io_thread_.message_loop(); } |
87 return io_thread_.message_loop(); | |
88 } | |
89 | 87 |
90 // Note this is lazy created, so don't call this before your bridge. | 88 // Note this is lazy created, so don't call this before your bridge. |
91 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { | 89 net::TestURLRequestContextGetter* GetTestRequestContextGetter() { |
92 return fake_default_request_context_getter_; | 90 return fake_default_request_context_getter_; |
93 } | 91 } |
94 | 92 |
95 net::SpawnedTestServer test_server_; | 93 net::SpawnedTestServer test_server_; |
96 | 94 |
97 base::Thread* io_thread() { return &io_thread_; } | 95 base::Thread* io_thread() { return &io_thread_; } |
98 | 96 |
99 HttpBridge* bridge_for_race_test() { return bridge_for_race_test_; } | 97 HttpBridge* bridge_for_race_test() { return bridge_for_race_test_; } |
100 | 98 |
101 private: | 99 private: |
102 // A make-believe "default" request context, as would be returned by | 100 // A make-believe "default" request context, as would be returned by |
103 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. | 101 // Profile::GetDefaultRequestContext(). Created lazily by BuildBridge. |
104 net::TestURLRequestContextGetter* fake_default_request_context_getter_; | 102 net::TestURLRequestContextGetter* fake_default_request_context_getter_; |
105 | 103 |
106 HttpBridge* bridge_for_race_test_; | 104 HttpBridge* bridge_for_race_test_; |
107 | 105 |
108 // Separate thread for IO used by the HttpBridge. | 106 // Separate thread for IO used by the HttpBridge. |
109 base::Thread io_thread_; | 107 base::Thread io_thread_; |
110 MessageLoop loop_; | 108 base::MessageLoop loop_; |
111 }; | 109 }; |
112 | 110 |
113 // An HttpBridge that doesn't actually make network requests and just calls | 111 // An HttpBridge that doesn't actually make network requests and just calls |
114 // back with dummy response info. | 112 // back with dummy response info. |
115 // TODO(tim): Instead of inheriting here we should inject a component | 113 // TODO(tim): Instead of inheriting here we should inject a component |
116 // responsible for the MakeAsynchronousPost bit. | 114 // responsible for the MakeAsynchronousPost bit. |
117 class ShuntedHttpBridge : public HttpBridge { | 115 class ShuntedHttpBridge : public HttpBridge { |
118 public: | 116 public: |
119 // If |never_finishes| is true, the simulated request never actually | 117 // If |never_finishes| is true, the simulated request never actually |
120 // returns. | 118 // returns. |
121 ShuntedHttpBridge(net::URLRequestContextGetter* baseline_context_getter, | 119 ShuntedHttpBridge(net::URLRequestContextGetter* baseline_context_getter, |
122 SyncHttpBridgeTest* test, bool never_finishes) | 120 SyncHttpBridgeTest* test, bool never_finishes) |
123 : HttpBridge( | 121 : HttpBridge( |
124 new HttpBridge::RequestContextGetter( | 122 new HttpBridge::RequestContextGetter( |
125 baseline_context_getter, "user agent"), | 123 baseline_context_getter, "user agent"), |
126 NetworkTimeUpdateCallback()), | 124 NetworkTimeUpdateCallback()), |
127 test_(test), never_finishes_(never_finishes) { } | 125 test_(test), never_finishes_(never_finishes) { } |
128 protected: | 126 protected: |
129 virtual void MakeAsynchronousPost() OVERRIDE { | 127 virtual void MakeAsynchronousPost() OVERRIDE { |
130 ASSERT_TRUE(MessageLoop::current() == test_->GetIOThreadLoop()); | 128 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); |
131 if (never_finishes_) | 129 if (never_finishes_) |
132 return; | 130 return; |
133 | 131 |
134 // We don't actually want to make a request for this test, so just callback | 132 // We don't actually want to make a request for this test, so just callback |
135 // as if it completed. | 133 // as if it completed. |
136 test_->GetIOThreadLoop()->PostTask(FROM_HERE, | 134 test_->GetIOThreadLoop()->PostTask(FROM_HERE, |
137 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); | 135 base::Bind(&ShuntedHttpBridge::CallOnURLFetchComplete, this)); |
138 } | 136 } |
139 private: | 137 private: |
140 virtual ~ShuntedHttpBridge() {} | 138 virtual ~ShuntedHttpBridge() {} |
141 | 139 |
142 void CallOnURLFetchComplete() { | 140 void CallOnURLFetchComplete() { |
143 ASSERT_TRUE(MessageLoop::current() == test_->GetIOThreadLoop()); | 141 ASSERT_TRUE(base::MessageLoop::current() == test_->GetIOThreadLoop()); |
144 // We return no cookies and a dummy content response. | 142 // We return no cookies and a dummy content response. |
145 net::ResponseCookies cookies; | 143 net::ResponseCookies cookies; |
146 | 144 |
147 std::string response_content = "success!"; | 145 std::string response_content = "success!"; |
148 net::TestURLFetcher fetcher(0, GURL(), NULL); | 146 net::TestURLFetcher fetcher(0, GURL(), NULL); |
149 fetcher.set_url(GURL("www.google.com")); | 147 fetcher.set_url(GURL("www.google.com")); |
150 fetcher.set_response_code(200); | 148 fetcher.set_response_code(200); |
151 fetcher.set_cookies(cookies); | 149 fetcher.set_cookies(cookies); |
152 fetcher.SetResponseString(response_content); | 150 fetcher.SetResponseString(response_content); |
153 OnURLFetchComplete(&fetcher); | 151 OnURLFetchComplete(&fetcher); |
(...skipping 19 matching lines...) Expand all Loading... |
173 int response_code = 0; | 171 int response_code = 0; |
174 bridge->MakeSynchronousPost(&os_error, &response_code); | 172 bridge->MakeSynchronousPost(&os_error, &response_code); |
175 bridge_for_race_test_ = NULL; | 173 bridge_for_race_test_ = NULL; |
176 } | 174 } |
177 signal_when_released->Signal(); | 175 signal_when_released->Signal(); |
178 } | 176 } |
179 | 177 |
180 TEST_F(SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { | 178 TEST_F(SyncHttpBridgeTest, TestUsesSameHttpNetworkSession) { |
181 // Run this test on the IO thread because we can only call | 179 // Run this test on the IO thread because we can only call |
182 // URLRequestContextGetter::GetURLRequestContext on the IO thread. | 180 // URLRequestContextGetter::GetURLRequestContext on the IO thread. |
183 io_thread()->message_loop()->PostTask( | 181 io_thread()->message_loop() |
184 FROM_HERE, | 182 ->PostTask(FROM_HERE, |
185 base::Bind(&SyncHttpBridgeTest::TestSameHttpNetworkSession, | 183 base::Bind(&SyncHttpBridgeTest::TestSameHttpNetworkSession, |
186 MessageLoop::current(), this)); | 184 base::MessageLoop::current(), |
187 MessageLoop::current()->Run(); | 185 this)); |
| 186 base::MessageLoop::current()->Run(); |
188 } | 187 } |
189 | 188 |
190 // Test the HttpBridge without actually making any network requests. | 189 // Test the HttpBridge without actually making any network requests. |
191 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { | 190 TEST_F(SyncHttpBridgeTest, TestMakeSynchronousPostShunted) { |
192 scoped_refptr<net::URLRequestContextGetter> ctx_getter( | 191 scoped_refptr<net::URLRequestContextGetter> ctx_getter( |
193 new net::TestURLRequestContextGetter(io_thread()->message_loop_proxy())); | 192 new net::TestURLRequestContextGetter(io_thread()->message_loop_proxy())); |
194 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( | 193 scoped_refptr<HttpBridge> http_bridge(new ShuntedHttpBridge( |
195 ctx_getter, this, false)); | 194 ctx_getter, this, false)); |
196 http_bridge->SetURL("http://www.google.com", 9999); | 195 http_bridge->SetURL("http://www.google.com", 9999); |
197 http_bridge->SetPostPayload("text/plain", 2, " "); | 196 http_bridge->SetPostPayload("text/plain", 2, " "); |
(...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 // succeed even though we Release()d the bridge above because the call to | 399 // succeed even though we Release()d the bridge above because the call to |
401 // Abort should have held a reference. | 400 // Abort should have held a reference. |
402 io_waiter.Signal(); | 401 io_waiter.Signal(); |
403 | 402 |
404 // Done. | 403 // Done. |
405 sync_thread.Stop(); | 404 sync_thread.Stop(); |
406 io_thread()->Stop(); | 405 io_thread()->Stop(); |
407 } | 406 } |
408 | 407 |
409 } // namespace syncer | 408 } // namespace syncer |
OLD | NEW |