qtbase/tests/auto/wasm/fetchapi/tst_fetchapi.cpp
Piotr Wierciński 9907ef0d64 wasm: Allow fetching from background thread
Allow network request from background thread by proxing it to main
thread if needed.
Introduce "fetchHelper" which is stored on heap and owns
"outgoingData" which must be valid during entire fetch operation.
It is also used for synchronization between thread that has
scheduled fetch operation and the one that is executing it.
Enable the test that was skipped before fix.

Fixes: QTBUG-124111
Change-Id: Ifafa4c40fa435122639fa861a61fbf96340a7747
Reviewed-by: Piotr Wierciński <piotr.wiercinski@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
Reviewed-by: Jøger Hansegård <joger.hansegard@qt.io>
2024-05-10 09:55:19 +00:00

85 lines
2.0 KiB
C++

// Copyright (C) 2023 The Qt Company Ltd.
// Copyright (C) 2016 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QEventLoop>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QScopedPointer>
#include <memory>
namespace {
const QUrl URL = QUrl("http://localhost:6931/test_batch.html");
class BackgroundThread : public QThread
{
Q_OBJECT
void run() override
{
manager = std::make_unique<QNetworkAccessManager>();
eventLoop = std::make_unique<QEventLoop>();
reply = manager->get(request);
QObject::connect(reply, &QNetworkReply::finished, this, &BackgroundThread::requestFinished);
eventLoop->exec();
}
void requestFinished()
{
eventLoop->quit();
}
public:
QNetworkReply *reply{ nullptr };
private:
std::unique_ptr<QNetworkAccessManager> manager;
std::unique_ptr<QEventLoop> eventLoop;
QNetworkRequest request{ URL };
};
} // namespace
class tst_FetchApi : public QObject
{
Q_OBJECT
public:
tst_FetchApi() = default;
private Q_SLOTS:
void sendRequestOnMainThread();
void sendRequestOnBackgroundThread();
};
void tst_FetchApi::sendRequestOnMainThread()
{
QNetworkAccessManager manager;
QNetworkRequest request(URL);
QNetworkReply *reply = manager.get(request);
QEventLoop loop;
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
loop.exec();
QVERIFY(reply != nullptr);
QVERIFY(reply->error() == QNetworkReply::NoError);
}
void tst_FetchApi::sendRequestOnBackgroundThread()
{
QEventLoop mainEventLoop;
BackgroundThread *backgroundThread = new BackgroundThread();
connect(backgroundThread, &BackgroundThread::finished, &mainEventLoop, &QEventLoop::quit);
backgroundThread->start();
mainEventLoop.exec();
QVERIFY(backgroundThread != nullptr);
QVERIFY(backgroundThread->reply != nullptr);
QVERIFY(backgroundThread->reply->error() == QNetworkReply::NoError);
}
QTEST_MAIN(tst_FetchApi);
#include "tst_fetchapi.moc"