Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Qt SCXML state machines

Tags:

qt

scxml

I'm trying to verify the behavior of a state machine using Qt test framework. I simply don't get how I am supposed to tests Qt SCXML implementation. Sure there is QSignalSpy, but that is only for signals/slops which do not require the event loop to run. What I essentially want to do is:

myStateMachine.submitEvent("MyEvent");
// Run event loop
// Check result

I tried to QCoreApplication::processEvents() this sometimes worked, but sometimes also got stuck when calling processEvents(). I guess I might triggered an infinite loop. Also googling did not help, but there must be a way to do this properly.

like image 982
Nils Avatar asked Oct 18 '22 22:10

Nils


1 Answers

In a QtTest based test you can use QTest::qWait() to run the event loop for a given time.

You can also use QSignalSpy to wait for a signal to happen within a certain time, see QSignalSpy::wait()

If there are more than one signal that can trigger test continuation, you can also use a nested event loop, e.g. something like this

QEventLoop loop;
connect(sender1, SIGNAL(signal1()), &loop, SLOT(quit()));
connect(sender2, SIGNAL(signal2()), &loop, SLOT(quit()));
loop.exec();

Potentially combined with a timer to end the loop in case none of those signals arrive

like image 160
Kevin Krammer Avatar answered Oct 21 '22 02:10

Kevin Krammer