18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-19 04:05:58 +01:00

fix(simulation): any exception ends a test run with error, other runs as on master

main() tells a test run (--test) apart before anything runs. Only a test run
catches every exception and installs a std::terminate handler, both ending the
run with the error code and a test: line; any other run keeps master's
exception handling, so on Linux an uncaught runtime_error aborts at the throw
site again.

A test run doesn't start the Discord RPC thread: it published the test
scenery as the player's presence and held the closing up to 5 s.

run.py drops a case scenery's .sbt cache after a run cut short (wall_timeout,
error, a crash, killed by run.py), which could have been writing it.

The -edit branch of the test command line parser had no effect (a token that
isn't an option is ignored either way) and is gone; its test checks what can
be seen instead.
This commit is contained in:
jerrrrycho
2026-11-08 19:46:16 +01:00
parent cea92f0170
commit 8fb596f218
7 changed files with 125 additions and 41 deletions

View File

@@ -31,7 +31,8 @@ With `--test` the simulator:
motion telemetry is off,
- gives nothing to the outside either: no keyboard leds, LPT or PoKeys (`feedbackmode`), no
messages to a multiplayer server (`multiplayer`), no program run at the end (`execonexit`),
no console window of its own (`showsystemconsole`), and the game's `imgui.ini` stays as it is,
no console window of its own (`showsystemconsole`), no Discord presence, and the game's `imgui.ini`
stays as it is,
- keeps all of the above whatever the `config` block of the scenery says: the settings of the
test mode are applied again after it,
- runs the simulation as fast as it can in fixed steps of 0.03 s of simulation time per
@@ -39,8 +40,9 @@ With `--test` the simulator:
- seeds its random numbers with a fixed seed unless `-seed` or `randomseed` gives one, so two
runs of a scenery come out the same (the physicslogs of two runs are byte for byte equal),
- quits when the test ends, and says how it ended in the exit code and in a `test: ...`
line on stdout and in the log. A command line it can't take, or a start that fails, ends
the same way with the `error` code. That line is all there is on stdout: the log goes to
line on stdout and in the log. A command line it can't take, a start that fails, or an
exception thrown by the simulator (of any type, on Linux too) ends the same way, with the
`error` code. That line is all there is on stdout: the log goes to
the log file only (`debuglog` doesn't print it on the console in test mode).
On Windows the simulator is a windowed program, which the `cmd` prompt and PowerShell don't wait
@@ -167,7 +169,8 @@ For each case the runner:
The lead vehicle is named `autotest_<case>`, so every case has its own physicslog and log
files and cases can run in parallel. The `autotest_*` sceneries (and the simulator's `.sbt`
geometry cache of them) stay in the game's `scenery/` dir; the cache is dropped when the base
scenery is newer.
scenery is newer, and after a run cut short (`wall_timeout`, `error`, a crash, or killed by run.py),
which could have been writing it.
## Writing a case

View File

@@ -137,6 +137,11 @@ def fill_vars(fragment: str, values: dict, keep=frozenset()) -> str:
return re.sub(r"\{\{(\w+)\}\}", value, fragment)
def scenery_cache(game_dir: Path, case_name: str) -> Path:
"""The simulator's geometry cache of the case scenery."""
return game_dir / "scenery" / f"autotest_{case_name}.sbt"
def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> str:
"""Writes the case scenery into the game's scenery dir and returns its file name."""
scenery_dir = game_dir / "scenery"
@@ -174,7 +179,7 @@ def build_scenery(case: dict, game_dir: Path, case_name: str, vehicle: str) -> s
# the simulator caches the scenery's static geometry in <name>.sbt and reads it instead of
# the terrain in the .scn (tracks, events and trainsets always come from the .scn), so
# drop the cache once the base scenery has changed
cache = scenery_dir / f"autotest_{case_name}.sbt"
cache = scenery_cache(game_dir, case_name)
if cache.exists() and cache.stat().st_mtime < (scenery_dir / case["base_scenery"]).stat().st_mtime:
cache.unlink()
return name
@@ -548,6 +553,11 @@ def run_case(name: str, exe: Path, game_dir: Path, timespeed: float, watch: bool
watchdog = ", from the real time watchdog" if got == WALL_TIMEOUT else ""
say(f" {'PASS' if passed else 'FAIL'} exit code {describe_exit(result.exit_code)}{watchdog}, expected {expect}")
ok &= passed
# a run cut short (the real time watchdog, an error, a crash, or closed by us) could have been writing
# the scenery's geometry cache; the simulator checks only the header of it, so the next runs would read
# a truncated one. it is written again on the next run
if result.failure or (not watch and got in (None, "error", WALL_TIMEOUT)):
scenery_cache(game_dir, name).unlink(missing_ok=True)
if result.failure:
say(f" FAIL {result.failure}")
ok = False

View File

@@ -4,6 +4,9 @@
#include "simulation/simulationtestrules.h"
#include <array>
#include <exception>
#include <new>
#include <stdexcept>
#include <vector>
// the test mode of the simulator (eu07 --test, eu07/maszyna#541): its exit codes, command line and end conditions
@@ -117,10 +120,13 @@ TEST_CASE("test mode: the command line")
CHECK_FALSE(commandline.enabled);
CHECK(commandline.error.empty());
}
SUBCASE("the scenery of -edit is optional")
SUBCASE("-edit takes no option for its scenery, which is optional")
{
// a scenery after -edit or none: the option after it is read either way
CHECK(parse({"-edit", "--test", "-s", "td.scn"}).enabled);
CHECK_FALSE(parse({"-edit", "td.scn"}).enabled);
CHECK(parse({"-edit", "td.scn", "--test"}).enabled);
// -edit leaves -s alone, which takes --test for its scenery, as the simulator reads it
CHECK_FALSE(parse({"-edit", "-s", "--test"}).enabled);
}
}
@@ -241,3 +247,22 @@ TEST_CASE("test mode: the end conditions")
CHECK(verdict->result == test_result::fail);
}
}
TEST_CASE("test mode: what ended a run the simulator didn't end")
{
SUBCASE("an exception says what it was")
{
CHECK(simulation::describe_exception(std::make_exception_ptr(std::runtime_error("no scenery"))) == "critical error: no scenery");
// e.g. std::stof on a token of a scenery which isn't a number
CHECK(simulation::describe_exception(std::make_exception_ptr(std::invalid_argument("stof"))) == "critical error: stof");
CHECK(simulation::describe_exception(std::make_exception_ptr(std::bad_alloc())).starts_with("critical error: "));
}
SUBCASE("something thrown which isn't a std::exception")
{
CHECK(simulation::describe_exception(std::make_exception_ptr(42)) == "critical error: an exception of unknown type");
}
SUBCASE("terminated without an exception")
{
CHECK(simulation::describe_exception(nullptr) == "the simulator was terminated");
}
}