Fix Ethernet runtime test read ownership races
This commit is contained in:
1 parent
c131c2f75a
commit
2c3e75326d
2 files changed
+118
-17
No files matched your search
+47
-17
@@ -70,6 +70,11 @@ static void write_all(const uint8_t* data, size_t size) {
|
||||
return accepted == size;
|
||||
});
|
||||
}
|
||||
static void read_complete_line(char* line, size_t capacity) {
|
||||
// Online is only a snapshot: the worker can take SPI before the read.
|
||||
// Retry the operation itself, preserving partial input between turns.
|
||||
wait_until([&] { return ethernet_read_line(line, capacity); });
|
||||
}
|
||||
void mock_bus_touch() {
|
||||
assert(bus_owner.load() && spi_active.load());
|
||||
assert(ethernet_spi_access.load() != EthernetSpiAccess::Idle);
|
||||
@@ -577,7 +582,8 @@ int main(int argc, char** argv) {
|
||||
wait_until([&] { return output().find(first + second.substr(0, 112)) != std::string::npos && ethernet_is_running(); });
|
||||
assert(ethernet_client.write(reinterpret_cast<const uint8_t*>(second.data() + 112), 288) == 288);
|
||||
wait_until([&] { return output().find(first + second) != std::string::npos && ethernet_is_running(); });
|
||||
assert(ethernet_read_line(line, sizeof(line)) && std::string(line) == "eth off");
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "eth off");
|
||||
finish_off();
|
||||
break;
|
||||
}
|
||||
@@ -587,14 +593,19 @@ int main(int argc, char** argv) {
|
||||
(void)ethernet_take_session_reset();
|
||||
mock_ethernet_input = "eth off" + std::string(80, 'x') + "\rget eth\r";
|
||||
char line[32] = {};
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
wait_until([&] {
|
||||
assert(!ethernet_read_line(line, sizeof(line)));
|
||||
return ethernet_session_reset.load();
|
||||
});
|
||||
assert(line[0] == 0 && mock_ethernet_input == "get eth\r");
|
||||
assert(ethernet_get_enabled() && ethernet_take_session_reset());
|
||||
wait_until([] { return ethernet_is_running(); });
|
||||
assert(ethernet_read_line(line, sizeof(line)) && std::string(line) == "get eth");
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "get eth");
|
||||
wait_until([] { return output().find("invalid or oversized command; discarded") != std::string::npos && ethernet_is_running(); });
|
||||
line[0] = 0;
|
||||
mock_ethernet_input = std::string(31, 'm') + "\r";
|
||||
assert(ethernet_read_line(line, sizeof(line)) && std::strlen(line) == 31);
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == std::string(31, 'm'));
|
||||
finish_off();
|
||||
break;
|
||||
}
|
||||
@@ -604,10 +615,14 @@ int main(int argc, char** argv) {
|
||||
(void)ethernet_take_session_reset();
|
||||
mock_ethernet_input = std::string("eth off\0junk", 12) + "\rget eth\r";
|
||||
char line[32] = {};
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
wait_until([&] {
|
||||
assert(!ethernet_read_line(line, sizeof(line)));
|
||||
return ethernet_session_reset.load();
|
||||
});
|
||||
assert(line[0] == 0 && mock_ethernet_input == "get eth\r");
|
||||
assert(ethernet_get_enabled() && ethernet_take_session_reset());
|
||||
wait_until([] { return ethernet_is_running(); });
|
||||
assert(ethernet_read_line(line, sizeof(line)) && std::string(line) == "get eth");
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "get eth");
|
||||
finish_off();
|
||||
break;
|
||||
}
|
||||
@@ -669,13 +684,20 @@ int main(int argc, char** argv) {
|
||||
(void)ethernet_take_session_reset();
|
||||
mock_ethernet_input = std::string(220, 'x') + "\nget eth\n";
|
||||
char line[32] = {};
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
const size_t initial_size = mock_ethernet_input.size();
|
||||
wait_until([&] {
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
return mock_ethernet_input.size() != initial_size;
|
||||
});
|
||||
assert(mock_ethernet_input.size() == 101 && ethernet_line_discarding);
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
wait_until([&] {
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
return ethernet_session_reset.load();
|
||||
});
|
||||
assert(!ethernet_line_discarding);
|
||||
(void)ethernet_take_session_reset();
|
||||
wait_until([] { return ethernet_is_running(); });
|
||||
assert(ethernet_read_line(line, sizeof(line)) && std::string(line) == "get eth");
|
||||
assert(mock_ethernet_input == "get eth\n" && ethernet_take_session_reset());
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "get eth");
|
||||
finish_off();
|
||||
break;
|
||||
}
|
||||
@@ -715,7 +737,8 @@ int main(int argc, char** argv) {
|
||||
(void)ethernet_take_session_reset();
|
||||
mock_ethernet_input = "get eth\r";
|
||||
char line[32] = {};
|
||||
assert(ethernet_read_line(line, sizeof(line)));
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "get eth");
|
||||
const uint32_t previous = ethernet_session_generation.load();
|
||||
mock_accept_client.store(true);
|
||||
wait_until([&] { return ethernet_session_generation.load() != previous && ethernet_is_running(); });
|
||||
@@ -733,15 +756,22 @@ int main(int argc, char** argv) {
|
||||
(void)ethernet_take_session_reset();
|
||||
mock_ethernet_input = "eth ";
|
||||
char line[32] = {};
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && std::string(line) == "eth ");
|
||||
wait_until([&] {
|
||||
assert(!ethernet_read_line(line, sizeof(line)));
|
||||
return std::string(line) == "eth ";
|
||||
});
|
||||
const uint32_t before = ethernet_session_generation.load();
|
||||
mock_accept_client.store(true);
|
||||
wait_until([&] { return ethernet_session_generation.load() != before && ethernet_is_running(); });
|
||||
mock_ethernet_input = "off\r";
|
||||
assert(!ethernet_read_line(line, sizeof(line)) && line[0] == 0);
|
||||
wait_until([&] {
|
||||
assert(!ethernet_read_line(line, sizeof(line)));
|
||||
return line[0] == 0;
|
||||
});
|
||||
assert(mock_ethernet_input == "off\r");
|
||||
(void)ethernet_take_session_reset();
|
||||
assert(ethernet_read_line(line, sizeof(line)) && std::string(line) == "off");
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "off");
|
||||
finish_off();
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -53,6 +53,75 @@ SCENARIOS = (
|
||||
|
||||
|
||||
class Rak4631EthernetRuntimeTests(unittest.TestCase):
|
||||
def test_fixture_retries_busy_reads_and_preserves_partial_commands(self):
|
||||
compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
|
||||
if compiler is None:
|
||||
self.skipTest("a host C++17 compiler is required")
|
||||
fixture = (FIXTURE / "test.cpp").read_text(encoding="ascii")
|
||||
waiter = method(fixture, "static void wait_until(")
|
||||
reader = method(fixture, "static void read_complete_line(")
|
||||
harness = r'''
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
static unsigned read_calls = 0;
|
||||
static bool ethernet_read_line(char* line, size_t capacity) {
|
||||
assert(capacity >= sizeof("get eth"));
|
||||
switch (read_calls++) {
|
||||
case 0: // Worker acquired SPI after the caller observed Online.
|
||||
assert(line[0] == 0);
|
||||
return false;
|
||||
case 1: // An admitted turn received only part of the command.
|
||||
std::strcpy(line, "get ");
|
||||
return false;
|
||||
case 2: // Busy again; the caller must retain the partial command.
|
||||
assert(std::string(line) == "get ");
|
||||
return false;
|
||||
case 3:
|
||||
assert(std::string(line) == "get ");
|
||||
std::strcat(line, "eth");
|
||||
return true;
|
||||
default:
|
||||
assert(false && "reader retried after completing the command");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@WAITER@
|
||||
@READER@
|
||||
int main() {
|
||||
char line[32] = {};
|
||||
read_complete_line(line, sizeof(line));
|
||||
assert(std::string(line) == "get eth" && read_calls == 4);
|
||||
}
|
||||
'''
|
||||
with tempfile.TemporaryDirectory(prefix="meshcore-ethernet-fixture-read-") as directory:
|
||||
for name, implementation in (
|
||||
("retry-partial", reader),
|
||||
("single-attempt", "static void read_complete_line(char* line, size_t capacity) {\n"
|
||||
"wait_until([] { return true; });\n"
|
||||
"assert(ethernet_read_line(line, capacity));\n}"),
|
||||
("lost-prefix", reader.replace(
|
||||
"return ethernet_read_line(line, capacity);",
|
||||
"line[0] = 0; return ethernet_read_line(line, capacity);")),
|
||||
):
|
||||
with self.subTest(implementation=name):
|
||||
binary = Path(directory) / name
|
||||
compiled = subprocess.run(
|
||||
[compiler, "-std=c++17", "-pthread", "-Wall", "-Wextra", "-Werror",
|
||||
*SANITIZER_FLAGS, "-x", "c++", "-", "-o", str(binary)],
|
||||
input=harness.replace("@WAITER@", waiter).replace("@READER@", implementation),
|
||||
capture_output=True, text=True, timeout=60)
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stdout + compiled.stderr)
|
||||
checked = subprocess.run([str(binary)], capture_output=True, text=True, timeout=10)
|
||||
if name == "retry-partial":
|
||||
self.assertEqual(checked.returncode, 0, checked.stdout + checked.stderr)
|
||||
else:
|
||||
self.assertNotEqual(checked.returncode, 0)
|
||||
self.assertIn("Assertion", checked.stderr)
|
||||
|
||||
def test_fixture_retries_busy_and_short_writes_without_duplicates(self):
|
||||
compiler = os.environ.get("CXX") or shutil.which("g++") or shutil.which("clang++")
|
||||
if compiler is None:
|
||||
@@ -201,6 +270,8 @@ int main() {
|
||||
self.assertEqual(compiled.returncode, 0, compiled.stdout + compiled.stderr)
|
||||
cases = list(enumerate(SCENARIOS))
|
||||
cases.extend((7, f"online ownership race repeat {index}") for index in range(32))
|
||||
cases.extend((scenario, f"parser ownership race repeat {index}: {SCENARIOS[scenario]}")
|
||||
for index in range(8) for scenario in (18, 19, 23, 27))
|
||||
for scenario, description in cases:
|
||||
with self.subTest(scenario=description):
|
||||
checked = subprocess.run([str(binary), str(scenario)],
|
||||
|
||||
Reference in new issue
Block a user