|
| 1 | +/* |
| 2 | + This file is part of the Arduino_CloudUtils library. |
| 3 | +
|
| 4 | + Copyright (c) 2024 Arduino SA |
| 5 | +
|
| 6 | + This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | + License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | + file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +*/ |
| 10 | + |
| 11 | +#include <catch2/catch_test_macros.hpp> |
| 12 | + |
| 13 | +#include <time/TimedAttempt.h> |
| 14 | +#include <Arduino.h> |
| 15 | +#include <limits.h> |
| 16 | + |
| 17 | +using namespace arduino::time; |
| 18 | + |
| 19 | +SCENARIO("Test broker connection retries") { |
| 20 | + TimedAttempt _connection_attempt(0,0); |
| 21 | + |
| 22 | + _connection_attempt.begin(1000UL, 32000UL); |
| 23 | + |
| 24 | + /* 100000 retries are more or less 37 days without connection */ |
| 25 | + while(_connection_attempt.getRetryCount() < 100000) { |
| 26 | + _connection_attempt.retry(); |
| 27 | + |
| 28 | + switch(_connection_attempt.getRetryCount()) { |
| 29 | + case 1: |
| 30 | + REQUIRE(_connection_attempt.getWaitTime() == 2000); |
| 31 | + break; |
| 32 | + case 2: |
| 33 | + REQUIRE(_connection_attempt.getWaitTime() == 4000); |
| 34 | + break; |
| 35 | + case 3: |
| 36 | + REQUIRE(_connection_attempt.getWaitTime() == 8000); |
| 37 | + break; |
| 38 | + case 4: |
| 39 | + REQUIRE(_connection_attempt.getWaitTime() == 16000); |
| 40 | + break; |
| 41 | + default: |
| 42 | + REQUIRE(_connection_attempt.getWaitTime() == 32000); |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +SCENARIO("Test thing id request with no answer from the cloud") { |
| 49 | + TimedAttempt _attachAttempt(0,0); |
| 50 | + |
| 51 | + _attachAttempt.begin(2000UL, 32000UL); |
| 52 | + |
| 53 | + /* 100000 retries are more or less 37 days of requests */ |
| 54 | + while(_attachAttempt.getRetryCount() < 100000) { |
| 55 | + _attachAttempt.retry(); |
| 56 | + |
| 57 | + switch(_attachAttempt.getRetryCount()) { |
| 58 | + case 1: |
| 59 | + REQUIRE(_attachAttempt.getWaitTime() == 4000); |
| 60 | + break; |
| 61 | + case 2: |
| 62 | + REQUIRE(_attachAttempt.getWaitTime() == 8000); |
| 63 | + break; |
| 64 | + case 3: |
| 65 | + REQUIRE(_attachAttempt.getWaitTime() == 16000); |
| 66 | + break; |
| 67 | + default: |
| 68 | + REQUIRE(_attachAttempt.getWaitTime() == 32000); |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +SCENARIO("Test thing id request of a detached device") { |
| 75 | + TimedAttempt _attachAttempt(0,0); |
| 76 | + |
| 77 | + _attachAttempt.begin(2000UL, 32000UL); |
| 78 | + |
| 79 | + while(_attachAttempt.getRetryCount() < 100000) { |
| 80 | + _attachAttempt.retry(); |
| 81 | + |
| 82 | + switch(_attachAttempt.getRetryCount()) { |
| 83 | + case 1: |
| 84 | + REQUIRE(_attachAttempt.getWaitTime() == 4000); |
| 85 | + _attachAttempt.reconfigure(2000UL * 10UL, 32000UL * 40UL); |
| 86 | + break; |
| 87 | + case 2: |
| 88 | + REQUIRE(_attachAttempt.getWaitTime() == 80000); |
| 89 | + break; |
| 90 | + case 3: |
| 91 | + REQUIRE(_attachAttempt.getWaitTime() == 160000); |
| 92 | + break; |
| 93 | + case 4: |
| 94 | + REQUIRE(_attachAttempt.getWaitTime() == 320000); |
| 95 | + break; |
| 96 | + case 5: |
| 97 | + REQUIRE(_attachAttempt.getWaitTime() == 640000); |
| 98 | + break; |
| 99 | + default: |
| 100 | + REQUIRE(_attachAttempt.getWaitTime() == 1280000); |
| 101 | + break; |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +SCENARIO("Test last value request") { |
| 107 | + TimedAttempt _syncAttempt(0,0); |
| 108 | + |
| 109 | + _syncAttempt.begin(30000UL); |
| 110 | + |
| 111 | + /* 100000 retries are more or less 37 days of requests */ |
| 112 | + while(_syncAttempt.getRetryCount() < 100000) { |
| 113 | + _syncAttempt.retry(); |
| 114 | + |
| 115 | + switch(_syncAttempt.getRetryCount()) { |
| 116 | + default: |
| 117 | + REQUIRE(_syncAttempt.getWaitTime() == 30000); |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +SCENARIO("Test isExpired() and isRetry()") { |
| 124 | + TimedAttempt attempt(0,0); |
| 125 | + |
| 126 | + attempt.begin(1000UL, 32000UL); |
| 127 | + |
| 128 | + /* Initial condition */ |
| 129 | + set_millis(0); |
| 130 | + REQUIRE(attempt.isExpired() == false); |
| 131 | + REQUIRE(attempt.isRetry() == false); |
| 132 | + |
| 133 | + /* Normal retry 2000ms */ |
| 134 | + attempt.retry(); |
| 135 | + REQUIRE(attempt.isRetry() == true); |
| 136 | + set_millis(1000); |
| 137 | + REQUIRE(attempt.isExpired() == false); |
| 138 | + set_millis(1999); |
| 139 | + REQUIRE(attempt.isExpired() == false); |
| 140 | + set_millis(2000); |
| 141 | + REQUIRE(attempt.isExpired() == false); |
| 142 | + set_millis(2001); |
| 143 | + REQUIRE(attempt.isExpired() == true); |
| 144 | + |
| 145 | + /* Retry with rollover 4000ms */ |
| 146 | + set_millis(ULONG_MAX - 1999); |
| 147 | + attempt.retry(); |
| 148 | + REQUIRE(attempt.isRetry() == true); |
| 149 | + set_millis(0); |
| 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 | + /* Normal retry 8000ms */ |
| 159 | + set_millis(4000); |
| 160 | + attempt.retry(); |
| 161 | + REQUIRE(attempt.isRetry() == true); |
| 162 | + set_millis(4000); |
| 163 | + REQUIRE(attempt.isExpired() == false); |
| 164 | + set_millis(11999); |
| 165 | + REQUIRE(attempt.isExpired() == false); |
| 166 | + set_millis(12000); |
| 167 | + REQUIRE(attempt.isExpired() == false); |
| 168 | + set_millis(12001); |
| 169 | + REQUIRE(attempt.isExpired() == true); |
| 170 | + |
| 171 | + attempt.reset(); |
| 172 | + REQUIRE(attempt.isRetry() == false); |
| 173 | +} |
0 commit comments