Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Waiting for QNetworkManager in a test
Qt 6.11 is out! See what's new in the release blog

Waiting for QNetworkManager in a test

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 777 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Uncle_MastaU Offline
    Uncle_MastaU Offline
    Uncle_Masta
    wrote on last edited by
    #1

    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"?

    Christian EhrlicherC C 2 Replies Last reply
    0
    • Uncle_MastaU Uncle_Masta

      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"?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      As for all other async stuff to test use QTest::qWait().

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Uncle_MastaU 1 Reply Last reply
      3
      • Uncle_MastaU Uncle_Masta

        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"?

        C Offline
        C Offline
        ChrisW67
        wrote on last edited by ChrisW67
        #3

        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"
        
        Uncle_MastaU 1 Reply Last reply
        1
        • C ChrisW67

          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"
          
          Uncle_MastaU Offline
          Uncle_MastaU Offline
          Uncle_Masta
          wrote on last edited by
          #4

          @ChrisW67 said in Waiting for QNetworkManager in a test:

          QSignalSpy

          QSignalSpy?! Cool!

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            As for all other async stuff to test use QTest::qWait().

            Uncle_MastaU Offline
            Uncle_MastaU Offline
            Uncle_Masta
            wrote on last edited by
            #5

            @Christian-Ehrlicher Ah! That's what I needed. Thanks!!

            1 Reply Last reply
            0
            • Uncle_MastaU Uncle_Masta has marked this topic as solved on

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved