| OLD | NEW |
| 1 // Copyright (c) 2013, 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 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
| 11 import "dart:async"; | 11 import "dart:async"; |
| 12 import "dart:io"; | 12 import "dart:io"; |
| 13 | 13 |
| 14 | 14 |
| 15 void testPing(int totalConnections) { | 15 void testPing(int totalConnections) { |
| 16 HttpServer.bind('localhost', 0).then((server) { | 16 HttpServer.bind('localhost', 0).then((server) { |
| 17 // The completers will be completed when the pingInterval have terminated | |
| 18 // the webSockets. | |
| 19 var completers = new List.generate( | |
| 20 totalConnections, (_) => new Completer()); | |
| 21 | |
| 22 int conns = 0; | |
| 23 server.transform(new WebSocketTransformer()).listen((webSocket) { | 17 server.transform(new WebSocketTransformer()).listen((webSocket) { |
| 24 int i = conns++; | 18 webSocket.pingInterval = const Duration(milliseconds: 500); |
| 25 webSocket.pingInterval = const Duration(milliseconds: 100); | |
| 26 webSocket.drain(); | 19 webSocket.drain(); |
| 27 var timer = new Timer.periodic(const Duration(milliseconds: 10), (_) { | |
| 28 webSocket.add(new List.filled(10 * 1024, 0)); | |
| 29 }); | |
| 30 webSocket.done.then((_) { | |
| 31 completers[i].complete(); | |
| 32 timer.cancel(); | |
| 33 Expect.equals(WebSocketStatus.GOING_AWAY, webSocket.closeCode); | |
| 34 webSocket.close(); | |
| 35 }); | |
| 36 }); | 20 }); |
| 37 | 21 |
| 38 var futures = []; | 22 var futures = []; |
| 39 for (int i = 0; i < totalConnections; i++) { | 23 for (int i = 0; i < totalConnections; i++) { |
| 40 futures.add( | 24 futures.add( |
| 41 WebSocket.connect('ws://localhost:${server.port}') | 25 WebSocket.connect('ws://localhost:${server.port}').then((webSocket) { |
| 42 .then((webSocket) { | 26 webSocket.pingInterval = const Duration(milliseconds: 500); |
| 43 // Don't drain yet, to block data. | 27 webSocket.drain(); |
| 44 // Once the server is done, drain the peers. | 28 new Timer(const Duration(seconds: 2), () { |
| 45 return Future.wait(completers.map((c) => c.future)) | 29 // Should not be closed yet. |
| 46 .then((_) => webSocket.drain()); | 30 Expect.equals(null, webSocket.closeCode); |
| 47 })); | 31 webSocket.close(); |
| 32 }); |
| 33 return webSocket.done; |
| 34 })); |
| 48 } | 35 } |
| 49 Future.wait(futures).then((_) => server.close()); | 36 Future.wait(futures).then((_) => server.close()); |
| 50 }); | 37 }); |
| 51 } | 38 } |
| 52 | 39 |
| 53 | 40 |
| 54 void main() { | 41 void main() { |
| 55 testPing(10); | 42 testPing(10); |
| 56 } | 43 } |
| OLD | NEW |