2021-08-04 23:18:34 +02:00
|
|
|
#include "util/RatelimitBucket.hpp"
|
|
|
|
|
2024-05-05 15:01:07 +02:00
|
|
|
#include "Test.hpp"
|
|
|
|
|
2021-08-04 23:18:34 +02:00
|
|
|
#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);
|
|
|
|
}
|