Skip to content

Commit

Permalink
test: add networking tests (issue #2515)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Nov 14, 2022
1 parent 128b33c commit 007a022
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/lib/src/network/network-manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ int NetworkManager::maxConcurrency() const
return m_maxConcurrency;
}

int NetworkManager::activeQueries() const
{
return m_activeQueries;
}

void NetworkManager::setMaxConcurrency(int maxConcurrency)
{
m_maxConcurrency = maxConcurrency;
Expand Down
1 change: 1 addition & 0 deletions src/lib/src/network/network-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class NetworkManager : public QObject
explicit NetworkManager(QObject *parent = nullptr);

int maxConcurrency() const;
int activeQueries() const;
void setMaxConcurrency(int maxConcurrency);
int interval(int key) const;
void setInterval(int key, int msInterval);
Expand Down
112 changes: 112 additions & 0 deletions src/tests/src/network-manager-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <QEventLoop>
#include <QNetworkRequest>
#include <QUrl>
#include "catch.h"
#include "network/network-manager.h"
#include "network/network-reply.h"
#include "functions.h"


TEST_CASE("NetworkManager", "[.][network]")
{
setTestModeEnabled(false);

/**
* Start a request that should take 300ms.
* Wait for 2s.
* The request should have been received and the active query count reset.
*/
SECTION("Basic")
{
NetworkManager manager;
manager.setMaxConcurrency(1);

REQUIRE(manager.activeQueries() == 0);

QNetworkRequest request(QUrl("https://httpbin.org/delay/0.1"));
NetworkReply *reply = manager.get(request);

bool received = false;
QObject::connect(reply, &NetworkReply::finished, [&]() {
received = true;
});

QEventLoop loop;
QTimer::singleShot(2000, &loop, &QEventLoop::quit);
loop.exec();

REQUIRE(received);
REQUIRE(manager.activeQueries() == 0);
}

/**
* Start a request that should take 300ms.
* Abort the request after 100ms.
* Wait for 2s.
* The request should have been finished (without data) and the active query count reset.
*/
SECTION("Abort after starting")
{
NetworkManager manager;
manager.setMaxConcurrency(1);

REQUIRE(manager.activeQueries() == 0);

QNetworkRequest request(QUrl("https://httpbin.org/delay/0.3"));
NetworkReply *reply = manager.get(request);

bool received = false;
QObject::connect(reply, &NetworkReply::finished, [&]() {
received = true;
});
QTimer::singleShot(100, [&]() {
reply->abort();
});

QEventLoop loop;
QTimer::singleShot(2000, &loop, &QEventLoop::quit);
loop.exec();

REQUIRE(received);
REQUIRE(manager.activeQueries() == 0);
}

/**
* Start a request that should take 300ms and should start in 2s.
* Abort the request after 100ms.
* Wait for 5s.
* The request should have been finished (without data) and the active query count reset.
*/
SECTION("Abort before starting")
{
NetworkManager manager;
manager.setMaxConcurrency(1);
manager.setInterval(-1, 2000);

REQUIRE(manager.activeQueries() == 0);

// Start a random request to trigger the interval
QNetworkRequest fakeRequest(QUrl("https://httpbin.org/status/200"));
manager.get(fakeRequest);

QNetworkRequest request(QUrl("https://httpbin.org/delay/0.3"));
NetworkReply *reply = manager.get(request);

bool received = false;
QObject::connect(reply, &NetworkReply::finished, [&]() {
received = true;
});
QTimer::singleShot(100, [&]() {
reply->abort();
});

QEventLoop loop;
QTimer::singleShot(5000, &loop, &QEventLoop::quit);
loop.exec();

REQUIRE(received);
REQUIRE(manager.activeQueries() == 0);
}

setTestModeEnabled(true);
}

0 comments on commit 007a022

Please sign in to comment.