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>
bb10
Jarek Kobus 2020-09-03 15:05:48 +02:00
parent 85b0335c41
commit 225cc60954
1 changed files with 22 additions and 11 deletions

View File

@ -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 &)