fix(firmware): event_log thread safety and NVS wear
- Remove monotonic counter writes to NVS (stop burning flash on every event). Derive head and cnt by scanning slots on boot. - Widen seq to uint32 so slot scan works across multi-year lifetimes. - Add FreeRTOS mutex around write/read so WiFi event handlers can safely call event_log_write from another task. - Check Preferences.begin() return; disable logging if NVS unavailable. - Extract NTP_SYNC_THRESHOLD constant; drop misleading native uptime. - Add tests for empty read, max_entries truncation, real-path hash.
This commit is contained in:
@@ -46,11 +46,42 @@ void test_ring_buffer_wraps_after_32_entries() {
|
||||
TEST_ASSERT_EQUAL(8, buf[31].data0);
|
||||
}
|
||||
|
||||
void test_empty_log_read_returns_zero() {
|
||||
event_log_init();
|
||||
EventLogEntry buf[8];
|
||||
size_t n = event_log_read_recent(buf, 8);
|
||||
TEST_ASSERT_EQUAL(0, n);
|
||||
}
|
||||
|
||||
void test_read_recent_truncates_to_max_entries() {
|
||||
event_log_init();
|
||||
for (int i = 0; i < 10; i++) event_log_write(EVT_HTTP_OK, (uint16_t)i, 0);
|
||||
EventLogEntry buf[3];
|
||||
size_t n = event_log_read_recent(buf, 3);
|
||||
TEST_ASSERT_EQUAL(3, n);
|
||||
// Newest 3: data0 == 9, 8, 7
|
||||
TEST_ASSERT_EQUAL(9, buf[0].data0);
|
||||
TEST_ASSERT_EQUAL(8, buf[1].data0);
|
||||
TEST_ASSERT_EQUAL(7, buf[2].data0);
|
||||
}
|
||||
|
||||
void test_path_hash_distinguishes_real_api_paths() {
|
||||
uint16_t h1 = event_log_path_hash("/api/v1/heartbeat");
|
||||
uint16_t h2 = event_log_path_hash("/api/v1/camera/events/batch");
|
||||
uint16_t h3 = event_log_path_hash("/api/v1/events/batch");
|
||||
TEST_ASSERT_NOT_EQUAL(h1, h2);
|
||||
TEST_ASSERT_NOT_EQUAL(h1, h3);
|
||||
TEST_ASSERT_NOT_EQUAL(h2, h3);
|
||||
}
|
||||
|
||||
int main() {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_entry_is_32_bytes);
|
||||
RUN_TEST(test_path_hash_is_stable_and_differs);
|
||||
RUN_TEST(test_write_then_read_recent_returns_newest_first);
|
||||
RUN_TEST(test_ring_buffer_wraps_after_32_entries);
|
||||
RUN_TEST(test_empty_log_read_returns_zero);
|
||||
RUN_TEST(test_read_recent_truncates_to_max_entries);
|
||||
RUN_TEST(test_path_hash_distinguishes_real_api_paths);
|
||||
return UNITY_END();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user