Use an exponential backoff when deciding how long we need to wait for reconnects (#2892)
Co-authored-by: Leon Richardt <leon.richardt@gmail.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#include "util/ExponentialBackoff.hpp"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace chatterino;
|
||||
|
||||
TEST(ExponentialBackoff, MaxSteps)
|
||||
{
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
ExponentialBackoff<3> foo{10ms};
|
||||
|
||||
// First usage should be the start value
|
||||
EXPECT_EQ(foo.next(), 10ms);
|
||||
EXPECT_EQ(foo.next(), 20ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
// We reached the max steps, so we should continue returning the max value without increasing
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
}
|
||||
|
||||
TEST(ExponentialBackoff, Reset)
|
||||
{
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
ExponentialBackoff<3> foo{10ms};
|
||||
|
||||
// First usage should be the start value
|
||||
EXPECT_EQ(foo.next(), 10ms);
|
||||
EXPECT_EQ(foo.next(), 20ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
// We reached the max steps, so we should continue returning the max value without increasing
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
|
||||
foo.reset();
|
||||
|
||||
// After a reset, we should start at the beginning value again
|
||||
EXPECT_EQ(foo.next(), 10ms);
|
||||
EXPECT_EQ(foo.next(), 20ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
// We reached the max steps, so we should continue returning the max value without increasing
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
EXPECT_EQ(foo.next(), 40ms);
|
||||
}
|
||||
|
||||
TEST(ExponentialBackoff, BadMaxSteps)
|
||||
{
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
// this will not compile
|
||||
// ExponentialBackoff<1> foo{10ms};
|
||||
// ExponentialBackoff<0> foo{10ms};
|
||||
// ExponentialBackoff<-1> foo{10ms};
|
||||
}
|
||||
Reference in New Issue
Block a user