diff options
author | Dan Vrátil <dvratil@redhat.com> | 2014-12-11 14:22:27 +0100 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2014-12-11 14:22:27 +0100 |
commit | 1aee1bda9fc81c888ad18fea107c271133dd5442 (patch) | |
tree | ffb728821a78c0bdb1ff82543b7b3c18553da1c2 /async/autotests/asynctest.cpp | |
parent | 81b8158cca02defbfce5f1d59f06f7448fb5ea57 (diff) | |
download | sink-1aee1bda9fc81c888ad18fea107c271133dd5442.tar.gz sink-1aee1bda9fc81c888ad18fea107c271133dd5442.zip |
Async: change syntax of callables
We now pass our own Async::Future to each task, instead of expecting tasks
to return their future. This allows us to re-use the same Future for repeated
invocations, like in the Each task.
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r-- | async/autotests/asynctest.cpp | 36 |
1 files changed, 24 insertions, 12 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 786ff3c..19e40f7 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp | |||
@@ -40,28 +40,26 @@ public: | |||
40 | private Q_SLOTS: | 40 | private Q_SLOTS: |
41 | void testSyncPromises(); | 41 | void testSyncPromises(); |
42 | void testAsyncPromises(); | 42 | void testAsyncPromises(); |
43 | void testSyncEach(); | ||
43 | }; | 44 | }; |
44 | 45 | ||
45 | void AsyncTest::testSyncPromises() | 46 | void AsyncTest::testSyncPromises() |
46 | { | 47 | { |
47 | auto baseJob = Async::start<int>( | 48 | auto baseJob = Async::start<int>( |
48 | []() -> Async::Future<int> { | 49 | [](Async::Future<int> &f) { |
49 | auto f = Async::Future<int>(42); | 50 | f.setValue(42); |
50 | f.setFinished(); | 51 | f.setFinished(); |
51 | return f; | ||
52 | }) | 52 | }) |
53 | .then<QString, int>( | 53 | .then<QString, int>( |
54 | [](int v) -> Async::Future<QString> { | 54 | [](int v, Async::Future<QString> &f) { |
55 | auto f = Async::Future<QString>("Result is " + QString::number(v)); | 55 | f.setValue("Result is " + QString::number(v)); |
56 | f.setFinished(); | 56 | f.setFinished(); |
57 | return f; | ||
58 | }); | 57 | }); |
59 | 58 | ||
60 | auto job = baseJob.then<QString, QString>( | 59 | auto job = baseJob.then<QString, QString>( |
61 | [](const QString &v) -> Async::Future<QString> { | 60 | [](const QString &v, Async::Future<QString> &f) { |
62 | auto f = Async::Future<QString>(v.toUpper()); | 61 | f.setValue(v.toUpper()); |
63 | f.setFinished(); | 62 | f.setFinished(); |
64 | return f; | ||
65 | }); | 63 | }); |
66 | 64 | ||
67 | job.exec(); | 65 | job.exec(); |
@@ -73,8 +71,7 @@ void AsyncTest::testSyncPromises() | |||
73 | void AsyncTest::testAsyncPromises() | 71 | void AsyncTest::testAsyncPromises() |
74 | { | 72 | { |
75 | auto job = Async::start<int>( | 73 | auto job = Async::start<int>( |
76 | []() -> Async::Future<int> { | 74 | [](Async::Future<int> &future) { |
77 | Async::Future<int> future(-1); | ||
78 | QTimer *timer = new QTimer(); | 75 | QTimer *timer = new QTimer(); |
79 | QObject::connect(timer, &QTimer::timeout, | 76 | QObject::connect(timer, &QTimer::timeout, |
80 | [&]() { | 77 | [&]() { |
@@ -85,7 +82,6 @@ void AsyncTest::testAsyncPromises() | |||
85 | timer, &QObject::deleteLater); | 82 | timer, &QObject::deleteLater); |
86 | timer->setSingleShot(true); | 83 | timer->setSingleShot(true); |
87 | timer->start(200); | 84 | timer->start(200); |
88 | return future; | ||
89 | }); | 85 | }); |
90 | 86 | ||
91 | job.exec(); | 87 | job.exec(); |
@@ -93,6 +89,22 @@ void AsyncTest::testAsyncPromises() | |||
93 | QCOMPARE(future.value(), 42); | 89 | QCOMPARE(future.value(), 42); |
94 | } | 90 | } |
95 | 91 | ||
92 | void AsyncTest::testSyncEach() | ||
93 | { | ||
94 | /* | ||
95 | auto job = Async::start<QList<int>>( | ||
96 | []() -> Async::Future<QList<int>> { | ||
97 | Async::Future<QList<int>> future(QList<int>{ 1, 2, 3, 4 }); | ||
98 | future.setFinished(); | ||
99 | return future; | ||
100 | }) | ||
101 | .each<QList<int>, int>( | ||
102 | [](const int &v, Async::Future<QList<int>> &future) { | ||
103 | setFinished(); | ||
104 | }); | ||
105 | */ | ||
106 | } | ||
107 | |||
96 | 108 | ||
97 | QTEST_MAIN(AsyncTest); | 109 | QTEST_MAIN(AsyncTest); |
98 | 110 | ||