diff options
author | Dan Vrátil <dvratil@redhat.com> | 2015-05-18 15:20:35 +0200 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2015-05-18 15:20:35 +0200 |
commit | 5e580299e342bd77fc7479bbfd235f4446d7f05b (patch) | |
tree | 648aacd4de1f239d72be89ab9f2d4a97867d7920 /async/autotests/asynctest.cpp | |
parent | b43c0cf97615957e097daef29ff8febc1ec884c8 (diff) | |
download | sink-5e580299e342bd77fc7479bbfd235f4446d7f05b.tar.gz sink-5e580299e342bd77fc7479bbfd235f4446d7f05b.zip |
KAsync has moved to it's own kasync.git repository
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r-- | async/autotests/asynctest.cpp | 859 |
1 files changed, 0 insertions, 859 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp deleted file mode 100644 index ffc732c..0000000 --- a/async/autotests/asynctest.cpp +++ /dev/null | |||
@@ -1,859 +0,0 @@ | |||
1 | /* | ||
2 | * Copyright 2014 Daniel Vrátil <dvratil@redhat.com> | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or | ||
5 | * modify it under the terms of the GNU General Public License as | ||
6 | * published by the Free Software Foundation; either version 2 of | ||
7 | * the License or (at your option) version 3 or any later version | ||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||
10 | * defined in Section 14 of version 3 of the license. | ||
11 | * | ||
12 | * This program is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | */ | ||
21 | |||
22 | #include "../src/async.h" | ||
23 | |||
24 | #include <QObject> | ||
25 | #include <QString> | ||
26 | #include <QTimer> | ||
27 | #include <QtTest/QTest> | ||
28 | #include <QDebug> | ||
29 | |||
30 | #include <functional> | ||
31 | |||
32 | class AsyncTest : public QObject | ||
33 | { | ||
34 | Q_OBJECT | ||
35 | |||
36 | public: | ||
37 | AsyncTest() | ||
38 | {} | ||
39 | |||
40 | ~AsyncTest() | ||
41 | {} | ||
42 | |||
43 | private Q_SLOTS: | ||
44 | void testSyncPromises(); | ||
45 | void testAsyncPromises(); | ||
46 | void testAsyncPromises2(); | ||
47 | void testNestedAsync(); | ||
48 | void testStartValue(); | ||
49 | |||
50 | void testAsyncThen(); | ||
51 | void testSyncThen(); | ||
52 | void testJoinedThen(); | ||
53 | void testVoidThen(); | ||
54 | |||
55 | void testAsyncEach(); | ||
56 | void testSyncEach(); | ||
57 | void testJoinedEach(); | ||
58 | void testVoidEachThen(); | ||
59 | void testAsyncVoidEachThen(); | ||
60 | |||
61 | void testAsyncReduce(); | ||
62 | void testSyncReduce(); | ||
63 | void testJoinedReduce(); | ||
64 | void testVoidReduce(); | ||
65 | |||
66 | void testProgressReporting(); | ||
67 | void testErrorHandler(); | ||
68 | void testErrorPropagation(); | ||
69 | void testErrorHandlerAsync(); | ||
70 | void testErrorPropagationAsync(); | ||
71 | void testNestedErrorPropagation(); | ||
72 | |||
73 | void testChainingRunningJob(); | ||
74 | void testChainingFinishedJob(); | ||
75 | |||
76 | void testLifetimeWithoutHandle(); | ||
77 | void testLifetimeWithHandle(); | ||
78 | |||
79 | void benchmarkSyncThenExecutor(); | ||
80 | |||
81 | private: | ||
82 | template<typename T> | ||
83 | class AsyncSimulator { | ||
84 | public: | ||
85 | AsyncSimulator(KAsync::Future<T> &future, const T &result) | ||
86 | : mFuture(future) | ||
87 | , mResult(result) | ||
88 | { | ||
89 | QObject::connect(&mTimer, &QTimer::timeout, | ||
90 | [this]() { | ||
91 | mFuture.setValue(mResult); | ||
92 | mFuture.setFinished(); | ||
93 | }); | ||
94 | QObject::connect(&mTimer, &QTimer::timeout, | ||
95 | [this]() { | ||
96 | delete this; | ||
97 | }); | ||
98 | mTimer.setSingleShot(true); | ||
99 | mTimer.start(200); | ||
100 | } | ||
101 | |||
102 | AsyncSimulator(KAsync::Future<T> &future, std::function<void(KAsync::Future<T>&)> callback) | ||
103 | : mFuture(future) | ||
104 | , mCallback(callback) | ||
105 | { | ||
106 | QObject::connect(&mTimer, &QTimer::timeout, | ||
107 | [this]() { | ||
108 | mCallback(mFuture); | ||
109 | }); | ||
110 | QObject::connect(&mTimer, &QTimer::timeout, | ||
111 | [this]() { | ||
112 | delete this; | ||
113 | }); | ||
114 | mTimer.setSingleShot(true); | ||
115 | mTimer.start(200); | ||
116 | } | ||
117 | |||
118 | private: | ||
119 | KAsync::Future<T> mFuture; | ||
120 | std::function<void(KAsync::Future<T>&)> mCallback; | ||
121 | T mResult; | ||
122 | QTimer mTimer; | ||
123 | }; | ||
124 | }; | ||
125 | |||
126 | |||
127 | template<> | ||
128 | class AsyncTest::AsyncSimulator<void> { | ||
129 | public: | ||
130 | AsyncSimulator(KAsync::Future<void> &future) | ||
131 | : mFuture(future) | ||
132 | { | ||
133 | QObject::connect(&mTimer, &QTimer::timeout, | ||
134 | [this]() { | ||
135 | mFuture.setFinished(); | ||
136 | }); | ||
137 | QObject::connect(&mTimer, &QTimer::timeout, | ||
138 | [this]() { | ||
139 | delete this; | ||
140 | }); | ||
141 | mTimer.setSingleShot(true); | ||
142 | mTimer.start(200); | ||
143 | } | ||
144 | |||
145 | private: | ||
146 | KAsync::Future<void> mFuture; | ||
147 | QTimer mTimer; | ||
148 | }; | ||
149 | |||
150 | |||
151 | |||
152 | void AsyncTest::testSyncPromises() | ||
153 | { | ||
154 | auto baseJob = KAsync::start<int>( | ||
155 | [](KAsync::Future<int> &f) { | ||
156 | f.setValue(42); | ||
157 | f.setFinished(); | ||
158 | }) | ||
159 | .then<QString, int>( | ||
160 | [](int v, KAsync::Future<QString> &f) { | ||
161 | f.setValue(QLatin1String("Result is ") + QString::number(v)); | ||
162 | f.setFinished(); | ||
163 | }); | ||
164 | |||
165 | auto job = baseJob.then<QString, QString>( | ||
166 | [](const QString &v, KAsync::Future<QString> &f) { | ||
167 | f.setValue(v.toUpper()); | ||
168 | f.setFinished(); | ||
169 | }); | ||
170 | |||
171 | KAsync::Future<QString> future = job.exec(); | ||
172 | |||
173 | QVERIFY(future.isFinished()); | ||
174 | QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); | ||
175 | } | ||
176 | |||
177 | void AsyncTest::testAsyncPromises() | ||
178 | { | ||
179 | auto job = KAsync::start<int>( | ||
180 | [](KAsync::Future<int> &future) { | ||
181 | new AsyncSimulator<int>(future, 42); | ||
182 | }); | ||
183 | |||
184 | KAsync::Future<int> future = job.exec(); | ||
185 | |||
186 | future.waitForFinished(); | ||
187 | QCOMPARE(future.value(), 42); | ||
188 | } | ||
189 | |||
190 | void AsyncTest::testAsyncPromises2() | ||
191 | { | ||
192 | bool done = false; | ||
193 | |||
194 | auto job = KAsync::start<int>( | ||
195 | [](KAsync::Future<int> &future) { | ||
196 | new AsyncSimulator<int>(future, 42); | ||
197 | } | ||
198 | ).then<int, int>([&done](int result, KAsync::Future<int> &future) { | ||
199 | done = true; | ||
200 | future.setValue(result); | ||
201 | future.setFinished(); | ||
202 | }); | ||
203 | auto future = job.exec(); | ||
204 | |||
205 | QTRY_VERIFY(done); | ||
206 | QCOMPARE(future.value(), 42); | ||
207 | } | ||
208 | |||
209 | void AsyncTest::testNestedAsync() | ||
210 | { | ||
211 | bool done = false; | ||
212 | |||
213 | auto job = KAsync::start<int>( | ||
214 | [](KAsync::Future<int> &future) { | ||
215 | auto innerJob = KAsync::start<int>([](KAsync::Future<int> &innerFuture) { | ||
216 | new AsyncSimulator<int>(innerFuture, 42); | ||
217 | }).then<void>([&future](KAsync::Future<void> &innerThenFuture) { | ||
218 | future.setFinished(); | ||
219 | innerThenFuture.setFinished(); | ||
220 | }); | ||
221 | innerJob.exec().waitForFinished(); | ||
222 | } | ||
223 | ).then<int, int>([&done](int result, KAsync::Future<int> &future) { | ||
224 | done = true; | ||
225 | future.setValue(result); | ||
226 | future.setFinished(); | ||
227 | }); | ||
228 | job.exec(); | ||
229 | |||
230 | QTRY_VERIFY(done); | ||
231 | } | ||
232 | |||
233 | void AsyncTest::testStartValue() | ||
234 | { | ||
235 | auto job = KAsync::start<int, int>( | ||
236 | [](int in, KAsync::Future<int> &future) { | ||
237 | future.setValue(in); | ||
238 | future.setFinished(); | ||
239 | }); | ||
240 | |||
241 | auto future = job.exec(42); | ||
242 | QVERIFY(future.isFinished()); | ||
243 | QCOMPARE(future.value(), 42); | ||
244 | } | ||
245 | |||
246 | |||
247 | |||
248 | |||
249 | |||
250 | void AsyncTest::testAsyncThen() | ||
251 | { | ||
252 | auto job = KAsync::start<int>( | ||
253 | [](KAsync::Future<int> &future) { | ||
254 | new AsyncSimulator<int>(future, 42); | ||
255 | }); | ||
256 | |||
257 | auto future = job.exec(); | ||
258 | future.waitForFinished(); | ||
259 | |||
260 | QVERIFY(future.isFinished()); | ||
261 | QCOMPARE(future.value(), 42); | ||
262 | } | ||
263 | |||
264 | |||
265 | void AsyncTest::testSyncThen() | ||
266 | { | ||
267 | auto job = KAsync::start<int>( | ||
268 | []() -> int { | ||
269 | return 42; | ||
270 | }) | ||
271 | .then<int, int>( | ||
272 | [](int in) -> int { | ||
273 | return in * 2; | ||
274 | }); | ||
275 | |||
276 | auto future = job.exec(); | ||
277 | QVERIFY(future.isFinished()); | ||
278 | QCOMPARE(future.value(), 84); | ||
279 | } | ||
280 | |||
281 | void AsyncTest::testJoinedThen() | ||
282 | { | ||
283 | auto job1 = KAsync::start<int, int>( | ||
284 | [](int in, KAsync::Future<int> &future) { | ||
285 | new AsyncSimulator<int>(future, in * 2); | ||
286 | }); | ||
287 | |||
288 | auto job2 = KAsync::start<int>( | ||
289 | [](KAsync::Future<int> &future) { | ||
290 | new AsyncSimulator<int>(future, 42); | ||
291 | }) | ||
292 | .then<int>(job1); | ||
293 | |||
294 | auto future = job2.exec(); | ||
295 | future.waitForFinished(); | ||
296 | |||
297 | QVERIFY(future.isFinished()); | ||
298 | QCOMPARE(future.value(), 84); | ||
299 | } | ||
300 | |||
301 | void AsyncTest::testVoidThen() | ||
302 | { | ||
303 | int check = 0; | ||
304 | |||
305 | auto job = KAsync::start<void>( | ||
306 | [&check](KAsync::Future<void> &future) { | ||
307 | new AsyncSimulator<void>(future); | ||
308 | ++check; | ||
309 | }) | ||
310 | .then<void>( | ||
311 | [&check](KAsync::Future<void> &future) { | ||
312 | new AsyncSimulator<void>(future); | ||
313 | ++check; | ||
314 | }) | ||
315 | .then<void>( | ||
316 | [&check]() { | ||
317 | ++check; | ||
318 | }); | ||
319 | |||
320 | auto future = job.exec(); | ||
321 | future.waitForFinished(); | ||
322 | |||
323 | QVERIFY(future.isFinished()); | ||
324 | QCOMPARE(check, 3); | ||
325 | } | ||
326 | |||
327 | |||
328 | |||
329 | void AsyncTest::testAsyncEach() | ||
330 | { | ||
331 | auto job = KAsync::start<QList<int>>( | ||
332 | [](KAsync::Future<QList<int>> &future) { | ||
333 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); | ||
334 | }) | ||
335 | .each<QList<int>, int>( | ||
336 | [](const int &v, KAsync::Future<QList<int>> &future) { | ||
337 | new AsyncSimulator<QList<int>>(future, { v + 1 }); | ||
338 | }); | ||
339 | |||
340 | auto future = job.exec(); | ||
341 | future.waitForFinished(); | ||
342 | |||
343 | const QList<int> expected({ 2, 3, 4, 5 }); | ||
344 | QVERIFY(future.isFinished()); | ||
345 | QCOMPARE(future.value(), expected); | ||
346 | } | ||
347 | |||
348 | void AsyncTest::testSyncEach() | ||
349 | { | ||
350 | auto job = KAsync::start<QList<int>>( | ||
351 | []() -> QList<int> { | ||
352 | return { 1, 2, 3, 4 }; | ||
353 | }) | ||
354 | .each<QList<int>, int>( | ||
355 | [](const int &v) -> QList<int> { | ||
356 | return { v + 1 }; | ||
357 | }); | ||
358 | |||
359 | KAsync::Future<QList<int>> future = job.exec(); | ||
360 | |||
361 | const QList<int> expected({ 2, 3, 4, 5 }); | ||
362 | QVERIFY(future.isFinished()); | ||
363 | QCOMPARE(future.value(), expected); | ||
364 | } | ||
365 | |||
366 | void AsyncTest::testJoinedEach() | ||
367 | { | ||
368 | auto job1 = KAsync::start<QList<int>, int>( | ||
369 | [](int v, KAsync::Future<QList<int>> &future) { | ||
370 | new AsyncSimulator<QList<int>>(future, { v * 2 }); | ||
371 | }); | ||
372 | |||
373 | auto job = KAsync::start<QList<int>>( | ||
374 | []() -> QList<int> { | ||
375 | return { 1, 2, 3, 4 }; | ||
376 | }) | ||
377 | .each(job1); | ||
378 | |||
379 | auto future = job.exec(); | ||
380 | future.waitForFinished(); | ||
381 | |||
382 | const QList<int> expected({ 2, 4, 6, 8 }); | ||
383 | QVERIFY(future.isFinished()); | ||
384 | QCOMPARE(future.value(), expected); | ||
385 | } | ||
386 | |||
387 | void AsyncTest::testVoidEachThen() | ||
388 | { | ||
389 | QList<int> check; | ||
390 | auto job = KAsync::start<QList<int>>( | ||
391 | []() -> QList<int> { | ||
392 | return { 1, 2, 3, 4 }; | ||
393 | }).each<void, int>( | ||
394 | [&check](const int &v) { | ||
395 | check << v; | ||
396 | }).then<void>([](){}); | ||
397 | |||
398 | auto future = job.exec(); | ||
399 | |||
400 | const QList<int> expected({ 1, 2, 3, 4 }); | ||
401 | QVERIFY(future.isFinished()); | ||
402 | QCOMPARE(check, expected); | ||
403 | } | ||
404 | |||
405 | void AsyncTest::testAsyncVoidEachThen() | ||
406 | { | ||
407 | bool completedJob = false; | ||
408 | QList<int> check; | ||
409 | auto job = KAsync::start<QList<int>>( | ||
410 | [](KAsync::Future<QList<int> > &future) { | ||
411 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); | ||
412 | }).each<void, int>( | ||
413 | [&check](const int &v, KAsync::Future<void> &future) { | ||
414 | check << v; | ||
415 | new AsyncSimulator<void>(future); | ||
416 | }).then<void>([&completedJob](KAsync::Future<void> &future) { | ||
417 | completedJob = true; | ||
418 | future.setFinished(); | ||
419 | }); | ||
420 | |||
421 | auto future = job.exec(); | ||
422 | future.waitForFinished(); | ||
423 | |||
424 | const QList<int> expected({ 1, 2, 3, 4 }); | ||
425 | QVERIFY(future.isFinished()); | ||
426 | QVERIFY(completedJob); | ||
427 | QCOMPARE(check, expected); | ||
428 | } | ||
429 | |||
430 | |||
431 | |||
432 | |||
433 | |||
434 | void AsyncTest::testAsyncReduce() | ||
435 | { | ||
436 | auto job = KAsync::start<QList<int>>( | ||
437 | [](KAsync::Future<QList<int>> &future) { | ||
438 | new AsyncSimulator<QList<int>>(future, { 1, 2, 3, 4 }); | ||
439 | }) | ||
440 | .reduce<int, QList<int>>( | ||
441 | [](const QList<int> &list, KAsync::Future<int> &future) { | ||
442 | new AsyncSimulator<int>(future, | ||
443 | [list](KAsync::Future<int> &future) { | ||
444 | int sum = 0; | ||
445 | for (int i : list) sum += i; | ||
446 | future.setValue(sum); | ||
447 | future.setFinished(); | ||
448 | } | ||
449 | ); | ||
450 | }); | ||
451 | |||
452 | KAsync::Future<int> future = job.exec(); | ||
453 | future.waitForFinished(); | ||
454 | |||
455 | QVERIFY(future.isFinished()); | ||
456 | QCOMPARE(future.value(), 10); | ||
457 | } | ||
458 | |||
459 | void AsyncTest::testSyncReduce() | ||
460 | { | ||
461 | auto job = KAsync::start<QList<int>>( | ||
462 | []() -> QList<int> { | ||
463 | return { 1, 2, 3, 4 }; | ||
464 | }) | ||
465 | .reduce<int, QList<int>>( | ||
466 | [](const QList<int> &list) -> int { | ||
467 | int sum = 0; | ||
468 | for (int i : list) sum += i; | ||
469 | return sum; | ||
470 | }); | ||
471 | |||
472 | KAsync::Future<int> future = job.exec(); | ||
473 | |||
474 | QVERIFY(future.isFinished()); | ||
475 | QCOMPARE(future.value(), 10); | ||
476 | } | ||
477 | |||
478 | |||
479 | void AsyncTest::testJoinedReduce() | ||
480 | { | ||
481 | auto job1 = KAsync::start<int, QList<int>>( | ||
482 | [](const QList<int> &list, KAsync::Future<int> &future) { | ||
483 | int sum = 0; | ||
484 | for (int i : list) sum += i; | ||
485 | new AsyncSimulator<int>(future, sum); | ||
486 | }); | ||
487 | |||
488 | auto job = KAsync::start<QList<int>>( | ||
489 | []() -> QList<int> { | ||
490 | return { 1, 2, 3, 4 }; | ||
491 | }) | ||
492 | .reduce(job1); | ||
493 | |||
494 | auto future = job.exec(); | ||
495 | future.waitForFinished(); | ||
496 | |||
497 | QVERIFY(future.isFinished()); | ||
498 | QCOMPARE(future.value(), 10); | ||
499 | } | ||
500 | |||
501 | void AsyncTest::testVoidReduce() | ||
502 | { | ||
503 | // This must not compile (reduce with void result makes no sense) | ||
504 | #ifdef TEST_BUILD_FAIL | ||
505 | auto job = KAsync::start<QList<int>>( | ||
506 | []() -> QList<int> { | ||
507 | return { 1, 2, 3, 4 }; | ||
508 | }) | ||
509 | .reduce<void, QList<int>>( | ||
510 | [](const QList<int> &list) -> int { | ||
511 | return; | ||
512 | }); | ||
513 | |||
514 | auto future = job.exec(); | ||
515 | QVERIFY(future.isFinished()); | ||
516 | #endif | ||
517 | } | ||
518 | |||
519 | |||
520 | void AsyncTest::testProgressReporting() | ||
521 | { | ||
522 | static int progress; | ||
523 | progress = 0; | ||
524 | |||
525 | auto job = KAsync::start<void>( | ||
526 | [](KAsync::Future<void> &f) { | ||
527 | QTimer *timer = new QTimer(); | ||
528 | connect(timer, &QTimer::timeout, | ||
529 | [&f, timer]() { | ||
530 | f.setProgress(++progress); | ||
531 | if (progress == 100) { | ||
532 | timer->stop(); | ||
533 | timer->deleteLater(); | ||
534 | f.setFinished(); | ||
535 | } | ||
536 | }); | ||
537 | timer->start(1); | ||
538 | }); | ||
539 | |||
540 | int progressCheck = 0; | ||
541 | KAsync::FutureWatcher<void> watcher; | ||
542 | connect(&watcher, &KAsync::FutureWatcher<void>::futureProgress, | ||
543 | [&progressCheck](qreal progress) { | ||
544 | progressCheck++; | ||
545 | // FIXME: Don't use Q_ASSERT in unit tests | ||
546 | Q_ASSERT((int) progress == progressCheck); | ||
547 | }); | ||
548 | watcher.setFuture(job.exec()); | ||
549 | watcher.future().waitForFinished(); | ||
550 | |||
551 | QVERIFY(watcher.future().isFinished()); | ||
552 | QCOMPARE(progressCheck, 100); | ||
553 | } | ||
554 | |||
555 | void AsyncTest::testErrorHandler() | ||
556 | { | ||
557 | |||
558 | { | ||
559 | auto job = KAsync::start<int>( | ||
560 | [](KAsync::Future<int> &f) { | ||
561 | f.setError(1, QLatin1String("error")); | ||
562 | }); | ||
563 | |||
564 | auto future = job.exec(); | ||
565 | QVERIFY(future.isFinished()); | ||
566 | QCOMPARE(future.errorCode(), 1); | ||
567 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
568 | } | ||
569 | |||
570 | { | ||
571 | int error = 0; | ||
572 | auto job = KAsync::start<int>( | ||
573 | [](KAsync::Future<int> &f) { | ||
574 | f.setError(1, QLatin1String("error")); | ||
575 | }, | ||
576 | [&error](int errorCode, const QString &errorMessage) { | ||
577 | error += errorCode; | ||
578 | } | ||
579 | ); | ||
580 | |||
581 | auto future = job.exec(); | ||
582 | QVERIFY(future.isFinished()); | ||
583 | QCOMPARE(error, 1); | ||
584 | QCOMPARE(future.errorCode(), 1); | ||
585 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
586 | } | ||
587 | } | ||
588 | |||
589 | void AsyncTest::testErrorPropagation() | ||
590 | { | ||
591 | int error = 0; | ||
592 | bool called = false; | ||
593 | auto job = KAsync::start<int>( | ||
594 | [](KAsync::Future<int> &f) { | ||
595 | f.setError(1, QLatin1String("error")); | ||
596 | }) | ||
597 | .then<int, int>( | ||
598 | [&called](int v, KAsync::Future<int> &f) { | ||
599 | called = true; | ||
600 | f.setFinished(); | ||
601 | }, | ||
602 | [&error](int errorCode, const QString &errorMessage) { | ||
603 | error += errorCode; | ||
604 | } | ||
605 | ); | ||
606 | auto future = job.exec(); | ||
607 | QVERIFY(future.isFinished()); | ||
608 | QCOMPARE(future.errorCode(), 1); | ||
609 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
610 | QCOMPARE(called, false); | ||
611 | QCOMPARE(error, 1); | ||
612 | } | ||
613 | |||
614 | void AsyncTest::testErrorHandlerAsync() | ||
615 | { | ||
616 | { | ||
617 | auto job = KAsync::start<int>( | ||
618 | [](KAsync::Future<int> &f) { | ||
619 | new AsyncSimulator<int>(f, | ||
620 | [](KAsync::Future<int> &f) { | ||
621 | f.setError(1, QLatin1String("error")); | ||
622 | } | ||
623 | ); | ||
624 | } | ||
625 | ); | ||
626 | |||
627 | auto future = job.exec(); | ||
628 | future.waitForFinished(); | ||
629 | |||
630 | QVERIFY(future.isFinished()); | ||
631 | QCOMPARE(future.errorCode(), 1); | ||
632 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
633 | } | ||
634 | |||
635 | { | ||
636 | int error = 0; | ||
637 | auto job = KAsync::start<int>( | ||
638 | [](KAsync::Future<int> &f) { | ||
639 | new AsyncSimulator<int>(f, | ||
640 | [](KAsync::Future<int> &f) { | ||
641 | f.setError(1, QLatin1String("error")); | ||
642 | } | ||
643 | ); | ||
644 | }, | ||
645 | [&error](int errorCode, const QString &errorMessage) { | ||
646 | error += errorCode; | ||
647 | } | ||
648 | ); | ||
649 | |||
650 | auto future = job.exec(); | ||
651 | future.waitForFinished(); | ||
652 | |||
653 | QVERIFY(future.isFinished()); | ||
654 | QCOMPARE(error, 1); | ||
655 | QCOMPARE(future.errorCode(), 1); | ||
656 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
657 | } | ||
658 | } | ||
659 | |||
660 | void AsyncTest::testErrorPropagationAsync() | ||
661 | { | ||
662 | int error = 0; | ||
663 | bool called = false; | ||
664 | auto job = KAsync::start<int>( | ||
665 | [](KAsync::Future<int> &f) { | ||
666 | new AsyncSimulator<int>(f, | ||
667 | [](KAsync::Future<int> &f) { | ||
668 | f.setError(1, QLatin1String("error")); | ||
669 | } | ||
670 | ); | ||
671 | }) | ||
672 | .then<int, int>( | ||
673 | [&called](int v, KAsync::Future<int> &f) { | ||
674 | called = true; | ||
675 | f.setFinished(); | ||
676 | }, | ||
677 | [&error](int errorCode, const QString &errorMessage) { | ||
678 | error += errorCode; | ||
679 | } | ||
680 | ); | ||
681 | |||
682 | auto future = job.exec(); | ||
683 | future.waitForFinished(); | ||
684 | |||
685 | QVERIFY(future.isFinished()); | ||
686 | QCOMPARE(future.errorCode(), 1); | ||
687 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
688 | QCOMPARE(called, false); | ||
689 | QCOMPARE(error, 1); | ||
690 | } | ||
691 | |||
692 | void AsyncTest::testNestedErrorPropagation() | ||
693 | { | ||
694 | int error = 0; | ||
695 | auto job = KAsync::start<void>([](){}) | ||
696 | .then<void>(KAsync::error<void>(1, QLatin1String("error"))) //Nested job that throws error | ||
697 | .then<void>([](KAsync::Future<void> &future) { | ||
698 | //We should never get here | ||
699 | Q_ASSERT(false); | ||
700 | }, | ||
701 | [&error](int errorCode, const QString &errorMessage) { | ||
702 | error += errorCode; | ||
703 | } | ||
704 | ); | ||
705 | auto future = job.exec(); | ||
706 | |||
707 | future.waitForFinished(); | ||
708 | |||
709 | QVERIFY(future.isFinished()); | ||
710 | QCOMPARE(future.errorCode(), 1); | ||
711 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
712 | QCOMPARE(error, 1); | ||
713 | } | ||
714 | |||
715 | |||
716 | |||
717 | |||
718 | void AsyncTest::testChainingRunningJob() | ||
719 | { | ||
720 | int check = 0; | ||
721 | |||
722 | auto job = KAsync::start<int>( | ||
723 | [&check](KAsync::Future<int> &future) { | ||
724 | QTimer *timer = new QTimer(); | ||
725 | QObject::connect(timer, &QTimer::timeout, | ||
726 | [&future, &check]() { | ||
727 | ++check; | ||
728 | future.setValue(42); | ||
729 | future.setFinished(); | ||
730 | }); | ||
731 | QObject::connect(timer, &QTimer::timeout, | ||
732 | timer, &QObject::deleteLater); | ||
733 | timer->setSingleShot(true); | ||
734 | timer->start(500); | ||
735 | }); | ||
736 | |||
737 | auto future1 = job.exec(); | ||
738 | QTest::qWait(200); | ||
739 | |||
740 | auto job2 = job.then<int, int>( | ||
741 | [&check](int in) -> int { | ||
742 | ++check; | ||
743 | return in * 2; | ||
744 | }); | ||
745 | |||
746 | auto future2 = job2.exec(); | ||
747 | QVERIFY(!future1.isFinished()); | ||
748 | future2.waitForFinished(); | ||
749 | |||
750 | QEXPECT_FAIL("", "Chaining new job to a running job no longer executes the new job. " | ||
751 | "This is a trade-off for being able to re-execute single job multiple times.", | ||
752 | Abort); | ||
753 | |||
754 | QCOMPARE(check, 2); | ||
755 | |||
756 | QVERIFY(future1.isFinished()); | ||
757 | QVERIFY(future2.isFinished()); | ||
758 | QCOMPARE(future1.value(), 42); | ||
759 | QCOMPARE(future2.value(), 84); | ||
760 | } | ||
761 | |||
762 | void AsyncTest::testChainingFinishedJob() | ||
763 | { | ||
764 | int check = 0; | ||
765 | |||
766 | auto job = KAsync::start<int>( | ||
767 | [&check]() -> int { | ||
768 | ++check; | ||
769 | return 42; | ||
770 | }); | ||
771 | |||
772 | auto future1 = job.exec(); | ||
773 | QVERIFY(future1.isFinished()); | ||
774 | |||
775 | auto job2 = job.then<int, int>( | ||
776 | [&check](int in) -> int { | ||
777 | ++check; | ||
778 | return in * 2; | ||
779 | }); | ||
780 | |||
781 | auto future2 = job2.exec(); | ||
782 | QVERIFY(future2.isFinished()); | ||
783 | |||
784 | QEXPECT_FAIL("", "Resuming finished job by chaining a new job and calling exec() is no longer suppported. " | ||
785 | "This is a trade-off for being able to re-execute single job multiple times.", | ||
786 | Abort); | ||
787 | |||
788 | QCOMPARE(check, 2); | ||
789 | |||
790 | QCOMPARE(future1.value(), 42); | ||
791 | QCOMPARE(future2.value(), 84); | ||
792 | } | ||
793 | |||
794 | /* | ||
795 | * We want to be able to execute jobs without keeping a handle explicitly alive. | ||
796 | * If the future handle inside the continuation would keep the executor alive, that would probably already work. | ||
797 | */ | ||
798 | void AsyncTest::testLifetimeWithoutHandle() | ||
799 | { | ||
800 | bool done = false; | ||
801 | { | ||
802 | auto job = KAsync::start<void>([&done](KAsync::Future<void> &future) { | ||
803 | QTimer *timer = new QTimer(); | ||
804 | QObject::connect(timer, &QTimer::timeout, | ||
805 | [&future, &done]() { | ||
806 | done = true; | ||
807 | future.setFinished(); | ||
808 | }); | ||
809 | QObject::connect(timer, &QTimer::timeout, | ||
810 | timer, &QObject::deleteLater); | ||
811 | timer->setSingleShot(true); | ||
812 | timer->start(500); | ||
813 | }); | ||
814 | job.exec(); | ||
815 | } | ||
816 | |||
817 | QTRY_VERIFY(done); | ||
818 | } | ||
819 | |||
820 | /* | ||
821 | * The future handle should keep the executor alive, and the future reference should probably not become invalid inside the continuation, | ||
822 | * until the job is done (alternatively a copy of the future inside the continuation should work as well). | ||
823 | */ | ||
824 | void AsyncTest::testLifetimeWithHandle() | ||
825 | { | ||
826 | KAsync::Future<void> future; | ||
827 | { | ||
828 | auto job = KAsync::start<void>([](KAsync::Future<void> &future) { | ||
829 | QTimer *timer = new QTimer(); | ||
830 | QObject::connect(timer, &QTimer::timeout, | ||
831 | [&future]() { | ||
832 | future.setFinished(); | ||
833 | }); | ||
834 | QObject::connect(timer, &QTimer::timeout, | ||
835 | timer, &QObject::deleteLater); | ||
836 | timer->setSingleShot(true); | ||
837 | timer->start(500); | ||
838 | }); | ||
839 | future = job.exec(); | ||
840 | } | ||
841 | |||
842 | QTRY_VERIFY(future.isFinished()); | ||
843 | } | ||
844 | |||
845 | void AsyncTest::benchmarkSyncThenExecutor() | ||
846 | { | ||
847 | auto job = KAsync::start<int>( | ||
848 | []() -> int { | ||
849 | return 0; | ||
850 | }); | ||
851 | |||
852 | QBENCHMARK { | ||
853 | job.exec(); | ||
854 | } | ||
855 | } | ||
856 | |||
857 | QTEST_MAIN(AsyncTest); | ||
858 | |||
859 | #include "asynctest.moc" | ||