Skip to content

Commit 3dd0423

Browse files
committed
TimedAttempt: add tests
1 parent e940c43 commit 3dd0423

File tree

3 files changed

+172
-1
lines changed

3 files changed

+172
-1
lines changed

extras/test/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ set(TEST_SRCS
4545
src/test_writeOnly.cpp
4646
src/test_writeOnDemand.cpp
4747
src/test_writeOnChange.cpp
48+
src/test_TimedAttempt.cpp
4849
)
4950

5051
set(TEST_UTIL_SRCS
@@ -53,6 +54,7 @@ set(TEST_UTIL_SRCS
5354
)
5455

5556
set(TEST_DUT_SRCS
57+
../../src/utility/time/TimedAttempt.cpp
5658
../../src/property/Property.cpp
5759
../../src/property/PropertyContainer.cpp
5860
../../src/cbor/CBORDecoder.cpp
@@ -84,7 +86,7 @@ set(TEST_TARGET_SRCS
8486

8587
##########################################################################
8688

87-
add_compile_definitions(HOST)
89+
add_compile_definitions(HOST HAS_TCP)
8890
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
8991
add_compile_options(-Wno-cast-function-type)
9092

extras/test/include/Arduino.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111

1212
#include <string>
1313

14+
/******************************************************************************
15+
DEFINES
16+
******************************************************************************/
17+
#ifndef min
18+
#define min(a,b) ((a)<(b)?(a):(b))
19+
#endif
20+
1421
/******************************************************************************
1522
TYPEDEF
1623
******************************************************************************/

extras/test/src/test_TimedAttempt.cpp

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
Copyright (c) 2024 Arduino. All rights reserved.
3+
*/
4+
5+
/******************************************************************************
6+
INCLUDE
7+
******************************************************************************/
8+
9+
#include <catch.hpp>
10+
11+
#include <TimedAttempt.h>
12+
#include <AIoTC_Config.h>
13+
#include <Arduino.h>
14+
#include <limits.h>
15+
16+
/******************************************************************************
17+
TEST CODE
18+
******************************************************************************/
19+
20+
SCENARIO("Test broker connection retries")
21+
{
22+
TimedAttempt _connection_attempt(0,0);
23+
24+
_connection_attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms,
25+
AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
26+
27+
/* 100000 retries are more or less 37 days without connection */
28+
while(_connection_attempt.getRetryCount() < 100000) {
29+
_connection_attempt.retry();
30+
31+
switch(_connection_attempt.getRetryCount()) {
32+
case 1:
33+
REQUIRE(_connection_attempt.getWaitTime() == 2000);
34+
break;
35+
case 2:
36+
REQUIRE(_connection_attempt.getWaitTime() == 4000);
37+
break;
38+
case 3:
39+
REQUIRE(_connection_attempt.getWaitTime() == 8000);
40+
break;
41+
case 4:
42+
REQUIRE(_connection_attempt.getWaitTime() == 16000);
43+
break;
44+
default:
45+
REQUIRE(_connection_attempt.getWaitTime() == 32000);
46+
break;
47+
}
48+
}
49+
}
50+
51+
SCENARIO("Test thing id request with no answer from the cloud")
52+
{
53+
TimedAttempt _attachAttempt(0,0);
54+
55+
_attachAttempt.begin(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms,
56+
AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms);
57+
58+
/* 100000 retries are more or less 37 days of requests */
59+
while(_attachAttempt.getRetryCount() < 100000) {
60+
_attachAttempt.retry();
61+
62+
switch(_attachAttempt.getRetryCount()) {
63+
case 1:
64+
REQUIRE(_attachAttempt.getWaitTime() == 4000);
65+
break;
66+
case 2:
67+
REQUIRE(_attachAttempt.getWaitTime() == 8000);
68+
break;
69+
case 3:
70+
REQUIRE(_attachAttempt.getWaitTime() == 16000);
71+
break;
72+
default:
73+
REQUIRE(_attachAttempt.getWaitTime() == 32000);
74+
break;
75+
}
76+
}
77+
}
78+
79+
SCENARIO("Test thing id request of a detached device")
80+
{
81+
TimedAttempt _attachAttempt(0,0);
82+
83+
_attachAttempt.begin(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms,
84+
AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms);
85+
86+
/* 100000 retries are more or less 37 days of requests */
87+
while(_attachAttempt.getRetryCount() < 100000) {
88+
_attachAttempt.retry();
89+
90+
switch(_attachAttempt.getRetryCount()) {
91+
case 1:
92+
REQUIRE(_attachAttempt.getWaitTime() == 4000);
93+
_attachAttempt.reconfigure(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms *
94+
AIOT_CONFIG_DEVICE_REGISTERED_RETRY_DELAY_k,
95+
AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms *
96+
AIOT_CONFIG_MAX_DEVICE_REGISTERED_RETRY_DELAY_k);
97+
break;
98+
case 2:
99+
REQUIRE(_attachAttempt.getWaitTime() == 80000);
100+
break;
101+
case 3:
102+
REQUIRE(_attachAttempt.getWaitTime() == 160000);
103+
break;
104+
case 4:
105+
REQUIRE(_attachAttempt.getWaitTime() == 320000);
106+
break;
107+
case 5:
108+
REQUIRE(_attachAttempt.getWaitTime() == 640000);
109+
break;
110+
default:
111+
REQUIRE(_attachAttempt.getWaitTime() == 1280000);
112+
break;
113+
}
114+
}
115+
}
116+
117+
SCENARIO("Test isExpired()")
118+
{
119+
TimedAttempt attempt(0,0);
120+
121+
attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms,
122+
AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms);
123+
124+
/* Initial condition */
125+
set_millis(0);
126+
REQUIRE(attempt.isExpired() == false);
127+
128+
/* Normal retry 2000ms */
129+
attempt.retry();
130+
set_millis(1000);
131+
REQUIRE(attempt.isExpired() == false);
132+
set_millis(1999);
133+
REQUIRE(attempt.isExpired() == false);
134+
set_millis(2000);
135+
REQUIRE(attempt.isExpired() == false);
136+
set_millis(2001);
137+
REQUIRE(attempt.isExpired() == true);
138+
139+
/* Retry with rollover 4000ms */
140+
set_millis(ULONG_MAX - 1999);
141+
attempt.retry();
142+
set_millis(0);
143+
REQUIRE(attempt.isExpired() == false);
144+
set_millis(1999);
145+
REQUIRE(attempt.isExpired() == false);
146+
set_millis(2000);
147+
REQUIRE(attempt.isExpired() == false);
148+
set_millis(2001);
149+
REQUIRE(attempt.isExpired() == true);
150+
151+
/* Normal retry 8000ms */
152+
set_millis(4000);
153+
attempt.retry();
154+
set_millis(4000);
155+
REQUIRE(attempt.isExpired() == false);
156+
set_millis(11999);
157+
REQUIRE(attempt.isExpired() == false);
158+
set_millis(12000);
159+
REQUIRE(attempt.isExpired() == false);
160+
set_millis(12001);
161+
REQUIRE(attempt.isExpired() == true);
162+
}

0 commit comments

Comments
 (0)