2022-04-08 03:33:29 +05:30
|
|
|
#include <QTest>
|
2023-01-18 07:21:54 +05:30
|
|
|
#include <QTimer>
|
|
|
|
#include <QThread>
|
2022-04-08 03:33:29 +05:30
|
|
|
|
2022-09-12 01:55:18 +05:30
|
|
|
#include <tasks/ConcurrentTask.h>
|
|
|
|
#include <tasks/MultipleOptionsTask.h>
|
|
|
|
#include <tasks/SequentialTask.h>
|
|
|
|
#include <tasks/Task.h>
|
2022-04-08 03:33:29 +05:30
|
|
|
|
|
|
|
/* Does nothing. Only used for testing. */
|
|
|
|
class BasicTask : public Task {
|
|
|
|
Q_OBJECT
|
2022-04-08 04:12:26 +05:30
|
|
|
|
|
|
|
friend class TaskTest;
|
|
|
|
|
2023-01-18 07:21:54 +05:30
|
|
|
public:
|
|
|
|
BasicTask(bool show_debug_log = true) : Task(nullptr, show_debug_log) {}
|
|
|
|
|
2022-04-08 04:12:26 +05:30
|
|
|
private:
|
2022-07-22 09:51:57 +05:30
|
|
|
void executeTask() override
|
|
|
|
{
|
|
|
|
emitSucceeded();
|
|
|
|
};
|
2022-04-08 04:12:26 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
/* Does nothing. Only used for testing. */
|
|
|
|
class BasicTask_MultiStep : public Task {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
friend class TaskTest;
|
|
|
|
|
2022-04-08 03:33:29 +05:30
|
|
|
private:
|
2022-04-08 04:12:26 +05:30
|
|
|
auto isMultiStep() const -> bool override { return true; }
|
|
|
|
|
2022-04-08 03:33:29 +05:30
|
|
|
void executeTask() override {};
|
|
|
|
};
|
|
|
|
|
2023-01-18 07:21:54 +05:30
|
|
|
class BigConcurrentTask : public QThread {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
ConcurrentTask big_task;
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
|
|
|
QTimer deadline;
|
|
|
|
deadline.setInterval(10000);
|
|
|
|
connect(&deadline, &QTimer::timeout, this, [this]{ passed_the_deadline = true; });
|
|
|
|
deadline.start();
|
|
|
|
|
2023-01-20 21:25:38 +05:30
|
|
|
// NOTE: Arbitrary value that manages to trigger a problem when there is one.
|
2023-01-18 07:21:54 +05:30
|
|
|
static const unsigned s_num_tasks = 1 << 14;
|
|
|
|
auto sub_tasks = new BasicTask*[s_num_tasks];
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < s_num_tasks; i++) {
|
|
|
|
sub_tasks[i] = new BasicTask(false);
|
|
|
|
big_task.addTask(sub_tasks[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
big_task.run();
|
|
|
|
|
|
|
|
while (!big_task.isFinished() && !passed_the_deadline)
|
|
|
|
QCoreApplication::processEvents();
|
|
|
|
|
|
|
|
emit finished();
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
bool passed_the_deadline = false;
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void finished();
|
|
|
|
};
|
|
|
|
|
2022-04-08 03:33:29 +05:30
|
|
|
class TaskTest : public QObject {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
2022-04-08 04:12:26 +05:30
|
|
|
void test_SetStatus_NoMultiStep(){
|
2022-04-08 03:33:29 +05:30
|
|
|
BasicTask t;
|
|
|
|
QString status {"test status"};
|
|
|
|
|
|
|
|
t.setStatus(status);
|
|
|
|
|
|
|
|
QCOMPARE(t.getStatus(), status);
|
2022-04-08 04:12:26 +05:30
|
|
|
QCOMPARE(t.getStepStatus(), status);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_SetStatus_MultiStep(){
|
|
|
|
BasicTask_MultiStep t;
|
|
|
|
QString status {"test status"};
|
|
|
|
|
|
|
|
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.
|
|
|
|
QCOMPARE(t.getStepStatus(), status);
|
2022-04-08 03:33:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void test_SetProgress(){
|
|
|
|
BasicTask t;
|
|
|
|
int current = 42;
|
|
|
|
int total = 207;
|
|
|
|
|
|
|
|
t.setProgress(current, total);
|
|
|
|
|
|
|
|
QCOMPARE(t.getProgress(), current);
|
|
|
|
QCOMPARE(t.getTotalProgress(), total);
|
|
|
|
}
|
2022-07-22 09:51:57 +05:30
|
|
|
|
|
|
|
void test_basicRun(){
|
|
|
|
BasicTask t;
|
|
|
|
QObject::connect(&t, &Task::finished, [&]{ QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been."); });
|
|
|
|
t.start();
|
|
|
|
|
|
|
|
QVERIFY2(QTest::qWaitFor([&]() {
|
|
|
|
return t.isFinished();
|
|
|
|
}, 1000), "Task didn't finish as it should.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_basicConcurrentRun(){
|
|
|
|
BasicTask t1;
|
|
|
|
BasicTask t2;
|
|
|
|
BasicTask t3;
|
|
|
|
|
|
|
|
ConcurrentTask t;
|
|
|
|
|
|
|
|
t.addTask(&t1);
|
|
|
|
t.addTask(&t2);
|
|
|
|
t.addTask(&t3);
|
|
|
|
|
|
|
|
QObject::connect(&t, &Task::finished, [&]{
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1.wasSuccessful());
|
|
|
|
QVERIFY(t2.wasSuccessful());
|
|
|
|
QVERIFY(t3.wasSuccessful());
|
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
|
|
|
QVERIFY2(QTest::qWaitFor([&]() {
|
|
|
|
return t.isFinished();
|
|
|
|
}, 1000), "Task didn't finish as it should.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests if starting new tasks after the 6 initial ones is working
|
|
|
|
void test_moreConcurrentRun(){
|
|
|
|
BasicTask t1, t2, t3, t4, t5, t6, t7, t8, t9;
|
|
|
|
|
|
|
|
ConcurrentTask t;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
QObject::connect(&t, &Task::finished, [&]{
|
|
|
|
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());
|
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
|
|
|
QVERIFY2(QTest::qWaitFor([&]() {
|
|
|
|
return t.isFinished();
|
|
|
|
}, 1000), "Task didn't finish as it should.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_basicSequentialRun(){
|
|
|
|
BasicTask t1;
|
|
|
|
BasicTask t2;
|
|
|
|
BasicTask t3;
|
|
|
|
|
|
|
|
SequentialTask t;
|
|
|
|
|
|
|
|
t.addTask(&t1);
|
|
|
|
t.addTask(&t2);
|
|
|
|
t.addTask(&t3);
|
|
|
|
|
|
|
|
QObject::connect(&t, &Task::finished, [&]{
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1.wasSuccessful());
|
|
|
|
QVERIFY(t2.wasSuccessful());
|
|
|
|
QVERIFY(t3.wasSuccessful());
|
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
|
|
|
QVERIFY2(QTest::qWaitFor([&]() {
|
|
|
|
return t.isFinished();
|
|
|
|
}, 1000), "Task didn't finish as it should.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_basicMultipleOptionsRun(){
|
|
|
|
BasicTask t1;
|
|
|
|
BasicTask t2;
|
|
|
|
BasicTask t3;
|
|
|
|
|
|
|
|
MultipleOptionsTask t;
|
|
|
|
|
|
|
|
t.addTask(&t1);
|
|
|
|
t.addTask(&t2);
|
|
|
|
t.addTask(&t3);
|
|
|
|
|
|
|
|
QObject::connect(&t, &Task::finished, [&]{
|
|
|
|
QVERIFY2(t.wasSuccessful(), "Task finished but was not successful when it should have been.");
|
|
|
|
QVERIFY(t1.wasSuccessful());
|
|
|
|
QVERIFY(!t2.wasSuccessful());
|
|
|
|
QVERIFY(!t3.wasSuccessful());
|
|
|
|
});
|
|
|
|
|
|
|
|
t.start();
|
|
|
|
QVERIFY2(QTest::qWaitFor([&]() {
|
|
|
|
return t.isFinished();
|
|
|
|
}, 1000), "Task didn't finish as it should.");
|
|
|
|
}
|
2023-01-18 07:21:54 +05:30
|
|
|
|
|
|
|
void test_stackOverflowInConcurrentTask()
|
|
|
|
{
|
|
|
|
QEventLoop loop;
|
|
|
|
|
|
|
|
auto thread = new BigConcurrentTask;
|
2023-01-20 21:25:38 +05:30
|
|
|
// NOTE: This is an arbitrary value, big enough to not cause problems on normal execution, but low enough
|
|
|
|
// so that the number of tasks that needs to get ran to potentially cause a problem isn't too big.
|
2023-01-18 07:21:54 +05:30
|
|
|
thread->setStackSize(32 * 1024);
|
|
|
|
|
|
|
|
connect(thread, &BigConcurrentTask::finished, &loop, &QEventLoop::quit);
|
|
|
|
|
|
|
|
thread->start();
|
|
|
|
|
|
|
|
loop.exec();
|
|
|
|
|
|
|
|
QVERIFY(!thread->passed_the_deadline);
|
|
|
|
thread->deleteLater();
|
|
|
|
}
|
2022-04-08 03:33:29 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
QTEST_GUILESS_MAIN(TaskTest)
|
|
|
|
|
|
|
|
#include "Task_test.moc"
|