OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 |
12 #import("dart:io"); | 12 #import("dart:io"); |
13 #import("dart:isolate"); | 13 #import("dart:isolate"); |
14 | 14 |
15 final SERVERSHUTDOWN = -1; | 15 final SERVERSHUTDOWN = -1; |
16 final ITERATIONS = 10; | 16 final ITERATIONS = 10; |
17 | 17 |
18 | 18 |
19 class SocketClose { | 19 class SocketClose { |
20 | 20 |
21 SocketClose.start(this._mode, this._donePort) | 21 SocketClose.start(this._mode, this._donePort) |
22 : _receivePort = new ReceivePort(), | 22 : _receivePort = new ReceivePort(), |
23 _sendPort = null, | 23 _sendPort = null, |
24 _readBytes = 0, | 24 _readBytes = 0, |
25 _dataEvents = 0, | 25 _dataEvents = 0, |
26 _closeEvents = 0, | 26 _closeEvents = 0, |
27 _errorEvents = 0, | 27 _errorEvents = 0, |
28 _iterations = 0 { | 28 _iterations = 0 { |
29 new SocketCloseServer().spawn().then((SendPort port) { | 29 _sendPort = spawnFunction(startSocketCloseServer); |
30 _sendPort = port; | 30 start(); |
31 start(); | |
32 }); | |
33 } | 31 } |
34 | 32 |
35 void proceed() { | 33 void proceed() { |
36 if (_iterations < ITERATIONS) { | 34 if (_iterations < ITERATIONS) { |
37 new Timer(0, sendData); | 35 new Timer(0, sendData); |
38 } else { | 36 } else { |
39 shutdown(); | 37 shutdown(); |
40 } | 38 } |
41 } | 39 } |
42 | 40 |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 } | 206 } |
209 | 207 |
210 | 208 |
211 class ConnectionData { | 209 class ConnectionData { |
212 ConnectionData(Socket this.connection) : readBytes = 0; | 210 ConnectionData(Socket this.connection) : readBytes = 0; |
213 Socket connection; | 211 Socket connection; |
214 int readBytes; | 212 int readBytes; |
215 } | 213 } |
216 | 214 |
217 | 215 |
218 class SocketCloseServer extends Isolate { | 216 void startSocketCloseServer() { |
217 var server = new SocketCloseServer(); | |
218 port.receive((message, replyTo) { | |
219 server.dispatch(message, replyTo); | |
kasperl
2012/08/03 05:20:27
!
| |
220 }); | |
221 } | |
222 | |
223 | |
224 class SocketCloseServer { | |
219 | 225 |
220 static final HOST = "127.0.0.1"; | 226 static final HOST = "127.0.0.1"; |
221 | 227 |
222 SocketCloseServer() : super() {} | 228 SocketCloseServer() : super() {} |
223 | 229 |
224 void main() { | 230 void connectionHandler(ConnectionData data) { |
231 var connection = data.connection; | |
225 | 232 |
226 void connectionHandler(ConnectionData data) { | 233 void readBytes(whenFiveBytes) { |
227 var connection = data.connection; | 234 var read = connection.inputStream.read(); |
228 | 235 data.readBytes += read.length; |
229 void readBytes(whenFiveBytes) { | 236 if (data.readBytes == 5) { |
230 var read = connection.inputStream.read(); | 237 whenFiveBytes(); |
231 data.readBytes += read.length; | |
232 if (data.readBytes == 5) { | |
233 whenFiveBytes(); | |
234 } | |
235 } | |
236 | |
237 void dataHandler() { | |
238 switch (_mode) { | |
239 case 0: | |
240 Expect.fail("No data expected"); | |
241 break; | |
242 case 1: | |
243 readBytes(() { | |
244 _dataEvents++; | |
245 }); | |
246 break; | |
247 case 2: | |
248 readBytes(() { | |
249 _dataEvents++; | |
250 connection.inputStream.close(); | |
251 }); | |
252 break; | |
253 case 3: | |
254 readBytes(() { | |
255 _dataEvents++; | |
256 connection.outputStream.write("Hello".charCodes()); | |
257 connection.outputStream.onNoPendingWrites = () { | |
258 connection.inputStream.close(); | |
259 }; | |
260 }); | |
261 break; | |
262 case 4: | |
263 readBytes(() { | |
264 _dataEvents++; | |
265 connection.outputStream.write("Hello".charCodes()); | |
266 connection.inputStream.close(); | |
267 }); | |
268 break; | |
269 case 5: | |
270 readBytes(() { | |
271 _dataEvents++; | |
272 connection.outputStream.write("Hello".charCodes()); | |
273 }); | |
274 break; | |
275 case 6: | |
276 case 7: | |
277 readBytes(() { | |
278 _dataEvents++; | |
279 connection.outputStream.write("Hello".charCodes()); | |
280 connection.outputStream.onNoPendingWrites = () { | |
281 connection.outputStream.close(); | |
282 }; | |
283 }); | |
284 break; | |
285 case 8: | |
286 readBytes(() { | |
287 _dataEvents++; | |
288 connection.outputStream.write("Hello".charCodes()); | |
289 connection.outputStream.close(); | |
290 }); | |
291 break; | |
292 default: | |
293 Expect.fail("Unknown test mode"); | |
294 } | |
295 } | |
296 | |
297 void closeHandler() { | |
298 _closeEvents++; | |
299 connection.outputStream.close(); | |
300 } | |
301 | |
302 void errorHandler(Exception e) { | |
303 Expect.fail("Socket error $e"); | |
304 } | |
305 | |
306 _iterations++; | |
307 | |
308 connection.inputStream.onData = dataHandler; | |
309 connection.inputStream.onClosed = closeHandler; | |
310 connection.onError = errorHandler; | |
311 } | |
312 | |
313 void errorHandlerServer(Exception e) { | |
314 Expect.fail("Server socket error"); | |
315 } | |
316 | |
317 waitForResult(Timer timer) { | |
318 // Make sure all iterations have been run. In multiple of these | |
319 // scenarios it is possible to get the SERVERSHUTDOWN message | |
320 // before we have received the last close event on the | |
321 // server. In these cases we wait for the correct number of | |
322 // close events. | |
323 if (_iterations == ITERATIONS && | |
324 (_closeEvents == ITERATIONS || | |
325 (_mode == 2 || _mode == 3 || _mode == 4))) { | |
326 switch (_mode) { | |
327 case 0: | |
328 Expect.equals(0, _dataEvents); | |
329 Expect.equals(ITERATIONS, _closeEvents); | |
330 break; | |
331 case 1: | |
332 Expect.equals(ITERATIONS, _dataEvents); | |
333 Expect.equals(ITERATIONS, _closeEvents); | |
334 break; | |
335 case 2: | |
336 case 3: | |
337 case 4: | |
338 Expect.equals(ITERATIONS, _dataEvents); | |
339 Expect.equals(0, _closeEvents); | |
340 break; | |
341 case 5: | |
342 case 6: | |
343 case 7: | |
344 case 8: | |
345 Expect.equals(ITERATIONS, _dataEvents); | |
346 Expect.equals(ITERATIONS, _closeEvents); | |
347 break; | |
348 default: | |
349 Expect.fail("Unknown test mode"); | |
350 } | |
351 Expect.equals(0, _errorEvents); | |
352 _server.close(); | |
353 this.port.close(); | |
354 _donePort.send(null); | |
355 } else { | |
356 new Timer(100, waitForResult); | |
357 } | 238 } |
358 } | 239 } |
359 | 240 |
360 this.port.receive((message, SendPort replyTo) { | 241 void dataHandler() { |
361 _donePort = replyTo; | 242 switch (_mode) { |
362 if (message != SERVERSHUTDOWN) { | 243 case 0: |
363 _readBytes = 0; | 244 Expect.fail("No data expected"); |
364 _errorEvents = 0; | 245 break; |
365 _dataEvents = 0; | 246 case 1: |
366 _closeEvents = 0; | 247 readBytes(() { |
367 _iterations = 0; | 248 _dataEvents++; |
368 _mode = message; | 249 }); |
369 _server = new ServerSocket(HOST, 0, 10); | 250 break; |
370 Expect.equals(true, _server !== null); | 251 case 2: |
371 _server.onConnection = (connection) { | 252 readBytes(() { |
372 var data = new ConnectionData(connection); | 253 _dataEvents++; |
373 connectionHandler(data); | 254 connection.inputStream.close(); |
374 }; | 255 }); |
375 _server.onError = errorHandlerServer; | 256 break; |
376 replyTo.send(_server.port, null); | 257 case 3: |
377 } else { | 258 readBytes(() { |
378 new Timer(0, waitForResult); | 259 _dataEvents++; |
260 connection.outputStream.write("Hello".charCodes()); | |
261 connection.outputStream.onNoPendingWrites = () { | |
262 connection.inputStream.close(); | |
263 }; | |
264 }); | |
265 break; | |
266 case 4: | |
267 readBytes(() { | |
268 _dataEvents++; | |
269 connection.outputStream.write("Hello".charCodes()); | |
270 connection.inputStream.close(); | |
271 }); | |
272 break; | |
273 case 5: | |
274 readBytes(() { | |
275 _dataEvents++; | |
276 connection.outputStream.write("Hello".charCodes()); | |
277 }); | |
278 break; | |
279 case 6: | |
280 case 7: | |
281 readBytes(() { | |
282 _dataEvents++; | |
283 connection.outputStream.write("Hello".charCodes()); | |
284 connection.outputStream.onNoPendingWrites = () { | |
285 connection.outputStream.close(); | |
286 }; | |
287 }); | |
288 break; | |
289 case 8: | |
290 readBytes(() { | |
291 _dataEvents++; | |
292 connection.outputStream.write("Hello".charCodes()); | |
293 connection.outputStream.close(); | |
294 }); | |
295 break; | |
296 default: | |
297 Expect.fail("Unknown test mode"); | |
379 } | 298 } |
380 }); | 299 } |
300 | |
301 void closeHandler() { | |
302 _closeEvents++; | |
303 connection.outputStream.close(); | |
304 } | |
305 | |
306 void errorHandler(Exception e) { | |
307 Expect.fail("Socket error $e"); | |
308 } | |
309 | |
310 _iterations++; | |
311 | |
312 connection.inputStream.onData = dataHandler; | |
313 connection.inputStream.onClosed = closeHandler; | |
314 connection.onError = errorHandler; | |
315 } | |
316 | |
317 void errorHandlerServer(Exception e) { | |
318 Expect.fail("Server socket error"); | |
319 } | |
320 | |
321 waitForResult(Timer timer) { | |
322 // Make sure all iterations have been run. In multiple of these | |
323 // scenarios it is possible to get the SERVERSHUTDOWN message | |
324 // before we have received the last close event on the | |
325 // server. In these cases we wait for the correct number of | |
326 // close events. | |
327 if (_iterations == ITERATIONS && | |
328 (_closeEvents == ITERATIONS || | |
329 (_mode == 2 || _mode == 3 || _mode == 4))) { | |
330 switch (_mode) { | |
331 case 0: | |
332 Expect.equals(0, _dataEvents); | |
333 Expect.equals(ITERATIONS, _closeEvents); | |
334 break; | |
335 case 1: | |
336 Expect.equals(ITERATIONS, _dataEvents); | |
337 Expect.equals(ITERATIONS, _closeEvents); | |
338 break; | |
339 case 2: | |
340 case 3: | |
341 case 4: | |
342 Expect.equals(ITERATIONS, _dataEvents); | |
343 Expect.equals(0, _closeEvents); | |
344 break; | |
345 case 5: | |
346 case 6: | |
347 case 7: | |
348 case 8: | |
349 Expect.equals(ITERATIONS, _dataEvents); | |
350 Expect.equals(ITERATIONS, _closeEvents); | |
351 break; | |
352 default: | |
353 Expect.fail("Unknown test mode"); | |
354 } | |
355 Expect.equals(0, _errorEvents); | |
356 _server.close(); | |
357 port.close(); | |
358 _donePort.send(null); | |
359 } else { | |
360 new Timer(100, waitForResult); | |
361 } | |
362 } | |
363 | |
364 void dispatch(message, replyTo) { | |
365 _donePort = replyTo; | |
366 if (message != SERVERSHUTDOWN) { | |
367 _readBytes = 0; | |
368 _errorEvents = 0; | |
369 _dataEvents = 0; | |
370 _closeEvents = 0; | |
371 _iterations = 0; | |
372 _mode = message; | |
373 _server = new ServerSocket(HOST, 0, 10); | |
374 Expect.equals(true, _server !== null); | |
375 _server.onConnection = (connection) { | |
376 var data = new ConnectionData(connection); | |
377 connectionHandler(data); | |
378 }; | |
379 _server.onError = errorHandlerServer; | |
380 replyTo.send(_server.port, null); | |
381 } else { | |
382 new Timer(0, waitForResult); | |
383 } | |
381 } | 384 } |
382 | 385 |
383 ServerSocket _server; | 386 ServerSocket _server; |
384 SendPort _donePort; | 387 SendPort _donePort; |
385 int _readBytes; | 388 int _readBytes; |
386 int _errorEvents; | 389 int _errorEvents; |
387 int _dataEvents; | 390 int _dataEvents; |
388 int _closeEvents; | 391 int _closeEvents; |
389 int _iterations; | 392 int _iterations; |
390 int _mode; | 393 int _mode; |
(...skipping 16 matching lines...) Expand all Loading... | |
407 var tests = 9; | 410 var tests = 9; |
408 var port = new ReceivePort(); | 411 var port = new ReceivePort(); |
409 var completed = 0; | 412 var completed = 0; |
410 port.receive((message, ignore) { | 413 port.receive((message, ignore) { |
411 if (++completed == tests) port.close(); | 414 if (++completed == tests) port.close(); |
412 }); | 415 }); |
413 for (var i = 0; i < tests; i++) { | 416 for (var i = 0; i < tests; i++) { |
414 new SocketClose.start(i, port.toSendPort()); | 417 new SocketClose.start(i, port.toSendPort()); |
415 } | 418 } |
416 } | 419 } |
OLD | NEW |