OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 // |
| 5 // Test creating a large number of socket connections. |
| 6 |
| 7 final SERVERINIT = 0; |
| 8 final SERVERSHUTDOWN = -1; |
| 9 final CONNECTIONS = 200; |
| 10 final HOST = "127.0.0.1"; |
| 11 |
| 12 class SocketManyConnectionsTest { |
| 13 |
| 14 SocketManyConnectionsTest.start() |
| 15 : _receivePort = new ReceivePort(), |
| 16 _sendPort = null, |
| 17 _connections = 0, |
| 18 _sockets = new List<Socket>(CONNECTIONS) { |
| 19 new TestServer().spawn().then((SendPort port) { |
| 20 _sendPort = port; |
| 21 start(); |
| 22 }); |
| 23 } |
| 24 |
| 25 void run() { |
| 26 |
| 27 void connectHandler() { |
| 28 _connections++; |
| 29 if (_connections == CONNECTIONS) { |
| 30 for (int i = 0; i < CONNECTIONS; i++) { |
| 31 _sockets[i].close(); |
| 32 } |
| 33 shutdown(); |
| 34 } |
| 35 } |
| 36 |
| 37 for (int i = 0; i < CONNECTIONS; i++) { |
| 38 _sockets[i] = new Socket(HOST, _port); |
| 39 if (_sockets[i] !== null) { |
| 40 _sockets[i].setConnectHandler(connectHandler); |
| 41 } else { |
| 42 Expect.fail("socket creation failed"); |
| 43 } |
| 44 } |
| 45 } |
| 46 |
| 47 void start() { |
| 48 _receivePort.receive((var message, SendPort replyTo) { |
| 49 _port = message; |
| 50 run(); |
| 51 }); |
| 52 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); |
| 53 } |
| 54 |
| 55 void shutdown() { |
| 56 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); |
| 57 _receivePort.close(); |
| 58 } |
| 59 |
| 60 int _port; |
| 61 ReceivePort _receivePort; |
| 62 SendPort _sendPort; |
| 63 List<Socket> _sockets; |
| 64 int _connections; |
| 65 } |
| 66 |
| 67 class TestServer extends Isolate { |
| 68 |
| 69 void main() { |
| 70 |
| 71 void connectionHandler() { |
| 72 Socket _client; |
| 73 |
| 74 void closeHandler() { |
| 75 _client.close(); |
| 76 } |
| 77 |
| 78 void errorHandler() { |
| 79 print("Socket error"); |
| 80 _client.close(); |
| 81 } |
| 82 |
| 83 _client = _server.accept(); |
| 84 _connections++; |
| 85 _client.setCloseHandler(closeHandler); |
| 86 _client.setErrorHandler(errorHandler); |
| 87 } |
| 88 |
| 89 void errorHandlerServer() { |
| 90 print("Server socket error"); |
| 91 _server.close(); |
| 92 } |
| 93 |
| 94 this.port.receive((message, SendPort replyTo) { |
| 95 if (message == SERVERINIT) { |
| 96 _server = new ServerSocket(HOST, 0, 10); |
| 97 Expect.equals(true, _server !== null); |
| 98 _server.setConnectionHandler(connectionHandler); |
| 99 _server.setErrorHandler(errorHandlerServer); |
| 100 replyTo.send(_server.port, null); |
| 101 } else if (message == SERVERSHUTDOWN) { |
| 102 _server.close(); |
| 103 this.port.close(); |
| 104 } |
| 105 }); |
| 106 } |
| 107 |
| 108 ServerSocket _server; |
| 109 int _connections = 0; |
| 110 } |
| 111 |
| 112 main() { |
| 113 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); |
| 114 } |
OLD | NEW |