1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
#include <QTest>
#include <QThread>
#include <QTimer>
#include <tasks/ConcurrentTask.h>
#include <tasks/MultipleOptionsTask.h>
#include <tasks/SequentialTask.h>
#include <tasks/Task.h>
#include <array>
/* Does nothing. Only used for testing. */
class BasicTask : public Task {
Q_OBJECT
friend class TaskTest;
public:
BasicTask(bool show_debug_log = true) : Task(nullptr, show_debug_log) {}
private:
void executeTask() override { emitSucceeded(); };
};
/* Does nothing. Only used for testing. */
class BasicTask_MultiStep : public Task {
Q_OBJECT
friend class TaskTest;
private:
auto isMultiStep() const -> bool override { return true; }
void executeTask() override{};
};
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.
[[maybe_unused]] volatile std::array<uint32_t, 1024> some_data_on_the_stack{};
ConcurrentTask::startNext();
}
};
class BigConcurrentTaskThread : public QThread {
Q_OBJECT
QTimer m_deadline;
void run() override
{
BigConcurrentTask big_task;
m_deadline.setInterval(10000);
// NOTE: Arbitrary value that manages to trigger a problem when there is one.
// 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;
for (unsigned i = 0; i < s_num_tasks; i++) {
auto sub_task = makeShared<BasicTask>(false);
big_task.addTask(sub_task);
}
connect(&big_task, &Task::finished, this, &QThread::quit);
connect(&m_deadline, &QTimer::timeout, this, [&] {
passed_the_deadline = true;
quit();
});
m_deadline.start();
big_task.run();
exec();
}
public:
bool passed_the_deadline = false;
};
class TaskTest : public QObject {
Q_OBJECT
private slots:
void test_SetStatus_NoMultiStep()
{
BasicTask t;
QString status{ "test status" };
t.setStatus(status);
QCOMPARE(t.getStatus(), status);
QCOMPARE(t.getStepProgress().isEmpty(), TaskStepProgressList{}.isEmpty());
}
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.getStepProgress().isEmpty(), TaskStepProgressList{}.isEmpty());
}
void test_SetProgress()
{
BasicTask t;
int current = 42;
int total = 207;
t.setProgress(current, total);
QCOMPARE(t.getProgress(), current);
QCOMPARE(t.getTotalProgress(), total);
}
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()
{
auto t1 = makeShared<BasicTask>();
auto t2 = makeShared<BasicTask>();
auto t3 = makeShared<BasicTask>();
ConcurrentTask t;
t.addTask(t1);
t.addTask(t2);
t.addTask(t3);
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());
});
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()
{
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>();
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, [&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());
});
t.start();
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
}
void test_basicSequentialRun()
{
auto t1 = makeShared<BasicTask>();
auto t2 = makeShared<BasicTask>();
auto t3 = makeShared<BasicTask>();
SequentialTask t;
t.addTask(t1);
t.addTask(t2);
t.addTask(t3);
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());
});
t.start();
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
}
void test_basicMultipleOptionsRun()
{
auto t1 = makeShared<BasicTask>();
auto t2 = makeShared<BasicTask>();
auto t3 = makeShared<BasicTask>();
MultipleOptionsTask t;
t.addTask(t1);
t.addTask(t2);
t.addTask(t3);
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());
});
t.start();
QVERIFY2(QTest::qWaitFor([&]() { return t.isFinished(); }, 1000), "Task didn't finish as it should.");
}
void test_stackOverflowInConcurrentTask()
{
QEventLoop loop;
BigConcurrentTaskThread thread;
connect(&thread, &BigConcurrentTaskThread::finished, &loop, &QEventLoop::quit);
thread.start();
loop.exec();
QVERIFY(!thread.passed_the_deadline);
}
};
QTEST_GUILESS_MAIN(TaskTest)
#include "Task_test.moc"
|