OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 // | |
5 // Test creating a large number of socket connections. | |
6 | |
7 #import("dart:io"); | |
8 #import("dart:isolate"); | |
9 #source("TestingServer.dart"); | |
10 | |
11 final CONNECTIONS = 200; | |
12 | |
13 class SocketManyConnectionsTest { | |
14 | |
15 SocketManyConnectionsTest.start() | |
16 : _receivePort = new ReceivePort(), | |
17 _sendPort = null, | |
18 _connections = 0, | |
19 _sockets = new List<Socket>(CONNECTIONS) { | |
20 new TestServer().spawn().then((SendPort port) { | |
21 _sendPort = port; | |
22 start(); | |
23 }); | |
24 } | |
25 | |
26 void run() { | |
27 | |
28 void connectHandler() { | |
29 _connections++; | |
30 if (_connections == CONNECTIONS) { | |
31 for (int i = 0; i < CONNECTIONS; i++) { | |
32 _sockets[i].close(); | |
33 } | |
34 shutdown(); | |
35 } | |
36 } | |
37 | |
38 for (int i = 0; i < CONNECTIONS; i++) { | |
39 _sockets[i] = new Socket(TestingServer.HOST, _port); | |
40 if (_sockets[i] !== null) { | |
41 _sockets[i].onConnect = connectHandler; | |
42 } else { | |
43 Expect.fail("socket creation failed"); | |
44 } | |
45 } | |
46 } | |
47 | |
48 void start() { | |
49 _receivePort.receive((var message, SendPort replyTo) { | |
50 _port = message; | |
51 run(); | |
52 }); | |
53 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
54 } | |
55 | |
56 void shutdown() { | |
57 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | |
58 _receivePort.close(); | |
59 } | |
60 | |
61 int _port; | |
62 ReceivePort _receivePort; | |
63 SendPort _sendPort; | |
64 List<Socket> _sockets; | |
65 int _connections; | |
66 } | |
67 | |
68 class TestServer extends TestingServer { | |
69 | |
70 void onConnection(Socket connection) { | |
71 Socket _client; | |
72 | |
73 void closeHandler() { | |
74 connection.close(); | |
75 } | |
76 | |
77 void errorHandler(Exception e) { | |
78 print("Socket error $e"); | |
79 connection.close(); | |
80 } | |
81 | |
82 _connections++; | |
83 connection.onClosed = closeHandler; | |
84 connection.onError = errorHandler; | |
85 } | |
86 | |
87 int _connections = 0; | |
88 } | |
89 | |
90 main() { | |
91 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); | |
92 } | |
OLD | NEW |