summaryrefslogtreecommitdiffstats
path: root/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp
diff options
context:
space:
mode:
authorMårten Nordheim <marten.nordheim@qt.io>2024-01-23 17:32:39 +0100
committerMårten Nordheim <marten.nordheim@qt.io>2024-02-15 16:27:19 +0100
commit775a57f26682867e183d26863d85eb22a563cdbd (patch)
treeaeaed8543cb50d80ab3413b2afe657ac1ef74235 /tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp
parentde3d5dd73b748bc1591447fe81dff6427a86f77b (diff)
tst_QNetworkReply: Try to stabilize qtbug68821proxyError
The test is a bit silly, it was originally written to make sure that we produce meaningful errors when trying to connect to a proxy server where the connection is refused or the server doesn't respond at all. To test that, it creates a local QTcpServer and starts listening to any free port (by specifying port 0) and then it closed the server and uses the address-port of localhost:serverPort as the proxy to use, since we know it _was_ unused, since we were able to bind to it. However, just calling close() doesn't immediately tear down the internal socket descriptor, so the OS may still have the port reserved for some time. By moving the QTcpServer to a narrower scope we will quickly destroy it and the internal socket engine, which is parented to the server, and this in turn releases the socket descriptor. Pick-to: 6.7 6.6 6.5 Change-Id: If12128fc21d1f545df152f08f0d52c1b14ac6037 Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Diffstat (limited to 'tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp')
-rw-r--r--tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp17
1 files changed, 12 insertions, 5 deletions
diff --git a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp
index 30cf6b60e9..8d2a5b7a61 100644
--- a/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp
+++ b/tests/auto/network/access/qnetworkreply/tst_qnetworkreply.cpp
@@ -10392,13 +10392,20 @@ void tst_QNetworkReply::qtbug68821proxyError_data()
void tst_QNetworkReply::qtbug68821proxyError()
{
- QTcpServer proxyServer;
- QVERIFY(proxyServer.listen());
- quint16 proxyPort = proxyServer.serverPort();
- proxyServer.close();
+ auto getUnusedPort = []() -> std::optional<quint16> {
+ QTcpServer probeServer;
+ if (!probeServer.listen())
+ return std::nullopt;
+ // If we can listen on it, it was unused, and hopefully is also
+ // still unused after we stop listening.
+ return probeServer.serverPort();
+ };
+
+ auto proxyPort = getUnusedPort();
+ QVERIFY(proxyPort);
QFETCH(QString, proxyHost);
- QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort);
+ QNetworkProxy proxy(QNetworkProxy::HttpProxy, proxyHost, proxyPort.value());
manager.setProxy(proxy);