|
| 1 | +// Compile the actual implementation; only hardware/FreeRTOS calls are faked. |
| 2 | +#include <cstdio> |
| 3 | +#include "../../src/FindMyAdv.cpp" |
| 4 | + |
| 5 | +// Exit normally on failure, avoiding Windows assertion-dialog side effects. |
| 6 | +#define CHECK(condition) do { if (!(condition)) { \ |
| 7 | + std::fprintf(stderr, "Lifecycle check failed at line %d: %s\n", __LINE__, #condition); \ |
| 8 | + std::exit(1); \ |
| 9 | +} } while (false) |
| 10 | + |
| 11 | +static uint32_t clockMs = 0; |
| 12 | +static unsigned liveTasks = 0, createdTasks = 0, overlappingTasks = 0; |
| 13 | +static unsigned forcedDeletes = 0; |
| 14 | +static bool stalledTask = false; |
| 15 | + |
| 16 | +uint32_t millis() { return clockMs; } |
| 17 | +void delay(uint32_t ms) { |
| 18 | + clockMs += ms; |
| 19 | + // Model the old task seeing end()'s stop request on its next wakeup. |
| 20 | + if (!stalledTask && stopRequested && schedulerTask) { |
| 21 | + --liveTasks; |
| 22 | + schedulerTask = nullptr; |
| 23 | + } |
| 24 | +} |
| 25 | +void vTaskDelay(uint32_t ticks) { delay(ticks); } |
| 26 | +void vTaskDelete(TaskHandle_t task) { |
| 27 | + CHECK(task != nullptr); // The test never actually enters a scheduler loop. |
| 28 | + CHECK(liveTasks > 0); |
| 29 | + --liveTasks; |
| 30 | + ++forcedDeletes; |
| 31 | +} |
| 32 | +int xTaskCreate(void (*)(void *), const char *, uint32_t, void *, int, |
| 33 | + TaskHandle_t *handle) { |
| 34 | + if (liveTasks) ++overlappingTasks; |
| 35 | + ++liveTasks; |
| 36 | + *handle = reinterpret_cast<void *>(static_cast<uintptr_t>(++createdTasks)); |
| 37 | + return pdPASS; |
| 38 | +} |
| 39 | + |
| 40 | +int main() { |
| 41 | + FindMyAdvConfig config; |
| 42 | + config.googleAdvertisementEidHex = "0000000000000000000000000000000000000000"; |
| 43 | + CHECK(FindMyAdv.begin(config)); |
| 44 | + CHECK(liveTasks == 1); |
| 45 | + const unsigned initialCreated = createdTasks; |
| 46 | + CHECK(FindMyAdv.begin(config)); // Already running is still a no-op. |
| 47 | + CHECK(createdTasks == initialCreated); |
| 48 | + for (int i = 0; i < 5; ++i) { |
| 49 | + FindMyAdv.end(); |
| 50 | + CHECK(FindMyAdv.begin(config)); |
| 51 | + CHECK(overlappingTasks == 0); |
| 52 | + CHECK(liveTasks == 1); |
| 53 | + } |
| 54 | + CHECK(FindMyAdv.reconfigure(config)); |
| 55 | + CHECK(liveTasks == 1 && overlappingTasks == 0); |
| 56 | + FindMyAdv.end(); |
| 57 | + config.backgroundTask = false; |
| 58 | + CHECK(FindMyAdv.begin(config)); |
| 59 | + CHECK(liveTasks == 0); // Background -> manual must drain the old task. |
| 60 | + FindMyAdv.poll(); |
| 61 | + FindMyAdv.end(); |
| 62 | + config.backgroundTask = true; |
| 63 | + CHECK(FindMyAdv.begin(config)); |
| 64 | + FindMyAdv.end(); |
| 65 | + stalledTask = true; |
| 66 | + CHECK(FindMyAdv.begin(config)); |
| 67 | + CHECK(forcedDeletes == 1); // Preserve the bounded shutdown fallback. |
| 68 | + CHECK(liveTasks == 1 && overlappingTasks == 0); |
| 69 | + stalledTask = false; |
| 70 | + FindMyAdv.end(); |
| 71 | + delay(100); |
| 72 | + CHECK(liveTasks == 0); |
| 73 | +} |
0 commit comments