diff options
author | Dan Vrátil <dvratil@redhat.com> | 2015-02-21 12:11:42 +0100 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2015-02-21 12:11:44 +0100 |
commit | 3b8ebe6d4235f5ba12bc9c9854a6dd28cbff06b5 (patch) | |
tree | 4e2a987e6c62523994965789387cace578e4c8d8 /async/autotests/asynctest.cpp | |
parent | 76ec0cfe075e3af758657f9aecab7d7ce7e8d387 (diff) | |
download | sink-3b8ebe6d4235f5ba12bc9c9854a6dd28cbff06b5.tar.gz sink-3b8ebe6d4235f5ba12bc9c9854a6dd28cbff06b5.zip |
Async: allow appending Jobs to already running or finished Jobs
When user gets a Job (from a method call for instance), which is already running
or might have even finished already, they can still append a new Job to the chain
and re-execute it. The Job will internally chain up to the last finished Job, use
it's result and continue from the next Job in the chain. If a Job in the chain is
still running, it will wait for it to finish and pass the result to the next Job
in the chain.
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r-- | async/autotests/asynctest.cpp | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 73026bb..7437608 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp | |||
@@ -59,6 +59,10 @@ private Q_SLOTS: | |||
59 | 59 | ||
60 | void testErrorHandler(); | 60 | void testErrorHandler(); |
61 | 61 | ||
62 | |||
63 | void testChainingRunningJob(); | ||
64 | void testChainingFinishedJob(); | ||
65 | |||
62 | void benchmarkSyncThenExecutor(); | 66 | void benchmarkSyncThenExecutor(); |
63 | 67 | ||
64 | private: | 68 | private: |
@@ -399,6 +403,74 @@ void AsyncTest::testErrorHandler() | |||
399 | 403 | ||
400 | 404 | ||
401 | 405 | ||
406 | void AsyncTest::testChainingRunningJob() | ||
407 | { | ||
408 | int check = 0; | ||
409 | |||
410 | auto job = Async::start<int>( | ||
411 | [&check](Async::Future<int> &future) { | ||
412 | QTimer *timer = new QTimer(); | ||
413 | QObject::connect(timer, &QTimer::timeout, | ||
414 | [&future, &check]() { | ||
415 | ++check; | ||
416 | future.setValue(42); | ||
417 | future.setFinished(); | ||
418 | }); | ||
419 | QObject::connect(timer, &QTimer::timeout, | ||
420 | timer, &QObject::deleteLater); | ||
421 | timer->setSingleShot(true); | ||
422 | timer->start(500); | ||
423 | }); | ||
424 | |||
425 | auto future1 = job.exec(); | ||
426 | QTest::qWait(200); | ||
427 | |||
428 | auto job2 = job.then<int, int>( | ||
429 | [&check](int in) -> int { | ||
430 | ++check; | ||
431 | return in * 2; | ||
432 | }); | ||
433 | |||
434 | auto future2 = job2.exec(); | ||
435 | QVERIFY(!future1.isFinished()); | ||
436 | future2.waitForFinished(); | ||
437 | |||
438 | QCOMPARE(check, 2); | ||
439 | QVERIFY(future1.isFinished()); | ||
440 | QVERIFY(future2.isFinished()); | ||
441 | QCOMPARE(future1.value(), 42); | ||
442 | QCOMPARE(future2.value(), 84); | ||
443 | } | ||
444 | |||
445 | void AsyncTest::testChainingFinishedJob() | ||
446 | { | ||
447 | int check = 0; | ||
448 | |||
449 | auto job = Async::start<int>( | ||
450 | [&check]() -> int { | ||
451 | ++check; | ||
452 | return 42; | ||
453 | }); | ||
454 | |||
455 | auto future1 = job.exec(); | ||
456 | QVERIFY(future1.isFinished()); | ||
457 | |||
458 | auto job2 = job.then<int, int>( | ||
459 | [&check](int in) -> int { | ||
460 | ++check; | ||
461 | return in * 2; | ||
462 | }); | ||
463 | |||
464 | auto future2 = job2.exec(); | ||
465 | QVERIFY(future2.isFinished()); | ||
466 | |||
467 | QCOMPARE(check, 2); | ||
468 | QCOMPARE(future1.value(), 42); | ||
469 | QCOMPARE(future2.value(), 84); | ||
470 | } | ||
471 | |||
472 | |||
473 | |||
402 | 474 | ||
403 | 475 | ||
404 | void AsyncTest::benchmarkSyncThenExecutor() | 476 | void AsyncTest::benchmarkSyncThenExecutor() |