| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/websockets/websocket_throttle.h" | 5 #include "net/websockets/websocket_throttle.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "net/base/address_list.h" | 10 #include "net/base/address_list.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 // configuration. | 66 // configuration. |
| 67 WebSocketJob::set_websocket_over_spdy_enabled(true); | 67 WebSocketJob::set_websocket_over_spdy_enabled(true); |
| 68 | 68 |
| 69 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 69 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
| 70 AddressList addr; | 70 AddressList addr; |
| 71 addr.push_back(MakeAddr(1, 2, 3, 4)); | 71 addr.push_back(MakeAddr(1, 2, 3, 4)); |
| 72 addr.push_back(MakeAddr(1, 2, 3, 5)); | 72 addr.push_back(MakeAddr(1, 2, 3, 5)); |
| 73 addr.push_back(MakeAddr(1, 2, 3, 6)); | 73 addr.push_back(MakeAddr(1, 2, 3, 6)); |
| 74 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 74 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 75 scoped_refptr<SocketStream> s1( | 75 scoped_refptr<SocketStream> s1( |
| 76 new SocketStream(GURL("ws://host1/"), w1.get())); | 76 new SocketStream(GURL("ws://host1/"), w1.get(), &context, NULL)); |
| 77 s1->set_context(&context); | |
| 78 w1->InitSocketStream(s1.get()); | 77 w1->InitSocketStream(s1.get()); |
| 79 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); | 78 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); |
| 80 | 79 |
| 81 DVLOG(1) << "socket1"; | 80 DVLOG(1) << "socket1"; |
| 82 TestCompletionCallback callback_s1; | 81 TestCompletionCallback callback_s1; |
| 83 // Trying to open connection to host1 will start without wait. | 82 // Trying to open connection to host1 will start without wait. |
| 84 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); | 83 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
| 85 | 84 |
| 86 // Now connecting to host1, so waiting queue looks like | 85 // Now connecting to host1, so waiting queue looks like |
| 87 // Address | head -> tail | 86 // Address | head -> tail |
| 88 // 1.2.3.4 | w1 | 87 // 1.2.3.4 | w1 |
| 89 // 1.2.3.5 | w1 | 88 // 1.2.3.5 | w1 |
| 90 // 1.2.3.6 | w1 | 89 // 1.2.3.6 | w1 |
| 91 | 90 |
| 92 // For host2: 1.2.3.4 | 91 // For host2: 1.2.3.4 |
| 93 addr.clear(); | 92 addr.clear(); |
| 94 addr.push_back(MakeAddr(1, 2, 3, 4)); | 93 addr.push_back(MakeAddr(1, 2, 3, 4)); |
| 95 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); | 94 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
| 96 scoped_refptr<SocketStream> s2( | 95 scoped_refptr<SocketStream> s2( |
| 97 new SocketStream(GURL("ws://host2/"), w2.get())); | 96 new SocketStream(GURL("ws://host2/"), w2.get(), &context, NULL)); |
| 98 s2->set_context(&context); | |
| 99 w2->InitSocketStream(s2.get()); | 97 w2->InitSocketStream(s2.get()); |
| 100 WebSocketThrottleTest::MockSocketStreamConnect(s2.get(), addr); | 98 WebSocketThrottleTest::MockSocketStreamConnect(s2.get(), addr); |
| 101 | 99 |
| 102 DVLOG(1) << "socket2"; | 100 DVLOG(1) << "socket2"; |
| 103 TestCompletionCallback callback_s2; | 101 TestCompletionCallback callback_s2; |
| 104 // Trying to open connection to host2 will wait for w1. | 102 // Trying to open connection to host2 will wait for w1. |
| 105 EXPECT_EQ(ERR_IO_PENDING, | 103 EXPECT_EQ(ERR_IO_PENDING, |
| 106 w2->OnStartOpenConnection(s2.get(), callback_s2.callback())); | 104 w2->OnStartOpenConnection(s2.get(), callback_s2.callback())); |
| 107 // Now waiting queue looks like | 105 // Now waiting queue looks like |
| 108 // Address | head -> tail | 106 // Address | head -> tail |
| 109 // 1.2.3.4 | w1 w2 | 107 // 1.2.3.4 | w1 w2 |
| 110 // 1.2.3.5 | w1 | 108 // 1.2.3.5 | w1 |
| 111 // 1.2.3.6 | w1 | 109 // 1.2.3.6 | w1 |
| 112 | 110 |
| 113 // For host3: 1.2.3.5 | 111 // For host3: 1.2.3.5 |
| 114 addr.clear(); | 112 addr.clear(); |
| 115 addr.push_back(MakeAddr(1, 2, 3, 5)); | 113 addr.push_back(MakeAddr(1, 2, 3, 5)); |
| 116 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); | 114 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); |
| 117 scoped_refptr<SocketStream> s3( | 115 scoped_refptr<SocketStream> s3( |
| 118 new SocketStream(GURL("ws://host3/"), w3.get())); | 116 new SocketStream(GURL("ws://host3/"), w3.get(), &context, NULL)); |
| 119 s3->set_context(&context); | |
| 120 w3->InitSocketStream(s3.get()); | 117 w3->InitSocketStream(s3.get()); |
| 121 WebSocketThrottleTest::MockSocketStreamConnect(s3.get(), addr); | 118 WebSocketThrottleTest::MockSocketStreamConnect(s3.get(), addr); |
| 122 | 119 |
| 123 DVLOG(1) << "socket3"; | 120 DVLOG(1) << "socket3"; |
| 124 TestCompletionCallback callback_s3; | 121 TestCompletionCallback callback_s3; |
| 125 // Trying to open connection to host3 will wait for w1. | 122 // Trying to open connection to host3 will wait for w1. |
| 126 EXPECT_EQ(ERR_IO_PENDING, | 123 EXPECT_EQ(ERR_IO_PENDING, |
| 127 w3->OnStartOpenConnection(s3.get(), callback_s3.callback())); | 124 w3->OnStartOpenConnection(s3.get(), callback_s3.callback())); |
| 128 // Address | head -> tail | 125 // Address | head -> tail |
| 129 // 1.2.3.4 | w1 w2 | 126 // 1.2.3.4 | w1 w2 |
| 130 // 1.2.3.5 | w1 w3 | 127 // 1.2.3.5 | w1 w3 |
| 131 // 1.2.3.6 | w1 | 128 // 1.2.3.6 | w1 |
| 132 | 129 |
| 133 // For host4: 1.2.3.4, 1.2.3.6 | 130 // For host4: 1.2.3.4, 1.2.3.6 |
| 134 addr.clear(); | 131 addr.clear(); |
| 135 addr.push_back(MakeAddr(1, 2, 3, 4)); | 132 addr.push_back(MakeAddr(1, 2, 3, 4)); |
| 136 addr.push_back(MakeAddr(1, 2, 3, 6)); | 133 addr.push_back(MakeAddr(1, 2, 3, 6)); |
| 137 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); | 134 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); |
| 138 scoped_refptr<SocketStream> s4( | 135 scoped_refptr<SocketStream> s4( |
| 139 new SocketStream(GURL("ws://host4/"), w4.get())); | 136 new SocketStream(GURL("ws://host4/"), w4.get(), &context, NULL)); |
| 140 s4->set_context(&context); | |
| 141 w4->InitSocketStream(s4.get()); | 137 w4->InitSocketStream(s4.get()); |
| 142 WebSocketThrottleTest::MockSocketStreamConnect(s4.get(), addr); | 138 WebSocketThrottleTest::MockSocketStreamConnect(s4.get(), addr); |
| 143 | 139 |
| 144 DVLOG(1) << "socket4"; | 140 DVLOG(1) << "socket4"; |
| 145 TestCompletionCallback callback_s4; | 141 TestCompletionCallback callback_s4; |
| 146 // Trying to open connection to host4 will wait for w1, w2. | 142 // Trying to open connection to host4 will wait for w1, w2. |
| 147 EXPECT_EQ(ERR_IO_PENDING, | 143 EXPECT_EQ(ERR_IO_PENDING, |
| 148 w4->OnStartOpenConnection(s4.get(), callback_s4.callback())); | 144 w4->OnStartOpenConnection(s4.get(), callback_s4.callback())); |
| 149 // Address | head -> tail | 145 // Address | head -> tail |
| 150 // 1.2.3.4 | w1 w2 w4 | 146 // 1.2.3.4 | w1 w2 w4 |
| 151 // 1.2.3.5 | w1 w3 | 147 // 1.2.3.5 | w1 w3 |
| 152 // 1.2.3.6 | w1 w4 | 148 // 1.2.3.6 | w1 w4 |
| 153 | 149 |
| 154 // For host5: 1.2.3.6 | 150 // For host5: 1.2.3.6 |
| 155 addr.clear(); | 151 addr.clear(); |
| 156 addr.push_back(MakeAddr(1, 2, 3, 6)); | 152 addr.push_back(MakeAddr(1, 2, 3, 6)); |
| 157 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); | 153 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); |
| 158 scoped_refptr<SocketStream> s5( | 154 scoped_refptr<SocketStream> s5( |
| 159 new SocketStream(GURL("ws://host5/"), w5.get())); | 155 new SocketStream(GURL("ws://host5/"), w5.get(), &context, NULL)); |
| 160 s5->set_context(&context); | |
| 161 w5->InitSocketStream(s5.get()); | 156 w5->InitSocketStream(s5.get()); |
| 162 WebSocketThrottleTest::MockSocketStreamConnect(s5.get(), addr); | 157 WebSocketThrottleTest::MockSocketStreamConnect(s5.get(), addr); |
| 163 | 158 |
| 164 DVLOG(1) << "socket5"; | 159 DVLOG(1) << "socket5"; |
| 165 TestCompletionCallback callback_s5; | 160 TestCompletionCallback callback_s5; |
| 166 // Trying to open connection to host5 will wait for w1, w4 | 161 // Trying to open connection to host5 will wait for w1, w4 |
| 167 EXPECT_EQ(ERR_IO_PENDING, | 162 EXPECT_EQ(ERR_IO_PENDING, |
| 168 w5->OnStartOpenConnection(s5.get(), callback_s5.callback())); | 163 w5->OnStartOpenConnection(s5.get(), callback_s5.callback())); |
| 169 // Address | head -> tail | 164 // Address | head -> tail |
| 170 // 1.2.3.4 | w1 w2 w4 | 165 // 1.2.3.4 | w1 w2 w4 |
| 171 // 1.2.3.5 | w1 w3 | 166 // 1.2.3.5 | w1 w3 |
| 172 // 1.2.3.6 | w1 w4 w5 | 167 // 1.2.3.6 | w1 w4 w5 |
| 173 | 168 |
| 174 // For host6: 1.2.3.6 | 169 // For host6: 1.2.3.6 |
| 175 addr.clear(); | 170 addr.clear(); |
| 176 addr.push_back(MakeAddr(1, 2, 3, 6)); | 171 addr.push_back(MakeAddr(1, 2, 3, 6)); |
| 177 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); | 172 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); |
| 178 scoped_refptr<SocketStream> s6( | 173 scoped_refptr<SocketStream> s6( |
| 179 new SocketStream(GURL("ws://host6/"), w6.get())); | 174 new SocketStream(GURL("ws://host6/"), w6.get(), &context, NULL)); |
| 180 s6->set_context(&context); | |
| 181 w6->InitSocketStream(s6.get()); | 175 w6->InitSocketStream(s6.get()); |
| 182 WebSocketThrottleTest::MockSocketStreamConnect(s6.get(), addr); | 176 WebSocketThrottleTest::MockSocketStreamConnect(s6.get(), addr); |
| 183 | 177 |
| 184 DVLOG(1) << "socket6"; | 178 DVLOG(1) << "socket6"; |
| 185 TestCompletionCallback callback_s6; | 179 TestCompletionCallback callback_s6; |
| 186 // Trying to open connection to host6 will wait for w1, w4, w5 | 180 // Trying to open connection to host6 will wait for w1, w4, w5 |
| 187 EXPECT_EQ(ERR_IO_PENDING, | 181 EXPECT_EQ(ERR_IO_PENDING, |
| 188 w6->OnStartOpenConnection(s6.get(), callback_s6.callback())); | 182 w6->OnStartOpenConnection(s6.get(), callback_s6.callback())); |
| 189 // Address | head -> tail | 183 // Address | head -> tail |
| 190 // 1.2.3.4 | w1 w2 w4 | 184 // 1.2.3.4 | w1 w2 w4 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 TestURLRequestContext context; | 276 TestURLRequestContext context; |
| 283 DummySocketStreamDelegate delegate; | 277 DummySocketStreamDelegate delegate; |
| 284 WebSocketJob::set_websocket_over_spdy_enabled(true); | 278 WebSocketJob::set_websocket_over_spdy_enabled(true); |
| 285 | 279 |
| 286 // For localhost: 127.0.0.1, 127.0.0.1 | 280 // For localhost: 127.0.0.1, 127.0.0.1 |
| 287 AddressList addr; | 281 AddressList addr; |
| 288 addr.push_back(MakeAddr(127, 0, 0, 1)); | 282 addr.push_back(MakeAddr(127, 0, 0, 1)); |
| 289 addr.push_back(MakeAddr(127, 0, 0, 1)); | 283 addr.push_back(MakeAddr(127, 0, 0, 1)); |
| 290 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 284 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 291 scoped_refptr<SocketStream> s1( | 285 scoped_refptr<SocketStream> s1( |
| 292 new SocketStream(GURL("ws://localhost/"), w1.get())); | 286 new SocketStream(GURL("ws://localhost/"), w1.get(), &context, NULL)); |
| 293 s1->set_context(&context); | |
| 294 w1->InitSocketStream(s1.get()); | 287 w1->InitSocketStream(s1.get()); |
| 295 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); | 288 WebSocketThrottleTest::MockSocketStreamConnect(s1.get(), addr); |
| 296 | 289 |
| 297 DVLOG(1) << "socket1"; | 290 DVLOG(1) << "socket1"; |
| 298 TestCompletionCallback callback_s1; | 291 TestCompletionCallback callback_s1; |
| 299 // Trying to open connection to localhost will start without wait. | 292 // Trying to open connection to localhost will start without wait. |
| 300 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); | 293 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
| 301 | 294 |
| 302 DVLOG(1) << "socket1 close"; | 295 DVLOG(1) << "socket1 close"; |
| 303 w1->OnClose(s1.get()); | 296 w1->OnClose(s1.get()); |
| 304 s1->DetachDelegate(); | 297 s1->DetachDelegate(); |
| 305 DVLOG(1) << "Done"; | 298 DVLOG(1) << "Done"; |
| 306 base::MessageLoopForIO::current()->RunUntilIdle(); | 299 base::MessageLoopForIO::current()->RunUntilIdle(); |
| 307 } | 300 } |
| 308 | 301 |
| 309 // A connection should not be blocked by another connection to the same IP | 302 // A connection should not be blocked by another connection to the same IP |
| 310 // with a different port. | 303 // with a different port. |
| 311 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { | 304 TEST_F(WebSocketThrottleTest, NoThrottleForDistinctPort) { |
| 312 TestURLRequestContext context; | 305 TestURLRequestContext context; |
| 313 DummySocketStreamDelegate delegate; | 306 DummySocketStreamDelegate delegate; |
| 314 IPAddressNumber localhost; | 307 IPAddressNumber localhost; |
| 315 ParseIPLiteralToNumber("127.0.0.1", &localhost); | 308 ParseIPLiteralToNumber("127.0.0.1", &localhost); |
| 316 WebSocketJob::set_websocket_over_spdy_enabled(false); | 309 WebSocketJob::set_websocket_over_spdy_enabled(false); |
| 317 | 310 |
| 318 // socket1: 127.0.0.1:80 | 311 // socket1: 127.0.0.1:80 |
| 319 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 312 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 320 scoped_refptr<SocketStream> s1( | 313 scoped_refptr<SocketStream> s1( |
| 321 new SocketStream(GURL("ws://localhost:80/"), w1.get())); | 314 new SocketStream(GURL("ws://localhost:80/"), w1.get(), &context, NULL)); |
| 322 s1->set_context(&context); | |
| 323 w1->InitSocketStream(s1.get()); | 315 w1->InitSocketStream(s1.get()); |
| 324 MockSocketStreamConnect(s1.get(), | 316 MockSocketStreamConnect(s1.get(), |
| 325 AddressList::CreateFromIPAddress(localhost, 80)); | 317 AddressList::CreateFromIPAddress(localhost, 80)); |
| 326 | 318 |
| 327 DVLOG(1) << "connecting socket1"; | 319 DVLOG(1) << "connecting socket1"; |
| 328 TestCompletionCallback callback_s1; | 320 TestCompletionCallback callback_s1; |
| 329 // Trying to open connection to localhost:80 will start without waiting. | 321 // Trying to open connection to localhost:80 will start without waiting. |
| 330 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); | 322 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1.get(), callback_s1.callback())); |
| 331 | 323 |
| 332 // socket2: 127.0.0.1:81 | 324 // socket2: 127.0.0.1:81 |
| 333 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); | 325 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
| 334 scoped_refptr<SocketStream> s2( | 326 scoped_refptr<SocketStream> s2( |
| 335 new SocketStream(GURL("ws://localhost:81/"), w2.get())); | 327 new SocketStream(GURL("ws://localhost:81/"), w2.get(), &context, NULL)); |
| 336 s2->set_context(&context); | |
| 337 w2->InitSocketStream(s2.get()); | 328 w2->InitSocketStream(s2.get()); |
| 338 MockSocketStreamConnect(s2.get(), | 329 MockSocketStreamConnect(s2.get(), |
| 339 AddressList::CreateFromIPAddress(localhost, 81)); | 330 AddressList::CreateFromIPAddress(localhost, 81)); |
| 340 | 331 |
| 341 DVLOG(1) << "connecting socket2"; | 332 DVLOG(1) << "connecting socket2"; |
| 342 TestCompletionCallback callback_s2; | 333 TestCompletionCallback callback_s2; |
| 343 // Trying to open connection to localhost:81 will start without waiting. | 334 // Trying to open connection to localhost:81 will start without waiting. |
| 344 EXPECT_EQ(OK, w2->OnStartOpenConnection(s2.get(), callback_s2.callback())); | 335 EXPECT_EQ(OK, w2->OnStartOpenConnection(s2.get(), callback_s2.callback())); |
| 345 | 336 |
| 346 DVLOG(1) << "closing socket1"; | 337 DVLOG(1) << "closing socket1"; |
| 347 w1->OnClose(s1.get()); | 338 w1->OnClose(s1.get()); |
| 348 s1->DetachDelegate(); | 339 s1->DetachDelegate(); |
| 349 | 340 |
| 350 DVLOG(1) << "closing socket2"; | 341 DVLOG(1) << "closing socket2"; |
| 351 w2->OnClose(s2.get()); | 342 w2->OnClose(s2.get()); |
| 352 s2->DetachDelegate(); | 343 s2->DetachDelegate(); |
| 353 DVLOG(1) << "Done"; | 344 DVLOG(1) << "Done"; |
| 354 base::MessageLoopForIO::current()->RunUntilIdle(); | 345 base::MessageLoopForIO::current()->RunUntilIdle(); |
| 355 } | 346 } |
| 356 | 347 |
| 357 } | 348 } |
| OLD | NEW |