Waiting for QNetworkManager in a test
-
I have some old code that wraps QNetworkAccessManager that I'm cleaning up and adding tests. I've been reviewing how threads and the event loop works.
So, really basic: Sending a request with QNetworkAccessManager.get() will, as I understand it, do the work in a separate thread. But, our slots that we connect to the finished signals are going to be executed in the thread that the test slots tests are in. Am I right so far?
So, we don't want to exit the test until our slots connected to receiving results signals are called. How do we do that without blocking the event loop?
Or, am I "doing it wrong"?
-
I have some old code that wraps QNetworkAccessManager that I'm cleaning up and adding tests. I've been reviewing how threads and the event loop works.
So, really basic: Sending a request with QNetworkAccessManager.get() will, as I understand it, do the work in a separate thread. But, our slots that we connect to the finished signals are going to be executed in the thread that the test slots tests are in. Am I right so far?
So, we don't want to exit the test until our slots connected to receiving results signals are called. How do we do that without blocking the event loop?
Or, am I "doing it wrong"?
As for all other async stuff to test use QTest::qWait().
-
I have some old code that wraps QNetworkAccessManager that I'm cleaning up and adding tests. I've been reviewing how threads and the event loop works.
So, really basic: Sending a request with QNetworkAccessManager.get() will, as I understand it, do the work in a separate thread. But, our slots that we connect to the finished signals are going to be executed in the thread that the test slots tests are in. Am I right so far?
So, we don't want to exit the test until our slots connected to receiving results signals are called. How do we do that without blocking the event loop?
Or, am I "doing it wrong"?
The QNetworkAccessManager and request processing is happening in another thread. You have to enter a main test thread event loop to kick this off and then you do block until finished() is emitted (or timeout reached as in the qWait case).
Something like this also flies:
#include <QtTest> // add necessary includes here #include <QNetworkAccessManager> class ExampleQNAMTest : public QObject { Q_OBJECT public: ExampleQNAMTest(); ~ExampleQNAMTest(); private slots: void test_case1(); }; ExampleQNAMTest::ExampleQNAMTest() {} ExampleQNAMTest::~ExampleQNAMTest() {} void ExampleQNAMTest::test_case1() { QNetworkAccessManager mgr; QSignalSpy spy(&mgr, &QNetworkAccessManager::encrypted); QEventLoop loop; connect(&mgr, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit); QNetworkRequest req(QUrl("https://www.google.com")); QNetworkReply *reply = mgr.get(req); QVERIFY(reply); loop.exec(); QCOMPARE(spy.count(), 1); // did it get encrypted } QTEST_MAIN(ExampleQNAMTest) #include "tst_exampleqnamtest.moc" -
The QNetworkAccessManager and request processing is happening in another thread. You have to enter a main test thread event loop to kick this off and then you do block until finished() is emitted (or timeout reached as in the qWait case).
Something like this also flies:
#include <QtTest> // add necessary includes here #include <QNetworkAccessManager> class ExampleQNAMTest : public QObject { Q_OBJECT public: ExampleQNAMTest(); ~ExampleQNAMTest(); private slots: void test_case1(); }; ExampleQNAMTest::ExampleQNAMTest() {} ExampleQNAMTest::~ExampleQNAMTest() {} void ExampleQNAMTest::test_case1() { QNetworkAccessManager mgr; QSignalSpy spy(&mgr, &QNetworkAccessManager::encrypted); QEventLoop loop; connect(&mgr, &QNetworkAccessManager::finished, &loop, &QEventLoop::quit); QNetworkRequest req(QUrl("https://www.google.com")); QNetworkReply *reply = mgr.get(req); QVERIFY(reply); loop.exec(); QCOMPARE(spy.count(), 1); // did it get encrypted } QTEST_MAIN(ExampleQNAMTest) #include "tst_exampleqnamtest.moc" -
As for all other async stuff to test use QTest::qWait().
@Christian-Ehrlicher Ah! That's what I needed. Thanks!!
-
U Uncle_Masta has marked this topic as solved on