FileStreamer: Reuse TaskTree::runBlocking()

Reuse it also in FileSystemAccessTest.

Change-Id: I6ce1c926bd5d3a617b8badb0905e7b2fd58b4745
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2023-05-15 17:54:23 +02:00
parent 686a40d199
commit 2b174a763f
2 changed files with 8 additions and 65 deletions

View File

@@ -333,7 +333,7 @@ static Group interDeviceTransferTask(const FilePath &source, const FilePath &des
storage->writer, &FileStreamWriter::write);
};
const auto finalizeReader = [=](const FileStreamReader &) {
QTC_CHECK(storage->writer != nullptr);
if (storage->writer) // writer may be deleted before the reader on TaskTree::stop().
storage->writer->closeWriteChannel();
};
const auto setupWriter = [=](FileStreamWriter &writer) {
@@ -370,33 +370,8 @@ static void transfer(QPromise<void> &promise, const FilePath &source, const File
if (promise.isCanceled())
return;
std::unique_ptr<TaskTree> taskTree(new TaskTree(transferTask(source, destination)));
QEventLoop eventLoop;
bool finalized = false;
const auto finalize = [loop = &eventLoop, &taskTree, &finalized](int exitCode) {
if (finalized) // finalize only once
return;
finalized = true;
// Give the tree a chance to delete later all tasks that have finished and caused
// emission of tree's done or errorOccurred signal.
// TODO: maybe these signals should be sent queued already?
QMetaObject::invokeMethod(loop, [loop, &taskTree, exitCode] {
taskTree.reset();
loop->exit(exitCode);
}, Qt::QueuedConnection);
};
QTimer timer;
timer.setInterval(50);
QObject::connect(&timer, &QTimer::timeout, [&promise, finalize] {
if (promise.isCanceled())
finalize(2);
});
QObject::connect(taskTree.get(), &TaskTree::done, &eventLoop, [=] { finalize(0); });
QObject::connect(taskTree.get(), &TaskTree::errorOccurred, &eventLoop, [=] { finalize(1); });
taskTree->start();
timer.start();
if (eventLoop.exec())
TaskTree taskTree(transferTask(source, destination));
if (!taskTree.runBlocking(promise.future()))
promise.future().cancel();
}

View File

@@ -13,6 +13,7 @@
#include <utils/filestreamermanager.h>
#include <utils/process.h>
#include <utils/processinterface.h>
#include <utils/scopedtimer.h>
#include <QDebug>
#include <QFile>
@@ -390,8 +391,7 @@ void FileSystemAccessTest::testFileStreamer_data()
void FileSystemAccessTest::testFileStreamer()
{
QElapsedTimer timer;
timer.start();
QTC_SCOPED_TIMER("testFileStreamer");
QFETCH(QString, fileName);
QFETCH(QByteArray, data);
@@ -503,35 +503,8 @@ void FileSystemAccessTest::testFileStreamer()
}
};
QEventLoop eventLoop;
TaskTree taskTree(root);
int doneCount = 0;
int errorCount = 0;
connect(&taskTree, &TaskTree::done, this, [&doneCount, &eventLoop] {
++doneCount;
eventLoop.quit();
});
connect(&taskTree, &TaskTree::errorOccurred, this, [&errorCount, &eventLoop] {
++errorCount;
eventLoop.quit();
});
taskTree.start();
QVERIFY(taskTree.isRunning());
QTimer timeoutTimer;
bool timedOut = false;
connect(&timeoutTimer, &QTimer::timeout, &eventLoop, [&eventLoop, &timedOut] {
timedOut = true;
eventLoop.quit();
});
timeoutTimer.setInterval(10000);
timeoutTimer.setSingleShot(true);
timeoutTimer.start();
eventLoop.exec();
QCOMPARE(timedOut, false);
QCOMPARE(taskTree.isRunning(), false);
QCOMPARE(doneCount, 1);
QCOMPARE(errorCount, 0);
QVERIFY(taskTree.runBlocking(10000));
QVERIFY(localData);
QCOMPARE(*localData, data);
@@ -546,8 +519,6 @@ void FileSystemAccessTest::testFileStreamer()
QCOMPARE(*remoteLocalData, data);
QVERIFY(remoteRemoteData);
QCOMPARE(*remoteRemoteData, data);
qDebug() << "Elapsed time:" << timer.elapsed() << "ms.";
}
void FileSystemAccessTest::testFileStreamerManager_data()
@@ -557,8 +528,7 @@ void FileSystemAccessTest::testFileStreamerManager_data()
void FileSystemAccessTest::testFileStreamerManager()
{
QElapsedTimer timer;
timer.start();
QTC_SCOPED_TIMER("testFileStreamerManager");
QFETCH(QString, fileName);
QFETCH(QByteArray, data);
@@ -660,8 +630,6 @@ void FileSystemAccessTest::testFileStreamerManager()
QCOMPARE(*remoteLocalData, data);
QVERIFY(remoteRemoteData);
QCOMPARE(*remoteRemoteData, data);
qDebug() << "Elapsed time:" << timer.elapsed() << "ms.";
}
} // Internal