Skip to content

Commit

Permalink
TimedAttempt: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pennam committed Aug 8, 2024
1 parent a0c9dcd commit 8769d92
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 1 deletion.
4 changes: 3 additions & 1 deletion extras/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ set(TEST_SRCS
src/test_writeOnly.cpp
src/test_writeOnDemand.cpp
src/test_writeOnChange.cpp
src/test_TimedAttempt.cpp
)

set(TEST_UTIL_SRCS
Expand All @@ -53,6 +54,7 @@ set(TEST_UTIL_SRCS
)

set(TEST_DUT_SRCS
../../src/utility/time/TimedAttempt.cpp
../../src/property/Property.cpp
../../src/property/PropertyContainer.cpp
../../src/cbor/CBORDecoder.cpp
Expand Down Expand Up @@ -84,7 +86,7 @@ set(TEST_TARGET_SRCS

##########################################################################

add_compile_definitions(HOST)
add_compile_definitions(HOST HAS_TCP)
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
add_compile_options(-Wno-cast-function-type)

Expand Down
162 changes: 162 additions & 0 deletions extras/test/src/test_TimedAttempt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
Copyright (c) 2024 Arduino. All rights reserved.
*/

/******************************************************************************
INCLUDE
******************************************************************************/

#include <catch.hpp>

#include <TimedAttempt.h>
#include <AIoTC_Config.h>
#include <Arduino.h>
#include <limits.h>

/******************************************************************************
TEST CODE
******************************************************************************/

SCENARIO("Test broker connection retries")
{
TimedAttempt _connection_attempt(0,0);

_connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms,
AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);

/* 100000 retries are more or less 37 days without connection */
while(_connection_attempt.getRetryCount() < 100000) {
_connection_attempt.retry();

switch(_connection_attempt.getRetryCount()) {
case 1:
REQUIRE(_connection_attempt.getWaitTime() == 2000);
break;
case 2:
REQUIRE(_connection_attempt.getWaitTime() == 4000);
break;
case 3:
REQUIRE(_connection_attempt.getWaitTime() == 8000);
break;
case 4:
REQUIRE(_connection_attempt.getWaitTime() == 16000);
break;
default:
REQUIRE(_connection_attempt.getWaitTime() == 32000);
break;
}
}
}

SCENARIO("Test thing id request with no answer from the cloud")
{
TimedAttempt _attachAttempt(0,0);

_attachAttempt.begin(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms,
AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms);

/* 100000 retries are more or less 37 days of requests */
while(_attachAttempt.getRetryCount() < 100000) {
_attachAttempt.retry();

switch(_attachAttempt.getRetryCount()) {
case 1:
REQUIRE(_attachAttempt.getWaitTime() == 4000);
break;
case 2:
REQUIRE(_attachAttempt.getWaitTime() == 8000);
break;
case 3:
REQUIRE(_attachAttempt.getWaitTime() == 16000);
break;
default:
REQUIRE(_attachAttempt.getWaitTime() == 32000);
break;
}
}
}

SCENARIO("Test thing id request of a detached device")
{
TimedAttempt _attachAttempt(0,0);

_attachAttempt.begin(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms,
AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms);

/* 100000 retries are more or less 37 days of requests */
while(_attachAttempt.getRetryCount() < 100000) {
_attachAttempt.retry();

switch(_attachAttempt.getRetryCount()) {
case 1:
REQUIRE(_attachAttempt.getWaitTime() == 4000);
_attachAttempt.reconfigure(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms *
AIOT_CONFIG_DEVICE_REGISTERED_RETRY_DELAY_k,
AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms *
AIOT_CONFIG_MAX_DEVICE_REGISTERED_RETRY_DELAY_k);
break;
case 2:
REQUIRE(_attachAttempt.getWaitTime() == 80000);
break;
case 3:
REQUIRE(_attachAttempt.getWaitTime() == 160000);
break;
case 4:
REQUIRE(_attachAttempt.getWaitTime() == 320000);
break;
case 5:
REQUIRE(_attachAttempt.getWaitTime() == 640000);
break;
default:
REQUIRE(_attachAttempt.getWaitTime() == 1280000);
break;
}
}
}

SCENARIO("Test isExpired()")
{
TimedAttempt attempt(0,0);

attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms,
AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);

/* Initial condition */
set_millis(0);
REQUIRE(attempt.isExpired() == false);

/* Normal retry 2000ms */
attempt.retry();
set_millis(1000);
REQUIRE(attempt.isExpired() == false);
set_millis(1999);
REQUIRE(attempt.isExpired() == false);
set_millis(2000);
REQUIRE(attempt.isExpired() == false);
set_millis(2001);
REQUIRE(attempt.isExpired() == true);

/* Retry with rollover 4000ms */
set_millis(ULONG_MAX - 1999);
attempt.retry();
set_millis(0);
REQUIRE(attempt.isExpired() == false);
set_millis(1999);
REQUIRE(attempt.isExpired() == false);
set_millis(2000);
REQUIRE(attempt.isExpired() == false);
set_millis(2001);
REQUIRE(attempt.isExpired() == true);

/* Normal retry 8000ms */
set_millis(4000);
attempt.retry();
set_millis(4000);
REQUIRE(attempt.isExpired() == false);
set_millis(11999);
REQUIRE(attempt.isExpired() == false);
set_millis(12000);
REQUIRE(attempt.isExpired() == false);
set_millis(12001);
REQUIRE(attempt.isExpired() == true);
}

0 comments on commit 8769d92

Please sign in to comment.