|
1 | 1 | // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file |
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | +// |
| 5 | +// VMOptions= |
| 6 | +// VMOptions=--short_socket_read |
| 7 | +// VMOptions=--short_socket_write |
| 8 | +// VMOptions=--short_socket_read --short_socket_write |
| 9 | +// OtherResources=certificates/server_chain.pem |
| 10 | +// OtherResources=certificates/server_key.pem |
| 11 | +// OtherResources=certificates/trusted_certs.pem |
4 | 12 |
|
5 | | -// https://github.com/flutter/flutter/issues/170723 |
| 13 | +// Verifies that a large number of secure sockets can be connected |
| 14 | +// simultaneously. |
| 15 | +// |
| 16 | +// See https://github.com/flutter/flutter/issues/170723 |
6 | 17 |
|
| 18 | +import 'dart:async'; |
7 | 19 | import "dart:io"; |
8 | 20 |
|
9 | | -test(int i) async { |
10 | | - try { |
11 | | - var socket = await RawSecureSocket.connect("www.google.com", 443); |
12 | | - await Future.delayed( |
13 | | - Duration(seconds: 6), // More than the thread pool timeout. |
14 | | - ); |
15 | | - socket.close(); |
16 | | - } catch (e, st) { |
17 | | - // Ignore failures from the remote side rejecting/closing the connection. |
18 | | - if (!e.toString().contains("Connection reset by peer")) { |
19 | | - rethrow; |
20 | | - } |
21 | | - } |
| 21 | +// Should be more than the number of threads that the OS can easily create. |
| 22 | +const numConnections = 2000; |
| 23 | + |
| 24 | +String localFile(path) => Platform.script.resolve(path).toFilePath(); |
| 25 | + |
| 26 | +SecurityContext serverContext = new SecurityContext() |
| 27 | + ..useCertificateChain(localFile('certificates/server_chain.pem')) |
| 28 | + ..usePrivateKey( |
| 29 | + localFile('certificates/server_key.pem'), |
| 30 | + password: 'dartdart', |
| 31 | + ); |
| 32 | +SecurityContext clientContext = new SecurityContext() |
| 33 | + ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); |
| 34 | + |
| 35 | +Future<SecureServerSocket> startServer() async { |
| 36 | + final server = await SecureServerSocket.bind( |
| 37 | + InternetAddress.loopbackIPv4, |
| 38 | + 0, |
| 39 | + serverContext, |
| 40 | + ); |
| 41 | + server.listen((SecureSocket client) async { |
| 42 | + client.write('Connected!'); |
| 43 | + await client.flush(); |
| 44 | + client.listen((_) {}, onDone: () => client.close()); |
| 45 | + }); |
| 46 | + return server; |
| 47 | +} |
| 48 | + |
| 49 | +Future<RawSecureSocket> connectSocket(int port) async { |
| 50 | + final socket = await RawSecureSocket.connect( |
| 51 | + InternetAddress.loopbackIPv4, |
| 52 | + port, |
| 53 | + context: clientContext, |
| 54 | + ); |
| 55 | + await socket.firstWhere((e) => e == RawSocketEvent.read); |
| 56 | + return socket; |
22 | 57 | } |
23 | 58 |
|
24 | 59 | main() async { |
25 | | - var tests = <Future>[]; |
26 | | - for (var i = 0; i < 2000; i++) { |
27 | | - tests.add(test(i)); |
| 60 | + final server = await startServer(); |
| 61 | + final tests = <Future>[]; |
| 62 | + final allConnected = Completer<void>(); |
| 63 | + |
| 64 | + for (var i = 0; i < numConnections;) { |
| 65 | + // Performing thousands of simultaneous TLS connections can result in |
| 66 | + // timeouts. So connect 20 sockets at a time. |
| 67 | + final socketConnections = <Future<RawSecureSocket>>[]; |
| 68 | + for (var j = 0; i < numConnections && j < 20; i++, j++) { |
| 69 | + socketConnections.add(connectSocket(server.port)); |
| 70 | + } |
| 71 | + |
| 72 | + for (var socket in await Future.wait(socketConnections)) { |
| 73 | + Future<void> delayAndClose() async { |
| 74 | + await allConnected.future; |
| 75 | + await Future.delayed( |
| 76 | + Duration(seconds: 6), // More than the thread pool timeout. |
| 77 | + ); |
| 78 | + socket.close(); |
| 79 | + } |
| 80 | + |
| 81 | + tests.add(delayAndClose()); |
| 82 | + } |
28 | 83 | } |
| 84 | + allConnected.complete(); |
29 | 85 | await Future.wait(tests); |
| 86 | + server.close(); |
30 | 87 | } |
0 commit comments