From c847a7bf437f5c01cbd3ee95b86335ed6dcc8e46 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Wed, 3 Dec 2025 13:24:32 -0800 Subject: [PATCH] test: Fix many_pending_secure_sockets_test timeouts. Bug:https://github.com/dart-lang/sdk/issues/62064 Change-Id: Ie3aafd9549d7c26494faa7dddba85025e4b2cb69 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465462 Commit-Queue: Brian Quinlan Reviewed-by: Ryan Macnak --- .../io/many_pending_secure_sockets_test.dart | 91 +++++++++++++++---- tests/standalone/standalone.status | 1 + 2 files changed, 75 insertions(+), 17 deletions(-) diff --git a/tests/standalone/io/many_pending_secure_sockets_test.dart b/tests/standalone/io/many_pending_secure_sockets_test.dart index ef0b66cc1c7..91d9bb9573d 100644 --- a/tests/standalone/io/many_pending_secure_sockets_test.dart +++ b/tests/standalone/io/many_pending_secure_sockets_test.dart @@ -1,30 +1,87 @@ // Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +// +// VMOptions= +// VMOptions=--short_socket_read +// VMOptions=--short_socket_write +// VMOptions=--short_socket_read --short_socket_write +// OtherResources=certificates/server_chain.pem +// OtherResources=certificates/server_key.pem +// OtherResources=certificates/trusted_certs.pem -// https://github.com/flutter/flutter/issues/170723 +// Verifies that a large number of secure sockets can be connected +// simultaneously. +// +// See https://github.com/flutter/flutter/issues/170723 +import 'dart:async'; import "dart:io"; -test(int i) async { - try { - var socket = await RawSecureSocket.connect("www.google.com", 443); - await Future.delayed( - Duration(seconds: 6), // More than the thread pool timeout. - ); - socket.close(); - } catch (e, st) { - // Ignore failures from the remote side rejecting/closing the connection. - if (!e.toString().contains("Connection reset by peer")) { - rethrow; - } - } +// Should be more than the number of threads that the OS can easily create. +const numConnections = 2000; + +String localFile(path) => Platform.script.resolve(path).toFilePath(); + +SecurityContext serverContext = new SecurityContext() + ..useCertificateChain(localFile('certificates/server_chain.pem')) + ..usePrivateKey( + localFile('certificates/server_key.pem'), + password: 'dartdart', + ); +SecurityContext clientContext = new SecurityContext() + ..setTrustedCertificates(localFile('certificates/trusted_certs.pem')); + +Future startServer() async { + final server = await SecureServerSocket.bind( + InternetAddress.loopbackIPv4, + 0, + serverContext, + ); + server.listen((SecureSocket client) async { + client.write('Connected!'); + await client.flush(); + client.listen((_) {}, onDone: () => client.close()); + }); + return server; +} + +Future connectSocket(int port) async { + final socket = await RawSecureSocket.connect( + InternetAddress.loopbackIPv4, + port, + context: clientContext, + ); + await socket.firstWhere((e) => e == RawSocketEvent.read); + return socket; } main() async { - var tests = []; - for (var i = 0; i < 2000; i++) { - tests.add(test(i)); + final server = await startServer(); + final tests = []; + final allConnected = Completer(); + + for (var i = 0; i < numConnections;) { + // Performing thousands of simultaneous TLS connections can result in + // timeouts. So connect 20 sockets at a time. + final socketConnections = >[]; + for (var j = 0; i < numConnections && j < 20; i++, j++) { + socketConnections.add(connectSocket(server.port)); + } + + for (var socket in await Future.wait(socketConnections)) { + Future delayAndClose() async { + await allConnected.future; + await Future.delayed( + Duration(seconds: 6), // More than the thread pool timeout. + ); + socket.close(); + } + + tests.add(delayAndClose()); + } } + allConnected.complete(); await Future.wait(tests); + server.close(); } diff --git a/tests/standalone/standalone.status b/tests/standalone/standalone.status index e73f83a408f..e1cc2f7e3fc 100644 --- a/tests/standalone/standalone.status +++ b/tests/standalone/standalone.status @@ -8,6 +8,7 @@ io/http_close_stack_overflow_test: Skip # The test is heavy loaded. Should be used for manual test. io/http_linklocal_ipv6_test: SkipByDesign # This needs manual test. io/large_file_read_small_file_test: Slow, Pass # Test reads small file 1M times +io/many_pending_secure_sockets_test: Slow, Pass # Creates many TLS connections. io/non_utf8_directory_test: Skip # Issue 33519. Temp files causing bots to go purple. io/non_utf8_file_test: Skip # Issue 33519. Temp files causing bots to go purple. io/non_utf8_link_test: Skip # Issue 33519. Temp files causing bots to go purple.