| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/proxy/sync_host_resolver_bridge.h" | 5 #include "net/proxy/sync_host_resolver_bridge.h" |
| 6 | 6 |
| 7 #include "base/thread.h" | 7 #include "base/thread.h" |
| 8 #include "base/waitable_event.h" | 8 #include "base/waitable_event.h" |
| 9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
| 12 #include "net/proxy/multi_threaded_proxy_resolver.h" | |
| 13 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 14 #include "net/proxy/proxy_info.h" | 13 #include "net/proxy/proxy_info.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 15 |
| 17 // TODO(eroman): This test should be moved into | |
| 18 // multi_threaded_proxy_resolver_unittest.cc. | |
| 19 | |
| 20 namespace net { | 16 namespace net { |
| 21 | 17 |
| 22 namespace { | 18 namespace { |
| 23 | 19 |
| 24 // This implementation of HostResolver allows blocking until a resolve request | 20 // This implementation of HostResolver allows blocking until a resolve request |
| 25 // has been received. The resolve requests it receives will never be completed. | 21 // has been received. The resolve requests it receives will never be completed. |
| 26 class BlockableHostResolver : public HostResolver { | 22 class BlockableHostResolver : public HostResolver { |
| 27 public: | 23 public: |
| 28 BlockableHostResolver() | 24 BlockableHostResolver() |
| 29 : event_(true, false), | 25 : event_(true, false), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 private: | 68 private: |
| 73 // Event to notify when a resolve request was received. | 69 // Event to notify when a resolve request was received. |
| 74 base::WaitableEvent event_; | 70 base::WaitableEvent event_; |
| 75 bool was_request_cancelled_; | 71 bool was_request_cancelled_; |
| 76 }; | 72 }; |
| 77 | 73 |
| 78 // This implementation of ProxyResolver simply does a synchronous resolve | 74 // This implementation of ProxyResolver simply does a synchronous resolve |
| 79 // on |host_resolver| in response to GetProxyForURL(). | 75 // on |host_resolver| in response to GetProxyForURL(). |
| 80 class SyncProxyResolver : public ProxyResolver { | 76 class SyncProxyResolver : public ProxyResolver { |
| 81 public: | 77 public: |
| 82 explicit SyncProxyResolver(SyncHostResolverBridge* host_resolver) | 78 explicit SyncProxyResolver(HostResolver* host_resolver) |
| 83 : ProxyResolver(false), host_resolver_(host_resolver) {} | 79 : ProxyResolver(false), host_resolver_(host_resolver) {} |
| 84 | 80 |
| 85 virtual int GetProxyForURL(const GURL& url, | 81 virtual int GetProxyForURL(const GURL& url, |
| 86 ProxyInfo* results, | 82 ProxyInfo* results, |
| 87 CompletionCallback* callback, | 83 CompletionCallback* callback, |
| 88 RequestHandle* request, | 84 RequestHandle* request, |
| 89 const BoundNetLog& net_log) { | 85 const BoundNetLog& net_log) { |
| 90 EXPECT_FALSE(callback); | 86 EXPECT_FALSE(callback); |
| 91 EXPECT_FALSE(request); | 87 EXPECT_FALSE(request); |
| 92 | 88 |
| 93 // Do a synchronous host resolve. | 89 // Do a synchronous host resolve. |
| 94 HostResolver::RequestInfo info(url.host(), 80); | 90 HostResolver::RequestInfo info(url.host(), 80); |
| 95 AddressList addresses; | 91 AddressList addresses; |
| 96 int rv = | 92 int rv = |
| 97 host_resolver_->Resolve(info, &addresses, NULL, NULL, BoundNetLog()); | 93 host_resolver_->Resolve(info, &addresses, NULL, NULL, BoundNetLog()); |
| 98 | 94 |
| 99 EXPECT_EQ(ERR_ABORTED, rv); | 95 EXPECT_EQ(ERR_ABORTED, rv); |
| 100 | 96 |
| 101 return rv; | 97 return rv; |
| 102 } | 98 } |
| 103 | 99 |
| 104 virtual void CancelRequest(RequestHandle request) { | 100 virtual void CancelRequest(RequestHandle request) { |
| 105 NOTREACHED(); | 101 NOTREACHED(); |
| 106 } | 102 } |
| 107 | 103 |
| 108 virtual void Shutdown() { | |
| 109 host_resolver_->Shutdown(); | |
| 110 } | |
| 111 | |
| 112 private: | 104 private: |
| 113 virtual int SetPacScript(const GURL& pac_url, | 105 virtual int SetPacScript(const GURL& pac_url, |
| 114 const string16& pac_script, | 106 const string16& pac_script, |
| 115 CompletionCallback* callback) { | 107 CompletionCallback* callback) { |
| 108 NOTREACHED(); |
| 116 return OK; | 109 return OK; |
| 117 } | 110 } |
| 118 | 111 |
| 119 scoped_refptr<SyncHostResolverBridge> host_resolver_; | 112 scoped_refptr<HostResolver> host_resolver_; |
| 120 }; | |
| 121 | |
| 122 class SyncProxyResolverFactory : public ProxyResolverFactory { | |
| 123 public: | |
| 124 explicit SyncProxyResolverFactory(SyncHostResolverBridge* sync_host_resolver) | |
| 125 : ProxyResolverFactory(false), | |
| 126 sync_host_resolver_(sync_host_resolver) { | |
| 127 } | |
| 128 | |
| 129 virtual ProxyResolver* CreateProxyResolver() { | |
| 130 return new SyncProxyResolver(sync_host_resolver_); | |
| 131 } | |
| 132 | |
| 133 private: | |
| 134 scoped_refptr<SyncHostResolverBridge> sync_host_resolver_; | |
| 135 }; | 113 }; |
| 136 | 114 |
| 137 // This helper thread is used to create the circumstances for the deadlock. | 115 // This helper thread is used to create the circumstances for the deadlock. |
| 138 // It is analagous to the "IO thread" which would be main thread running the | 116 // It is analagous to the "IO thread" which would be main thread running the |
| 139 // network stack. | 117 // network stack. |
| 140 class IOThread : public base::Thread { | 118 class IOThread : public base::Thread { |
| 141 public: | 119 public: |
| 142 IOThread() : base::Thread("IO-thread") {} | 120 IOThread() : base::Thread("IO-thread") {} |
| 143 | 121 |
| 144 virtual ~IOThread() { | 122 virtual ~IOThread() { |
| 145 Stop(); | 123 Stop(); |
| 146 } | 124 } |
| 147 | 125 |
| 148 const scoped_refptr<BlockableHostResolver>& async_resolver() { | 126 const scoped_refptr<BlockableHostResolver>& async_resolver() { |
| 149 return async_resolver_; | 127 return async_resolver_; |
| 150 } | 128 } |
| 151 | 129 |
| 152 protected: | 130 protected: |
| 153 virtual void Init() { | 131 virtual void Init() { |
| 154 async_resolver_ = new BlockableHostResolver(); | 132 async_resolver_ = new BlockableHostResolver(); |
| 155 | 133 |
| 156 // Create a synchronous host resolver that operates the async host | 134 // Create a synchronous host resolver that operates the async host |
| 157 // resolver on THIS thread. | 135 // resolver on THIS thread. |
| 158 scoped_refptr<SyncHostResolverBridge> sync_resolver = | 136 scoped_refptr<SyncHostResolverBridge> sync_resolver = |
| 159 new SyncHostResolverBridge(async_resolver_, message_loop()); | 137 new SyncHostResolverBridge(async_resolver_, message_loop()); |
| 160 | 138 |
| 161 proxy_resolver_.reset( | 139 proxy_resolver_.reset( |
| 162 new MultiThreadedProxyResolver( | 140 new SingleThreadedProxyResolverUsingBridgedHostResolver( |
| 163 new SyncProxyResolverFactory(sync_resolver), | 141 new SyncProxyResolver(sync_resolver), |
| 164 1u)); | 142 sync_resolver)); |
| 165 | |
| 166 // Initialize the resolver. | |
| 167 TestCompletionCallback callback; | |
| 168 proxy_resolver_->SetPacScriptByUrl(GURL(), &callback); | |
| 169 EXPECT_EQ(OK, callback.WaitForResult()); | |
| 170 | 143 |
| 171 // Start an asynchronous request to the proxy resolver | 144 // Start an asynchronous request to the proxy resolver |
| 172 // (note that it will never complete). | 145 // (note that it will never complete). |
| 173 proxy_resolver_->GetProxyForURL(GURL("http://test/"), &results_, | 146 proxy_resolver_->GetProxyForURL(GURL("http://test/"), &results_, |
| 174 &callback_, &request_, BoundNetLog()); | 147 &callback_, &request_, BoundNetLog()); |
| 175 } | 148 } |
| 176 | 149 |
| 177 virtual void CleanUp() { | 150 virtual void CleanUp() { |
| 178 // Cancel the outstanding request (note however that this will not | 151 // Cancel the outstanding request (note however that this will not |
| 179 // unblock the PAC thread though). | 152 // unblock the PAC thread though). |
| (...skipping 18 matching lines...) Expand all Loading... |
| 198 | 171 |
| 199 // Data for the outstanding request to the single threaded proxy resolver. | 172 // Data for the outstanding request to the single threaded proxy resolver. |
| 200 TestCompletionCallback callback_; | 173 TestCompletionCallback callback_; |
| 201 ProxyInfo results_; | 174 ProxyInfo results_; |
| 202 ProxyResolver::RequestHandle request_; | 175 ProxyResolver::RequestHandle request_; |
| 203 }; | 176 }; |
| 204 | 177 |
| 205 // Test that a deadlock does not happen during shutdown when a host resolve | 178 // Test that a deadlock does not happen during shutdown when a host resolve |
| 206 // is outstanding on the SyncHostResolverBridge. | 179 // is outstanding on the SyncHostResolverBridge. |
| 207 // This is a regression test for http://crbug.com/41244. | 180 // This is a regression test for http://crbug.com/41244. |
| 208 TEST(MultiThreadedProxyResolverTest, ShutdownIsCalledBeforeThreadJoin) { | 181 TEST(SingleThreadedProxyResolverWithBridgedHostResolverTest, ShutdownDeadlock) { |
| 209 IOThread io_thread; | 182 IOThread io_thread; |
| 210 base::Thread::Options options; | 183 base::Thread::Options options; |
| 211 options.message_loop_type = MessageLoop::TYPE_IO; | 184 options.message_loop_type = MessageLoop::TYPE_IO; |
| 212 ASSERT_TRUE(io_thread.StartWithOptions(options)); | 185 ASSERT_TRUE(io_thread.StartWithOptions(options)); |
| 213 | 186 |
| 214 io_thread.async_resolver()->WaitUntilRequestIsReceived(); | 187 io_thread.async_resolver()->WaitUntilRequestIsReceived(); |
| 215 | 188 |
| 216 // Now upon exitting this scope, the IOThread is destroyed -- this will | 189 // Now upon exitting this scope, the IOThread is destroyed -- this will |
| 217 // stop the IOThread, which will in turn delete the | 190 // stop the IOThread, which will in turn delete the |
| 218 // SingleThreadedProxyResolver, which in turn will stop its internal | 191 // SingleThreadedProxyResolver, which in turn will stop its internal |
| 219 // PAC thread (which is currently blocked waiting on the host resolve which | 192 // PAC thread (which is currently blocked waiting on the host resolve which |
| 220 // is running on IOThread). The IOThread::Cleanup() will verify that after | 193 // is running on IOThread). The IOThread::Cleanup() will verify that after |
| 221 // the PAC thread is stopped, it cancels the request on the HostResolver. | 194 // the PAC thread is stopped, it cancels the request on the HostResolver. |
| 222 } | 195 } |
| 223 | 196 |
| 224 } // namespace | 197 } // namespace |
| 225 | 198 |
| 226 } // namespace net | 199 } // namespace net |
| OLD | NEW |