| 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 library ServerTest; | 6 library ServerTest; |
| 7 | 7 |
| 8 import "dart:io"; | 8 import "dart:io"; |
| 9 import "dart:isolate"; | 9 import "dart:isolate"; |
| 10 part "testing_server.dart"; | 10 part "testing_server.dart"; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 _sendPort = spawnFunction(startTestServer); | 21 _sendPort = spawnFunction(startTestServer); |
| 22 initialize(); | 22 initialize(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void run() { | 25 void run() { |
| 26 | 26 |
| 27 void connectHandler() { | 27 void connectHandler() { |
| 28 _connections++; | 28 _connections++; |
| 29 if (_connections == CONNECTIONS) { | 29 if (_connections == CONNECTIONS) { |
| 30 for (int i = 0; i < CONNECTIONS; i++) { | 30 for (int i = 0; i < CONNECTIONS; i++) { |
| 31 _sockets[i].close(); | 31 _sockets[i].destroy(); |
| 32 } | 32 } |
| 33 shutdown(); | 33 close(); |
| 34 } | 34 } |
| 35 } | 35 } |
| 36 | 36 |
| 37 for (int i = 0; i < CONNECTIONS; i++) { | 37 for (int i = 0; i < CONNECTIONS; i++) { |
| 38 _sockets[i] = new Socket(TestingServer.HOST, _port); | 38 Socket.connect(TestingServer.HOST, _port).then((socket) { |
| 39 if (_sockets[i] != null) { | 39 Expect.isNotNull(socket); |
| 40 _sockets[i].onConnect = connectHandler; | 40 _sockets[i] = socket; |
| 41 } else { | 41 connectHandler(); |
| 42 Expect.fail("socket creation failed"); | 42 }); |
| 43 } | |
| 44 } | 43 } |
| 45 } | 44 } |
| 46 | 45 |
| 47 void initialize() { | 46 void initialize() { |
| 48 _receivePort.receive((var message, SendPort replyTo) { | 47 _receivePort.receive((var message, SendPort replyTo) { |
| 49 _port = message; | 48 _port = message; |
| 50 run(); | 49 run(); |
| 51 }); | 50 }); |
| 52 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | 51 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void shutdown() { | 54 void close() { |
| 56 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | 55 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); |
| 57 _receivePort.close(); | 56 _receivePort.close(); |
| 58 } | 57 } |
| 59 | 58 |
| 60 int _port; | 59 int _port; |
| 61 ReceivePort _receivePort; | 60 ReceivePort _receivePort; |
| 62 SendPort _sendPort; | 61 SendPort _sendPort; |
| 63 List<Socket> _sockets; | 62 List<Socket> _sockets; |
| 64 int _connections; | 63 int _connections; |
| 65 } | 64 } |
| 66 | 65 |
| 67 | 66 |
| 68 void startTestServer() { | 67 void startTestServer() { |
| 69 var server = new TestServer(); | 68 var server = new TestServer(); |
| 70 port.receive(server.dispatch); | 69 port.receive(server.dispatch); |
| 71 } | 70 } |
| 72 | 71 |
| 73 class TestServer extends TestingServer { | 72 class TestServer extends TestingServer { |
| 74 | 73 |
| 75 void onConnection(Socket connection) { | 74 void onConnection(Socket connection) { |
| 76 Socket _client; | 75 Socket _client; |
| 77 | 76 |
| 78 void closeHandler() { | 77 void closeHandler() { |
| 79 connection.close(); | 78 connection.close(); |
| 80 } | 79 } |
| 81 | 80 |
| 82 void errorHandler(Exception e) { | 81 void errorHandler(e) { |
| 83 print("Socket error $e"); | 82 print("Socket error $e"); |
| 84 connection.close(); | 83 connection.close(); |
| 85 } | 84 } |
| 86 | 85 |
| 87 _connections++; | 86 _connections++; |
| 88 connection.onClosed = closeHandler; | 87 connection.listen( |
| 89 connection.onError = errorHandler; | 88 (data) {}, |
| 89 onDone: closeHandler, |
| 90 onError: errorHandler); |
| 90 } | 91 } |
| 91 | 92 |
| 92 int _connections = 0; | 93 int _connections = 0; |
| 93 } | 94 } |
| 94 | 95 |
| 95 main() { | 96 main() { |
| 96 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); | 97 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); |
| 97 } | 98 } |
| OLD | NEW |