Compare commits

..

5 Commits

Author SHA1 Message Date
Benjamin Sergeant
f7eb3688dd Update IXExponentialBackoffTest.cpp 2022-02-17 09:17:47 -08:00
Benjamin Sergeant
7360333aca Handle overflow in IXExponentialBackoff.cpp 2022-02-17 09:17:02 -08:00
Benjamin Sergeant
90f19e0280 Reference new IXExponentialBackoffTest test in CMakeLists.txt 2022-02-17 09:08:49 -08:00
Benjamin Sergeant
b72f81540b Create IXExponentialBackoffTest.cpp 2022-02-17 09:07:46 -08:00
Benjamin Sergeant
a77fd2d698 Update catch to v2.13.8 (#365) 2022-02-17 08:38:46 -08:00
3 changed files with 55 additions and 2 deletions

View File

@@ -14,14 +14,27 @@ namespace ix
uint32_t maxWaitBetweenReconnectionRetries, uint32_t maxWaitBetweenReconnectionRetries,
uint32_t minWaitBetweenReconnectionRetries) uint32_t minWaitBetweenReconnectionRetries)
{ {
uint32_t waitTime = (retryCount < 26) ? (std::pow(2, retryCount) * 100) : 0; // It's easy with a power function to go beyond 2^32, and then
// have unexpected results, so prepare for that
const uint32_t maxRetryCountWithoutOverflow = 26;
uint32_t waitTime = 0;
if (retryCount < maxRetryCountWithoutOverflow)
{
waitTime = std::pow(2, retryCount) * 100;
}
if (waitTime < minWaitBetweenReconnectionRetries) if (waitTime < minWaitBetweenReconnectionRetries)
{ {
waitTime = minWaitBetweenReconnectionRetries; waitTime = minWaitBetweenReconnectionRetries;
} }
if (waitTime > maxWaitBetweenReconnectionRetries || waitTime == 0) if (waitTime > maxWaitBetweenReconnectionRetries)
{
waitTime = maxWaitBetweenReconnectionRetries;
}
if (retryCount >= maxRetryCountWithoutOverflow)
{ {
waitTime = maxWaitBetweenReconnectionRetries; waitTime = maxWaitBetweenReconnectionRetries;
} }

View File

@@ -23,6 +23,7 @@ set (TEST_TARGET_NAMES
IXWebSocketSubProtocolTest IXWebSocketSubProtocolTest
# IXWebSocketBroadcastTest ## FIXME was depending on cobra / take a broadcast server from ws # IXWebSocketBroadcastTest ## FIXME was depending on cobra / take a broadcast server from ws
IXStrCaseCompareTest IXStrCaseCompareTest
IXExponentialBackoffTest
) )
# Some unittest don't work on windows yet # Some unittest don't work on windows yet

View File

@@ -0,0 +1,39 @@
/*
* IXExponentialBackoffTest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2022 Machine Zone. All rights reserved.
*/
#include "IXTest.h"
#include "catch.hpp"
#include <iostream>
#include <ixwebsocket/IXExponentialBackoff.h>
#include <string.h>
using namespace ix;
namespace ix
{
TEST_CASE("exponential_backoff", "[exponential_backoff]")
{
SECTION("1")
{
// First parameter is retrycount
REQUIRE(calculateRetryWaitMilliseconds(0, 10000, 100) == 100);
REQUIRE(calculateRetryWaitMilliseconds(1, 10000, 100) == 200);
REQUIRE(calculateRetryWaitMilliseconds(2, 10000, 100) == 400);
REQUIRE(calculateRetryWaitMilliseconds(3, 10000, 100) == 800);
REQUIRE(calculateRetryWaitMilliseconds(4, 10000, 100) == 1600);
REQUIRE(calculateRetryWaitMilliseconds(5, 10000, 100) == 3200);
REQUIRE(calculateRetryWaitMilliseconds(6, 10000, 100) == 6400);
REQUIRE(calculateRetryWaitMilliseconds(20, 10000, 100) == 10000);
REQUIRE(calculateRetryWaitMilliseconds(25, 10000, 100) == 10000);
// Things get special after 26 retries
REQUIRE(calculateRetryWaitMilliseconds(26, 10000, 100) == 10000);
REQUIRE(calculateRetryWaitMilliseconds(27, 10000, 100) == 10000);
REQUIRE(calculateRetryWaitMilliseconds(27, 10000, 100) == 10000);
}
}
} // namespace ix