TaskTree: Use DoneWith in TaskTree::runBlocking()

Instead of using ambiguous bool.
Reuse it in place of OnDone enum in tests.

Change-Id: Ie83e82d9debb88ca19f71ecab40f8ad081293f41
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2023-11-06 20:08:42 +01:00
parent f771faf82d
commit 0938e6cf4d
6 changed files with 155 additions and 159 deletions

View File

@@ -2342,7 +2342,7 @@ bool TaskTree::isRunning() const
\sa start()
*/
bool TaskTree::runBlocking()
DoneWith TaskTree::runBlocking()
{
QPromise<void> dummy;
dummy.start();
@@ -2353,17 +2353,17 @@ bool TaskTree::runBlocking()
\overload runBlocking()
The passed \a future is used for listening to the cancel event.
When the task tree finishes with an error, this method cancels the passed \a future.
When the task tree is canceled, this method cancels the passed \a future.
*/
bool TaskTree::runBlocking(const QFuture<void> &future)
DoneWith TaskTree::runBlocking(const QFuture<void> &future)
{
if (future.isCanceled())
return false;
return DoneWith::Cancel;
bool ok = false;
DoneWith doneWith = DoneWith::Cancel;
QEventLoop loop;
connect(this, &TaskTree::done, &loop, [&loop, &ok](DoneWith result) {
ok = result == DoneWith::Success;
connect(this, &TaskTree::done, &loop, [&loop, &doneWith](DoneWith result) {
doneWith = result;
// Otherwise, the tasks from inside the running tree that were deleteLater()
// will be leaked. Refer to the QObject::deleteLater() docs.
QMetaObject::invokeMethod(&loop, [&loop] { loop.quit(); }, Qt::QueuedConnection);
@@ -2375,11 +2375,11 @@ bool TaskTree::runBlocking(const QFuture<void> &future)
QTimer::singleShot(0, this, &TaskTree::start);
loop.exec(QEventLoop::ExcludeUserInputEvents);
if (!ok) {
if (doneWith == DoneWith::Cancel) {
auto nonConstFuture = future;
nonConstFuture.cancel();
}
return ok;
return doneWith;
}
/*!
@@ -2395,7 +2395,7 @@ bool TaskTree::runBlocking(const QFuture<void> &future)
\sa start()
*/
bool TaskTree::runBlocking(const Group &recipe, milliseconds timeout)
DoneWith TaskTree::runBlocking(const Group &recipe, milliseconds timeout)
{
QPromise<void> dummy;
dummy.start();
@@ -2406,9 +2406,9 @@ bool TaskTree::runBlocking(const Group &recipe, milliseconds timeout)
\overload runBlocking(const Group &recipe, milliseconds timeout)
The passed \a future is used for listening to the cancel event.
When the task tree finishes with an error, this method cancels the passed \a future.
When the task tree is canceled, this method cancels the passed \a future.
*/
bool TaskTree::runBlocking(const Group &recipe, const QFuture<void> &future, milliseconds timeout)
DoneWith TaskTree::runBlocking(const Group &recipe, const QFuture<void> &future, milliseconds timeout)
{
const Group root = timeout == milliseconds::max() ? recipe
: Group { recipe.withTimeout(timeout) };

View File

@@ -519,12 +519,12 @@ public:
// Helper methods. They execute a local event loop with ExcludeUserInputEvents.
// The passed future is used for listening to the cancel event.
// Don't use it in main thread. To be used in non-main threads or in auto tests.
bool runBlocking();
bool runBlocking(const QFuture<void> &future);
static bool runBlocking(const Group &recipe,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
static bool runBlocking(const Group &recipe, const QFuture<void> &future,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
DoneWith runBlocking();
DoneWith runBlocking(const QFuture<void> &future);
static DoneWith runBlocking(const Group &recipe,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
static DoneWith runBlocking(const Group &recipe, const QFuture<void> &future,
std::chrono::milliseconds timeout = std::chrono::milliseconds::max());
int taskCount() const;
int progressMaximum() const { return taskCount(); }

View File

@@ -369,8 +369,8 @@ static void transfer(QPromise<void> &promise, const FilePath &source, const File
if (promise.isCanceled())
return;
if (!TaskTree::runBlocking(transferTask(source, destination), promise.future()))
promise.future().cancel();
if (TaskTree::runBlocking(transferTask(source, destination), promise.future()) != DoneWith::Success)
promise.future().cancel(); // TODO: Is this needed?
}
class FileStreamerPrivate : public QObject

View File

@@ -504,7 +504,7 @@ void FileSystemAccessTest::testFileStreamer()
};
using namespace std::chrono_literals;
QVERIFY(TaskTree::runBlocking(root, 10000ms));
QCOMPARE(TaskTree::runBlocking(root, 10000ms), DoneWith::Success);
QVERIFY(localData);
QCOMPARE(*localData, data);