From 6faa4d4a87662a6254542da37b3a6fff528e0a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morten=20Johan=20S=C3=B8rvig?= Date: Mon, 11 Dec 2017 21:12:10 +0100 Subject: QFuture: Wait for result on iterator advance MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wait for the result at the target index if the future is running and the iterator index is past the current result count. Determine if there is a result at the target index after waitForResult() returns, and return -1/end if not. Also support decrementing the end iterator. In this case wait for the future to finish in order to get the final result count. Task-number: QTBUG-59811 Change-Id: I8fcc711bab2e72c3c5196a55b794d25e18bb324d Reviewed-by: MÃ¥rten Nordheim --- tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 67 +++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'tests/auto/corelib/thread/qfuture') diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index b8c82c2ea0..a42454124e 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -49,6 +49,24 @@ struct ResultStoreInt : QtPrivate::ResultStoreBase ~ResultStoreInt() { clear(); } }; +class LambdaThread : public QThread +{ +public: + LambdaThread(std::function fn) + :m_fn(fn) + { + + } + + void run() override + { + m_fn(); + } + +private: + std::function m_fn; +}; + class tst_QFuture: public QObject { Q_OBJECT @@ -67,6 +85,7 @@ private slots: void resultsAsList(); void implicitConversions(); void iterators(); + void iteratorsThread(); void pause(); void throttling(); void voidConversions(); @@ -1144,6 +1163,54 @@ void tst_QFuture::iterators() } } } +void tst_QFuture::iteratorsThread() +{ + const int expectedResultCount = 10; + const int delay = 10; + QFutureInterface futureInterface; + + // Create result producer thread. The results are + // produced with delays in order to make the consumer + // wait. + QSemaphore sem; + LambdaThread thread = {[=, &futureInterface, &sem](){ + for (int i = 1; i <= expectedResultCount; i += 2) { + int result = i; + futureInterface.reportResult(&result); + result = i + 1; + futureInterface.reportResult(&result); + } + + sem.acquire(2); + futureInterface.reportFinished(); + }}; + + futureInterface.reportStarted(); + QFuture future = futureInterface.future(); + + // Iterate over results while the thread is producing them. + thread.start(); + int resultCount = 0; + int resultSum = 0; + for (int result : future) { + sem.release(); + ++resultCount; + resultSum += result; + } + thread.wait(); + + QCOMPARE(resultCount, expectedResultCount); + QCOMPARE(resultSum, expectedResultCount * (expectedResultCount + 1) / 2); + + // Reverse iterate + resultSum = 0; + QFutureIterator it(future); + it.toBack(); + while (it.hasPrevious()) + resultSum += it.previous(); + + QCOMPARE(resultSum, expectedResultCount * (expectedResultCount + 1) / 2); +} class SignalSlotObject : public QObject { -- cgit v1.2.3