OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "net/socket_stream/socket_stream.h" | 5 #include "net/socket_stream/socket_stream.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
174 base::Callback<void(SocketStreamEvent*)> on_close_; | 174 base::Callback<void(SocketStreamEvent*)> on_close_; |
175 base::Callback<void(SocketStreamEvent*)> on_auth_required_; | 175 base::Callback<void(SocketStreamEvent*)> on_auth_required_; |
176 base::Callback<void(SocketStreamEvent*)> on_error_; | 176 base::Callback<void(SocketStreamEvent*)> on_error_; |
177 const CompletionCallback callback_; | 177 const CompletionCallback callback_; |
178 CompletionCallback connection_callback_; | 178 CompletionCallback connection_callback_; |
179 AuthCredentials credentials_; | 179 AuthCredentials credentials_; |
180 | 180 |
181 DISALLOW_COPY_AND_ASSIGN(SocketStreamEventRecorder); | 181 DISALLOW_COPY_AND_ASSIGN(SocketStreamEventRecorder); |
182 }; | 182 }; |
183 | 183 |
184 // This is used for the test OnErrorDetachDelegate. | |
185 class SelfDeletingDelegate : public SocketStream::Delegate { | |
186 public: | |
187 explicit SelfDeletingDelegate(scoped_ptr<SelfDeletingDelegate>* self, | |
188 const CompletionCallback& callback) | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
two arg constructor. it's ok but you don't to spec
Adam Rice
2013/05/27 07:16:33
I changed it to a one arg constructor.
| |
189 : self_(self), socket_stream_(), callback_(callback) {} | |
190 | |
191 virtual ~SelfDeletingDelegate() {} | |
192 | |
193 // Call DetachDelegate() and then delete |this|. The callback is called first, | |
194 // to provide a way for the test to complete (the test won't complete | |
195 // synchronously, it will return to the message loop first). | |
196 virtual void OnError(const SocketStream* socket, int error) OVERRIDE { | |
197 callback_.Run(error); | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
let's copy callback_ and run at the end of this me
Adam Rice
2013/05/27 07:16:33
Done.
| |
198 socket_stream_->DetachDelegate(); | |
199 CHECK_EQ(self_->get(), this); | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
this should go to the constructor?
basically we d
Adam Rice
2013/05/27 07:16:33
I used a CHECK here because I was verifying that t
tyoshino (SeeGerritForStatus)
2013/05/27 08:09:13
Great!
| |
200 self_->reset(); | |
201 } | |
202 | |
203 // This can't be passed in the constructor because this object needs to be | |
204 // created before SocketStream. | |
205 void set_socket_stream(const scoped_refptr<SocketStream>& socket_stream) { | |
206 socket_stream_ = socket_stream; | |
207 CHECK_EQ(socket_stream_->delegate(), this); | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
ditto
Adam Rice
2013/05/27 07:16:33
Done.
| |
208 } | |
209 | |
210 virtual void OnConnected(SocketStream* socket, int max_pending_send_allowed) | |
211 OVERRIDE { | |
212 NOTREACHED(); | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
ditto. if you can't use ADD_FAILURE, use CHECK ins
Adam Rice
2013/05/27 07:16:33
Done.
| |
213 } | |
214 virtual void OnSentData(SocketStream* socket, int amount_sent) OVERRIDE { | |
215 NOTREACHED(); | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
ditto
Adam Rice
2013/05/27 07:16:33
Done.
| |
216 } | |
217 virtual void OnReceivedData(SocketStream* socket, const char* data, int len) | |
218 OVERRIDE { | |
219 NOTREACHED(); | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
ditto
Adam Rice
2013/05/27 07:16:33
Done.
| |
220 } | |
221 virtual void OnClose(SocketStream* socket) OVERRIDE { NOTREACHED(); } | |
tyoshino (SeeGerritForStatus)
2013/05/27 05:55:36
ditto
Adam Rice
2013/05/27 07:16:33
Done.
| |
222 | |
223 private: | |
224 scoped_ptr<SelfDeletingDelegate>* const self_; | |
225 scoped_refptr<SocketStream> socket_stream_; | |
226 const CompletionCallback callback_; | |
227 | |
228 DISALLOW_COPY_AND_ASSIGN(SelfDeletingDelegate); | |
229 }; | |
230 | |
184 class TestURLRequestContextWithProxy : public TestURLRequestContext { | 231 class TestURLRequestContextWithProxy : public TestURLRequestContext { |
185 public: | 232 public: |
186 explicit TestURLRequestContextWithProxy(const std::string& proxy) | 233 explicit TestURLRequestContextWithProxy(const std::string& proxy) |
187 : TestURLRequestContext(true) { | 234 : TestURLRequestContext(true) { |
188 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); | 235 context_storage_.set_proxy_service(ProxyService::CreateFixed(proxy)); |
189 Init(); | 236 Init(); |
190 } | 237 } |
191 virtual ~TestURLRequestContextWithProxy() {} | 238 virtual ~TestURLRequestContextWithProxy() {} |
192 }; | 239 }; |
193 | 240 |
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
839 test_callback.WaitForResult(); | 886 test_callback.WaitForResult(); |
840 | 887 |
841 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); | 888 const std::vector<SocketStreamEvent>& events = delegate->GetSeenEvents(); |
842 ASSERT_EQ(2U, events.size()); | 889 ASSERT_EQ(2U, events.size()); |
843 | 890 |
844 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[0].event_type); | 891 EXPECT_EQ(SocketStreamEvent::EVENT_ERROR, events[0].event_type); |
845 EXPECT_EQ(ERR_ACCESS_DENIED, events[0].error_code); | 892 EXPECT_EQ(ERR_ACCESS_DENIED, events[0].error_code); |
846 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); | 893 EXPECT_EQ(SocketStreamEvent::EVENT_CLOSE, events[1].event_type); |
847 } | 894 } |
848 | 895 |
896 // Check that a connect failure, followed by the delegate calling DetachDelegate | |
897 // and deleting itself in the OnError callback, is handled correctly. | |
898 TEST_F(SocketStreamTest, OnErrorDetachDelegate) { | |
899 MockClientSocketFactory mock_socket_factory; | |
900 TestCompletionCallback test_callback; | |
901 | |
902 scoped_ptr<SelfDeletingDelegate> delegate; | |
903 delegate.reset(new SelfDeletingDelegate(&delegate, test_callback.callback())); | |
904 MockConnect mock_connect(ASYNC, ERR_CONNECTION_REFUSED); | |
905 StaticSocketDataProvider data; | |
906 data.set_connect_data(mock_connect); | |
907 mock_socket_factory.AddSocketDataProvider(&data); | |
908 | |
909 TestURLRequestContext context; | |
910 scoped_refptr<SocketStream> socket_stream( | |
911 new SocketStream(GURL("ws://localhost:9998/echo"), delegate.get())); | |
912 socket_stream->set_context(&context); | |
913 socket_stream->SetClientSocketFactory(&mock_socket_factory); | |
914 delegate->set_socket_stream(socket_stream); | |
915 | |
916 socket_stream->Connect(); | |
917 | |
918 test_callback.WaitForResult(); | |
919 EXPECT_TRUE(!delegate); | |
920 } | |
921 | |
849 } // namespace net | 922 } // namespace net |
OLD | NEW |