Calling QCoreApplication::processEvents from QtTest appears not to work
-
This is a question more out of curiosity than anything - maybe I'll understand things better !
So, I have a logging interface class, CLogger, to which I want to send a QString to be logged from different threads. To ensure thread safety (I prefer my messages not to be interleaved) I have implemented a signal-slot within the CLogger. There is a public CLogger::DoLog( QString ) signal, which is connected to a private CLogger::Log( QString ) slot. To enter a message into the log, I just
emit a_Logger.DoLog( "message" );
from any thread.This works nicely. Indeed, my unit test just finished running with all passes.
My question stems from the fact that my initial CLogger connected the signal as a Qt::QueuedConnection.
This should make no difference, as you are saying. The only thing is that it will queue the log entry in the message loop for the UI thread. And to be honest, I only did it since I thought it might make the UI run more smoothly (just keeping each event processing time to a minumum seems like generally good practice).
This also works fine in my Application.
But in QtTest, less so ! Obviously, if the event is queued, after
emit m_Logger.DoLog( MessageLevel, Message );
The event loop needs to run. So I
QTest::qWait( 1000 );
However the private slot CLogger::Log( QString ) is not called (I run the test code in debug with a break point) and my test fails to find a new message in the log file.
I have also tried replacing qWait with
QElapsedTimer Timer; Timer.start(); do { QCoreApplication::processEvents(QEventLoop::AllEvents); } while( Timer.elapsed() < a_MSecs );
(which is the kind of thing that I suppose QTest::qWait() is doing - given that the documentation says "While waiting, events will be processed...").
I have used QCoreApplication::processEvents() in the past, and it has worked as I expected - that is although I am currently in the middle of processing one event, new incoming events are processed.
Am I wrong about processEvents() ? Is it something about QTest which is different ?
As I said, there is no big problem - I can live with a direct signal-slot connection from the UI thread.
Aha - I have just spotted
QWARN : TestLogging::TestEntries() QEventLoop: Cannot be used without QApplication
Buried near the top of the other messages...So I guess my implementaion of QTest has no event loop - which would explain things ;-)
-
Yes - using
QTEST_GUILESS_MAIN(TestLogging)
rather thanQTEST_APPLESS_MAIN
does help to provide an event loop :-D -