Order radio schedules and preserve temporary lease deadlines
Apply due radio2 settings by requested start time and expire every old temporary window before starting its successor. Anchor temporary deadlines to command acceptance so idle time before a command cannot shorten the new lease. Keep failed radio2 saves in their original order and retry without rewriting their scheduled timestamps. Give repeater persistence its own backoff and cache the next actionable deadline, preventing repeated writes while allowing temporary windows to start and expire on time. Exercise all 24 slot insertion orders with punctual and delayed servicing, plus adjacent windows, storage failures, command-time anchoring, and timer rollover.
This commit is contained in:
1 parent
878c4695ae
commit
a2a7d3b168
7 files changed
+256
-40
No files matched your search
+149
-1
@@ -3,6 +3,7 @@
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
struct Clock : mesh::RTCClock {
|
||||
uint32_t epoch = 1700000000;
|
||||
@@ -42,7 +43,154 @@ struct Fixture {
|
||||
}
|
||||
void advance(uint32_t ms, bool epoch=true) { g_mock_millis+=ms; if(epoch)clock.epoch+=ms/1000; cli.loop(); }
|
||||
};
|
||||
int main() {
|
||||
static void permanentScheduleOrder() {
|
||||
for (bool fail_first_save : {false, true}) {
|
||||
Fixture f;
|
||||
char command[160];
|
||||
snprintf(command, sizeof(command), "set radioat2 912.5,500,8,5,rx,%lu,80",
|
||||
(unsigned long)(f.clock.epoch + 120));
|
||||
f.cmd(command);
|
||||
snprintf(command, sizeof(command), "set radioat2 910.5,500,8,5,rx,%lu,80",
|
||||
(unsigned long)(f.clock.epoch + 60));
|
||||
f.cmd(command);
|
||||
f.fs.fail_write = fail_first_save;
|
||||
f.advance(120000); // both are due after sleep or a forward clock correction
|
||||
if (fail_first_save) {
|
||||
f.fs.fail_write = false;
|
||||
f.advance(60000);
|
||||
}
|
||||
assert(f.radio.p.secondary.params.freq == 912.5f);
|
||||
Radio reboot; mesh::RadioProfileCLI restored;
|
||||
restored.begin(&f.fs, &reboot, &f.clock);
|
||||
assert(reboot.p.secondary.params.freq == 912.5f);
|
||||
assert(!strcmp(f.cmd("get radioat2 all"), "> slots: "));
|
||||
}
|
||||
}
|
||||
|
||||
static void adjacentTemporarySchedules() {
|
||||
for (bool later_first : {false, true}) {
|
||||
Fixture f;
|
||||
const uint32_t epoch = f.clock.epoch;
|
||||
for (unsigned i = 0; i < 2; ++i) {
|
||||
const bool later = later_first ? i == 0 : i == 1;
|
||||
char command[160];
|
||||
snprintf(command, sizeof(command), "set tempradioat2 %.1f,500,8,5,rxtx,%lu,%lu,80",
|
||||
later ? 912.5 : 910.5, (unsigned long)(epoch + (later ? 60 : 1)),
|
||||
(unsigned long)(epoch + (later ? 120 : 60)));
|
||||
f.cmd(command);
|
||||
}
|
||||
f.advance(1000);
|
||||
assert(f.radio.p.secondary_temporary && f.radio.p.secondary.params.freq == 910.5f);
|
||||
f.advance(59000);
|
||||
assert(f.radio.p.secondary_temporary && f.radio.p.secondary.params.freq == 912.5f);
|
||||
f.advance(59000);
|
||||
assert(f.radio.p.secondary_temporary && f.radio.p.secondary.params.freq == 912.5f);
|
||||
f.advance(1000);
|
||||
assert(!f.radio.p.enabled());
|
||||
}
|
||||
}
|
||||
|
||||
static void scheduledLeaseStartsAtCommandTime() {
|
||||
for (bool rollover : {false, true}) {
|
||||
Fixture f;
|
||||
if (rollover) { g_mock_millis = UINT32_MAX - 30000; f.cli.loop(); }
|
||||
g_mock_millis += 10000; f.clock.epoch += 10; // command arrives between loop calls
|
||||
char command[160];
|
||||
snprintf(command, sizeof(command), "set tempradioat2 910.5,500,8,5,rx,%lu,%lu,80",
|
||||
(unsigned long)(f.clock.epoch + 1), (unsigned long)(f.clock.epoch + 60));
|
||||
f.cmd(command);
|
||||
f.advance(1000);
|
||||
assert(f.radio.p.secondary_temporary);
|
||||
f.advance(49000);
|
||||
assert(f.radio.p.secondary_temporary); // idle time before the command is not charged to it
|
||||
f.advance(9000);
|
||||
assert(f.radio.p.secondary_temporary);
|
||||
f.advance(1000);
|
||||
assert(!f.radio.p.enabled());
|
||||
}
|
||||
}
|
||||
|
||||
static void schedulePermutations() {
|
||||
int order[] = {0, 1, 2, 3};
|
||||
do {
|
||||
for (bool temporary : {false, true}) for (bool late : {false, true}) {
|
||||
Fixture f;
|
||||
const uint32_t epoch = f.clock.epoch;
|
||||
for (int entry : order) {
|
||||
char command[160];
|
||||
if (temporary) {
|
||||
snprintf(command, sizeof(command), "set tempradioat2 %.1f,500,8,5,rx,%lu,%lu,80",
|
||||
910.5 + entry, (unsigned long)(epoch + 1 + entry * 60),
|
||||
(unsigned long)(epoch + 61 + entry * 60));
|
||||
} else {
|
||||
snprintf(command, sizeof(command), "set radioat2 %.1f,500,8,5,rx,%lu,80",
|
||||
910.5 + entry, (unsigned long)(epoch + (entry + 1) * 60));
|
||||
}
|
||||
f.cmd(command);
|
||||
}
|
||||
if (late) {
|
||||
f.advance(temporary ? 181000 : 240000);
|
||||
assert(f.radio.p.secondary.params.freq == 913.5f);
|
||||
} else {
|
||||
for (int entry = 0; entry < 4; ++entry) {
|
||||
f.advance(temporary && entry == 0 ? 1000 : 60000);
|
||||
assert(f.radio.p.secondary.params.freq == 910.5f + entry);
|
||||
}
|
||||
}
|
||||
assert(f.radio.p.secondary_temporary == temporary);
|
||||
if (temporary) { f.advance(60000); assert(!f.radio.p.enabled()); }
|
||||
}
|
||||
} while (std::next_permutation(order, order + 4));
|
||||
}
|
||||
|
||||
static void storageBackoffDoesNotDelayTemporaryWindows() {
|
||||
Fixture f;
|
||||
f.cmd("set radio2 910.5,500,8,5,rx,80"); f.advance(2000);
|
||||
char command[160];
|
||||
snprintf(command, sizeof(command), "set radioat2 912.5,500,8,5,rx,%lu,80", (unsigned long)(f.clock.epoch + 2));
|
||||
f.cmd(command);
|
||||
snprintf(command, sizeof(command), "set tempradioat2 911.5,500,8,5,rx,%lu,%lu,80",
|
||||
(unsigned long)(f.clock.epoch + 30), (unsigned long)(f.clock.epoch + 90));
|
||||
f.cmd(command);
|
||||
f.fs.fail_write = true;
|
||||
f.advance(2000);
|
||||
f.advance(28000);
|
||||
assert(f.radio.p.secondary_temporary && f.radio.p.secondary.params.freq == 911.5f);
|
||||
f.advance(60000);
|
||||
assert(!f.radio.p.secondary_temporary && f.radio.p.secondary.params.freq == 910.5f);
|
||||
f.fs.fail_write = false;
|
||||
f.advance(60000);
|
||||
assert(f.radio.p.secondary.params.freq == 912.5f);
|
||||
}
|
||||
|
||||
static void scheduledSaveFailureKeepsOriginalTimes() {
|
||||
Fixture f;
|
||||
char command[160];
|
||||
const uint32_t start = f.clock.epoch + 60;
|
||||
snprintf(command, sizeof(command), "set radioat2 910.5,500,8,5,rx,%lu,80", (unsigned long)start);
|
||||
f.cmd(command);
|
||||
f.fs.fail_write = true;
|
||||
f.advance(60000);
|
||||
char timestamp[32]; snprintf(timestamp, sizeof(timestamp), "@%lu", (unsigned long)start);
|
||||
assert(strstr(f.cmd("get radioat2 all"), timestamp));
|
||||
f.fs.fail_write = false;
|
||||
f.advance(59000);
|
||||
assert(!f.radio.p.enabled());
|
||||
f.advance(1000);
|
||||
assert(f.radio.p.secondary.params.freq == 910.5f);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc == 2) {
|
||||
if (!strcmp(argv[1], "permanent_order")) permanentScheduleOrder();
|
||||
else if (!strcmp(argv[1], "temporary_boundary")) adjacentTemporarySchedules();
|
||||
else if (!strcmp(argv[1], "lease_creation")) scheduledLeaseStartsAtCommandTime();
|
||||
else if (!strcmp(argv[1], "save_failure")) scheduledSaveFailureKeepsOriginalTimes();
|
||||
else if (!strcmp(argv[1], "schedule_permutations")) schedulePermutations();
|
||||
else if (!strcmp(argv[1], "storage_isolation")) storageBackoffDoesNotDelayTemporaryWindows();
|
||||
else return 2;
|
||||
return 0;
|
||||
}
|
||||
for (bool damaged_primary : {false, true}) {
|
||||
Fixture f(true);
|
||||
f.cmd("set radio2 910.5,500,8,5,rxtx,80");
|
||||
|
||||
@@ -19,7 +19,10 @@ class RadioProfileCLITest(unittest.TestCase):
|
||||
str(ROOT/'src/helpers/RadioProfileCLI.cpp'),
|
||||
str(ROOT/'test/fixtures/radio_profiles/cli_test.cpp'),
|
||||
'-o', str(exe)], check=True, capture_output=True, text=True)
|
||||
subprocess.run([str(exe)], check=True)
|
||||
for scenario in (None, 'permanent_order', 'temporary_boundary', 'lease_creation',
|
||||
'save_failure', 'schedule_permutations', 'storage_isolation'):
|
||||
with self.subTest(scenario=scenario or 'commands_persistence_and_timers'):
|
||||
subprocess.run([str(exe)] + ([scenario] if scenario else []), check=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -23,6 +23,7 @@ HARNESS = r'''
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <initializer_list>
|
||||
#include <helpers/RepeaterRadioTiming.h>
|
||||
#include <helpers/TempRadioLeaseDeadline.h>
|
||||
#define MESH_DEBUG_PRINTLN(...) ((void)0)
|
||||
@@ -53,7 +54,9 @@ struct Board { int reboots = 0; void reboot() { ++reboots; } };
|
||||
struct CLI {
|
||||
Board board; Board* getBoard() { return &board; }
|
||||
CLI& radioProfiles() { return *this; }
|
||||
bool savePrimaryPreamble(uint16_t) { return true; }
|
||||
bool preamble_save_success = true;
|
||||
unsigned preamble_saves = 0;
|
||||
bool savePrimaryPreamble(uint16_t) { ++preamble_saves; return preamble_save_success; }
|
||||
};
|
||||
struct Radio { uint32_t last_rx = 0; uint32_t getLastRecvMillis() { return last_rx; } };
|
||||
struct Barrier { bool held = false; bool waiting() const { return held; } void clear() { held = false; } };
|
||||
@@ -92,6 +95,7 @@ public:
|
||||
uint32_t scheduled_temp_radio_end_time = 0, scheduled_temp_radio_end_check_at = 0;
|
||||
bool scheduled_temp_radio_end_check_final = false;
|
||||
uint32_t scheduled_radio_retry_at = 0;
|
||||
uint32_t scheduled_radio_save_retry_at = 0;
|
||||
uint8_t scheduled_radio_retry_failures = 0;
|
||||
bool pending_self_advert = false, pending_self_advert_flood = false;
|
||||
uint32_t pending_self_advert_delay = 0;
|
||||
@@ -153,6 +157,41 @@ static void startTemp(MyMesh& m, int minutes) {
|
||||
assert(m.temp_radio_applied);
|
||||
}
|
||||
int main() {
|
||||
for (bool rollover : {false, true}) {
|
||||
MyMesh m;
|
||||
if (rollover) { now_ms = UINT32_MAX - 30000; m.last_millis = now_ms; }
|
||||
char reply[160];
|
||||
m.addScheduledRadioParams(false, 912.5, 250, 5, 5, m.rtc.now + 2, 0, reply, 48);
|
||||
assert(!strncmp(reply, "OK", 2));
|
||||
m._cli.preamble_save_success = false;
|
||||
m.advance(2); m.servicePostMeshLoop();
|
||||
assert(m._cli.preamble_saves == 1 && m.saves == 0);
|
||||
assert(m.next_scheduled_radio_check_at == m.scheduled_radio_save_retry_at);
|
||||
assert(m._prefs.freq == 909.5f);
|
||||
for (unsigned i = 0; i < 10; ++i) m.servicePostMeshLoop();
|
||||
assert(m._cli.preamble_saves == 1); // storage backoff survives the scheduler's cleanup
|
||||
m._cli.preamble_save_success = true;
|
||||
m.advance(59); m.servicePostMeshLoop();
|
||||
assert(m._cli.preamble_saves == 1);
|
||||
m.advance(1); m.servicePostMeshLoop();
|
||||
assert(m._cli.preamble_saves == 2 && m.saves == 1);
|
||||
assert(m._prefs.freq == 912.5f && !m.saved_radio_apply_pending);
|
||||
}
|
||||
{ // Backing off a failed permanent save must not delay temporary start/end.
|
||||
MyMesh m;
|
||||
char reply[160];
|
||||
m.addScheduledRadioParams(false, 912.5, 250, 5, 5, m.rtc.now + 2, 0, reply, 48);
|
||||
m.addScheduledRadioParams(true, 911.5, 250, 5, 5, m.rtc.now + 30, m.rtc.now + 90, reply, 80);
|
||||
m._cli.preamble_save_success = false;
|
||||
m.advance(2); m.servicePostMeshLoop();
|
||||
assert(m._cli.preamble_saves == 1);
|
||||
m.advance(28); m.servicePostMeshLoop();
|
||||
assert(m.temp_radio_applied && m.radio_timing.isTemporary());
|
||||
assert(m._cli.preamble_saves == 1);
|
||||
m.advance(60); m.servicePostMeshLoop();
|
||||
assert(!m.temp_radio_applied && !m.radio_timing.isTemporary());
|
||||
assert(m._prefs.freq == 909.5f);
|
||||
}
|
||||
{ // Pending schedules do not change current timings, even on an apply failure.
|
||||
MyMesh m;
|
||||
const uint32_t normal_local = m.next_local_advert;
|
||||
@@ -283,7 +322,7 @@ int main() {
|
||||
m.advance(23 * HOUR); m.servicePostMeshLoop();
|
||||
assert(m._cli.board.reboots == 1);
|
||||
}
|
||||
puts("13 production scheduler/watchdog/advert scenarios passed");
|
||||
puts("Production scheduler, storage-backoff, watchdog and advert scenarios passed");
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
Reference in new issue
Block a user