|
| 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 | + while(_attachAttempt.getRetryCount() < 100000) { |
| 87 | + _attachAttempt.retry(); |
| 88 | + |
| 89 | + switch(_attachAttempt.getRetryCount()) { |
| 90 | + case 1: |
| 91 | + REQUIRE(_attachAttempt.getWaitTime() == 4000); |
| 92 | + _attachAttempt.reconfigure(AIOT_CONFIG_THING_ID_REQUEST_RETRY_DELAY_ms * |
| 93 | + AIOT_CONFIG_DEVICE_REGISTERED_RETRY_DELAY_k, |
| 94 | + AIOT_CONFIG_MAX_THING_ID_REQUEST_RETRY_DELAY_ms * |
| 95 | + AIOT_CONFIG_MAX_DEVICE_REGISTERED_RETRY_DELAY_k); |
| 96 | + break; |
| 97 | + case 2: |
| 98 | + REQUIRE(_attachAttempt.getWaitTime() == 80000); |
| 99 | + break; |
| 100 | + case 3: |
| 101 | + REQUIRE(_attachAttempt.getWaitTime() == 160000); |
| 102 | + break; |
| 103 | + case 4: |
| 104 | + REQUIRE(_attachAttempt.getWaitTime() == 320000); |
| 105 | + break; |
| 106 | + case 5: |
| 107 | + REQUIRE(_attachAttempt.getWaitTime() == 640000); |
| 108 | + break; |
| 109 | + default: |
| 110 | + REQUIRE(_attachAttempt.getWaitTime() == 1280000); |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +SCENARIO("Test last value request") |
| 117 | +{ |
| 118 | + TimedAttempt _syncAttempt(0,0); |
| 119 | + |
| 120 | + _syncAttempt.begin(AIOT_CONFIG_TIMEOUT_FOR_LASTVALUES_SYNC_ms); |
| 121 | + |
| 122 | + /* 100000 retries are more or less 37 days of requests */ |
| 123 | + while(_syncAttempt.getRetryCount() < 100000) { |
| 124 | + _syncAttempt.retry(); |
| 125 | + |
| 126 | + switch(_syncAttempt.getRetryCount()) { |
| 127 | + default: |
| 128 | + REQUIRE(_syncAttempt.getWaitTime() == 30000); |
| 129 | + break; |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +SCENARIO("Test isExpired() and isRetry()") |
| 135 | +{ |
| 136 | + TimedAttempt attempt(0,0); |
| 137 | + |
| 138 | + attempt.begin(AIOT_CONFIG_RECONNECTION_RETRY_DELAY_ms, |
| 139 | + AIOT_CONFIG_MAX_RECONNECTION_RETRY_DELAY_ms); |
| 140 | + |
| 141 | + /* Initial condition */ |
| 142 | + set_millis(0); |
| 143 | + REQUIRE(attempt.isExpired() == false); |
| 144 | + REQUIRE(attempt.isRetry() == false); |
| 145 | + |
| 146 | + /* Normal retry 2000ms */ |
| 147 | + attempt.retry(); |
| 148 | + REQUIRE(attempt.isRetry() == true); |
| 149 | + set_millis(1000); |
| 150 | + REQUIRE(attempt.isExpired() == false); |
| 151 | + set_millis(1999); |
| 152 | + REQUIRE(attempt.isExpired() == false); |
| 153 | + set_millis(2000); |
| 154 | + REQUIRE(attempt.isExpired() == false); |
| 155 | + set_millis(2001); |
| 156 | + REQUIRE(attempt.isExpired() == true); |
| 157 | + |
| 158 | + /* Retry with rollover 4000ms */ |
| 159 | + set_millis(ULONG_MAX - 1999); |
| 160 | + attempt.retry(); |
| 161 | + REQUIRE(attempt.isRetry() == true); |
| 162 | + set_millis(0); |
| 163 | + REQUIRE(attempt.isExpired() == false); |
| 164 | + set_millis(1999); |
| 165 | + REQUIRE(attempt.isExpired() == false); |
| 166 | + set_millis(2000); |
| 167 | + REQUIRE(attempt.isExpired() == false); |
| 168 | + set_millis(2001); |
| 169 | + REQUIRE(attempt.isExpired() == true); |
| 170 | + |
| 171 | + /* Normal retry 8000ms */ |
| 172 | + set_millis(4000); |
| 173 | + attempt.retry(); |
| 174 | + REQUIRE(attempt.isRetry() == true); |
| 175 | + set_millis(4000); |
| 176 | + REQUIRE(attempt.isExpired() == false); |
| 177 | + set_millis(11999); |
| 178 | + REQUIRE(attempt.isExpired() == false); |
| 179 | + set_millis(12000); |
| 180 | + REQUIRE(attempt.isExpired() == false); |
| 181 | + set_millis(12001); |
| 182 | + REQUIRE(attempt.isExpired() == true); |
| 183 | + |
| 184 | + attempt.reset(); |
| 185 | + REQUIRE(attempt.isRetry() == false); |
| 186 | +} |
0 commit comments