OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // | 4 // |
5 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 // | 9 // |
10 // Test socket close events. | 10 // Test socket close events. |
11 | 11 |
(...skipping 22 matching lines...) Expand all Loading... |
34 void proceed() { | 34 void proceed() { |
35 if (_iterations < ITERATIONS) { | 35 if (_iterations < ITERATIONS) { |
36 Timer.run(sendData); | 36 Timer.run(sendData); |
37 } else { | 37 } else { |
38 shutdown(); | 38 shutdown(); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 void sendData() { | 42 void sendData() { |
43 | 43 |
44 void dataHandler() { | 44 void dataHandler(bytes) { |
45 switch (_mode) { | 45 switch (_mode) { |
46 case 0: | 46 case 0: |
47 case 1: | 47 case 1: |
48 case 2: | 48 case 2: |
49 Expect.fail("No data expected"); | 49 Expect.fail("No data expected"); |
50 break; | 50 break; |
51 case 3: | 51 case 3: |
52 case 4: | 52 case 4: |
53 case 5: | 53 case 5: |
54 case 6: | 54 case 6: |
55 List<int> b = new List<int>.fixedLength(5); | 55 _readBytes += bytes.length; |
56 _readBytes += _socket.readList(b, 0, 5); | |
57 if ((_readBytes % 5) == 0) { | 56 if ((_readBytes % 5) == 0) { |
58 _dataEvents++; | 57 _dataEvents++; |
59 } | 58 } |
60 break; | 59 break; |
61 default: | 60 default: |
62 Expect.fail("Unknown test mode"); | 61 Expect.fail("Unknown test mode"); |
63 } | 62 } |
64 } | 63 } |
65 | 64 |
66 void closeHandler() { | 65 void closeHandler(socket) { |
67 _closeEvents++; | 66 _closeEvents++; |
68 switch (_mode) { | 67 switch (_mode) { |
69 case 0: | 68 case 0: |
70 case 1: | 69 case 1: |
71 Expect.fail("No close expected"); | 70 socket.close(); |
72 break; | 71 break; |
73 case 2: | 72 case 2: |
74 case 3: | 73 case 3: |
75 _socket.close(); | 74 socket.close(); |
76 proceed(); | 75 proceed(); |
77 break; | 76 break; |
78 case 4: | 77 case 4: |
79 proceed(); | 78 proceed(); |
80 break; | 79 break; |
81 case 5: | 80 case 5: |
82 _socket.close(); | 81 socket.close(); |
83 proceed(); | 82 proceed(); |
84 break; | 83 break; |
85 case 6: | 84 case 6: |
86 proceed(); | 85 proceed(); |
87 break; | 86 break; |
88 default: | 87 default: |
89 Expect.fail("Unknown test mode"); | 88 Expect.fail("Unknown test mode"); |
90 } | 89 } |
91 } | 90 } |
92 | 91 |
93 void errorHandler(Exception e) { | 92 void errorHandler(Exception e) { |
94 _errorEvents++; | 93 _errorEvents++; |
95 _socket.close(); | 94 socket.close(); |
96 } | 95 } |
97 | 96 |
98 void connectHandler() { | 97 void connectHandler(socket) { |
99 _socket.onData = dataHandler; | 98 socket.listen( |
100 _socket.onClosed = closeHandler; | 99 dataHandler, |
101 _socket.onError = errorHandler; | 100 onDone: () => closeHandler(socket), |
| 101 onError: (error) => errorHandler(socket)); |
102 | 102 |
103 void writeHello() { | 103 void writeHello() { |
104 int bytesWritten = 0; | 104 socket.add("Hello".charCodes); |
105 while (bytesWritten != 5) { | |
106 bytesWritten += _socket.writeList("Hello".charCodes, | |
107 bytesWritten, | |
108 5 - bytesWritten); | |
109 } | |
110 } | 105 } |
111 | 106 |
112 _iterations++; | 107 _iterations++; |
113 switch (_mode) { | 108 switch (_mode) { |
114 case 0: | 109 case 0: |
115 _socket.close(); | 110 socket.destroy(); |
116 proceed(); | 111 proceed(); |
117 break; | 112 break; |
118 case 1: | 113 case 1: |
119 writeHello(); | 114 writeHello(); |
120 _socket.close(); | 115 socket.destroy(); |
121 proceed(); | 116 proceed(); |
122 break; | 117 break; |
123 case 2: | 118 case 2: |
124 case 3: | 119 case 3: |
125 writeHello(); | 120 writeHello(); |
126 break; | 121 break; |
127 case 4: | 122 case 4: |
128 writeHello(); | 123 writeHello(); |
129 _socket.close(true); | 124 socket.close(); // Half close. |
130 break; | 125 break; |
131 case 5: | 126 case 5: |
132 writeHello(); | 127 writeHello(); |
133 break; | 128 break; |
134 case 6: | 129 case 6: |
135 writeHello(); | 130 writeHello(); |
136 _socket.close(true); | 131 socket.close(); // Half close. |
137 break; | 132 break; |
138 default: | 133 default: |
139 Expect.fail("Unknown test mode"); | 134 Expect.fail("Unknown test mode"); |
140 } | 135 } |
141 } | 136 } |
142 | 137 |
143 _socket = new Socket(SocketCloseServer.HOST, _port); | 138 Socket.connect(SocketCloseServer.HOST, _port).then(connectHandler); |
144 Expect.equals(true, _socket != null); | |
145 _socket.onConnect = connectHandler; | |
146 } | 139 } |
147 | 140 |
148 void initialize() { | 141 void initialize() { |
149 _receivePort.receive((var message, SendPort replyTo) { | 142 _receivePort.receive((var message, SendPort replyTo) { |
150 _port = message; | 143 _port = message; |
151 proceed(); | 144 proceed(); |
152 }); | 145 }); |
153 _sendPort.send(_mode, _receivePort.toSendPort()); | 146 _sendPort.send(_mode, _receivePort.toSendPort()); |
154 } | 147 } |
155 | 148 |
156 void shutdown() { | 149 void shutdown() { |
157 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); | 150 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); |
158 _receivePort.receive((message, ignore) { | 151 _receivePort.receive((message, ignore) { |
159 _donePort.send(null); | 152 _donePort.send(null); |
160 _receivePort.close(); | 153 _receivePort.close(); |
161 }); | 154 }); |
162 | 155 |
163 switch (_mode) { | 156 switch (_mode) { |
164 case 0: | 157 case 0: |
165 case 1: | 158 case 1: |
166 Expect.equals(0, _dataEvents); | 159 Expect.equals(0, _dataEvents); |
167 Expect.equals(0, _closeEvents); | 160 Expect.equals(ITERATIONS, _closeEvents); |
168 break; | 161 break; |
169 case 2: | 162 case 2: |
170 Expect.equals(0, _dataEvents); | 163 Expect.equals(0, _dataEvents); |
171 Expect.equals(ITERATIONS, _closeEvents); | 164 Expect.equals(ITERATIONS, _closeEvents); |
172 break; | 165 break; |
173 case 3: | 166 case 3: |
174 case 4: | 167 case 4: |
| 168 Expect.isTrue(_dataEvents <= ITERATIONS); |
| 169 Expect.isTrue(_dataEvents >= 0); |
| 170 Expect.equals(ITERATIONS, _closeEvents); |
| 171 break; |
175 case 5: | 172 case 5: |
176 case 6: | 173 case 6: |
177 Expect.equals(ITERATIONS, _dataEvents); | 174 Expect.equals(ITERATIONS, _dataEvents); |
178 Expect.equals(ITERATIONS, _closeEvents); | 175 Expect.equals(ITERATIONS, _closeEvents); |
179 break; | 176 break; |
180 default: | 177 default: |
181 Expect.fail("Unknown test mode"); | 178 Expect.fail("Unknown test mode"); |
182 } | 179 } |
183 Expect.equals(0, _errorEvents); | 180 Expect.equals(0, _errorEvents); |
184 } | 181 } |
185 | 182 |
186 int _port; | 183 int _port; |
187 ReceivePort _receivePort; | 184 ReceivePort _receivePort; |
188 SendPort _sendPort; | 185 SendPort _sendPort; |
189 Socket _socket; | |
190 List<int> _buffer; | 186 List<int> _buffer; |
191 int _readBytes; | 187 int _readBytes; |
192 int _dataEvents; | 188 int _dataEvents; |
193 int _closeEvents; | 189 int _closeEvents; |
194 int _errorEvents; | 190 int _errorEvents; |
195 int _iterations; | 191 int _iterations; |
196 int _mode; | 192 int _mode; |
197 SendPort _donePort; | 193 SendPort _donePort; |
198 } | 194 } |
199 | 195 |
(...skipping 12 matching lines...) Expand all Loading... |
212 | 208 |
213 class SocketCloseServer { | 209 class SocketCloseServer { |
214 | 210 |
215 static const HOST = "127.0.0.1"; | 211 static const HOST = "127.0.0.1"; |
216 | 212 |
217 SocketCloseServer() : super() {} | 213 SocketCloseServer() : super() {} |
218 | 214 |
219 void connectionHandler(ConnectionData data) { | 215 void connectionHandler(ConnectionData data) { |
220 var connection = data.connection; | 216 var connection = data.connection; |
221 | 217 |
222 void readBytes(whenFiveBytes) { | 218 void readBytes(bytes, whenFiveBytes) { |
223 List<int> b = new List<int>.fixedLength(5); | 219 data.readBytes += bytes.length; |
224 data.readBytes += connection.readList(b, 0, 5); | 220 Expect.isTrue(data.readBytes <= 5); |
225 if (data.readBytes == 5) { | 221 if (data.readBytes == 5) { |
226 whenFiveBytes(); | 222 whenFiveBytes(); |
227 } | 223 } |
228 } | 224 } |
229 | 225 |
230 void writeHello() { | 226 void writeHello() { |
231 int bytesWritten = 0; | 227 connection.add("Hello".charCodes); |
232 while (bytesWritten != 5) { | |
233 bytesWritten += connection.writeList("Hello".charCodes, | |
234 bytesWritten, | |
235 5 - bytesWritten); | |
236 } | |
237 } | 228 } |
238 | 229 |
239 void dataHandler() { | 230 void dataHandler(bytes) { |
240 switch (_mode) { | 231 switch (_mode) { |
241 case 0: | 232 case 0: |
242 Expect.fail("No data expected"); | 233 Expect.fail("No data expected"); |
243 break; | 234 break; |
244 case 1: | 235 case 1: |
245 readBytes(() { _dataEvents++; }); | 236 readBytes(bytes, () { _dataEvents++; }); |
246 break; | 237 break; |
247 case 2: | 238 case 2: |
248 readBytes(() { | 239 readBytes(bytes, () { |
249 _dataEvents++; | 240 _dataEvents++; |
250 connection.close(); | 241 connection.destroy(); |
251 }); | 242 }); |
252 break; | 243 break; |
253 case 3: | 244 case 3: |
254 readBytes(() { | 245 readBytes(bytes, () { |
255 _dataEvents++; | 246 _dataEvents++; |
256 writeHello(); | 247 writeHello(); |
257 connection.close(); | 248 connection.destroy(); |
258 }); | 249 }); |
259 break; | 250 break; |
260 case 4: | 251 case 4: |
261 readBytes(() { | 252 readBytes(bytes, () { |
262 _dataEvents++; | 253 _dataEvents++; |
263 writeHello(); | 254 writeHello(); |
264 }); | 255 }); |
265 break; | 256 break; |
266 case 5: | 257 case 5: |
267 case 6: | 258 case 6: |
268 readBytes(() { | 259 readBytes(bytes, () { |
269 _dataEvents++; | 260 _dataEvents++; |
270 writeHello(); | 261 writeHello(); |
271 connection.close(true); | 262 connection.close(); // Half close. |
272 }); | 263 }); |
273 break; | 264 break; |
274 default: | 265 default: |
275 Expect.fail("Unknown test mode"); | 266 Expect.fail("Unknown test mode"); |
276 } | 267 } |
277 } | 268 } |
278 | 269 |
279 void closeHandler() { | 270 void closeHandler() { |
280 _closeEvents++; | 271 _closeEvents++; |
281 connection.close(); | 272 connection.close(); |
282 } | 273 } |
283 | 274 |
284 void errorHandler(Exception e) { | 275 void errorHandler(e) { |
285 Expect.fail("Socket error $e"); | 276 Expect.fail("Socket error $e"); |
286 } | 277 } |
287 | 278 |
288 _iterations++; | 279 _iterations++; |
289 | 280 |
290 connection.onData = dataHandler; | 281 connection.listen( |
291 connection.onClosed = closeHandler; | 282 dataHandler, |
292 connection.onError = errorHandler; | 283 onDone: closeHandler, |
| 284 onError: errorHandler); |
293 } | 285 } |
294 | 286 |
295 void errorHandlerServer(Exception e) { | 287 void errorHandlerServer(e) { |
296 Expect.fail("Server socket error"); | 288 Expect.fail("Server socket error"); |
297 } | 289 } |
298 | 290 |
299 waitForResult() { | 291 waitForResult() { |
300 // Make sure all iterations have been run. In multiple of these | 292 // Make sure all iterations have been run. In multiple of these |
301 // scenarios it is possible to get the SERVERSHUTDOWN message | 293 // scenarios it is possible to get the SERVERSHUTDOWN message |
302 // before we have received the last close event on the | 294 // before we have received the last close event on the |
303 // server. In these cases we wait for the correct number of | 295 // server. In these cases we wait for the correct number of |
304 // close events. | 296 // close events. |
305 if (_iterations == ITERATIONS && | 297 if (_iterations == ITERATIONS && |
306 (_closeEvents == ITERATIONS || (_mode == 2 || _mode == 3))) { | 298 (_closeEvents == ITERATIONS || (_mode == 2 || _mode == 3))) { |
307 switch (_mode) { | 299 switch (_mode) { |
308 case 0: | 300 case 0: |
309 Expect.equals(0, _dataEvents); | 301 Expect.equals(0, _dataEvents); |
310 Expect.equals(ITERATIONS, _closeEvents); | 302 Expect.equals(ITERATIONS, _closeEvents); |
311 break; | 303 break; |
312 case 1: | 304 case 1: |
313 Expect.equals(ITERATIONS, _dataEvents); | 305 Expect.isTrue(_dataEvents <= ITERATIONS); |
| 306 Expect.isTrue(_dataEvents >= 0); |
314 Expect.equals(ITERATIONS, _closeEvents); | 307 Expect.equals(ITERATIONS, _closeEvents); |
315 break; | 308 break; |
316 case 2: | 309 case 2: |
317 case 3: | 310 case 3: |
318 Expect.equals(ITERATIONS, _dataEvents); | 311 Expect.equals(ITERATIONS, _dataEvents); |
319 Expect.equals(0, _closeEvents); | 312 Expect.equals(ITERATIONS, _closeEvents); |
320 break; | 313 break; |
321 case 4: | 314 case 4: |
322 case 5: | 315 case 5: |
323 case 6: | 316 case 6: |
324 Expect.equals(ITERATIONS, _dataEvents); | 317 Expect.equals(ITERATIONS, _dataEvents); |
325 Expect.equals(ITERATIONS, _closeEvents); | 318 Expect.equals(ITERATIONS, _closeEvents); |
326 break; | 319 break; |
327 default: | 320 default: |
328 Expect.fail("Unknown test mode"); | 321 Expect.fail("Unknown test mode"); |
329 } | 322 } |
330 Expect.equals(0, _errorEvents); | 323 Expect.equals(0, _errorEvents); |
331 _server.close(); | 324 _server.close(); |
332 port.close(); | 325 port.close(); |
333 _donePort.send(null); | 326 _donePort.send(null); |
334 } else { | 327 } else { |
335 new Timer(new Duration(milliseconds: 100), waitForResult); | 328 new Timer(new Duration(milliseconds: 100), waitForResult); |
336 } | 329 } |
337 } | 330 } |
338 | 331 |
339 void dispatch(message, SendPort replyTo) { | 332 void dispatch(message, SendPort replyTo) { |
340 _donePort = replyTo; | 333 _donePort = replyTo; |
341 if (message != SERVERSHUTDOWN) { | 334 if (message != SERVERSHUTDOWN) { |
342 _readBytes = 0; | 335 _readBytes = 0; |
343 _errorEvents = 0; | 336 _errorEvents = 0; |
344 _dataEvents = 0; | 337 _dataEvents = 0; |
345 _closeEvents = 0; | 338 _closeEvents = 0; |
346 _iterations = 0; | 339 _iterations = 0; |
347 _mode = message; | 340 _mode = message; |
348 _server = new ServerSocket(HOST, 0, 10); | 341 ServerSocket.bind().then((server) { |
349 Expect.equals(true, _server != null); | 342 _server = server; |
350 _server.onConnection = (connection) { | 343 _server.listen( |
351 var data = new ConnectionData(connection); | 344 (socket) { |
352 connectionHandler(data); | 345 var data = new ConnectionData(socket); |
353 }; | 346 connectionHandler(data); |
354 _server.onError = errorHandlerServer; | 347 }, |
355 replyTo.send(_server.port, null); | 348 onError: errorHandlerServer |
| 349 ); |
| 350 replyTo.send(_server.port, null); |
| 351 }); |
356 } else { | 352 } else { |
357 Timer.run(waitForResult); | 353 Timer.run(waitForResult); |
358 } | 354 } |
359 } | 355 } |
360 | 356 |
361 ServerSocket _server; | 357 ServerSocket _server; |
362 SendPort _donePort; | 358 SendPort _donePort; |
363 int _readBytes; | 359 int _readBytes; |
364 int _errorEvents; | 360 int _errorEvents; |
365 int _dataEvents; | 361 int _dataEvents; |
366 int _closeEvents; | 362 int _closeEvents; |
367 int _iterations; | 363 int _iterations; |
368 int _mode; | 364 int _mode; |
369 } | 365 } |
370 | 366 |
371 | 367 |
372 main() { | 368 main() { |
373 // Run the close test in these different "modes". | 369 // Run the close test in these different "modes". |
374 // 0: Client closes without sending at all. | 370 // 0: Client closes without sending at all. |
375 // 1: Client sends and closes. | 371 // 1: Client sends and destroys. |
376 // 2: Client sends. Server closes. | 372 // 2: Client sends. Server destroys. |
377 // 3: Client sends. Server responds and closes. | 373 // 3: Client sends. Server responds and destroys. |
378 // 4: Client sends and half-closes. Server responds and closes. | 374 // 4: Client sends and half-closes. Server responds and destroys. |
379 // 5: Client sends. Server responds and half closes. | 375 // 5: Client sends. Server responds and half closes. |
380 // 6: Client sends and half-closes. Server responds and half closes. | 376 // 6: Client sends and half-closes. Server responds and half closes. |
381 var tests = 7; | 377 var tests = 7; |
382 var port = new ReceivePort(); | 378 var port = new ReceivePort(); |
383 var completed = 0; | 379 var completed = 0; |
384 port.receive((message, ignore) { | 380 port.receive((message, ignore) { |
385 if (++completed == tests) port.close(); | 381 if (++completed == tests) port.close(); |
386 }); | 382 }); |
387 for (var i = 0; i < tests; i++) { | 383 for (var i = 0; i < tests; i++) { |
388 new SocketClose.start(i, port.toSendPort()); | 384 new SocketClose.start(i, port.toSendPort()); |
389 } | 385 } |
390 } | 386 } |
OLD | NEW |