Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QSignalSpy wait and two signals

I'm trying to write an unit test for a class in a Qt-based project (Qt 5, C++03).

class Transaction { // This is just a sample class
//..
public signals:
   void succeeded();
   void failed();
}

Transaction* transaction = new Transaction(this);
QSignalSpy spy(transaction, SIGNAL(succeeded()));
transaction->run();
spy.wait(5000); // wait for 5 seconds

I want my test to run faster. How to interrupt this wait() call after signal failed() is emitted, in case of failed transaction?

I do not see any slots available in the QSignalSpy class.

I should use QEventLoop instead?

like image 808
zenden2k Avatar asked Nov 05 '15 07:11

zenden2k


2 Answers

You'll probably have to use a loop and manually call QTest::qWait() while neither signal has been emitted:

QSignalSpy succeededSpy(transaction, SIGNAL(succeeded()));
QSignalSpy failedSpy(transaction, SIGNAL(failed()));
for (int waitDelay = 5000; waitDelay > 0 && succeededSpy.count() == 0 && failedSpy.count() == 0; waitDelay -= 100) {
    QTest::qWait(100);
}

QCOMPARE(succeededSpy.count(), 1);
like image 120
Mitch Avatar answered Oct 06 '22 00:10

Mitch


Solution with QTestEventLoop:

QTestEventLoop loop;
QObject::connect(transaction, SIGNAL(succeeded()), &loop, SLOT(exitLoop()));
QObject::connect(transaction, SIGNAL(failed()), &loop, SLOT(exitLoop()));
transaction->run();
loop.enterLoopMSecs(3000);

Solution with a timer and QEventLoop:

Transaction* transaction = new Transaction(this);
QSignalSpy spy(transaction, SIGNAL(succeeded()));  
QEventLoop loop;  
QTimer timer;
QObject::connect(transaction, SIGNAL(succeeded()), &loop, SLOT(quit()));
QObject::connect(transaction, SIGNAL(failed()), &loop, SLOT(quit()));
QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
timer.start(3000);
loop.exec();
transaction->run();
QCOMPARE(spy.count(), 1);
like image 35
zenden2k Avatar answered Oct 06 '22 01:10

zenden2k