tst_QNetworkReply: check whether we actually use six TCP connections in HTTP/1

Pick-to: 6.4 6.2
Task-number: QTBUG-25280
Task-number: QTBUG-108215
Change-Id: I5f94866e30f08465943922e3dee5150f37054225
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Markus Goetz 2022-11-28 11:06:35 +01:00 committed by Marc Mutz
parent b3b15f4caf
commit 9329e4e8c7

View File

@ -64,6 +64,8 @@
Q_DECLARE_METATYPE(QSharedPointer<char>)
#endif
#include <memory>
#ifdef Q_OS_UNIX
# include <sys/types.h>
# include <unistd.h> // for getuid()
@ -443,6 +445,8 @@ private Q_SLOTS:
void varyingCacheExpiry_data();
void varyingCacheExpiry();
void amountOfHttp1ConnectionsQtbug25280();
void dontInsertPartialContentIntoTheCache();
void httpUserAgent();
@ -8102,6 +8106,48 @@ void tst_QNetworkReply::varyingCacheExpiry()
QVERIFY(success);
}
class Qtbug25280Server : public MiniHttpServer
{
public:
Qtbug25280Server(QByteArray qba) : MiniHttpServer(qba, false) {}
QSet<QTcpSocket*> receivedSockets;
virtual void reply()
{
// Save sockets in a list
receivedSockets.insert((QTcpSocket*)sender());
qobject_cast<QTcpSocket*>(sender())->write(dataToTransmit);
//qDebug() << "count=" << receivedSockets.count();
}
};
// Also kind of QTBUG-8468
void tst_QNetworkReply::amountOfHttp1ConnectionsQtbug25280()
{
const int amount = 6;
QNetworkAccessManager manager; // function local instance
Qtbug25280Server server(tst_QNetworkReply::httpEmpty200Response);
server.doClose = false;
server.multiple = true;
QUrl url(QLatin1String("http://127.0.0.1")); // not "localhost" to prevent "Happy Eyeballs"
// from skewing the counting
url.setPort(server.serverPort());
constexpr int NumRequests = 200; // send a lot more than we have sockets
int finished = 0;
std::array<std::unique_ptr<QNetworkReply>, NumRequests> replies;
for (auto &reply : replies) {
QNetworkRequest request(url);
reply.reset(manager.get(request));
QObject::connect(reply.get(), &QNetworkReply::finished,
[&finished] { ++finished; });
}
QTRY_COMPARE_WITH_TIMEOUT(finished, NumRequests, 60'000);
for (const auto &reply : replies) {
QCOMPARE(reply->error(), QNetworkReply::NoError);
QCOMPARE(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(), 200);
}
QCOMPARE(server.receivedSockets.size(), amount);
}
void tst_QNetworkReply::dontInsertPartialContentIntoTheCache()
{
QByteArray reply206 =