<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Waiting for QNetworkManager in a test]]></title><description><![CDATA[<p dir="auto">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.</p>
<p dir="auto">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?</p>
<p dir="auto">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?</p>
<p dir="auto">Or, am I "doing it wrong"?</p>
]]></description><link>https://forum.qt.io/topic/154750/waiting-for-qnetworkmanager-in-a-test</link><generator>RSS for Node</generator><lastBuildDate>Mon, 08 Jun 2026 14:35:26 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/154750.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 21 Feb 2024 04:06:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Waiting for QNetworkManager in a test on Wed, 21 Feb 2024 05:45:17 GMT]]></title><description><![CDATA[<p dir="auto">As for all other async stuff to test use QTest::qWait().</p>
]]></description><link>https://forum.qt.io/post/790798</link><guid isPermaLink="true">https://forum.qt.io/post/790798</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 21 Feb 2024 05:45:17 GMT</pubDate></item><item><title><![CDATA[Reply to Waiting for QNetworkManager in a test on Wed, 21 Feb 2024 14:13:40 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/christian-ehrlicher">@<bdi>Christian-Ehrlicher</bdi></a> Ah! That's what I needed. Thanks!!</p>
]]></description><link>https://forum.qt.io/post/790871</link><guid isPermaLink="true">https://forum.qt.io/post/790871</guid><dc:creator><![CDATA[Uncle_Masta]]></dc:creator><pubDate>Wed, 21 Feb 2024 14:13:40 GMT</pubDate></item><item><title><![CDATA[Reply to Waiting for QNetworkManager in a test on Wed, 21 Feb 2024 14:12:24 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/chrisw67">@<bdi>ChrisW67</bdi></a> said in <a href="/post/790801">Waiting for QNetworkManager in a test</a>:</p>
<blockquote>
<p dir="auto">QSignalSpy</p>
</blockquote>
<p dir="auto">QSignalSpy?! Cool!</p>
]]></description><link>https://forum.qt.io/post/790870</link><guid isPermaLink="true">https://forum.qt.io/post/790870</guid><dc:creator><![CDATA[Uncle_Masta]]></dc:creator><pubDate>Wed, 21 Feb 2024 14:12:24 GMT</pubDate></item><item><title><![CDATA[Reply to Waiting for QNetworkManager in a test on Wed, 21 Feb 2024 06:05:53 GMT]]></title><description><![CDATA[<p dir="auto">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).</p>
<p dir="auto">Something like this also flies:</p>
<pre><code>#include &lt;QtTest&gt;

// add necessary includes here
#include &lt;QNetworkAccessManager&gt;

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(&amp;mgr, &amp;QNetworkAccessManager::encrypted);
    QEventLoop loop;
    connect(&amp;mgr, &amp;QNetworkAccessManager::finished, &amp;loop, &amp;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"
</code></pre>
]]></description><link>https://forum.qt.io/post/790801</link><guid isPermaLink="true">https://forum.qt.io/post/790801</guid><dc:creator><![CDATA[ChrisW67]]></dc:creator><pubDate>Wed, 21 Feb 2024 06:05:53 GMT</pubDate></item><item><title><![CDATA[Reply to Waiting for QNetworkManager in a test on Wed, 21 Feb 2024 05:45:17 GMT]]></title><description><![CDATA[<p dir="auto">As for all other async stuff to test use QTest::qWait().</p>
]]></description><link>https://forum.qt.io/post/790798</link><guid isPermaLink="true">https://forum.qt.io/post/790798</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Wed, 21 Feb 2024 05:45:17 GMT</pubDate></item></channel></rss>