mirror of
https://github.com/Chatterino/chatterino2.git
synced 2024-11-13 19:49:51 +01:00
fix: create NetworkManager statics in init()
(#5254)
This commit is contained in:
parent
e4ea9af004
commit
044d457d20
|
@ -188,6 +188,7 @@
|
|||
- Dev: Cleaned up and optimized resources. (#5222)
|
||||
- Dev: Refactor `StreamerMode`. (#5216, #5236)
|
||||
- Dev: Cleaned up unused code in `MessageElement` and `MessageLayoutElement`. (#5225)
|
||||
- Dev: `NetworkManager`'s statics are now created in its `init` method. (#5254)
|
||||
|
||||
## 2.4.6
|
||||
|
||||
|
|
|
@ -4,19 +4,36 @@
|
|||
|
||||
namespace chatterino {
|
||||
|
||||
QThread NetworkManager::workerThread;
|
||||
QNetworkAccessManager NetworkManager::accessManager;
|
||||
QThread *NetworkManager::workerThread = nullptr;
|
||||
QNetworkAccessManager *NetworkManager::accessManager = nullptr;
|
||||
|
||||
void NetworkManager::init()
|
||||
{
|
||||
NetworkManager::accessManager.moveToThread(&NetworkManager::workerThread);
|
||||
NetworkManager::workerThread.start();
|
||||
assert(!NetworkManager::workerThread);
|
||||
assert(!NetworkManager::accessManager);
|
||||
|
||||
NetworkManager::workerThread = new QThread;
|
||||
NetworkManager::workerThread->start();
|
||||
|
||||
NetworkManager::accessManager = new QNetworkAccessManager;
|
||||
NetworkManager::accessManager->moveToThread(NetworkManager::workerThread);
|
||||
}
|
||||
|
||||
void NetworkManager::deinit()
|
||||
{
|
||||
NetworkManager::workerThread.quit();
|
||||
NetworkManager::workerThread.wait();
|
||||
assert(NetworkManager::workerThread);
|
||||
assert(NetworkManager::accessManager);
|
||||
|
||||
if (NetworkManager::workerThread)
|
||||
{
|
||||
NetworkManager::workerThread->quit();
|
||||
NetworkManager::workerThread->wait();
|
||||
}
|
||||
|
||||
delete NetworkManager::accessManager;
|
||||
NetworkManager::accessManager = nullptr;
|
||||
delete NetworkManager::workerThread;
|
||||
NetworkManager::workerThread = nullptr;
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
|
|
@ -10,8 +10,8 @@ class NetworkManager : public QObject
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
static QThread workerThread;
|
||||
static QNetworkAccessManager accessManager;
|
||||
static QThread *workerThread;
|
||||
static QNetworkAccessManager *accessManager;
|
||||
|
||||
static void init();
|
||||
static void deinit();
|
||||
|
|
|
@ -48,7 +48,7 @@ void loadUncached(std::shared_ptr<NetworkData> &&data)
|
|||
NetworkRequester requester;
|
||||
auto *worker = new NetworkTask(std::move(data));
|
||||
|
||||
worker->moveToThread(&NetworkManager::workerThread);
|
||||
worker->moveToThread(NetworkManager::workerThread);
|
||||
|
||||
QObject::connect(&requester, &NetworkRequester::requestUrl, worker,
|
||||
&NetworkTask::run);
|
||||
|
|
|
@ -54,41 +54,41 @@ QNetworkReply *NetworkTask::createReply()
|
|||
{
|
||||
const auto &data = this->data_;
|
||||
const auto &request = this->data_->request;
|
||||
auto &accessManager = NetworkManager::accessManager;
|
||||
auto *accessManager = NetworkManager::accessManager;
|
||||
switch (this->data_->requestType)
|
||||
{
|
||||
case NetworkRequestType::Get:
|
||||
return accessManager.get(request);
|
||||
return accessManager->get(request);
|
||||
|
||||
case NetworkRequestType::Put:
|
||||
return accessManager.put(request, data->payload);
|
||||
return accessManager->put(request, data->payload);
|
||||
|
||||
case NetworkRequestType::Delete:
|
||||
return accessManager.deleteResource(data->request);
|
||||
return accessManager->deleteResource(data->request);
|
||||
|
||||
case NetworkRequestType::Post:
|
||||
if (data->multiPartPayload)
|
||||
{
|
||||
assert(data->payload.isNull());
|
||||
|
||||
return accessManager.post(request,
|
||||
data->multiPartPayload.get());
|
||||
return accessManager->post(request,
|
||||
data->multiPartPayload.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
return accessManager.post(request, data->payload);
|
||||
return accessManager->post(request, data->payload);
|
||||
}
|
||||
case NetworkRequestType::Patch:
|
||||
if (data->multiPartPayload)
|
||||
{
|
||||
assert(data->payload.isNull());
|
||||
|
||||
return accessManager.sendCustomRequest(
|
||||
return accessManager->sendCustomRequest(
|
||||
request, "PATCH", data->multiPartPayload.get());
|
||||
}
|
||||
else
|
||||
{
|
||||
return NetworkManager::accessManager.sendCustomRequest(
|
||||
return NetworkManager::accessManager->sendCustomRequest(
|
||||
request, "PATCH", data->payload);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -74,7 +74,7 @@ TEST(NetworkRequest, Success)
|
|||
{
|
||||
const std::vector<int> codes{200, 201, 202, 203, 204, 205, 206};
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
for (const auto code : codes)
|
||||
{
|
||||
|
@ -96,14 +96,14 @@ TEST(NetworkRequest, Success)
|
|||
waiter.waitForRequest();
|
||||
}
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
}
|
||||
|
||||
TEST(NetworkRequest, FinallyCallbackOnSuccess)
|
||||
{
|
||||
const std::vector<int> codes{200, 201, 202, 203, 204, 205, 206};
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
for (const auto code : codes)
|
||||
{
|
||||
|
@ -132,7 +132,7 @@ TEST(NetworkRequest, Error)
|
|||
411, 412, 413, 414, 418, 500, 501, 502, 503, 504,
|
||||
};
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
for (const auto code : codes)
|
||||
{
|
||||
|
@ -155,7 +155,7 @@ TEST(NetworkRequest, Error)
|
|||
waiter.waitForRequest();
|
||||
}
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
}
|
||||
|
||||
TEST(NetworkRequest, FinallyCallbackOnError)
|
||||
|
@ -165,7 +165,7 @@ TEST(NetworkRequest, FinallyCallbackOnError)
|
|||
411, 412, 413, 414, 418, 500, 501, 502, 503, 504,
|
||||
};
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
for (const auto code : codes)
|
||||
{
|
||||
|
@ -189,7 +189,7 @@ TEST(NetworkRequest, FinallyCallbackOnError)
|
|||
|
||||
TEST(NetworkRequest, TimeoutTimingOut)
|
||||
{
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
auto url = getDelayURL(5);
|
||||
RequestWaiter waiter;
|
||||
|
@ -214,12 +214,12 @@ TEST(NetworkRequest, TimeoutTimingOut)
|
|||
|
||||
waiter.waitForRequest();
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
}
|
||||
|
||||
TEST(NetworkRequest, TimeoutNotTimingOut)
|
||||
{
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
auto url = getDelayURL(1);
|
||||
RequestWaiter waiter;
|
||||
|
@ -240,12 +240,12 @@ TEST(NetworkRequest, TimeoutNotTimingOut)
|
|||
|
||||
waiter.waitForRequest();
|
||||
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
}
|
||||
|
||||
TEST(NetworkRequest, FinallyCallbackOnTimeout)
|
||||
{
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
|
||||
auto url = getDelayURL(5);
|
||||
|
||||
|
@ -276,5 +276,5 @@ TEST(NetworkRequest, FinallyCallbackOnTimeout)
|
|||
EXPECT_TRUE(finallyCalled);
|
||||
EXPECT_TRUE(onErrorCalled);
|
||||
EXPECT_FALSE(onSuccessCalled);
|
||||
EXPECT_TRUE(NetworkManager::workerThread.isRunning());
|
||||
EXPECT_TRUE(NetworkManager::workerThread->isRunning());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue