| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "net/base/address_list.h" | 9 #include "net/base/address_list.h" |
| 10 #include "net/base/sys_addrinfo.h" | 10 #include "net/base/sys_addrinfo.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 } | 76 } |
| 77 }; | 77 }; |
| 78 | 78 |
| 79 TEST_F(WebSocketThrottleTest, Throttle) { | 79 TEST_F(WebSocketThrottleTest, Throttle) { |
| 80 DummySocketStreamDelegate delegate; | 80 DummySocketStreamDelegate delegate; |
| 81 | 81 |
| 82 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 82 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
| 83 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); | 83 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); |
| 84 addr = AddAddr(1, 2, 3, 5, addr); | 84 addr = AddAddr(1, 2, 3, 5, addr); |
| 85 addr = AddAddr(1, 2, 3, 6, addr); | 85 addr = AddAddr(1, 2, 3, 6, addr); |
| 86 scoped_refptr<WebSocketJob> w1 = new WebSocketJob(&delegate); | 86 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 87 scoped_refptr<SocketStream> s1 = | 87 scoped_refptr<SocketStream> s1( |
| 88 new SocketStream(GURL("ws://host1/"), w1.get()); | 88 new SocketStream(GURL("ws://host1/"), w1.get())); |
| 89 w1->InitSocketStream(s1.get()); | 89 w1->InitSocketStream(s1.get()); |
| 90 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 90 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); |
| 91 DeleteAddrInfo(addr); | 91 DeleteAddrInfo(addr); |
| 92 | 92 |
| 93 DVLOG(1) << "socket1"; | 93 DVLOG(1) << "socket1"; |
| 94 TestCompletionCallback callback_s1; | 94 TestCompletionCallback callback_s1; |
| 95 // Trying to open connection to host1 will start without wait. | 95 // Trying to open connection to host1 will start without wait. |
| 96 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, &callback_s1)); | 96 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, &callback_s1)); |
| 97 | 97 |
| 98 // Now connecting to host1, so waiting queue looks like | 98 // Now connecting to host1, so waiting queue looks like |
| 99 // Address | head -> tail | 99 // Address | head -> tail |
| 100 // 1.2.3.4 | w1 | 100 // 1.2.3.4 | w1 |
| 101 // 1.2.3.5 | w1 | 101 // 1.2.3.5 | w1 |
| 102 // 1.2.3.6 | w1 | 102 // 1.2.3.6 | w1 |
| 103 | 103 |
| 104 // For host2: 1.2.3.4 | 104 // For host2: 1.2.3.4 |
| 105 addr = AddAddr(1, 2, 3, 4, NULL); | 105 addr = AddAddr(1, 2, 3, 4, NULL); |
| 106 scoped_refptr<WebSocketJob> w2 = new WebSocketJob(&delegate); | 106 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
| 107 scoped_refptr<SocketStream> s2 = | 107 scoped_refptr<SocketStream> s2( |
| 108 new SocketStream(GURL("ws://host2/"), w2.get()); | 108 new SocketStream(GURL("ws://host2/"), w2.get())); |
| 109 w2->InitSocketStream(s2.get()); | 109 w2->InitSocketStream(s2.get()); |
| 110 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); | 110 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); |
| 111 DeleteAddrInfo(addr); | 111 DeleteAddrInfo(addr); |
| 112 | 112 |
| 113 DVLOG(1) << "socket2"; | 113 DVLOG(1) << "socket2"; |
| 114 TestCompletionCallback callback_s2; | 114 TestCompletionCallback callback_s2; |
| 115 // Trying to open connection to host2 will wait for w1. | 115 // Trying to open connection to host2 will wait for w1. |
| 116 EXPECT_EQ(ERR_IO_PENDING, w2->OnStartOpenConnection(s2, &callback_s2)); | 116 EXPECT_EQ(ERR_IO_PENDING, w2->OnStartOpenConnection(s2, &callback_s2)); |
| 117 // Now waiting queue looks like | 117 // Now waiting queue looks like |
| 118 // Address | head -> tail | 118 // Address | head -> tail |
| 119 // 1.2.3.4 | w1 w2 | 119 // 1.2.3.4 | w1 w2 |
| 120 // 1.2.3.5 | w1 | 120 // 1.2.3.5 | w1 |
| 121 // 1.2.3.6 | w1 | 121 // 1.2.3.6 | w1 |
| 122 | 122 |
| 123 // For host3: 1.2.3.5 | 123 // For host3: 1.2.3.5 |
| 124 addr = AddAddr(1, 2, 3, 5, NULL); | 124 addr = AddAddr(1, 2, 3, 5, NULL); |
| 125 scoped_refptr<WebSocketJob> w3 = new WebSocketJob(&delegate); | 125 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); |
| 126 scoped_refptr<SocketStream> s3 = | 126 scoped_refptr<SocketStream> s3( |
| 127 new SocketStream(GURL("ws://host3/"), w3.get()); | 127 new SocketStream(GURL("ws://host3/"), w3.get())); |
| 128 w3->InitSocketStream(s3.get()); | 128 w3->InitSocketStream(s3.get()); |
| 129 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); | 129 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); |
| 130 DeleteAddrInfo(addr); | 130 DeleteAddrInfo(addr); |
| 131 | 131 |
| 132 DVLOG(1) << "socket3"; | 132 DVLOG(1) << "socket3"; |
| 133 TestCompletionCallback callback_s3; | 133 TestCompletionCallback callback_s3; |
| 134 // Trying to open connection to host3 will wait for w1. | 134 // Trying to open connection to host3 will wait for w1. |
| 135 EXPECT_EQ(ERR_IO_PENDING, w3->OnStartOpenConnection(s3, &callback_s3)); | 135 EXPECT_EQ(ERR_IO_PENDING, w3->OnStartOpenConnection(s3, &callback_s3)); |
| 136 // Address | head -> tail | 136 // Address | head -> tail |
| 137 // 1.2.3.4 | w1 w2 | 137 // 1.2.3.4 | w1 w2 |
| 138 // 1.2.3.5 | w1 w3 | 138 // 1.2.3.5 | w1 w3 |
| 139 // 1.2.3.6 | w1 | 139 // 1.2.3.6 | w1 |
| 140 | 140 |
| 141 // For host4: 1.2.3.4, 1.2.3.6 | 141 // For host4: 1.2.3.4, 1.2.3.6 |
| 142 addr = AddAddr(1, 2, 3, 4, NULL); | 142 addr = AddAddr(1, 2, 3, 4, NULL); |
| 143 addr = AddAddr(1, 2, 3, 6, addr); | 143 addr = AddAddr(1, 2, 3, 6, addr); |
| 144 scoped_refptr<WebSocketJob> w4 = new WebSocketJob(&delegate); | 144 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); |
| 145 scoped_refptr<SocketStream> s4 = | 145 scoped_refptr<SocketStream> s4( |
| 146 new SocketStream(GURL("ws://host4/"), w4.get()); | 146 new SocketStream(GURL("ws://host4/"), w4.get())); |
| 147 w4->InitSocketStream(s4.get()); | 147 w4->InitSocketStream(s4.get()); |
| 148 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); | 148 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); |
| 149 DeleteAddrInfo(addr); | 149 DeleteAddrInfo(addr); |
| 150 | 150 |
| 151 DVLOG(1) << "socket4"; | 151 DVLOG(1) << "socket4"; |
| 152 TestCompletionCallback callback_s4; | 152 TestCompletionCallback callback_s4; |
| 153 // Trying to open connection to host4 will wait for w1, w2. | 153 // Trying to open connection to host4 will wait for w1, w2. |
| 154 EXPECT_EQ(ERR_IO_PENDING, w4->OnStartOpenConnection(s4, &callback_s4)); | 154 EXPECT_EQ(ERR_IO_PENDING, w4->OnStartOpenConnection(s4, &callback_s4)); |
| 155 // Address | head -> tail | 155 // Address | head -> tail |
| 156 // 1.2.3.4 | w1 w2 w4 | 156 // 1.2.3.4 | w1 w2 w4 |
| 157 // 1.2.3.5 | w1 w3 | 157 // 1.2.3.5 | w1 w3 |
| 158 // 1.2.3.6 | w1 w4 | 158 // 1.2.3.6 | w1 w4 |
| 159 | 159 |
| 160 // For host5: 1.2.3.6 | 160 // For host5: 1.2.3.6 |
| 161 addr = AddAddr(1, 2, 3, 6, NULL); | 161 addr = AddAddr(1, 2, 3, 6, NULL); |
| 162 scoped_refptr<WebSocketJob> w5 = new WebSocketJob(&delegate); | 162 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); |
| 163 scoped_refptr<SocketStream> s5 = | 163 scoped_refptr<SocketStream> s5( |
| 164 new SocketStream(GURL("ws://host5/"), w5.get()); | 164 new SocketStream(GURL("ws://host5/"), w5.get())); |
| 165 w5->InitSocketStream(s5.get()); | 165 w5->InitSocketStream(s5.get()); |
| 166 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); | 166 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); |
| 167 DeleteAddrInfo(addr); | 167 DeleteAddrInfo(addr); |
| 168 | 168 |
| 169 DVLOG(1) << "socket5"; | 169 DVLOG(1) << "socket5"; |
| 170 TestCompletionCallback callback_s5; | 170 TestCompletionCallback callback_s5; |
| 171 // Trying to open connection to host5 will wait for w1, w4 | 171 // Trying to open connection to host5 will wait for w1, w4 |
| 172 EXPECT_EQ(ERR_IO_PENDING, w5->OnStartOpenConnection(s5, &callback_s5)); | 172 EXPECT_EQ(ERR_IO_PENDING, w5->OnStartOpenConnection(s5, &callback_s5)); |
| 173 // Address | head -> tail | 173 // Address | head -> tail |
| 174 // 1.2.3.4 | w1 w2 w4 | 174 // 1.2.3.4 | w1 w2 w4 |
| 175 // 1.2.3.5 | w1 w3 | 175 // 1.2.3.5 | w1 w3 |
| 176 // 1.2.3.6 | w1 w4 w5 | 176 // 1.2.3.6 | w1 w4 w5 |
| 177 | 177 |
| 178 // For host6: 1.2.3.6 | 178 // For host6: 1.2.3.6 |
| 179 addr = AddAddr(1, 2, 3, 6, NULL); | 179 addr = AddAddr(1, 2, 3, 6, NULL); |
| 180 scoped_refptr<WebSocketJob> w6 = new WebSocketJob(&delegate); | 180 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); |
| 181 scoped_refptr<SocketStream> s6 = | 181 scoped_refptr<SocketStream> s6( |
| 182 new SocketStream(GURL("ws://host6/"), w6.get()); | 182 new SocketStream(GURL("ws://host6/"), w6.get())); |
| 183 w6->InitSocketStream(s6.get()); | 183 w6->InitSocketStream(s6.get()); |
| 184 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); | 184 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); |
| 185 DeleteAddrInfo(addr); | 185 DeleteAddrInfo(addr); |
| 186 | 186 |
| 187 DVLOG(1) << "socket6"; | 187 DVLOG(1) << "socket6"; |
| 188 TestCompletionCallback callback_s6; | 188 TestCompletionCallback callback_s6; |
| 189 // Trying to open connection to host6 will wait for w1, w4, w5 | 189 // Trying to open connection to host6 will wait for w1, w4, w5 |
| 190 EXPECT_EQ(ERR_IO_PENDING, w6->OnStartOpenConnection(s6, &callback_s6)); | 190 EXPECT_EQ(ERR_IO_PENDING, w6->OnStartOpenConnection(s6, &callback_s6)); |
| 191 // Address | head -> tail | 191 // Address | head -> tail |
| 192 // 1.2.3.4 | w1 w2 w4 | 192 // 1.2.3.4 | w1 w2 w4 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 DVLOG(1) << "Done"; | 279 DVLOG(1) << "Done"; |
| 280 MessageLoopForIO::current()->RunAllPending(); | 280 MessageLoopForIO::current()->RunAllPending(); |
| 281 } | 281 } |
| 282 | 282 |
| 283 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { | 283 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { |
| 284 DummySocketStreamDelegate delegate; | 284 DummySocketStreamDelegate delegate; |
| 285 | 285 |
| 286 // For localhost: 127.0.0.1, 127.0.0.1 | 286 // For localhost: 127.0.0.1, 127.0.0.1 |
| 287 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); | 287 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); |
| 288 addr = AddAddr(127, 0, 0, 1, addr); | 288 addr = AddAddr(127, 0, 0, 1, addr); |
| 289 scoped_refptr<WebSocketJob> w1 = new WebSocketJob(&delegate); | 289 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
| 290 scoped_refptr<SocketStream> s1 = | 290 scoped_refptr<SocketStream> s1( |
| 291 new SocketStream(GURL("ws://localhost/"), w1.get()); | 291 new SocketStream(GURL("ws://localhost/"), w1.get())); |
| 292 w1->InitSocketStream(s1.get()); | 292 w1->InitSocketStream(s1.get()); |
| 293 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 293 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); |
| 294 DeleteAddrInfo(addr); | 294 DeleteAddrInfo(addr); |
| 295 | 295 |
| 296 DVLOG(1) << "socket1"; | 296 DVLOG(1) << "socket1"; |
| 297 TestCompletionCallback callback_s1; | 297 TestCompletionCallback callback_s1; |
| 298 // Trying to open connection to localhost will start without wait. | 298 // Trying to open connection to localhost will start without wait. |
| 299 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, &callback_s1)); | 299 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, &callback_s1)); |
| 300 | 300 |
| 301 DVLOG(1) << "socket1 close"; | 301 DVLOG(1) << "socket1 close"; |
| 302 w1->OnClose(s1.get()); | 302 w1->OnClose(s1.get()); |
| 303 s1->DetachDelegate(); | 303 s1->DetachDelegate(); |
| 304 DVLOG(1) << "Done"; | 304 DVLOG(1) << "Done"; |
| 305 MessageLoopForIO::current()->RunAllPending(); | 305 MessageLoopForIO::current()->RunAllPending(); |
| 306 } | 306 } |
| 307 | 307 |
| 308 } | 308 } |
| OLD | NEW |