fix(liveupdates): add subscription to backlog if failed (#6763)

Reviewed-by: pajlada <rasmus.karlsson@pajlada.com>
This commit is contained in:
Nerixyz
2026-01-24 13:04:24 +01:00
committed by GitHub
parent 18b5c64d2d
commit f2c4c63d59
3 changed files with 70 additions and 7 deletions
+50 -4
View File
@@ -80,8 +80,9 @@ class MyManager;
class MyClient : public BasicPubSubClient<DummySubscription, MyClient>
{
public:
MyClient(MyManager &manager)
: manager(manager)
MyClient(MyManager &manager, size_t limit)
: BasicPubSubClient(limit)
, manager(manager)
{
}
@@ -94,8 +95,9 @@ private:
class MyManager : public BasicPubSubManager<MyManager, MyClient>
{
public:
MyManager(QString host)
MyManager(QString host, size_t limit = 100)
: BasicPubSubManager(std::move(host), "Test")
, limit_(limit)
{
}
@@ -126,12 +128,13 @@ public:
std::shared_ptr<MyClient> makeClient()
{
return std::make_shared<MyClient>(*this);
return std::make_shared<MyClient>(*this, this->limit_);
}
private:
std::mutex messageMtx_;
std::deque<QString> messageQueue_;
size_t limit_;
friend MyClient;
};
@@ -177,3 +180,46 @@ TEST(BasicPubSub, SubscriptionCycle)
ASSERT_EQ(manager.diag.connectionsFailed, 0);
ASSERT_EQ(manager.messagesReceived, 2);
}
TEST(BasicPubSub, SubLimits)
{
mock::BaseApplication app;
const QString host("wss://127.0.0.1:9050/liveupdates/sub-unsub");
MyManager manager(host, 1);
manager.sub({.type = 1, .condition = "foo"});
manager.sub({.type = 2, .condition = "foo"});
manager.sub({.type = 3, .condition = "foo"});
manager.sub({.type = 4, .condition = "foo"});
manager.sub({.type = 5, .condition = "foo"});
QTest::qWait(500);
ASSERT_EQ(manager.diag.connectionsOpened, 5);
ASSERT_EQ(manager.diag.connectionsClosed, 0);
ASSERT_EQ(manager.diag.connectionsFailed, 0);
ASSERT_EQ(manager.messagesReceived, 5);
// The messages come from multiple connections, so they're not necessarily
// in order.
std::vector<QString> messages;
for (size_t i = 0; i < 5; i++)
{
auto msg = manager.popMessage();
ASSERT_TRUE(msg.has_value());
messages.push_back(*std::move(msg));
}
std::ranges::sort(messages);
ASSERT_EQ(messages[0], QString("ack-sub-1-foo"));
ASSERT_EQ(messages[1], QString("ack-sub-2-foo"));
ASSERT_EQ(messages[2], QString("ack-sub-3-foo"));
ASSERT_EQ(messages[3], QString("ack-sub-4-foo"));
ASSERT_EQ(messages[4], QString("ack-sub-5-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, 5);
ASSERT_EQ(manager.diag.connectionsClosed, 5);
ASSERT_EQ(manager.diag.connectionsFailed, 0);
ASSERT_EQ(manager.messagesReceived, 5);
}