-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |