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 "dart:async"; | 10 import "dart:async"; |
10 import "dart:io"; | 11 import "dart:io"; |
11 import "dart:isolate"; | 12 import "dart:isolate"; |
12 part "testing_server.dart"; | 13 part "testing_server.dart"; |
13 | 14 |
14 const CONNECTIONS = 200; | 15 const CONNECTIONS = 200; |
15 | 16 |
16 class SocketManyConnectionsTest { | 17 class SocketManyConnectionsTest { |
17 | 18 |
18 SocketManyConnectionsTest.start() | 19 SocketManyConnectionsTest.start() |
19 : _receivePort = new ReceivePort(), | 20 : _connections = 0, |
20 _sendPort = null, | |
21 _connections = 0, | |
22 _sockets = new List<Socket>(CONNECTIONS) { | 21 _sockets = new List<Socket>(CONNECTIONS) { |
23 _sendPort = spawnFunction(startTestServer); | |
24 initialize(); | 22 initialize(); |
25 } | 23 } |
26 | 24 |
27 void run() { | 25 void run() { |
28 | 26 |
29 void connectHandler() { | 27 void connectHandler() { |
30 _connections++; | 28 _connections++; |
31 if (_connections == CONNECTIONS) { | 29 if (_connections == CONNECTIONS) { |
32 for (int i = 0; i < CONNECTIONS; i++) { | 30 for (int i = 0; i < CONNECTIONS; i++) { |
33 _sockets[i].destroy(); | 31 _sockets[i].destroy(); |
34 } | 32 } |
35 close(); | 33 close(); |
36 } | 34 } |
37 } | 35 } |
38 | 36 |
39 for (int i = 0; i < CONNECTIONS; i++) { | 37 for (int i = 0; i < CONNECTIONS; i++) { |
40 Socket.connect(TestingServer.HOST, _port).then((socket) { | 38 Socket.connect(TestingServer.HOST, _port).then((socket) { |
41 Expect.isNotNull(socket); | 39 Expect.isNotNull(socket); |
42 _sockets[i] = socket; | 40 _sockets[i] = socket; |
43 connectHandler(); | 41 connectHandler(); |
44 }); | 42 }); |
45 } | 43 } |
46 } | 44 } |
47 | 45 |
48 void initialize() { | 46 void initialize() { |
49 _receivePort.receive((var message, SendPort replyTo) { | 47 var receivePort = new ReceivePort(); |
50 _port = message; | 48 var remote = Isolate.spawn(startTestServer, receivePort.sendPort); |
| 49 receivePort.first.then((msg) { |
| 50 this._port = msg[0]; |
| 51 this._closeSendPort = msg[1]; |
51 run(); | 52 run(); |
52 }); | 53 }); |
53 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
54 } | 54 } |
55 | 55 |
56 void close() { | 56 void close() { |
57 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | 57 _closeSendPort.send(null); |
58 _receivePort.close(); | 58 asyncEnd(); |
59 } | 59 } |
60 | 60 |
61 int _port; | 61 int _port; |
62 ReceivePort _receivePort; | 62 SendPort _closeSendPort; |
63 SendPort _sendPort; | |
64 List<Socket> _sockets; | 63 List<Socket> _sockets; |
65 int _connections; | 64 int _connections; |
66 } | 65 } |
67 | 66 |
68 | 67 |
69 void startTestServer() { | 68 void startTestServer(SendPort replyPort) { |
70 var server = new TestServer(); | 69 var server = new TestServer(); |
71 port.receive(server.dispatch); | 70 server.init().then((port) { |
| 71 replyPort.send([port, server.closeSendPort]); |
| 72 }); |
72 } | 73 } |
73 | 74 |
74 class TestServer extends TestingServer { | 75 class TestServer extends TestingServer { |
75 | 76 |
76 void onConnection(Socket connection) { | 77 void onConnection(Socket connection) { |
77 Socket _client; | 78 Socket _client; |
78 | 79 |
79 void closeHandler() { | 80 void closeHandler() { |
80 connection.close(); | 81 connection.close(); |
81 } | 82 } |
(...skipping 10 matching lines...) Expand all Loading... |
92 connection.listen( | 93 connection.listen( |
93 (data) {}, | 94 (data) {}, |
94 onDone: closeHandler, | 95 onDone: closeHandler, |
95 onError: errorHandler); | 96 onError: errorHandler); |
96 } | 97 } |
97 | 98 |
98 int _connections = 0; | 99 int _connections = 0; |
99 } | 100 } |
100 | 101 |
101 main() { | 102 main() { |
| 103 asyncStart(); |
102 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); | 104 SocketManyConnectionsTest test = new SocketManyConnectionsTest.start(); |
103 } | 105 } |
OLD | NEW |