Rate limit outgoing JOIN messages (#3115)
Co-authored-by: Rasmus Karlsson <rasmus.karlsson@pajlada.com> Co-authored-by: Tal Neoran <talneoran@gmail.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
#include "util/RatelimitBucket.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include <QtConcurrent>
|
||||
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
using namespace chatterino;
|
||||
|
||||
TEST(RatelimitBucket, BatchTwoParts)
|
||||
{
|
||||
const int cooldown = 100;
|
||||
int n = 0;
|
||||
auto cb = [&n](QString msg) {
|
||||
qDebug() << msg;
|
||||
++n;
|
||||
};
|
||||
auto bucket = std::make_unique<RatelimitBucket>(5, cooldown, cb, nullptr);
|
||||
bucket->send("1");
|
||||
EXPECT_EQ(n, 1);
|
||||
|
||||
bucket->send("2");
|
||||
EXPECT_EQ(n, 2);
|
||||
|
||||
bucket->send("3");
|
||||
EXPECT_EQ(n, 3);
|
||||
|
||||
bucket->send("4");
|
||||
EXPECT_EQ(n, 4);
|
||||
|
||||
bucket->send("5");
|
||||
EXPECT_EQ(n, 5);
|
||||
|
||||
bucket->send("6");
|
||||
// Rate limit reached, n will not have changed yet. If we wait for the cooldown to run, n should have changed
|
||||
EXPECT_EQ(n, 5);
|
||||
|
||||
QCoreApplication::processEvents();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{cooldown});
|
||||
QCoreApplication::processEvents();
|
||||
|
||||
EXPECT_EQ(n, 6);
|
||||
}
|
||||
Reference in New Issue
Block a user