Move shared network utilities to a header

This commit is contained in:
Mm2PL 2024-10-15 02:40:30 +02:00
parent 6c1273dbbd
commit b867cd2596
No known key found for this signature in database
GPG key ID: 94AC9B80EFA15ED9
3 changed files with 58 additions and 98 deletions

View file

@ -0,0 +1,54 @@
#pragma once
#include "Test.hpp"
#include <QCoreApplication>
namespace chatterino {
#ifdef CHATTERINO_TEST_USE_PUBLIC_HTTPBIN
// Not using httpbin.org, since it can be really slow and cause timeouts.
// postman-echo has the same API.
const char *const HTTPBIN_BASE_URL = "https://postman-echo.com";
#else
const char *const HTTPBIN_BASE_URL = "http://127.0.0.1:9051";
#endif
class RequestWaiter
{
public:
void requestDone()
{
{
std::unique_lock lck(this->mutex_);
ASSERT_FALSE(this->requestDone_);
this->requestDone_ = true;
}
this->condition_.notify_one();
}
void waitForRequest()
{
using namespace std::chrono_literals;
while (true)
{
{
std::unique_lock lck(this->mutex_);
bool done = this->condition_.wait_for(lck, 10ms, [this] {
return this->requestDone_;
});
if (done)
{
break;
}
}
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
}
private:
std::mutex mutex_;
std::condition_variable condition_;
bool requestDone_ = false;
};
} // namespace chatterino

View file

@ -2,6 +2,7 @@
#include "common/network/NetworkManager.hpp"
#include "common/network/NetworkResult.hpp"
#include "NetworkHelpers.hpp"
#include "Test.hpp"
#include <QCoreApplication>
@ -10,14 +11,6 @@ using namespace chatterino;
namespace {
#ifdef CHATTERINO_TEST_USE_PUBLIC_HTTPBIN
// Not using httpbin.org, since it can be really slow and cause timeouts.
// postman-echo has the same API.
const char *const HTTPBIN_BASE_URL = "https://postman-echo.com";
#else
const char *const HTTPBIN_BASE_URL = "http://127.0.0.1:9051";
#endif
QString getStatusURL(int code)
{
return QString("%1/status/%2").arg(HTTPBIN_BASE_URL).arg(code);
@ -28,46 +21,6 @@ QString getDelayURL(int delay)
return QString("%1/delay/%2").arg(HTTPBIN_BASE_URL).arg(delay);
}
class RequestWaiter
{
public:
void requestDone()
{
{
std::unique_lock lck(this->mutex_);
ASSERT_FALSE(this->requestDone_);
this->requestDone_ = true;
}
this->condition_.notify_one();
}
void waitForRequest()
{
using namespace std::chrono_literals;
while (true)
{
{
std::unique_lock lck(this->mutex_);
bool done = this->condition_.wait_for(lck, 10ms, [this] {
return this->requestDone_;
});
if (done)
{
break;
}
}
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
}
private:
std::mutex mutex_;
std::condition_variable condition_;
bool requestDone_ = false;
};
} // namespace
TEST(NetworkRequest, Success)

View file

@ -2,22 +2,22 @@
# include "Application.hpp"
# include "common/Channel.hpp"
# include "common/network/NetworkCommon.hpp"
# include "controllers/commands/Command.hpp" // IT IS USED
# include "controllers/commands/Command.hpp" // IWYU pragma: keep
# include "controllers/commands/CommandController.hpp"
# include "controllers/plugins/api/ChannelRef.hpp"
# include "controllers/plugins/Plugin.hpp"
# include "controllers/plugins/PluginController.hpp"
# include "controllers/plugins/PluginPermission.hpp"
# include "controllers/plugins/SolTypes.hpp" // IT IS USE
# include "controllers/plugins/SolTypes.hpp" // IWYU pragma: keep
# include "mocks/BaseApplication.hpp"
# include "mocks/Channel.hpp"
# include "mocks/Emotes.hpp"
# include "mocks/Logging.hpp"
# include "mocks/TwitchIrcServer.hpp"
# include "NetworkHelpers.hpp"
# include "singletons/Logging.hpp"
# include "Test.hpp"
# include <gtest/gtest.h>
# include <lauxlib.h>
# include <sol/state_view.hpp>
# include <sol/table.hpp>
@ -307,53 +307,6 @@ TEST_F(PluginTest, testChannel)
EXPECT_ANY_THROW(lua->script(R"lua( return chn:get_twitch_id() )lua"));
}
# ifdef CHATTERINO_TEST_USE_PUBLIC_HTTPBIN
// Not using httpbin.org, since it can be really slow and cause timeouts.
// postman-echo has the same API.
const char *const HTTPBIN_BASE_URL = "https://postman-echo.com";
# else
const char *const HTTPBIN_BASE_URL = "http://127.0.0.1:9051";
# endif
class RequestWaiter
{
public:
void requestDone()
{
{
std::unique_lock lck(this->mutex_);
ASSERT_FALSE(this->requestDone_);
this->requestDone_ = true;
}
this->condition_.notify_one();
}
void waitForRequest()
{
using namespace std::chrono_literals;
while (true)
{
{
std::unique_lock lck(this->mutex_);
bool done = this->condition_.wait_for(lck, 10ms, [this] {
return this->requestDone_;
});
if (done)
{
break;
}
}
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
}
}
private:
std::mutex mutex_;
std::condition_variable condition_;
bool requestDone_ = false;
};
TEST_F(PluginTest, testHttp)
{
{