2022-04-07 23:03:29 +01:00
|
|
|
#include <QTest>
|
2023-01-18 01:51:54 +00:00
|
|
|
#include <QThread>
|
2023-07-05 19:51:37 +01:00
|
|
|
#include <QTimer>
|
2022-04-07 23:03:29 +01:00
|
|
|
|
2022-09-11 21:25:18 +01:00
|
|
|
#include <tasks/ConcurrentTask.h>
|
|
|
|
#include <tasks/MultipleOptionsTask.h>
|
|
|
|
#include <tasks/SequentialTask.h>
|
|
|
|
#include <tasks/Task.h>
|
2022-04-07 23:03:29 +01:00
|
|
|
|
2023-02-01 14:44:50 +00:00
|
|
|
#include <array>
|
|
|
|
|
2022-04-07 23:03:29 +01:00
|
|
|
/* Does nothing. Only used for testing. */
|
|
|
|
class BasicTask : public Task {
|
|
|
|
Q_OBJECT
|
2022-04-07 23:42:26 +01:00
|
|
|
|
|
|
|
friend class TaskTest;
|
|
|
|
|
2023-01-18 01:51:54 +00:00
|
|
|
public:
|
|
|
|
BasicTask(bool show_debug_log = true) : Task(nullptr, show_debug_log) {}
|
|
|
|
|
2022-04-07 23:42:26 +01:00
|
|
|
private:
|
2023-07-05 19:51:37 +01:00
|
|
|
void executeTask() override { emitSucceeded(); };
|
2022-04-07 23:42:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Does nothing. Only used for testing. */
|
|
|
|
class BasicTask_MultiStep : public Task {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
friend class TaskTest;
|
|
|
|
|
2022-04-07 23:03:29 +01:00
|
|
|
private:
|
2022-04-07 23:42:26 +01:00
|
|
|
auto isMultiStep() const -> bool override { return true; }
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void executeTask() override{};
|
2022-04-07 23:03:29 +01:00
|
|
|
};
|
|
|
|
|
2023-02-01 14:44:50 +00:00
|
|
|
class BigConcurrentTask : public ConcurrentTask {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
void startNext() override
|
|
|
|
{
|
|
|
|
// This is here only to help fill the stack a bit more quickly (if there's an issue, of course :^))
|
|
|
|
// Each tasks thus adds 1024 * 4 bytes to the stack, at the very least.
|
2023-07-05 19:51:37 +01:00
|
|
|
[[maybe_unused]] volatile std::array<uint32_t, 1024> some_data_on_the_stack{};
|
2023-02-01 14:44:50 +00:00
|
|
|
|
|
|
|
ConcurrentTask::startNext();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BigConcurrentTaskThread : public QThread {
|
2023-01-18 01:51:54 +00:00
|
|
|
Q_OBJECT
|
|
|
|
|
2023-02-01 14:44:50 +00:00
|
|
|
BigConcurrentTask big_task;
|
2023-01-18 01:51:54 +00:00
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
|
|
|
QTimer deadline;
|
|
|
|
deadline.setInterval(10000);
|
2023-07-05 19:51:37 +01:00
|
|
|
connect(&deadline, &QTimer::timeout, this, [this] { passed_the_deadline = true; });
|
2023-01-18 01:51:54 +00:00
|
|
|
deadline.start();
|
|
|
|
|
2023-01-20 15:55:38 +00:00
|
|
|
// NOTE: Arbitrary value that manages to trigger a problem when there is one.
|
2023-02-01 14:44:50 +00:00
|
|
|
// Considering each tasks, in a problematic state, adds 1024 * 4 bytes to the stack,
|
|
|
|
// this number is enough to fill up 16 MiB of stack, more than enough to cause a problem.
|
|
|
|
static const unsigned s_num_tasks = 1 << 12;
|
2023-07-05 19:51:37 +01:00
|
|
|
{
|
|
|
|
auto sub_tasks = std::array<BasicTask::Ptr, s_num_tasks>();
|
2023-01-18 01:51:54 +00:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
for (unsigned i = 0; i < s_num_tasks; i++) {
|
|
|
|
auto sub_task = makeShared<BasicTask>(false);
|
|
|
|
sub_tasks[i] = sub_task;
|
|
|
|
big_task.addTask(sub_task);
|
|
|
|
}
|
2023-01-18 01:51:54 +00:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
big_task.run();
|
2023-01-18 01:51:54 +00:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
while (!big_task.isFinished() && !passed_the_deadline)
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
} // drop before emit
|
2023-01-18 01:51:54 +00:00
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool passed_the_deadline = false;
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void finished();
|
|
|
|
};
|
|
|
|
|
2022-04-07 23:03:29 +01:00
|
|
|
class TaskTest : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_SetStatus_NoMultiStep()
|
|
|
|
{
|
2022-04-07 23:03:29 +01:00
|
|
|
BasicTask t;
|
2023-07-05 19:51:37 +01:00
|
|
|
QString status{ "test status" };
|
2022-04-07 23:03:29 +01:00
|
|
|
|
|
|
|
t.setStatus(status);
|
|
|
|
|
|
|
|
QCOMPARE(t.getStatus(), status);
|
2023-03-31 07:50:29 +01:00
|
|
|
QCOMPARE(t.getStepProgress().isEmpty(), TaskStepProgressList{}.isEmpty());
|
2022-04-07 23:42:26 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_SetStatus_MultiStep()
|
|
|
|
{
|
2022-04-07 23:42:26 +01:00
|
|
|
BasicTask_MultiStep t;
|
2023-07-05 19:51:37 +01:00
|
|
|
QString status{ "test status" };
|
2022-04-07 23:42:26 +01:00
|
|
|
|
|
|
|
t.setStatus(status);
|
|
|
|
|
|
|
|
QCOMPARE(t.getStatus(), status);
|
|
|
|
// Even though it is multi step, it does not override the getStepStatus method,
|
|
|
|
// so it should remain the same.
|
2023-03-31 07:50:29 +01:00
|
|
|
QCOMPARE(t.getStepProgress().isEmpty(), TaskStepProgressList{}.isEmpty());
|
2022-04-07 23:03:29 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_SetProgress()
|
|
|
|
{
|
2022-04-07 23:03:29 +01:00
|
|
|
BasicTask t;
|
|
|
|
int current = 42;
|
|
|
|
int total = 207;
|
|
|
|
|
|
|
|
t.setProgress(current, total);
|
|
|
|
|
|
|
|
QCOMPARE(t.getProgress(), current);
|
|
|
|
QCOMPARE(t.getTotalProgress(), total);
|
|
|
|
}
|
2022-07-22 05:21:57 +01:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_basicRun()
|
|
|
|
{
|
2022-07-22 05:21:57 +01:00
|
|
|
BasicTask t;
|
2023-07-05 19:51:37 +01:00
|
|
|
QObject::connect(&t, &Task::finished,
|
|
|
|
[&] { QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been."); });
|
2022-07-22 05:21:57 +01:00
|
|
|
t.start();
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
|
2022-07-22 05:21:57 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_basicConcurrentRun()
|
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto t1 = makeShared<BasicTask>();
|
|
|
|
auto t2 = makeShared<BasicTask>();
|
|
|
|
auto t3 = makeShared<BasicTask>();
|
2022-07-22 05:21:57 +01:00
|
|
|
|
|
|
|
ConcurrentTask t;
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
t.addTask(t1);
|
|
|
|
t.addTask(t2);
|
|
|
|
t.addTask(t3);
|
2022-07-22 05:21:57 +01:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
QObject::connect(&t, &Task::finished, [&t, &t1, &t2, &t3] {
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1->wasSuccessful());
|
|
|
|
QVERIFY(t2->wasSuccessful());
|
|
|
|
QVERIFY(t3->wasSuccessful());
|
2022-07-22 05:21:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
2023-07-05 19:51:37 +01:00
|
|
|
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
|
2022-07-22 05:21:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests if starting new tasks after the 6 initial ones is working
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_moreConcurrentRun()
|
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto t1 = makeShared<BasicTask>();
|
|
|
|
auto t2 = makeShared<BasicTask>();
|
|
|
|
auto t3 = makeShared<BasicTask>();
|
|
|
|
auto t4 = makeShared<BasicTask>();
|
|
|
|
auto t5 = makeShared<BasicTask>();
|
|
|
|
auto t6 = makeShared<BasicTask>();
|
|
|
|
auto t7 = makeShared<BasicTask>();
|
|
|
|
auto t8 = makeShared<BasicTask>();
|
|
|
|
auto t9 = makeShared<BasicTask>();
|
2022-07-22 05:21:57 +01:00
|
|
|
|
|
|
|
ConcurrentTask t;
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
t.addTask(t1);
|
|
|
|
t.addTask(t2);
|
|
|
|
t.addTask(t3);
|
|
|
|
t.addTask(t4);
|
|
|
|
t.addTask(t5);
|
|
|
|
t.addTask(t6);
|
|
|
|
t.addTask(t7);
|
|
|
|
t.addTask(t8);
|
|
|
|
t.addTask(t9);
|
2022-07-22 05:21:57 +01:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
QObject::connect(&t, &Task::finished, [&t, &t1, &t2, &t3, &t4, &t5, &t6, &t7, &t8, &t9] {
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1->wasSuccessful());
|
|
|
|
QVERIFY(t2->wasSuccessful());
|
|
|
|
QVERIFY(t3->wasSuccessful());
|
|
|
|
QVERIFY(t4->wasSuccessful());
|
|
|
|
QVERIFY(t5->wasSuccessful());
|
|
|
|
QVERIFY(t6->wasSuccessful());
|
|
|
|
QVERIFY(t7->wasSuccessful());
|
|
|
|
QVERIFY(t8->wasSuccessful());
|
|
|
|
QVERIFY(t9->wasSuccessful());
|
2022-07-22 05:21:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
2023-07-05 19:51:37 +01:00
|
|
|
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
|
2022-07-22 05:21:57 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_basicSequentialRun()
|
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto t1 = makeShared<BasicTask>();
|
|
|
|
auto t2 = makeShared<BasicTask>();
|
|
|
|
auto t3 = makeShared<BasicTask>();
|
2022-07-22 05:21:57 +01:00
|
|
|
|
|
|
|
SequentialTask t;
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
t.addTask(t1);
|
|
|
|
t.addTask(t2);
|
|
|
|
t.addTask(t3);
|
2022-07-22 05:21:57 +01:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
QObject::connect(&t, &Task::finished, [&t, &t1, &t2, &t3] {
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1->wasSuccessful());
|
|
|
|
QVERIFY(t2->wasSuccessful());
|
|
|
|
QVERIFY(t3->wasSuccessful());
|
2022-07-22 05:21:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
2023-07-05 19:51:37 +01:00
|
|
|
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
|
2022-07-22 05:21:57 +01:00
|
|
|
}
|
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
void test_basicMultipleOptionsRun()
|
|
|
|
{
|
2023-01-24 19:52:09 +00:00
|
|
|
auto t1 = makeShared<BasicTask>();
|
|
|
|
auto t2 = makeShared<BasicTask>();
|
|
|
|
auto t3 = makeShared<BasicTask>();
|
2022-07-22 05:21:57 +01:00
|
|
|
|
|
|
|
MultipleOptionsTask t;
|
|
|
|
|
2023-01-24 19:52:09 +00:00
|
|
|
t.addTask(t1);
|
|
|
|
t.addTask(t2);
|
|
|
|
t.addTask(t3);
|
2022-07-22 05:21:57 +01:00
|
|
|
|
2023-07-05 19:51:37 +01:00
|
|
|
QObject::connect(&t, &Task::finished, [&t, &t1, &t2, &t3] {
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1->wasSuccessful());
|
|
|
|
QVERIFY(!t2->wasSuccessful());
|
|
|
|
QVERIFY(!t3->wasSuccessful());
|
2022-07-22 05:21:57 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
2023-07-05 19:51:37 +01:00
|
|
|
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
|
2022-07-22 05:21:57 +01:00
|
|
|
}
|
2023-01-18 01:51:54 +00:00
|
|
|
|
|
|
|
void test_stackOverflowInConcurrentTask()
|
|
|
|
{
|
|
|
|
QEventLoop loop;
|
|
|
|
|
2023-02-01 14:44:50 +00:00
|
|
|
auto thread = new BigConcurrentTaskThread;
|
2023-01-18 01:51:54 +00:00
|
|
|
|
2023-02-01 14:44:50 +00:00
|
|
|
connect(thread, &BigConcurrentTaskThread::finished, &loop, &QEventLoop::quit);
|
2023-01-18 01:51:54 +00:00
|
|
|
|
|
|
|
thread->start();
|
|
|
|
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
QVERIFY(!thread->passed_the_deadline);
|
|
|
|
thread->deleteLater();
|
|
|
|
}
|
2022-04-07 23:03:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TaskTest)
|
|
|
|
|
|
|
|
#include "Task_test.moc"
|