From 225cc6095499dca17054f969b47f6d0bab95d5cd Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Thu, 3 Sep 2020 15:05:48 +0200 Subject: [PATCH] Fix flaky QFutureWatcher::startFinish() test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since waiting for a spy employs polling, it may happen that while waiting for a startedSpy we had received already a signal for finishedSpy. This explains current flakiness. The fix is to connect to lambdas instead and update the hit count accordingly. Inside lambdas we also ensure the correct order for started / finised signals. After waitForFinished() unblocks we ensure that possible pending asynchronous signals (started / finished) are processed and check the final state. Task-number: QTBUG-83076 Change-Id: I16963ef9c011cb613d7b409d3e3032303a942336 Reviewed-by: MÃ¥rten Nordheim --- .../qfuturewatcher/tst_qfuturewatcher.cpp | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp b/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp index 63277b02df..0f14336a24 100644 --- a/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp +++ b/tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp @@ -82,20 +82,31 @@ void tst_QFutureWatcher::startFinish() { QFutureWatcher futureWatcher; - QSignalSpy startedSpy(&futureWatcher, &QFutureWatcher::started); - QSignalSpy finishedSpy(&futureWatcher, &QFutureWatcher::finished); - - QVERIFY(startedSpy.isValid()); - QVERIFY(finishedSpy.isValid()); + int startedCount = 0; + int finishedCount = 0; + QObject::connect(&futureWatcher, &QFutureWatcher::started, + [&startedCount, &finishedCount](){ + ++startedCount; + QCOMPARE(startedCount, 1); + QCOMPARE(finishedCount, 0); + }); + QObject::connect(&futureWatcher, &QFutureWatcher::finished, + [&startedCount, &finishedCount](){ + ++finishedCount; + QCOMPARE(startedCount, 1); + QCOMPARE(finishedCount, 1); + }); futureWatcher.setFuture(QtConcurrent::run(sleeper)); - QVERIFY(startedSpy.wait()); - QCOMPARE(startedSpy.count(), 1); - QCOMPARE(finishedSpy.count(), 0); futureWatcher.future().waitForFinished(); - QVERIFY(finishedSpy.wait()); - QCOMPARE(startedSpy.count(), 1); - QCOMPARE(finishedSpy.count(), 1); + + // waitForFinished() may unblock before asynchronous + // started() and finished() signals are delivered to the main thread. + // prosessEvents() should empty the pending queue. + qApp->processEvents(); + + QCOMPARE(startedCount, 1); + QCOMPARE(finishedCount, 1); } void mapSleeper(int &)