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 "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import "dart:async"; | 10 import "dart:async"; |
11 import "dart:io"; | 11 import "dart:io"; |
12 import "dart:isolate"; | 12 import "dart:isolate"; |
13 part "testing_server.dart"; | 13 part "testing_server.dart"; |
14 | 14 |
15 const CONNECTIONS = 200; | 15 const CONNECTIONS = 200; |
16 | 16 |
17 class SocketManyConnectionsTest { | 17 class SocketManyConnectionsTest { |
18 | |
19 SocketManyConnectionsTest.start() | 18 SocketManyConnectionsTest.start() |
20 : _connections = 0, | 19 : _connections = 0, |
21 _sockets = new List<Socket>(CONNECTIONS) { | 20 _sockets = new List<Socket>(CONNECTIONS) { |
22 initialize(); | 21 initialize(); |
23 } | 22 } |
24 | 23 |
25 void run() { | 24 void run() { |
26 | |
27 void connectHandler() { | 25 void connectHandler() { |
28 _connections++; | 26 _connections++; |
29 if (_connections == CONNECTIONS) { | 27 if (_connections == CONNECTIONS) { |
30 for (int i = 0; i < CONNECTIONS; i++) { | 28 for (int i = 0; i < CONNECTIONS; i++) { |
31 _sockets[i].destroy(); | 29 _sockets[i].destroy(); |
32 } | 30 } |
33 close(); | 31 close(); |
34 } | 32 } |
35 } | 33 } |
36 | 34 |
(...skipping 20 matching lines...) Expand all Loading... |
57 _closeSendPort.send(null); | 55 _closeSendPort.send(null); |
58 asyncEnd(); | 56 asyncEnd(); |
59 } | 57 } |
60 | 58 |
61 int _port; | 59 int _port; |
62 SendPort _closeSendPort; | 60 SendPort _closeSendPort; |
63 List<Socket> _sockets; | 61 List<Socket> _sockets; |
64 int _connections; | 62 int _connections; |
65 } | 63 } |
66 | 64 |
67 | |
68 void startTestServer(SendPort replyPort) { | 65 void startTestServer(SendPort replyPort) { |
69 var server = new TestServer(); | 66 var server = new TestServer(); |
70 server.init().then((port) { | 67 server.init().then((port) { |
71 replyPort.send([port, server.closeSendPort]); | 68 replyPort.send([port, server.closeSendPort]); |
72 }); | 69 }); |
73 } | 70 } |
74 | 71 |
75 class TestServer extends TestingServer { | 72 class TestServer extends TestingServer { |
76 | |
77 void onConnection(Socket connection) { | 73 void onConnection(Socket connection) { |
78 Socket _client; | 74 Socket _client; |
79 | 75 |
80 void closeHandler() { | 76 void closeHandler() { |
81 connection.close(); | 77 connection.close(); |
82 } | 78 } |
83 | 79 |
84 void errorHandler(e, trace) { | 80 void errorHandler(e, trace) { |
85 String msg = "Socket error $e"; | 81 String msg = "Socket error $e"; |
86 if (trace != null) msg += "\nStackTrace: $trace"; | 82 if (trace != null) msg += "\nStackTrace: $trace"; |
87 print(msg); | 83 print(msg); |
88 connection.close(); | 84 connection.close(); |
89 } | 85 } |
90 | 86 |
91 _connections++; | 87 _connections++; |
92 connection.listen( | 88 connection.listen((data) {}, onDone: closeHandler, onError: errorHandler); |
93 (data) {}, | |
94 onDone: closeHandler, | |
95 onError: errorHandler); | |
96 } | 89 } |
97 | 90 |
98 int _connections = 0; | 91 int _connections = 0; |
99 } | 92 } |
100 | 93 |
101 main() { | 94 main() { |
102 asyncStart(); | 95 asyncStart(); |
103 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); | 96 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); |
104 } | 97 } |
OLD | NEW |