From cce649b65d31353754af9e34148c0e8d4068d3cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCri=20Valdmann?= Date: Thu, 8 Mar 2018 17:37:03 +0100 Subject: Stabilize tst_qwebenginedownloads The waitForRequest/waitForSignal function used by these tests is broken: it assumes that calling QEventLoop::exit() will immediately exit from the event loop. In actuality this is not immediate and the event loop may continue to execute more signals handlers even after exit() has been called. This means that in e.g. waitForRequest the 'result' variable may be assigned to twice. Additionally there is a race condition in downloadTwoLinks, where we sometimes skip the first download. This is not a bug but simply one pending navigation being aborted in favor of another. Changes - Delete waitForRequest. Define one HTTP request handler per test, using state variables to communicate with the main body of the test. Ignore unknown requests (including favicon requests). Same for downloadRequested signals. - Expand downloadTwoLinks and fix expectations. - Add logging to HTTPServer. - Unblacklist. Task-number: QTBUG-66888 Change-Id: I718cac6c4b32a8cc68400fa8ee5b853686c77fcb Reviewed-by: Peter Varga --- tests/auto/shared/httpserver.cpp | 61 +++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 23 deletions(-) (limited to 'tests/auto/shared/httpserver.cpp') diff --git a/tests/auto/shared/httpserver.cpp b/tests/auto/shared/httpserver.cpp index 6012379f2..8d14c18ff 100644 --- a/tests/auto/shared/httpserver.cpp +++ b/tests/auto/shared/httpserver.cpp @@ -27,44 +27,59 @@ ****************************************************************************/ #include "httpserver.h" -#include "waitforsignal.h" +#include + +Q_LOGGING_CATEGORY(gHttpServerLog, "HttpServer") HttpServer::HttpServer(QObject *parent) : QObject(parent) { connect(&m_tcpServer, &QTcpServer::newConnection, this, &HttpServer::handleNewConnection); - if (!m_tcpServer.listen()) - qWarning("HttpServer: listen() failed"); - m_url = QStringLiteral("http://127.0.0.1:") + QString::number(m_tcpServer.serverPort()); } -QUrl HttpServer::url(const QString &path) const +bool HttpServer::start() { - auto copy = m_url; - copy.setPath(path); - return copy; + m_error = false; + + if (!m_tcpServer.listen()) { + qCWarning(gHttpServerLog).noquote() << m_tcpServer.errorString(); + return false; + } + + m_url.setScheme(QStringLiteral("http")); + m_url.setHost(QStringLiteral("127.0.0.1")); + m_url.setPort(m_tcpServer.serverPort()); + + return true; } -void HttpServer::handleNewConnection() +bool HttpServer::stop() { - auto reqRep = new HttpReqRep(m_tcpServer.nextPendingConnection(), this); - connect(reqRep, &HttpReqRep::readFinished, this, &HttpServer::handleReadFinished); + m_tcpServer.close(); + return !m_error; } -void HttpServer::handleReadFinished(bool ok) +QUrl HttpServer::url(const QString &path) const { - auto reqRep = qobject_cast(sender()); - if (ok) - Q_EMIT newRequest(reqRep); - else - reqRep->deleteLater(); + auto copy = m_url; + copy.setPath(path); + return copy; } -std::unique_ptr waitForRequest(HttpServer *server) +void HttpServer::handleNewConnection() { - std::unique_ptr result; - waitForSignal(server, &HttpServer::newRequest, [&](HttpReqRep *rr) { - rr->setParent(nullptr); - result.reset(rr); + auto rr = new HttpReqRep(m_tcpServer.nextPendingConnection(), this); + connect(rr, &HttpReqRep::requestReceived, [this, rr]() { + Q_EMIT newRequest(rr); + rr->close(); + }); + connect(rr, &HttpReqRep::responseSent, [this, rr]() { + qCInfo(gHttpServerLog).noquote() << rr->requestMethod() << rr->requestPath() + << rr->responseStatus() << rr->responseBody().size(); + }); + connect(rr, &HttpReqRep::error, [this, rr](const QString &error) { + qCWarning(gHttpServerLog).noquote() << rr->requestMethod() << rr->requestPath() + << error; + m_error = true; }); - return result; + connect(rr, &HttpReqRep::closed, rr, &QObject::deleteLater); } -- cgit v1.2.3