[Interest] QTest + QSignalSpy doesn't seem to work with deferred signals

Viktor Engelmann viktor.engelmann at qt.io
Thu Jan 19 10:29:08 CET 2017

QSignalSpy::wait has a race condition though. When the signal is emitted
from a different thread, it might come before you called .wait and then
the wait will time out and return false (so it looks like the signal
wasn't emitted). You can store the value of spy.size() and after the
wait compare it to the current spy.size(). That is more robust, but when
the signal is emitted before you start the .wait, the .wait will still
wait for the full timeout time.

QTRY_COMPARE terminates as soon as the condition is met, so
QTRY_COMPARE(spy.size(), 1) is better, but it uses a timeout of only 5
seconds, which should be enough in most cases, but I have seen cases
where a signal came 11 seconds after I caused it to be emitted (due to
network and/or system load). I used QTRY_COMPARE_WITH_TIMEOUT instead to
set a higher timeout.



Am 18.01.2017 um 18:28 schrieb Konrad Rosenbaum:
> Hi,
> On Wednesday, January 18, 2017 10:16:30 Bob Hood wrote:
>> I'm trying to develop a unit test for an deferred signal; i.e. one that
>> won't be emitted until the event loop has been allowed to run.  The case
>> looks like:
>>    void MyTest::user_info_deferred()
>>    {
>>        QVERIFY(qnam_class_ptr != nullptr);
>>        QSignalSpy spy(qnam_class_ptr, &QNAMClass::signal_userInfoReceived);
>>        // Retrieve account user information (deferred)
>>        QCOMPARE(qname_class_ptr->userInfo(), true);
> I think you need to insert a spy.wait(...) here. Give the event loop a chance 
> to run...
>>        QCOMPARE(spy.count(), 1);   // make sure the signal was emitted
>> exactly one time
>> QNAMClassUser info = qvariant_cast<QNAMClassUser>(spy.at(0).at(0));
>>    }
> [cut]
> 	Konrad
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest


Viktor Engelmann
Software Engineer

The Qt Company GmbH
Rudower Chaussee 13
D-12489 Berlin

Viktor.Engelmann at qt.io
+49 151 26784521

Geschäftsführer: Mika Pälsi, Juha Varelius, Mika Harjuaho
Sitz der Gesellschaft: Berlin
Registergericht: Amtsgericht Charlottenburg, HRB 144331 B

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170119/ea0e6af9/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: OpenPGP digital signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170119/ea0e6af9/attachment.sig>

More information about the Interest mailing list