diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2022-11-21 15:05:39 +0100 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2022-11-23 12:59:18 +0000 |
commit | 3468cd20ca8feeccf023091582d1390947822bd8 (patch) | |
tree | b978531b171ba6de9bfe94dbc63a9df9852eea6b /tests | |
parent | 1667b062362f844d2542a4af2d47c40c63584e78 (diff) |
TaskTree: Introduce Storage item
The Storage item makes it possible to define the whole
subtree as a self-contained, full-functional recipe, without a need
for passing (together with recipe) a one-use only (disposable)
pointer to storage object.
That's the last closing element of making the idea of pure,
value-based recipe real. It makes the TaskTree machinery ultimately
powerful.
Change-Id: Icd81bdd3e94251e8b241b2b550957d566fa4ab75
Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/utils/tasktree/tst_tasktree.cpp | 88 |
1 files changed, 54 insertions, 34 deletions
diff --git a/tests/auto/utils/tasktree/tst_tasktree.cpp b/tests/auto/utils/tasktree/tst_tasktree.cpp index 29202fa0b11..5d467f7cdef 100644 --- a/tests/auto/utils/tasktree/tst_tasktree.cpp +++ b/tests/auto/utils/tasktree/tst_tasktree.cpp @@ -496,46 +496,72 @@ void tst_TaskTree::processTree() QCOMPARE(errorCount, expectedErrorCount); } +struct CustomStorage +{ + CustomStorage() { ++s_count; } + ~CustomStorage() { --s_count; } + Log m_log; + static int instanceCount() { return s_count; } +private: + static int s_count; +}; + +int CustomStorage::s_count = 0; +static Log s_log; + void tst_TaskTree::storage_data() { using namespace Tasking; using namespace std::placeholders; QTest::addColumn<Group>("root"); - QTest::addColumn<std::shared_ptr<Log>>("storageLog"); + QTest::addColumn<TreeStorage<CustomStorage>>("storageLog"); QTest::addColumn<Log>("expectedLog"); QTest::addColumn<bool>("runningAfterStart"); QTest::addColumn<bool>("success"); - // TODO: Much better approach would be that the TaskTree, when started, creates a - // storage dynamically (then Group should be a template with storage type as a - // template parameter) and a pointer (or reference) to the storage is being passed - // into handlers (? how ?). - std::shared_ptr<Log> log(new Log); + TreeStorage<CustomStorage> storageLog; - const auto setupProcessHelper = [this, log](QtcProcess &process, const QStringList &args, int processId) { - process.setCommand(CommandLine(m_testAppPath, args)); + const auto setupProcessHelper = [storageLog, testAppPath = m_testAppPath] + (QtcProcess &process, const QStringList &args, int processId) { + process.setCommand(CommandLine(testAppPath, args)); process.setProperty(s_processIdProperty, processId); - log->append({processId, Handler::Setup}); + storageLog->m_log.append({processId, Handler::Setup}); }; const auto setupProcess = [setupProcessHelper](QtcProcess &process, int processId) { setupProcessHelper(process, {"-return", "0"}, processId); }; - const auto readResult = [log](const QtcProcess &process) { + const auto readResult = [storageLog](const QtcProcess &process) { const int processId = process.property(s_processIdProperty).toInt(); - log->append({processId, Handler::Done}); + storageLog->m_log.append({processId, Handler::Done}); }; - const auto groupSetup = [log](int processId) { - log->append({processId, Handler::GroupSetup}); + const auto groupSetup = [storageLog](int processId) { + storageLog->m_log.append({processId, Handler::GroupSetup}); }; - const auto groupDone = [log](int processId) { - log->append({processId, Handler::GroupDone}); + const auto groupDone = [storageLog](int processId) { + storageLog->m_log.append({processId, Handler::GroupDone}); }; - const auto rootDone = [log] { - log->append({-1, Handler::GroupDone}); + const auto rootDone = [storageLog] { + storageLog->m_log.append({-1, Handler::GroupDone}); + s_log = storageLog->m_log; }; - const Group nestedRoot { + const Log expectedLog{{1, Handler::GroupSetup}, + {2, Handler::GroupSetup}, + {3, Handler::GroupSetup}, + {4, Handler::GroupSetup}, + {5, Handler::GroupSetup}, + {5, Handler::Setup}, + {5, Handler::Done}, + {5, Handler::GroupDone}, + {4, Handler::GroupDone}, + {3, Handler::GroupDone}, + {2, Handler::GroupDone}, + {1, Handler::GroupDone}, + {-1, Handler::GroupDone}}; + + const Group root { + Storage(storageLog), Group { Group { Group { @@ -560,20 +586,7 @@ void tst_TaskTree::storage_data() OnGroupDone(rootDone) }; - const Log nestedLog{{1, Handler::GroupSetup}, - {2, Handler::GroupSetup}, - {3, Handler::GroupSetup}, - {4, Handler::GroupSetup}, - {5, Handler::GroupSetup}, - {5, Handler::Setup}, - {5, Handler::Done}, - {5, Handler::GroupDone}, - {4, Handler::GroupDone}, - {3, Handler::GroupDone}, - {2, Handler::GroupDone}, - {1, Handler::GroupDone}, - {-1, Handler::GroupDone}}; - QTest::newRow("Nested") << nestedRoot << log << nestedLog << true << true; + QTest::newRow("Storage") << root << storageLog << expectedLog << true << true; } void tst_TaskTree::storage() @@ -581,11 +594,16 @@ void tst_TaskTree::storage() using namespace Tasking; QFETCH(Group, root); - QFETCH(std::shared_ptr<Log>, storageLog); + QFETCH(TreeStorage<CustomStorage>, storageLog); QFETCH(Log, expectedLog); QFETCH(bool, runningAfterStart); QFETCH(bool, success); + s_log.clear(); + + QVERIFY(storageLog.isValid()); + QCOMPARE(CustomStorage::instanceCount(), 0); + QEventLoop eventLoop; TaskTree processTree(root); int doneCount = 0; @@ -593,6 +611,7 @@ void tst_TaskTree::storage() connect(&processTree, &TaskTree::done, this, [&doneCount, &eventLoop] { ++doneCount; eventLoop.quit(); }); connect(&processTree, &TaskTree::errorOccurred, this, [&errorCount, &eventLoop] { ++errorCount; eventLoop.quit(); }); processTree.start(); + QCOMPARE(CustomStorage::instanceCount(), 1); QCOMPARE(processTree.isRunning(), runningAfterStart); QTimer timer; @@ -603,7 +622,8 @@ void tst_TaskTree::storage() eventLoop.exec(); QVERIFY(!processTree.isRunning()); - QCOMPARE(*storageLog, expectedLog); + QCOMPARE(s_log, expectedLog); + QCOMPARE(CustomStorage::instanceCount(), 0); const int expectedDoneCount = success ? 1 : 0; const int expectedErrorCount = success ? 0 : 1; |