refactor(liveupdates): use WebSocketPool over websocketpp (#6308)

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
nerix
2025-11-08 13:46:10 +01:00
committed by GitHub
parent 6aeb1ca6a7
commit 7f393e2401
26 changed files with 984 additions and 973 deletions
+43 -16
View File
@@ -1,3 +1,4 @@
#include "mocks/BaseApplication.hpp"
#include "providers/liveupdates/BasicPubSubClient.hpp"
#include "providers/liveupdates/BasicPubSubManager.hpp"
#include "Test.hpp"
@@ -6,6 +7,7 @@
#include <QJsonDocument>
#include <QJsonObject>
#include <QString>
#include <QtCore/qtestsupport_core.h>
#include <deque>
#include <mutex>
@@ -14,6 +16,8 @@
using namespace chatterino;
using namespace std::chrono_literals;
namespace {
struct DummySubscription {
int type;
QString condition;
@@ -54,6 +58,8 @@ struct DummySubscription {
}
};
} // namespace
namespace std {
template <>
struct hash<DummySubscription> {
@@ -64,7 +70,24 @@ struct hash<DummySubscription> {
};
} // namespace std
class MyManager : public BasicPubSubManager<DummySubscription>
namespace {
class MyManager;
class MyClient : public BasicPubSubClient<DummySubscription>
{
public:
MyClient(MyManager &manager)
: manager(manager)
{
}
void onMessage(const QByteArray &msg) /* override */;
private:
MyManager &manager;
};
class MyManager : public BasicPubSubManager<MyManager, MyClient>
{
public:
MyManager(QString host)
@@ -97,33 +120,34 @@ public:
this->unsubscribe(sub);
}
protected:
void onMessage(
websocketpp::connection_hdl /*hdl*/,
BasicPubSubManager<DummySubscription>::WebsocketMessagePtr msg) override
std::shared_ptr<MyClient> makeClient()
{
std::lock_guard<std::mutex> guard(this->messageMtx_);
this->messagesReceived.fetch_add(1, std::memory_order_acq_rel);
this->messageQueue_.emplace_back(
QString::fromStdString(msg->get_payload()));
return std::make_shared<MyClient>(*this);
}
private:
std::mutex messageMtx_;
std::deque<QString> messageQueue_;
friend MyClient;
};
void MyClient::onMessage(const QByteArray &msg)
{
std::lock_guard<std::mutex> guard(this->manager.messageMtx_);
this->manager.messagesReceived.fetch_add(1, std::memory_order_acq_rel);
this->manager.messageQueue_.emplace_back(QString::fromUtf8(msg));
}
} // namespace
TEST(BasicPubSub, SubscriptionCycle)
{
mock::BaseApplication app;
const QString host("wss://127.0.0.1:9050/liveupdates/sub-unsub");
MyManager manager(host);
manager.start();
std::this_thread::sleep_for(50ms);
manager.sub({1, "foo"});
std::this_thread::sleep_for(500ms);
ASSERT_EQ(manager.diag.connectionsOpened, 1);
QTest::qWait(500);
ASSERT_EQ(manager.diag.connectionsClosed, 0);
ASSERT_EQ(manager.diag.connectionsFailed, 0);
ASSERT_EQ(manager.messagesReceived, 1);
@@ -131,7 +155,7 @@ TEST(BasicPubSub, SubscriptionCycle)
ASSERT_EQ(manager.popMessage(), QString("ack-sub-1-foo"));
manager.unsub({1, "foo"});
std::this_thread::sleep_for(50ms);
QTest::qWait(50);
ASSERT_EQ(manager.diag.connectionsOpened, 1);
ASSERT_EQ(manager.diag.connectionsClosed, 0);
@@ -140,6 +164,9 @@ TEST(BasicPubSub, SubscriptionCycle)
ASSERT_EQ(manager.popMessage(), QString("ack-unsub-1-foo"));
manager.stop();
// after exactly one event loop iteration, we should see updated counters
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
ASSERT_EQ(manager.diag.connectionsOpened, 1);
ASSERT_EQ(manager.diag.connectionsClosed, 1);
+17 -9
View File
@@ -1,8 +1,12 @@
#include "providers/bttv/BttvLiveUpdates.hpp"
#include "mocks/BaseApplication.hpp"
#include "providers/bttv/liveupdates/BttvLiveUpdateMessages.hpp"
#include "providers/liveupdates/Diag.hpp"
#include "Test.hpp"
#include <QString>
#include <QtCore/qtestsupport_core.h>
#include <optional>
#include <tuple>
@@ -15,9 +19,10 @@ const QString TARGET_USER_NAME = "Alien";
TEST(BttvLiveUpdates, AllEvents)
{
mock::BaseApplication app;
const QString host("wss://127.0.0.1:9050/liveupdates/bttv/all-events");
chatterino::BttvLiveUpdates liveUpdates(host);
liveUpdates.start();
std::optional<BttvLiveUpdateEmoteUpdateAddMessage> addMessage;
std::optional<BttvLiveUpdateEmoteUpdateAddMessage> updateMessage;
@@ -33,13 +38,12 @@ TEST(BttvLiveUpdates, AllEvents)
removeMessage = m;
});
std::this_thread::sleep_for(50ms);
liveUpdates.joinChannel(TARGET_USER_ID, TARGET_USER_NAME);
std::this_thread::sleep_for(500ms);
QTest::qWait(500);
ASSERT_EQ(liveUpdates.diag.connectionsOpened, 1);
ASSERT_EQ(liveUpdates.diag.connectionsClosed, 0);
ASSERT_EQ(liveUpdates.diag.connectionsFailed, 0);
ASSERT_EQ(liveUpdates.diag().connectionsOpened, 1);
ASSERT_EQ(liveUpdates.diag().connectionsClosed, 0);
ASSERT_EQ(liveUpdates.diag().connectionsFailed, 0);
auto add = *addMessage;
ASSERT_EQ(add.channelID, TARGET_USER_ID);
@@ -56,7 +60,11 @@ TEST(BttvLiveUpdates, AllEvents)
ASSERT_EQ(rem.emoteID, QString("55898e122612142e6aaa935b"));
liveUpdates.stop();
ASSERT_EQ(liveUpdates.diag.connectionsOpened, 1);
ASSERT_EQ(liveUpdates.diag.connectionsClosed, 1);
ASSERT_EQ(liveUpdates.diag.connectionsFailed, 0);
// after exactly one event loop iteration, we should see updated counters
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
ASSERT_EQ(liveUpdates.diag().connectionsOpened, 1);
ASSERT_EQ(liveUpdates.diag().connectionsClosed, 1);
ASSERT_EQ(liveUpdates.diag().connectionsFailed, 0);
}
+1
View File
@@ -37,6 +37,7 @@
#include <QJsonObject>
#include <QJsonValue>
#include <QString>
#include <QStringBuilder>
#include <unordered_map>
#include <vector>
+21 -16
View File
@@ -1,11 +1,14 @@
#include "providers/seventv/SeventvEventAPI.hpp"
#include "mocks/BaseApplication.hpp"
#include "providers/liveupdates/Diag.hpp"
#include "providers/seventv/eventapi/Client.hpp"
#include "providers/seventv/eventapi/Dispatch.hpp"
#include "providers/seventv/eventapi/Message.hpp"
#include "Test.hpp"
#include <QString>
#include <QtCore/qtestsupport_core.h>
#include <optional>
@@ -19,9 +22,9 @@ const QString TARGET_USER_ID = "60b39e943e203cc169dfc106";
TEST(SeventvEventAPI, AllEvents)
{
mock::BaseApplication app;
const QString host("wss://127.0.0.1:9050/liveupdates/seventv/all-events");
SeventvEventAPI eventAPI(host, std::chrono::milliseconds(1000));
eventAPI.start();
std::optional<EmoteAddDispatch> addDispatch;
std::optional<EmoteUpdateDispatch> updateDispatch;
@@ -41,13 +44,12 @@ TEST(SeventvEventAPI, AllEvents)
userDispatch = d;
});
std::this_thread::sleep_for(50ms);
eventAPI.subscribeUser("", EMOTE_SET_A);
std::this_thread::sleep_for(500ms);
QTest::qWait(500);
ASSERT_EQ(eventAPI.diag.connectionsOpened, 1);
ASSERT_EQ(eventAPI.diag.connectionsClosed, 0);
ASSERT_EQ(eventAPI.diag.connectionsFailed, 0);
ASSERT_EQ(eventAPI.diag().connectionsOpened, 1);
ASSERT_EQ(eventAPI.diag().connectionsClosed, 0);
ASSERT_EQ(eventAPI.diag().connectionsFailed, 0);
auto add = *addDispatch;
ASSERT_EQ(add.emoteSetID, EMOTE_SET_A);
@@ -73,7 +75,7 @@ TEST(SeventvEventAPI, AllEvents)
removeDispatch = std::nullopt;
eventAPI.subscribeUser(TARGET_USER_ID, "");
std::this_thread::sleep_for(50ms);
QTest::qWait(50);
ASSERT_EQ(addDispatch.has_value(), false);
ASSERT_EQ(updateDispatch.has_value(), false);
@@ -87,23 +89,26 @@ TEST(SeventvEventAPI, AllEvents)
ASSERT_EQ(user.connectionIndex, 0);
eventAPI.stop();
ASSERT_EQ(eventAPI.diag.connectionsOpened, 1);
ASSERT_EQ(eventAPI.diag.connectionsClosed, 1);
ASSERT_EQ(eventAPI.diag.connectionsFailed, 0);
// after exactly one event loop iteration, we should see updated counters
QCoreApplication::processEvents(QEventLoop::AllEvents);
QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
ASSERT_EQ(eventAPI.diag().connectionsOpened, 1);
ASSERT_EQ(eventAPI.diag().connectionsClosed, 1);
ASSERT_EQ(eventAPI.diag().connectionsFailed, 0);
}
TEST(SeventvEventAPI, NoHeartbeat)
{
mock::BaseApplication app;
const QString host("wss://127.0.0.1:9050/liveupdates/seventv/no-heartbeat");
SeventvEventAPI eventApi(host, std::chrono::milliseconds(1000));
eventApi.start();
std::this_thread::sleep_for(50ms);
eventApi.subscribeUser("", EMOTE_SET_A);
std::this_thread::sleep_for(1250ms);
ASSERT_EQ(eventApi.diag.connectionsOpened, 2);
ASSERT_EQ(eventApi.diag.connectionsClosed, 1);
ASSERT_EQ(eventApi.diag.connectionsFailed, 0);
QTest::qWait(1250);
ASSERT_EQ(eventApi.diag().connectionsOpened, 2);
ASSERT_EQ(eventApi.diag().connectionsClosed, 1);
ASSERT_EQ(eventApi.diag().connectionsFailed, 0);
eventApi.stop();
}