diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-09-03 15:05:48 +0200 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2020-09-03 20:45:00 +0200 |
commit | 225cc6095499dca17054f969b47f6d0bab95d5cd (patch) | |
tree | eebd0d7605c0a9090213800db481c384fff15f6e /tests | |
parent | 85b0335c419118629b05b7c8891b844387382700 (diff) |
Fix flaky QFutureWatcher::startFinish() test
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 <marten.nordheim@qt.io>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/thread/qfuturewatcher/tst_qfuturewatcher.cpp | 33 |
1 files 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<void> futureWatcher; - QSignalSpy startedSpy(&futureWatcher, &QFutureWatcher<void>::started); - QSignalSpy finishedSpy(&futureWatcher, &QFutureWatcher<void>::finished); - - QVERIFY(startedSpy.isValid()); - QVERIFY(finishedSpy.isValid()); + int startedCount = 0; + int finishedCount = 0; + QObject::connect(&futureWatcher, &QFutureWatcher<void>::started, + [&startedCount, &finishedCount](){ + ++startedCount; + QCOMPARE(startedCount, 1); + QCOMPARE(finishedCount, 0); + }); + QObject::connect(&futureWatcher, &QFutureWatcher<void>::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 &) |