diff options
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r-- | async/autotests/asynctest.cpp | 166 |
1 files changed, 151 insertions, 15 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 65e604f..d709567 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp | |||
@@ -27,6 +27,8 @@ | |||
27 | #include <QtTest/QTest> | 27 | #include <QtTest/QTest> |
28 | #include <QDebug> | 28 | #include <QDebug> |
29 | 29 | ||
30 | #include <functional> | ||
31 | |||
30 | class AsyncTest : public QObject | 32 | class AsyncTest : public QObject |
31 | { | 33 | { |
32 | Q_OBJECT | 34 | Q_OBJECT |
@@ -62,6 +64,9 @@ private Q_SLOTS: | |||
62 | 64 | ||
63 | void testProgressReporting(); | 65 | void testProgressReporting(); |
64 | void testErrorHandler(); | 66 | void testErrorHandler(); |
67 | void testErrorPropagation(); | ||
68 | void testErrorHandlerAsync(); | ||
69 | void testErrorPropagationAsync(); | ||
65 | 70 | ||
66 | void testChainingRunningJob(); | 71 | void testChainingRunningJob(); |
67 | void testChainingFinishedJob(); | 72 | void testChainingFinishedJob(); |
@@ -92,8 +97,25 @@ private: | |||
92 | mTimer.start(200); | 97 | mTimer.start(200); |
93 | } | 98 | } |
94 | 99 | ||
100 | AsyncSimulator(Async::Future<T> &future, std::function<void(Async::Future<T>&)> callback) | ||
101 | : mFuture(future) | ||
102 | , mCallback(callback) | ||
103 | { | ||
104 | QObject::connect(&mTimer, &QTimer::timeout, | ||
105 | [this]() { | ||
106 | mCallback(mFuture); | ||
107 | }); | ||
108 | QObject::connect(&mTimer, &QTimer::timeout, | ||
109 | [this]() { | ||
110 | delete this; | ||
111 | }); | ||
112 | mTimer.setSingleShot(true); | ||
113 | mTimer.start(200); | ||
114 | } | ||
115 | |||
95 | private: | 116 | private: |
96 | Async::Future<T> mFuture; | 117 | Async::Future<T> mFuture; |
118 | std::function<void(Async::Future<T>&)> mCallback; | ||
97 | T mResult; | 119 | T mResult; |
98 | QTimer mTimer; | 120 | QTimer mTimer; |
99 | }; | 121 | }; |
@@ -390,18 +412,14 @@ void AsyncTest::testAsyncReduce() | |||
390 | }) | 412 | }) |
391 | .reduce<int, QList<int>>( | 413 | .reduce<int, QList<int>>( |
392 | [](const QList<int> &list, Async::Future<int> &future) { | 414 | [](const QList<int> &list, Async::Future<int> &future) { |
393 | QTimer *timer = new QTimer(); | 415 | new AsyncSimulator<int>(future, |
394 | QObject::connect(timer, &QTimer::timeout, | 416 | [list](Async::Future<int> &future) { |
395 | [list, &future]() { | 417 | int sum = 0; |
396 | int sum = 0; | 418 | for (int i : list) sum += i; |
397 | for (int i : list) sum += i; | 419 | future.setValue(sum); |
398 | future.setValue(sum); | 420 | future.setFinished(); |
399 | future.setFinished(); | 421 | } |
400 | }); | 422 | ); |
401 | QObject::connect(timer, &QTimer::timeout, | ||
402 | timer, &QObject::deleteLater); | ||
403 | timer->setSingleShot(true); | ||
404 | timer->start(0); | ||
405 | }); | 423 | }); |
406 | 424 | ||
407 | Async::Future<int> future = job.exec(); | 425 | Async::Future<int> future = job.exec(); |
@@ -509,27 +527,145 @@ void AsyncTest::testProgressReporting() | |||
509 | 527 | ||
510 | void AsyncTest::testErrorHandler() | 528 | void AsyncTest::testErrorHandler() |
511 | { | 529 | { |
530 | |||
531 | { | ||
532 | auto job = Async::start<int>( | ||
533 | [](Async::Future<int> &f) { | ||
534 | f.setError(1, "error"); | ||
535 | }); | ||
536 | |||
537 | auto future = job.exec(); | ||
538 | QVERIFY(future.isFinished()); | ||
539 | QCOMPARE(future.errorCode(), 1); | ||
540 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
541 | } | ||
542 | |||
543 | { | ||
544 | int error = 0; | ||
545 | auto job = Async::start<int>( | ||
546 | [](Async::Future<int> &f) { | ||
547 | f.setError(1, "error"); | ||
548 | }, | ||
549 | [&error](int errorCode, const QString &errorMessage) { | ||
550 | error += errorCode; | ||
551 | } | ||
552 | ); | ||
553 | |||
554 | auto future = job.exec(); | ||
555 | QVERIFY(future.isFinished()); | ||
556 | QCOMPARE(error, 1); | ||
557 | QCOMPARE(future.errorCode(), 1); | ||
558 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
559 | } | ||
560 | } | ||
561 | |||
562 | void AsyncTest::testErrorPropagation() | ||
563 | { | ||
512 | int error = 0; | 564 | int error = 0; |
565 | bool called = false; | ||
513 | auto job = Async::start<int>( | 566 | auto job = Async::start<int>( |
514 | [](Async::Future<int> &f) { | 567 | [](Async::Future<int> &f) { |
515 | f.setError(1, "error"); | 568 | f.setError(1, "error"); |
516 | }) | 569 | }) |
517 | .then<int, int>( | 570 | .then<int, int>( |
518 | [](int v, Async::Future<int> &f) { | 571 | [&called](int v, Async::Future<int> &f) { |
572 | called = true; | ||
519 | f.setFinished(); | 573 | f.setFinished(); |
520 | }, | 574 | }, |
521 | [&error](int errorCode, const QString &errorMessage) { | 575 | [&error](int errorCode, const QString &errorMessage) { |
522 | error = errorCode; | 576 | error += errorCode; |
523 | } | 577 | } |
524 | ); | 578 | ); |
525 | auto future = job.exec(); | 579 | auto future = job.exec(); |
526 | future.waitForFinished(); | 580 | QVERIFY(future.isFinished()); |
581 | QCOMPARE(future.errorCode(), 1); | ||
582 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
583 | QCOMPARE(called, false); | ||
527 | QCOMPARE(error, 1); | 584 | QCOMPARE(error, 1); |
585 | } | ||
586 | |||
587 | void AsyncTest::testErrorHandlerAsync() | ||
588 | { | ||
589 | { | ||
590 | auto job = Async::start<int>( | ||
591 | [](Async::Future<int> &f) { | ||
592 | new AsyncSimulator<int>(f, | ||
593 | [](Async::Future<int> &f) { | ||
594 | f.setError(1, "error"); | ||
595 | } | ||
596 | ); | ||
597 | } | ||
598 | ); | ||
599 | |||
600 | auto future = job.exec(); | ||
601 | future.waitForFinished(); | ||
602 | |||
603 | QVERIFY(future.isFinished()); | ||
604 | QCOMPARE(future.errorCode(), 1); | ||
605 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
606 | } | ||
607 | |||
608 | { | ||
609 | int error = 0; | ||
610 | auto job = Async::start<int>( | ||
611 | [](Async::Future<int> &f) { | ||
612 | new AsyncSimulator<int>(f, | ||
613 | [](Async::Future<int> &f) { | ||
614 | f.setError(1, "error"); | ||
615 | } | ||
616 | ); | ||
617 | }, | ||
618 | [&error](int errorCode, const QString &errorMessage) { | ||
619 | error += errorCode; | ||
620 | } | ||
621 | ); | ||
622 | |||
623 | auto future = job.exec(); | ||
624 | future.waitForFinished(); | ||
625 | |||
626 | QVERIFY(future.isFinished()); | ||
627 | QCOMPARE(error, 1); | ||
628 | QCOMPARE(future.errorCode(), 1); | ||
629 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
630 | } | ||
631 | } | ||
632 | |||
633 | void AsyncTest::testErrorPropagationAsync() | ||
634 | { | ||
635 | int error = 0; | ||
636 | bool called = false; | ||
637 | auto job = Async::start<int>( | ||
638 | [](Async::Future<int> &f) { | ||
639 | new AsyncSimulator<int>(f, | ||
640 | [](Async::Future<int> &f) { | ||
641 | f.setError(1, "error"); | ||
642 | } | ||
643 | ); | ||
644 | }) | ||
645 | .then<int, int>( | ||
646 | [&called](int v, Async::Future<int> &f) { | ||
647 | called = true; | ||
648 | f.setFinished(); | ||
649 | }, | ||
650 | [&error](int errorCode, const QString &errorMessage) { | ||
651 | error += errorCode; | ||
652 | } | ||
653 | ); | ||
654 | |||
655 | auto future = job.exec(); | ||
656 | future.waitForFinished(); | ||
657 | |||
528 | QVERIFY(future.isFinished()); | 658 | QVERIFY(future.isFinished()); |
659 | QCOMPARE(future.errorCode(), 1); | ||
660 | QCOMPARE(future.errorMessage(), QString::fromLatin1("error")); | ||
661 | QCOMPARE(called, false); | ||
662 | QCOMPARE(error, 1); | ||
529 | } | 663 | } |
530 | 664 | ||
531 | 665 | ||
532 | 666 | ||
667 | |||
668 | |||
533 | void AsyncTest::testChainingRunningJob() | 669 | void AsyncTest::testChainingRunningJob() |
534 | { | 670 | { |
535 | int check = 0; | 671 | int check = 0; |