summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMorten Johan Sørvig <morten.sorvig@qt.io>2017-12-11 21:12:10 +0100
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2019-05-13 10:41:25 +0200
commit6faa4d4a87662a6254542da37b3a6fff528e0a6c (patch)
tree04578d13b1c923a3a34e4f9ace27d2884759d76a /tests
parent486c55d7437c247472e287f0f2ccc883399d9f79 (diff)
QFuture: Wait for result on iterator advance
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 <marten.nordheim@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/thread/qfuture/tst_qfuture.cpp67
1 files changed, 67 insertions, 0 deletions
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<int>(); }
};
+class LambdaThread : public QThread
+{
+public:
+ LambdaThread(std::function<void ()> fn)
+ :m_fn(fn)
+ {
+
+ }
+
+ void run() override
+ {
+ m_fn();
+ }
+
+private:
+ std::function<void ()> 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<int> 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<int> 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<int> it(future);
+ it.toBack();
+ while (it.hasPrevious())
+ resultSum += it.previous();
+
+ QCOMPARE(resultSum, expectedResultCount * (expectedResultCount + 1) / 2);
+}
class SignalSlotObject : public QObject
{