TaskTree: Detect a misconfigured recipe of a nested task tree

Change-Id: I6652336023ac111cde5334e655f5dd972977b07f
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2024-06-12 15:42:07 +02:00
parent 55836c174d
commit e331329e4f
2 changed files with 64 additions and 8 deletions

View File

@@ -118,6 +118,7 @@ private slots:
void storageOperators();
void storageDestructor();
void storageZeroInitialization();
void nestedBrokenStorage();
void restart();
void destructorOfTaskEmittingDone();
};
@@ -3452,6 +3453,55 @@ void tst_Tasking::storageZeroInitialization()
QCOMPARE(defaultValue, 0);
}
// This test ensures that when a missing storage object inside the nested task tree is accessed
// directly from the outer task tree's handler, containing the same storage object, then we
// detect this misconfigured recipe, issue a warning, and return nullptr for the storage
// being accessed (instead of returning parent's task tree's storage instance).
// This test should also trigger a runtime assert that we are accessing the nullptr storage.
void tst_Tasking::nestedBrokenStorage()
{
int *outerStorage = nullptr;
int *innerStorage1 = nullptr;
int *innerStorage2 = nullptr;
const Storage<int> storage;
const auto onOuterSync = [storage, &outerStorage, &innerStorage1, &innerStorage2] {
outerStorage = &*storage;
const auto onInnerSync1 = [storage, &innerStorage1] {
innerStorage1 = &*storage; // Triggers the runtime assert on purpose.
};
const auto onInnerSync2 = [storage, &innerStorage2] {
innerStorage2 = &*storage; // Storage is accessible in currently running tree - all OK.
};
const Group innerRecipe {
Group {
// Broken subrecipe, the storage wasn't placed inside the recipe.
// storage,
Sync(onInnerSync1)
},
Group {
storage, // Subrecipe OK, another instance for the nested storage will be created.
Sync(onInnerSync2)
}
};
TaskTree::runBlocking(innerRecipe);
};
const Group outerRecipe {
storage,
Sync(onOuterSync)
};
TaskTree::runBlocking(outerRecipe);
QVERIFY(outerStorage != nullptr);
QCOMPARE(innerStorage1, nullptr);
QVERIFY(innerStorage2 != nullptr);
QVERIFY(innerStorage2 != outerStorage);
}
void tst_Tasking::restart()
{
TaskTree taskTree({TestTask([](TaskObject &taskObject) { taskObject = 1000ms; })});