| 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 // Test creating a large number of socket connections. | 5 // Test creating a large number of socket connections. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 #source("testing_server.dart"); | 9 #source("testing_server.dart"); |
| 10 | 10 |
| 11 final CONNECTIONS = 200; | 11 final CONNECTIONS = 200; |
| 12 | 12 |
| 13 class SocketManyConnectionsTest { | 13 class SocketManyConnectionsTest { |
| 14 | 14 |
| 15 SocketManyConnectionsTest.start() | 15 SocketManyConnectionsTest.start() |
| 16 : _receivePort = new ReceivePort(), | 16 : _receivePort = new ReceivePort(), |
| 17 _sendPort = null, | 17 _sendPort = null, |
| 18 _connections = 0, | 18 _connections = 0, |
| 19 _sockets = new List<Socket>(CONNECTIONS) { | 19 _sockets = new List<Socket>(CONNECTIONS) { |
| 20 new TestServer().spawn().then((SendPort port) { | 20 _sendPort = spawnFunction(startTestServer); |
| 21 _sendPort = port; | 21 start(); |
| 22 start(); | |
| 23 }); | |
| 24 } | 22 } |
| 25 | 23 |
| 26 void run() { | 24 void run() { |
| 27 | 25 |
| 28 void connectHandler() { | 26 void connectHandler() { |
| 29 _connections++; | 27 _connections++; |
| 30 if (_connections == CONNECTIONS) { | 28 if (_connections == CONNECTIONS) { |
| 31 for (int i = 0; i < CONNECTIONS; i++) { | 29 for (int i = 0; i < CONNECTIONS; i++) { |
| 32 _sockets[i].close(); | 30 _sockets[i].close(); |
| 33 } | 31 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 58 _receivePort.close(); | 56 _receivePort.close(); |
| 59 } | 57 } |
| 60 | 58 |
| 61 int _port; | 59 int _port; |
| 62 ReceivePort _receivePort; | 60 ReceivePort _receivePort; |
| 63 SendPort _sendPort; | 61 SendPort _sendPort; |
| 64 List<Socket> _sockets; | 62 List<Socket> _sockets; |
| 65 int _connections; | 63 int _connections; |
| 66 } | 64 } |
| 67 | 65 |
| 66 |
| 67 void startTestServer() { |
| 68 var server = new TestServer(); |
| 69 port.receive(server.dispatch); |
| 70 } |
| 71 |
| 68 class TestServer extends TestingServer { | 72 class TestServer extends TestingServer { |
| 69 | 73 |
| 70 void onConnection(Socket connection) { | 74 void onConnection(Socket connection) { |
| 71 Socket _client; | 75 Socket _client; |
| 72 | 76 |
| 73 void closeHandler() { | 77 void closeHandler() { |
| 74 connection.close(); | 78 connection.close(); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void errorHandler(Exception e) { | 81 void errorHandler(Exception e) { |
| 78 print("Socket error $e"); | 82 print("Socket error $e"); |
| 79 connection.close(); | 83 connection.close(); |
| 80 } | 84 } |
| 81 | 85 |
| 82 _connections++; | 86 _connections++; |
| 83 connection.onClosed = closeHandler; | 87 connection.onClosed = closeHandler; |
| 84 connection.onError = errorHandler; | 88 connection.onError = errorHandler; |
| 85 } | 89 } |
| 86 | 90 |
| 87 int _connections = 0; | 91 int _connections = 0; |
| 88 } | 92 } |
| 89 | 93 |
| 90 main() { | 94 main() { |
| 91 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); | 95 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); |
| 92 } | 96 } |
| OLD | NEW |