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/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.h" | 9 #include "base/message_loop.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "googleurl/src/gurl.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // next_state_ is not STATE_NONE. | 74 // next_state_ is not STATE_NONE. |
75 // If next_state_ is STATE_NONE, SocketStream::Close() or | 75 // If next_state_ is STATE_NONE, SocketStream::Close() or |
76 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), | 76 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), |
77 // so Release() won't be called. Thus, we don't need socket->AddRef() | 77 // so Release() won't be called. Thus, we don't need socket->AddRef() |
78 // here. | 78 // here. |
79 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); | 79 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); |
80 } | 80 } |
81 }; | 81 }; |
82 | 82 |
83 TEST_F(WebSocketThrottleTest, Throttle) { | 83 TEST_F(WebSocketThrottleTest, Throttle) { |
84 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 84 TestURLRequestContext context; |
85 DummySocketStreamDelegate delegate; | 85 DummySocketStreamDelegate delegate; |
86 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled | 86 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled |
87 // configuration. | 87 // configuration. |
88 WebSocketJob::set_websocket_over_spdy_enabled(true); | 88 WebSocketJob::set_websocket_over_spdy_enabled(true); |
89 | 89 |
90 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 90 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
91 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); | 91 struct addrinfo* addr = AddAddr(1, 2, 3, 4, NULL); |
92 addr = AddAddr(1, 2, 3, 5, addr); | 92 addr = AddAddr(1, 2, 3, 5, addr); |
93 addr = AddAddr(1, 2, 3, 6, addr); | 93 addr = AddAddr(1, 2, 3, 6, addr); |
94 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 94 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
95 scoped_refptr<SocketStream> s1( | 95 scoped_refptr<SocketStream> s1( |
96 new SocketStream(GURL("ws://host1/"), w1.get())); | 96 new SocketStream(GURL("ws://host1/"), w1.get())); |
97 s1->set_context(context.get()); | 97 s1->set_context(&context); |
98 w1->InitSocketStream(s1.get()); | 98 w1->InitSocketStream(s1.get()); |
99 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 99 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); |
100 DeleteAddrInfo(addr); | 100 DeleteAddrInfo(addr); |
101 | 101 |
102 DVLOG(1) << "socket1"; | 102 DVLOG(1) << "socket1"; |
103 TestCompletionCallback callback_s1; | 103 TestCompletionCallback callback_s1; |
104 // Trying to open connection to host1 will start without wait. | 104 // Trying to open connection to host1 will start without wait. |
105 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); | 105 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); |
106 | 106 |
107 // Now connecting to host1, so waiting queue looks like | 107 // Now connecting to host1, so waiting queue looks like |
108 // Address | head -> tail | 108 // Address | head -> tail |
109 // 1.2.3.4 | w1 | 109 // 1.2.3.4 | w1 |
110 // 1.2.3.5 | w1 | 110 // 1.2.3.5 | w1 |
111 // 1.2.3.6 | w1 | 111 // 1.2.3.6 | w1 |
112 | 112 |
113 // For host2: 1.2.3.4 | 113 // For host2: 1.2.3.4 |
114 addr = AddAddr(1, 2, 3, 4, NULL); | 114 addr = AddAddr(1, 2, 3, 4, NULL); |
115 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); | 115 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
116 scoped_refptr<SocketStream> s2( | 116 scoped_refptr<SocketStream> s2( |
117 new SocketStream(GURL("ws://host2/"), w2.get())); | 117 new SocketStream(GURL("ws://host2/"), w2.get())); |
118 s2->set_context(context.get()); | 118 s2->set_context(&context); |
119 w2->InitSocketStream(s2.get()); | 119 w2->InitSocketStream(s2.get()); |
120 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); | 120 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); |
121 DeleteAddrInfo(addr); | 121 DeleteAddrInfo(addr); |
122 | 122 |
123 DVLOG(1) << "socket2"; | 123 DVLOG(1) << "socket2"; |
124 TestCompletionCallback callback_s2; | 124 TestCompletionCallback callback_s2; |
125 // Trying to open connection to host2 will wait for w1. | 125 // Trying to open connection to host2 will wait for w1. |
126 EXPECT_EQ(ERR_IO_PENDING, | 126 EXPECT_EQ(ERR_IO_PENDING, |
127 w2->OnStartOpenConnection(s2, callback_s2.callback())); | 127 w2->OnStartOpenConnection(s2, callback_s2.callback())); |
128 // Now waiting queue looks like | 128 // Now waiting queue looks like |
129 // Address | head -> tail | 129 // Address | head -> tail |
130 // 1.2.3.4 | w1 w2 | 130 // 1.2.3.4 | w1 w2 |
131 // 1.2.3.5 | w1 | 131 // 1.2.3.5 | w1 |
132 // 1.2.3.6 | w1 | 132 // 1.2.3.6 | w1 |
133 | 133 |
134 // For host3: 1.2.3.5 | 134 // For host3: 1.2.3.5 |
135 addr = AddAddr(1, 2, 3, 5, NULL); | 135 addr = AddAddr(1, 2, 3, 5, NULL); |
136 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); | 136 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); |
137 scoped_refptr<SocketStream> s3( | 137 scoped_refptr<SocketStream> s3( |
138 new SocketStream(GURL("ws://host3/"), w3.get())); | 138 new SocketStream(GURL("ws://host3/"), w3.get())); |
139 s3->set_context(context.get()); | 139 s3->set_context(&context); |
140 w3->InitSocketStream(s3.get()); | 140 w3->InitSocketStream(s3.get()); |
141 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); | 141 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); |
142 DeleteAddrInfo(addr); | 142 DeleteAddrInfo(addr); |
143 | 143 |
144 DVLOG(1) << "socket3"; | 144 DVLOG(1) << "socket3"; |
145 TestCompletionCallback callback_s3; | 145 TestCompletionCallback callback_s3; |
146 // Trying to open connection to host3 will wait for w1. | 146 // Trying to open connection to host3 will wait for w1. |
147 EXPECT_EQ(ERR_IO_PENDING, | 147 EXPECT_EQ(ERR_IO_PENDING, |
148 w3->OnStartOpenConnection(s3, callback_s3.callback())); | 148 w3->OnStartOpenConnection(s3, callback_s3.callback())); |
149 // Address | head -> tail | 149 // Address | head -> tail |
150 // 1.2.3.4 | w1 w2 | 150 // 1.2.3.4 | w1 w2 |
151 // 1.2.3.5 | w1 w3 | 151 // 1.2.3.5 | w1 w3 |
152 // 1.2.3.6 | w1 | 152 // 1.2.3.6 | w1 |
153 | 153 |
154 // For host4: 1.2.3.4, 1.2.3.6 | 154 // For host4: 1.2.3.4, 1.2.3.6 |
155 addr = AddAddr(1, 2, 3, 4, NULL); | 155 addr = AddAddr(1, 2, 3, 4, NULL); |
156 addr = AddAddr(1, 2, 3, 6, addr); | 156 addr = AddAddr(1, 2, 3, 6, addr); |
157 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); | 157 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); |
158 scoped_refptr<SocketStream> s4( | 158 scoped_refptr<SocketStream> s4( |
159 new SocketStream(GURL("ws://host4/"), w4.get())); | 159 new SocketStream(GURL("ws://host4/"), w4.get())); |
160 s4->set_context(context.get()); | 160 s4->set_context(&context); |
161 w4->InitSocketStream(s4.get()); | 161 w4->InitSocketStream(s4.get()); |
162 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); | 162 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); |
163 DeleteAddrInfo(addr); | 163 DeleteAddrInfo(addr); |
164 | 164 |
165 DVLOG(1) << "socket4"; | 165 DVLOG(1) << "socket4"; |
166 TestCompletionCallback callback_s4; | 166 TestCompletionCallback callback_s4; |
167 // Trying to open connection to host4 will wait for w1, w2. | 167 // Trying to open connection to host4 will wait for w1, w2. |
168 EXPECT_EQ(ERR_IO_PENDING, | 168 EXPECT_EQ(ERR_IO_PENDING, |
169 w4->OnStartOpenConnection(s4, callback_s4.callback())); | 169 w4->OnStartOpenConnection(s4, callback_s4.callback())); |
170 // Address | head -> tail | 170 // Address | head -> tail |
171 // 1.2.3.4 | w1 w2 w4 | 171 // 1.2.3.4 | w1 w2 w4 |
172 // 1.2.3.5 | w1 w3 | 172 // 1.2.3.5 | w1 w3 |
173 // 1.2.3.6 | w1 w4 | 173 // 1.2.3.6 | w1 w4 |
174 | 174 |
175 // For host5: 1.2.3.6 | 175 // For host5: 1.2.3.6 |
176 addr = AddAddr(1, 2, 3, 6, NULL); | 176 addr = AddAddr(1, 2, 3, 6, NULL); |
177 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); | 177 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); |
178 scoped_refptr<SocketStream> s5( | 178 scoped_refptr<SocketStream> s5( |
179 new SocketStream(GURL("ws://host5/"), w5.get())); | 179 new SocketStream(GURL("ws://host5/"), w5.get())); |
180 s5->set_context(context.get()); | 180 s5->set_context(&context); |
181 w5->InitSocketStream(s5.get()); | 181 w5->InitSocketStream(s5.get()); |
182 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); | 182 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); |
183 DeleteAddrInfo(addr); | 183 DeleteAddrInfo(addr); |
184 | 184 |
185 DVLOG(1) << "socket5"; | 185 DVLOG(1) << "socket5"; |
186 TestCompletionCallback callback_s5; | 186 TestCompletionCallback callback_s5; |
187 // Trying to open connection to host5 will wait for w1, w4 | 187 // Trying to open connection to host5 will wait for w1, w4 |
188 EXPECT_EQ(ERR_IO_PENDING, | 188 EXPECT_EQ(ERR_IO_PENDING, |
189 w5->OnStartOpenConnection(s5, callback_s5.callback())); | 189 w5->OnStartOpenConnection(s5, callback_s5.callback())); |
190 // Address | head -> tail | 190 // Address | head -> tail |
191 // 1.2.3.4 | w1 w2 w4 | 191 // 1.2.3.4 | w1 w2 w4 |
192 // 1.2.3.5 | w1 w3 | 192 // 1.2.3.5 | w1 w3 |
193 // 1.2.3.6 | w1 w4 w5 | 193 // 1.2.3.6 | w1 w4 w5 |
194 | 194 |
195 // For host6: 1.2.3.6 | 195 // For host6: 1.2.3.6 |
196 addr = AddAddr(1, 2, 3, 6, NULL); | 196 addr = AddAddr(1, 2, 3, 6, NULL); |
197 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); | 197 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); |
198 scoped_refptr<SocketStream> s6( | 198 scoped_refptr<SocketStream> s6( |
199 new SocketStream(GURL("ws://host6/"), w6.get())); | 199 new SocketStream(GURL("ws://host6/"), w6.get())); |
200 s6->set_context(context.get()); | 200 s6->set_context(&context); |
201 w6->InitSocketStream(s6.get()); | 201 w6->InitSocketStream(s6.get()); |
202 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); | 202 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); |
203 DeleteAddrInfo(addr); | 203 DeleteAddrInfo(addr); |
204 | 204 |
205 DVLOG(1) << "socket6"; | 205 DVLOG(1) << "socket6"; |
206 TestCompletionCallback callback_s6; | 206 TestCompletionCallback callback_s6; |
207 // Trying to open connection to host6 will wait for w1, w4, w5 | 207 // Trying to open connection to host6 will wait for w1, w4, w5 |
208 EXPECT_EQ(ERR_IO_PENDING, | 208 EXPECT_EQ(ERR_IO_PENDING, |
209 w6->OnStartOpenConnection(s6, callback_s6.callback())); | 209 w6->OnStartOpenConnection(s6, callback_s6.callback())); |
210 // Address | head -> tail | 210 // Address | head -> tail |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 w3->OnClose(s3.get()); | 293 w3->OnClose(s3.get()); |
294 MessageLoopForIO::current()->RunAllPending(); | 294 MessageLoopForIO::current()->RunAllPending(); |
295 s3->DetachDelegate(); | 295 s3->DetachDelegate(); |
296 w4->OnClose(s4.get()); | 296 w4->OnClose(s4.get()); |
297 s4->DetachDelegate(); | 297 s4->DetachDelegate(); |
298 DVLOG(1) << "Done"; | 298 DVLOG(1) << "Done"; |
299 MessageLoopForIO::current()->RunAllPending(); | 299 MessageLoopForIO::current()->RunAllPending(); |
300 } | 300 } |
301 | 301 |
302 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { | 302 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { |
303 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 303 TestURLRequestContext context; |
304 DummySocketStreamDelegate delegate; | 304 DummySocketStreamDelegate delegate; |
305 WebSocketJob::set_websocket_over_spdy_enabled(true); | 305 WebSocketJob::set_websocket_over_spdy_enabled(true); |
306 | 306 |
307 // For localhost: 127.0.0.1, 127.0.0.1 | 307 // For localhost: 127.0.0.1, 127.0.0.1 |
308 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); | 308 struct addrinfo* addr = AddAddr(127, 0, 0, 1, NULL); |
309 addr = AddAddr(127, 0, 0, 1, addr); | 309 addr = AddAddr(127, 0, 0, 1, addr); |
310 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 310 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
311 scoped_refptr<SocketStream> s1( | 311 scoped_refptr<SocketStream> s1( |
312 new SocketStream(GURL("ws://localhost/"), w1.get())); | 312 new SocketStream(GURL("ws://localhost/"), w1.get())); |
313 s1->set_context(context.get()); | 313 s1->set_context(&context); |
314 w1->InitSocketStream(s1.get()); | 314 w1->InitSocketStream(s1.get()); |
315 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 315 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); |
316 DeleteAddrInfo(addr); | 316 DeleteAddrInfo(addr); |
317 | 317 |
318 DVLOG(1) << "socket1"; | 318 DVLOG(1) << "socket1"; |
319 TestCompletionCallback callback_s1; | 319 TestCompletionCallback callback_s1; |
320 // Trying to open connection to localhost will start without wait. | 320 // Trying to open connection to localhost will start without wait. |
321 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); | 321 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); |
322 | 322 |
323 DVLOG(1) << "socket1 close"; | 323 DVLOG(1) << "socket1 close"; |
324 w1->OnClose(s1.get()); | 324 w1->OnClose(s1.get()); |
325 s1->DetachDelegate(); | 325 s1->DetachDelegate(); |
326 DVLOG(1) << "Done"; | 326 DVLOG(1) << "Done"; |
327 MessageLoopForIO::current()->RunAllPending(); | 327 MessageLoopForIO::current()->RunAllPending(); |
328 } | 328 } |
329 | 329 |
330 } | 330 } |
OLD | NEW |