OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/spdy/spdy_session.h" | 5 #include "net/spdy/spdy_session.h" |
6 | 6 |
7 #include "net/base/ip_endpoint.h" | 7 #include "net/base/ip_endpoint.h" |
8 #include "net/spdy/spdy_io_buffer.h" | 8 #include "net/spdy/spdy_io_buffer.h" |
9 #include "net/spdy/spdy_session_pool.h" | 9 #include "net/spdy/spdy_session_pool.h" |
10 #include "net/spdy/spdy_stream.h" | 10 #include "net/spdy/spdy_stream.h" |
11 #include "net/spdy/spdy_test_util.h" | 11 #include "net/spdy/spdy_test_util.h" |
12 #include "testing/platform_test.h" | 12 #include "testing/platform_test.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 // TODO(cbentzel): Expose compression setter/getter in public SpdySession | 16 // TODO(cbentzel): Expose compression setter/getter in public SpdySession |
17 // interface rather than going through all these contortions. | 17 // interface rather than going through all these contortions. |
18 class SpdySessionTest : public PlatformTest { | 18 class SpdySessionTest : public PlatformTest { |
19 public: | 19 public: |
20 static void TurnOffCompression() { | 20 static void TurnOffCompression() { |
21 spdy::SpdyFramer::set_enable_compression_default(false); | 21 spdy::SpdyFramer::set_enable_compression_default(false); |
22 } | 22 } |
23 }; | 23 }; |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
| 27 class TestSpdyStreamDelegate : public net::SpdyStream::Delegate { |
| 28 public: |
| 29 TestSpdyStreamDelegate(OldCompletionCallback* callback) |
| 30 : callback_(callback) {} |
| 31 virtual ~TestSpdyStreamDelegate() {} |
| 32 |
| 33 virtual bool OnSendHeadersComplete(int status) { return true; } |
| 34 |
| 35 virtual int OnSendBody() { |
| 36 return ERR_UNEXPECTED; |
| 37 } |
| 38 |
| 39 virtual int OnSendBodyComplete(int /*status*/, bool* /*eof*/) { |
| 40 return ERR_UNEXPECTED; |
| 41 } |
| 42 |
| 43 virtual int OnResponseReceived(const spdy::SpdyHeaderBlock& response, |
| 44 base::Time response_time, |
| 45 int status) { |
| 46 return status; |
| 47 } |
| 48 |
| 49 virtual void OnDataReceived(const char* buffer, int bytes) { |
| 50 } |
| 51 |
| 52 virtual void OnDataSent(int length) { |
| 53 } |
| 54 |
| 55 virtual void OnClose(int status) { |
| 56 OldCompletionCallback* callback = callback_; |
| 57 callback_ = NULL; |
| 58 callback->Run(OK); |
| 59 } |
| 60 |
| 61 virtual void set_chunk_callback(net::ChunkCallback *) {} |
| 62 |
| 63 private: |
| 64 OldCompletionCallback* callback_; |
| 65 }; |
| 66 |
| 67 |
27 // Test the SpdyIOBuffer class. | 68 // Test the SpdyIOBuffer class. |
28 TEST_F(SpdySessionTest, SpdyIOBuffer) { | 69 TEST_F(SpdySessionTest, SpdyIOBuffer) { |
29 std::priority_queue<SpdyIOBuffer> queue_; | 70 std::priority_queue<SpdyIOBuffer> queue_; |
30 const size_t kQueueSize = 100; | 71 const size_t kQueueSize = 100; |
31 | 72 |
32 // Insert 100 items; pri 100 to 1. | 73 // Insert 100 items; pri 100 to 1. |
33 for (size_t index = 0; index < kQueueSize; ++index) { | 74 for (size_t index = 0; index < kQueueSize; ++index) { |
34 SpdyIOBuffer buffer(new IOBuffer(), 0, kQueueSize - index, NULL); | 75 SpdyIOBuffer buffer(new IOBuffer(), 0, kQueueSize - index, NULL); |
35 queue_.push(buffer); | 76 queue_.push(buffer); |
36 } | 77 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 spdy_session_pool->Get(pair, BoundNetLog()); | 156 spdy_session_pool->Get(pair, BoundNetLog()); |
116 | 157 |
117 // Delete the first session. | 158 // Delete the first session. |
118 session = NULL; | 159 session = NULL; |
119 | 160 |
120 // Delete the second session. | 161 // Delete the second session. |
121 spdy_session_pool->Remove(session2); | 162 spdy_session_pool->Remove(session2); |
122 session2 = NULL; | 163 session2 = NULL; |
123 } | 164 } |
124 | 165 |
| 166 TEST_F(SpdySessionTest, Ping) { |
| 167 SpdySessionDependencies session_deps; |
| 168 session_deps.host_resolver->set_synchronous_mode(true); |
| 169 |
| 170 MockConnect connect_data(false, OK); |
| 171 scoped_ptr<spdy::SpdyFrame> read_ping(ConstructSpdyPing()); |
| 172 MockRead reads[] = { |
| 173 CreateMockRead(*read_ping), |
| 174 CreateMockRead(*read_ping), |
| 175 MockRead(false, 0, 0) // EOF |
| 176 }; |
| 177 scoped_ptr<spdy::SpdyFrame> write_ping(ConstructSpdyPing()); |
| 178 MockRead writes[] = { |
| 179 CreateMockRead(*write_ping), |
| 180 CreateMockRead(*write_ping), |
| 181 }; |
| 182 StaticSocketDataProvider data( |
| 183 reads, arraysize(reads), writes, arraysize(writes)); |
| 184 data.set_connect_data(connect_data); |
| 185 session_deps.socket_factory->AddSocketDataProvider(&data); |
| 186 |
| 187 SSLSocketDataProvider ssl(false, OK); |
| 188 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl); |
| 189 |
| 190 scoped_refptr<HttpNetworkSession> http_session( |
| 191 SpdySessionDependencies::SpdyCreateSession(&session_deps)); |
| 192 |
| 193 const char* kStreamUrl = "http://www.google.com/"; |
| 194 GURL url(kStreamUrl); |
| 195 |
| 196 const std::string kTestHost("www.google.com"); |
| 197 const int kTestPort = 80; |
| 198 HostPortPair test_host_port_pair(kTestHost, kTestPort); |
| 199 HostPortProxyPair pair(test_host_port_pair, ProxyServer::Direct()); |
| 200 |
| 201 SpdySessionPool* spdy_session_pool(http_session->spdy_session_pool()); |
| 202 EXPECT_FALSE(spdy_session_pool->HasSession(pair)); |
| 203 scoped_refptr<SpdySession> session = |
| 204 spdy_session_pool->Get(pair, BoundNetLog()); |
| 205 EXPECT_TRUE(spdy_session_pool->HasSession(pair)); |
| 206 |
| 207 |
| 208 scoped_refptr<TransportSocketParams> transport_params( |
| 209 new TransportSocketParams(test_host_port_pair, |
| 210 MEDIUM, |
| 211 GURL(), |
| 212 false, |
| 213 false)); |
| 214 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 215 EXPECT_EQ(OK, |
| 216 connection->Init(test_host_port_pair.ToString(), |
| 217 transport_params, |
| 218 MEDIUM, |
| 219 NULL, |
| 220 http_session->transport_socket_pool(), |
| 221 BoundNetLog())); |
| 222 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), false, OK)); |
| 223 |
| 224 scoped_refptr<SpdyStream> spdy_stream1; |
| 225 TestOldCompletionCallback callback1; |
| 226 EXPECT_EQ(OK, session->CreateStream(url, |
| 227 MEDIUM, |
| 228 &spdy_stream1, |
| 229 BoundNetLog(), |
| 230 &callback1)); |
| 231 scoped_ptr<TestSpdyStreamDelegate> delegate( |
| 232 new TestSpdyStreamDelegate(&callback1)); |
| 233 spdy_stream1->SetDelegate(delegate.get()); |
| 234 |
| 235 base::TimeTicks before_ping_time = base::TimeTicks::Now(); |
| 236 |
| 237 // Enable sending of PING. |
| 238 SpdySession::set_send_ping_for_every_request(true); |
| 239 SpdySession::set_post_ping_delay_time_ms(0); |
| 240 SpdySession::set_check_status_delay_time_ms(50); |
| 241 SpdySession::set_hung_interval_ms(50); |
| 242 |
| 243 session->SendPing(); |
| 244 |
| 245 EXPECT_EQ(OK, callback1.WaitForResult()); |
| 246 |
| 247 EXPECT_EQ(0, session->pings_in_flight()); |
| 248 EXPECT_GT(session->unique_id_counter(), static_cast<uint32>(1)); |
| 249 EXPECT_FALSE(session->post_ping_pending()); |
| 250 // TODO(rtenneti): check_status_pending works in debug mode with breakpoints, |
| 251 // but fails in stand alone mode. |
| 252 // EXPECT_FALSE(session->check_status_pending()); |
| 253 EXPECT_GE(session->received_data_time(), before_ping_time); |
| 254 |
| 255 EXPECT_FALSE(spdy_session_pool->HasSession(pair)); |
| 256 |
| 257 // Disable sending of PING. |
| 258 SpdySession::set_send_ping_for_every_request(false); |
| 259 |
| 260 // Delete the first session. |
| 261 session = NULL; |
| 262 } |
| 263 |
125 class StreamReleaserCallback : public CallbackRunner<Tuple1<int> > { | 264 class StreamReleaserCallback : public CallbackRunner<Tuple1<int> > { |
126 public: | 265 public: |
127 StreamReleaserCallback(SpdySession* session, | 266 StreamReleaserCallback(SpdySession* session, |
128 SpdyStream* first_stream) | 267 SpdyStream* first_stream) |
129 : session_(session), first_stream_(first_stream) {} | 268 : session_(session), first_stream_(first_stream) {} |
130 ~StreamReleaserCallback() {} | 269 ~StreamReleaserCallback() {} |
131 | 270 |
132 int WaitForResult() { return callback_.WaitForResult(); } | 271 int WaitForResult() { return callback_.WaitForResult(); } |
133 | 272 |
134 virtual void RunWithParams(const Tuple1<int>& params) { | 273 virtual void RunWithParams(const Tuple1<int>& params) { |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 | 818 |
680 test_settings_storage->Set(test_host_port_pair, test_settings); | 819 test_settings_storage->Set(test_host_port_pair, test_settings); |
681 EXPECT_NE(0u, test_settings_storage->Get(test_host_port_pair).size()); | 820 EXPECT_NE(0u, test_settings_storage->Get(test_host_port_pair).size()); |
682 spdy_session_pool->OnIPAddressChanged(); | 821 spdy_session_pool->OnIPAddressChanged(); |
683 EXPECT_EQ(0u, test_settings_storage->Get(test_host_port_pair).size()); | 822 EXPECT_EQ(0u, test_settings_storage->Get(test_host_port_pair).size()); |
684 } | 823 } |
685 | 824 |
686 } // namespace | 825 } // namespace |
687 | 826 |
688 } // namespace net | 827 } // namespace net |
OLD | NEW |