FastLED 3.9.15
Loading...
Searching...
No Matches
LoopbackTestRunner.h
Go to the documentation of this file.
1
21
22#pragma once
23
24#include <FastLED.h>
25#include "fl/net/http/fetch.h"
26#include "fl/log/log.h"
27
29public:
30 using CompletionCallback = fl::function<void(bool success, int passed, int total)>;
31
42
44 : state(IDLE)
45 , tests_run(0)
46 , tests_passed(0)
47 , has_failure(false)
48 , callback_invoked(false)
49 {}
50
52 FL_WARN("[LOOPBACK] startTests() called - initializing test runner");
53 FL_WARN("[LOOPBACK] startTests() called");
54 completion_callback = callback;
55 tests_run = 0;
56 tests_passed = 0;
57 has_failure = false;
58 callback_invoked = false;
60 }
61
62 void update() {
63 // State machine advances through RUNNING -> WAITING -> RUNNING transitions
64 // Callbacks (in runTest) handle WAITING -> RUNNING_NEXT transitions
65 switch (state) {
67 runTest("GET /", "http://localhost:8080/",
68 "Hello from loopback test!\n", RUNNING_GET_PING);
69 break;
70
72 runTest("GET /ping", "http://localhost:8080/ping",
73 "pong\n", RUNNING_GET_TEST);
74 break;
75
77 runTest("GET /test", "http://localhost:8080/test",
78 "test response\n", COMPLETED);
79 break;
80
81 case COMPLETED:
83 // Print summary
84 Serial.println();
85 Serial.println("======================");
86 Serial.print("Test Results: ");
87 Serial.print(tests_passed);
88 Serial.print("/");
89 Serial.print(tests_run);
90 Serial.println(" passed");
91 Serial.println("======================");
92
93 bool success = !has_failure && tests_passed == tests_run;
94 if (success) {
95 Serial.println("✓ All loopback tests PASSED");
96 } else {
97 Serial.println("✗ Loopback tests FAILED");
98 }
99
100 // Invoke user callback with results
102 callback_invoked = true;
103 }
104 state = IDLE;
105 break;
106
107 default:
108 // WAITING states or IDLE - nothing to do
109 break;
110 }
111 }
112
113 bool isRunning() const {
114 return state != IDLE && state != COMPLETED;
115 }
116
118 return state;
119 }
120
121private:
122 void runTest(const char* name, const char* url,
123 const char* expected, TestSequenceState next_state) {
124 FL_WARN("[LOOPBACK] Running test: " << name << " -> " << url);
125 Serial.print("Running test: ");
126 Serial.println(name);
127
128 tests_run++;
129
130 // Launch async HTTP request with non-blocking .then()/.catch_() callbacks
132 .then([this, expected, next_state](const fl::net::http::Response& resp) {
133 // Success callback - validate response
134 if (resp.status() == 200 && resp.text() == expected) {
135 tests_passed++;
136 Serial.println(" ✓ PASSED");
137 } else {
138 has_failure = true;
139 Serial.print(" ✗ FAILED - ");
140 if (resp.status() != 200) {
141 Serial.print("Status: ");
142 Serial.print(static_cast<int>(resp.status()));
143 } else {
144 Serial.print("Expected: '");
145 Serial.print(expected);
146 Serial.print("', Got: '");
147 Serial.print(resp.text().c_str());
148 Serial.print("'");
149 }
150 Serial.println();
151 }
152
153 // Advance to next test state
154 state = next_state;
155 })
156 .catch_([this](const fl::task::Error& err) {
157 // Error callback - network/connection failure
158 has_failure = true;
159 Serial.print(" ✗ FAILED - Error: ");
160 Serial.println(err.message.c_str());
161
162 // Abort remaining tests on network error
164 });
165
166 // Immediately transition to waiting state (non-blocking)
167 state = static_cast<TestSequenceState>(state + 1); // RUNNING_X -> WAITING_X
168 }
169
176};
CompletionCallback completion_callback
fl::function< void(bool success, int passed, int total)> CompletionCallback
TestSequenceState getState() const
void runTest(const char *name, const char *url, const char *expected, TestSequenceState next_state)
void startTests(CompletionCallback callback)
TestSequenceState state
const char * c_str() const FL_NOEXCEPT
const fl::string & text() const
Response body as text (like JavaScript response.text())
Definition fetch.h:95
int status() const
HTTP status code (like JavaScript response.status)
Definition fetch.h:86
HTTP response class (unified interface)
Definition fetch.h:78
Unified HTTP fetch API for FastLED (cross-platform)
#define FL_WARN(X)
Definition log.h:276
Centralized logging categories for FastLED hardware interfaces and subsystems.
fl::task::Promise< Response > fetch_get(const fl::string &url, const FetchOptions &request)
HTTP GET request.
fl::string message
Definition promise.h:40
Error type for promises.
Definition promise.h:39
#define Serial
Definition serial.h:304