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/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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 // next_state_ is not STATE_NONE. | 52 // next_state_ is not STATE_NONE. |
53 // If next_state_ is STATE_NONE, SocketStream::Close() or | 53 // If next_state_ is STATE_NONE, SocketStream::Close() or |
54 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), | 54 // SocketStream::DetachDelegate() won't call SocketStream::Finish(), |
55 // so Release() won't be called. Thus, we don't need socket->AddRef() | 55 // so Release() won't be called. Thus, we don't need socket->AddRef() |
56 // here. | 56 // here. |
57 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); | 57 DCHECK_EQ(socket->next_state_, SocketStream::STATE_NONE); |
58 } | 58 } |
59 }; | 59 }; |
60 | 60 |
61 TEST_F(WebSocketThrottleTest, Throttle) { | 61 TEST_F(WebSocketThrottleTest, Throttle) { |
62 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 62 TestURLRequestContext context; |
63 DummySocketStreamDelegate delegate; | 63 DummySocketStreamDelegate delegate; |
64 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled | 64 // TODO(toyoshim): We need to consider both spdy-enabled and spdy-disabled |
65 // configuration. | 65 // configuration. |
66 WebSocketJob::set_websocket_over_spdy_enabled(true); | 66 WebSocketJob::set_websocket_over_spdy_enabled(true); |
67 | 67 |
68 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 | 68 // For host1: 1.2.3.4, 1.2.3.5, 1.2.3.6 |
69 AddressList addr; | 69 AddressList addr; |
70 addr.push_back(MakeAddr(1, 2, 3, 4)); | 70 addr.push_back(MakeAddr(1, 2, 3, 4)); |
71 addr.push_back(MakeAddr(1, 2, 3, 5)); | 71 addr.push_back(MakeAddr(1, 2, 3, 5)); |
72 addr.push_back(MakeAddr(1, 2, 3, 6)); | 72 addr.push_back(MakeAddr(1, 2, 3, 6)); |
73 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); | 73 scoped_refptr<WebSocketJob> w1(new WebSocketJob(&delegate)); |
74 scoped_refptr<SocketStream> s1( | 74 scoped_refptr<SocketStream> s1( |
75 new SocketStream(GURL("ws://host1/"), w1.get())); | 75 new SocketStream(GURL("ws://host1/"), w1.get())); |
76 s1->set_context(context.get()); | 76 s1->set_context(&context); |
77 w1->InitSocketStream(s1.get()); | 77 w1->InitSocketStream(s1.get()); |
78 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 78 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); |
79 | 79 |
80 DVLOG(1) << "socket1"; | 80 DVLOG(1) << "socket1"; |
81 TestCompletionCallback callback_s1; | 81 TestCompletionCallback callback_s1; |
82 // Trying to open connection to host1 will start without wait. | 82 // Trying to open connection to host1 will start without wait. |
83 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); | 83 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); |
84 | 84 |
85 // Now connecting to host1, so waiting queue looks like | 85 // Now connecting to host1, so waiting queue looks like |
86 // Address | head -> tail | 86 // Address | head -> tail |
87 // 1.2.3.4 | w1 | 87 // 1.2.3.4 | w1 |
88 // 1.2.3.5 | w1 | 88 // 1.2.3.5 | w1 |
89 // 1.2.3.6 | w1 | 89 // 1.2.3.6 | w1 |
90 | 90 |
91 // For host2: 1.2.3.4 | 91 // For host2: 1.2.3.4 |
92 addr.clear(); | 92 addr.clear(); |
93 addr.push_back(MakeAddr(1, 2, 3, 4)); | 93 addr.push_back(MakeAddr(1, 2, 3, 4)); |
94 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); | 94 scoped_refptr<WebSocketJob> w2(new WebSocketJob(&delegate)); |
95 scoped_refptr<SocketStream> s2( | 95 scoped_refptr<SocketStream> s2( |
96 new SocketStream(GURL("ws://host2/"), w2.get())); | 96 new SocketStream(GURL("ws://host2/"), w2.get())); |
97 s2->set_context(context.get()); | 97 s2->set_context(&context); |
98 w2->InitSocketStream(s2.get()); | 98 w2->InitSocketStream(s2.get()); |
99 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); | 99 WebSocketThrottleTest::MockSocketStreamConnect(s2, addr); |
100 | 100 |
101 DVLOG(1) << "socket2"; | 101 DVLOG(1) << "socket2"; |
102 TestCompletionCallback callback_s2; | 102 TestCompletionCallback callback_s2; |
103 // Trying to open connection to host2 will wait for w1. | 103 // Trying to open connection to host2 will wait for w1. |
104 EXPECT_EQ(ERR_IO_PENDING, | 104 EXPECT_EQ(ERR_IO_PENDING, |
105 w2->OnStartOpenConnection(s2, callback_s2.callback())); | 105 w2->OnStartOpenConnection(s2, callback_s2.callback())); |
106 // Now waiting queue looks like | 106 // Now waiting queue looks like |
107 // Address | head -> tail | 107 // Address | head -> tail |
108 // 1.2.3.4 | w1 w2 | 108 // 1.2.3.4 | w1 w2 |
109 // 1.2.3.5 | w1 | 109 // 1.2.3.5 | w1 |
110 // 1.2.3.6 | w1 | 110 // 1.2.3.6 | w1 |
111 | 111 |
112 // For host3: 1.2.3.5 | 112 // For host3: 1.2.3.5 |
113 addr.clear(); | 113 addr.clear(); |
114 addr.push_back(MakeAddr(1, 2, 3, 5)); | 114 addr.push_back(MakeAddr(1, 2, 3, 5)); |
115 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); | 115 scoped_refptr<WebSocketJob> w3(new WebSocketJob(&delegate)); |
116 scoped_refptr<SocketStream> s3( | 116 scoped_refptr<SocketStream> s3( |
117 new SocketStream(GURL("ws://host3/"), w3.get())); | 117 new SocketStream(GURL("ws://host3/"), w3.get())); |
118 s3->set_context(context.get()); | 118 s3->set_context(&context); |
119 w3->InitSocketStream(s3.get()); | 119 w3->InitSocketStream(s3.get()); |
120 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); | 120 WebSocketThrottleTest::MockSocketStreamConnect(s3, addr); |
121 | 121 |
122 DVLOG(1) << "socket3"; | 122 DVLOG(1) << "socket3"; |
123 TestCompletionCallback callback_s3; | 123 TestCompletionCallback callback_s3; |
124 // Trying to open connection to host3 will wait for w1. | 124 // Trying to open connection to host3 will wait for w1. |
125 EXPECT_EQ(ERR_IO_PENDING, | 125 EXPECT_EQ(ERR_IO_PENDING, |
126 w3->OnStartOpenConnection(s3, callback_s3.callback())); | 126 w3->OnStartOpenConnection(s3, callback_s3.callback())); |
127 // Address | head -> tail | 127 // Address | head -> tail |
128 // 1.2.3.4 | w1 w2 | 128 // 1.2.3.4 | w1 w2 |
129 // 1.2.3.5 | w1 w3 | 129 // 1.2.3.5 | w1 w3 |
130 // 1.2.3.6 | w1 | 130 // 1.2.3.6 | w1 |
131 | 131 |
132 // For host4: 1.2.3.4, 1.2.3.6 | 132 // For host4: 1.2.3.4, 1.2.3.6 |
133 addr.clear(); | 133 addr.clear(); |
134 addr.push_back(MakeAddr(1, 2, 3, 4)); | 134 addr.push_back(MakeAddr(1, 2, 3, 4)); |
135 addr.push_back(MakeAddr(1, 2, 3, 6)); | 135 addr.push_back(MakeAddr(1, 2, 3, 6)); |
136 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); | 136 scoped_refptr<WebSocketJob> w4(new WebSocketJob(&delegate)); |
137 scoped_refptr<SocketStream> s4( | 137 scoped_refptr<SocketStream> s4( |
138 new SocketStream(GURL("ws://host4/"), w4.get())); | 138 new SocketStream(GURL("ws://host4/"), w4.get())); |
139 s4->set_context(context.get()); | 139 s4->set_context(&context); |
140 w4->InitSocketStream(s4.get()); | 140 w4->InitSocketStream(s4.get()); |
141 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); | 141 WebSocketThrottleTest::MockSocketStreamConnect(s4, addr); |
142 | 142 |
143 DVLOG(1) << "socket4"; | 143 DVLOG(1) << "socket4"; |
144 TestCompletionCallback callback_s4; | 144 TestCompletionCallback callback_s4; |
145 // Trying to open connection to host4 will wait for w1, w2. | 145 // Trying to open connection to host4 will wait for w1, w2. |
146 EXPECT_EQ(ERR_IO_PENDING, | 146 EXPECT_EQ(ERR_IO_PENDING, |
147 w4->OnStartOpenConnection(s4, callback_s4.callback())); | 147 w4->OnStartOpenConnection(s4, callback_s4.callback())); |
148 // Address | head -> tail | 148 // Address | head -> tail |
149 // 1.2.3.4 | w1 w2 w4 | 149 // 1.2.3.4 | w1 w2 w4 |
150 // 1.2.3.5 | w1 w3 | 150 // 1.2.3.5 | w1 w3 |
151 // 1.2.3.6 | w1 w4 | 151 // 1.2.3.6 | w1 w4 |
152 | 152 |
153 // For host5: 1.2.3.6 | 153 // For host5: 1.2.3.6 |
154 addr.clear(); | 154 addr.clear(); |
155 addr.push_back(MakeAddr(1, 2, 3, 6)); | 155 addr.push_back(MakeAddr(1, 2, 3, 6)); |
156 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); | 156 scoped_refptr<WebSocketJob> w5(new WebSocketJob(&delegate)); |
157 scoped_refptr<SocketStream> s5( | 157 scoped_refptr<SocketStream> s5( |
158 new SocketStream(GURL("ws://host5/"), w5.get())); | 158 new SocketStream(GURL("ws://host5/"), w5.get())); |
159 s5->set_context(context.get()); | 159 s5->set_context(&context); |
160 w5->InitSocketStream(s5.get()); | 160 w5->InitSocketStream(s5.get()); |
161 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); | 161 WebSocketThrottleTest::MockSocketStreamConnect(s5, addr); |
162 | 162 |
163 DVLOG(1) << "socket5"; | 163 DVLOG(1) << "socket5"; |
164 TestCompletionCallback callback_s5; | 164 TestCompletionCallback callback_s5; |
165 // Trying to open connection to host5 will wait for w1, w4 | 165 // Trying to open connection to host5 will wait for w1, w4 |
166 EXPECT_EQ(ERR_IO_PENDING, | 166 EXPECT_EQ(ERR_IO_PENDING, |
167 w5->OnStartOpenConnection(s5, callback_s5.callback())); | 167 w5->OnStartOpenConnection(s5, callback_s5.callback())); |
168 // Address | head -> tail | 168 // Address | head -> tail |
169 // 1.2.3.4 | w1 w2 w4 | 169 // 1.2.3.4 | w1 w2 w4 |
170 // 1.2.3.5 | w1 w3 | 170 // 1.2.3.5 | w1 w3 |
171 // 1.2.3.6 | w1 w4 w5 | 171 // 1.2.3.6 | w1 w4 w5 |
172 | 172 |
173 // For host6: 1.2.3.6 | 173 // For host6: 1.2.3.6 |
174 addr.clear(); | 174 addr.clear(); |
175 addr.push_back(MakeAddr(1, 2, 3, 6)); | 175 addr.push_back(MakeAddr(1, 2, 3, 6)); |
176 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); | 176 scoped_refptr<WebSocketJob> w6(new WebSocketJob(&delegate)); |
177 scoped_refptr<SocketStream> s6( | 177 scoped_refptr<SocketStream> s6( |
178 new SocketStream(GURL("ws://host6/"), w6.get())); | 178 new SocketStream(GURL("ws://host6/"), w6.get())); |
179 s6->set_context(context.get()); | 179 s6->set_context(&context); |
180 w6->InitSocketStream(s6.get()); | 180 w6->InitSocketStream(s6.get()); |
181 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); | 181 WebSocketThrottleTest::MockSocketStreamConnect(s6, addr); |
182 | 182 |
183 DVLOG(1) << "socket6"; | 183 DVLOG(1) << "socket6"; |
184 TestCompletionCallback callback_s6; | 184 TestCompletionCallback callback_s6; |
185 // Trying to open connection to host6 will wait for w1, w4, w5 | 185 // Trying to open connection to host6 will wait for w1, w4, w5 |
186 EXPECT_EQ(ERR_IO_PENDING, | 186 EXPECT_EQ(ERR_IO_PENDING, |
187 w6->OnStartOpenConnection(s6, callback_s6.callback())); | 187 w6->OnStartOpenConnection(s6, callback_s6.callback())); |
188 // Address | head -> tail | 188 // Address | head -> tail |
189 // 1.2.3.4 | w1 w2 w4 | 189 // 1.2.3.4 | w1 w2 w4 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 w3->OnClose(s3.get()); | 271 w3->OnClose(s3.get()); |
272 MessageLoopForIO::current()->RunAllPending(); | 272 MessageLoopForIO::current()->RunAllPending(); |
273 s3->DetachDelegate(); | 273 s3->DetachDelegate(); |
274 w4->OnClose(s4.get()); | 274 w4->OnClose(s4.get()); |
275 s4->DetachDelegate(); | 275 s4->DetachDelegate(); |
276 DVLOG(1) << "Done"; | 276 DVLOG(1) << "Done"; |
277 MessageLoopForIO::current()->RunAllPending(); | 277 MessageLoopForIO::current()->RunAllPending(); |
278 } | 278 } |
279 | 279 |
280 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { | 280 TEST_F(WebSocketThrottleTest, NoThrottleForDuplicateAddress) { |
281 scoped_refptr<URLRequestContext> context(new TestURLRequestContext); | 281 TestURLRequestContext context; |
282 DummySocketStreamDelegate delegate; | 282 DummySocketStreamDelegate delegate; |
283 WebSocketJob::set_websocket_over_spdy_enabled(true); | 283 WebSocketJob::set_websocket_over_spdy_enabled(true); |
284 | 284 |
285 // For localhost: 127.0.0.1, 127.0.0.1 | 285 // For localhost: 127.0.0.1, 127.0.0.1 |
286 AddressList addr; | 286 AddressList addr; |
287 addr.push_back(MakeAddr(127, 0, 0, 1)); | 287 addr.push_back(MakeAddr(127, 0, 0, 1)); |
288 addr.push_back(MakeAddr(127, 0, 0, 1)); | 288 addr.push_back(MakeAddr(127, 0, 0, 1)); |
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 s1->set_context(context.get()); | 292 s1->set_context(&context); |
293 w1->InitSocketStream(s1.get()); | 293 w1->InitSocketStream(s1.get()); |
294 WebSocketThrottleTest::MockSocketStreamConnect(s1, addr); | 294 WebSocketThrottleTest::MockSocketStreamConnect(s1, 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.callback())); | 299 EXPECT_EQ(OK, w1->OnStartOpenConnection(s1, callback_s1.callback())); |
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 |