diff --git a/CLAUDE.md b/CLAUDE.md index 63539f1..72e29b0 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -20,6 +20,8 @@ continuity. | `make check` | Verifies the engine, checks binaries, parses all scripts, runs all tests. Must pass before every push. | | `make test` | Runs `test/**/test_*.gd` headless. | | `make test-selfcheck` | Proves the runner catches each kind of failure, using `test/fixtures/`. | +| `make smoke` | Boots the headless server for 1,000 ticks with 24 bots, prints tick timing, and checks exit codes and logs. Part of `make check`. | +| `tools/godot.sh -- --server --seed=N --bots=N --ticks=N` | Runs a headless dedicated server. See `src/server/server_config.gd` for every option. Logs are JSON lines carrying the seed. Exit codes: 0 clean, 1 script error, 2 bad command line. | | `tools/godot.sh ...` | Runs the pinned engine. Headless by default. | Run one group of tests with `tools/run_tests.sh --root=res://test/sim` or diff --git a/Makefile b/Makefile index 79fec78..adae03f 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,10 @@ # Tidewall build entry points. Everything runs headless with the repo-local engine. GODOT := tools/godot.sh -.PHONY: check import parse test test-selfcheck binaries verify-engine +.PHONY: check import parse test test-selfcheck smoke binaries verify-engine ## Everything that must pass before a push. -check: verify-engine binaries parse test test-selfcheck +check: verify-engine binaries parse test test-selfcheck smoke verify-engine: @$(GODOT) --verify @@ -25,3 +25,7 @@ test: import ## Proves the test runner detects each kind of failure. test-selfcheck: import @tools/test_selfcheck.sh + +## Boots the headless server for 1,000 ticks and checks exit codes, logs and timing. +smoke: import + @tools/smoke.sh diff --git a/src/app/main.gd b/src/app/main.gd index f92b4fc..6ee3c1d 100644 --- a/src/app/main.gd +++ b/src/app/main.gd @@ -1,6 +1,18 @@ -## Application entry point. Client startup is added by later milestones. +## Application entry point. Picks the mode from the command line: +## +## tools/godot.sh -- --server [options] headless dedicated server (see ServerConfig) +## +## Client startup is added by later milestones. extends Node func _ready() -> void: + var args: PackedStringArray = OS.get_cmdline_user_args() + if not args.has("--server"): + # Also accept --server before the "--" separator. + args = OS.get_cmdline_args() + var config: ServerConfig = ServerConfig.parse(args) + if config.server: + add_child(HeadlessServer.new(config)) + return print("Tidewall") diff --git a/src/bots/wander_bot.gd b/src/bots/wander_bot.gd new file mode 100644 index 0000000..6380ecd --- /dev/null +++ b/src/bots/wander_bot.gd @@ -0,0 +1,27 @@ +## A placeholder bot that walks in a seeded direction and turns now and then. +## It exists so headless servers and smoke runs carry realistic load before the real +## bot brains arrive in v0.4. Like every bot, it only produces PlayerCommands. +class_name WanderBot +extends RefCounted + +## How often, in ticks, the bot may pick a new heading. +const TURN_INTERVAL_TICKS: int = 45 + +var player_id: int = 0 + +var _yaw: float = 0.0 +var _sprint: bool = false + + +func _init(id: int) -> void: + player_id = id + + +## Returns this bot's command for `sim.tick`. Draws only from the "bots" stream. +func think(sim: Simulation) -> PlayerCommand: + var rng: RandomNumberGenerator = sim.rng.stream("bots") + if sim.tick % TURN_INTERVAL_TICKS == 0: + _yaw = rng.randf() * TAU + _sprint = rng.randf() < 0.3 + var buttons: int = PlayerCommand.BUTTON_SPRINT if _sprint else 0 + return PlayerCommand.from_input(sim.tick, Vector2(0.0, 1.0), _yaw, 0.0, buttons) diff --git a/src/bots/wander_bot.gd.uid b/src/bots/wander_bot.gd.uid new file mode 100644 index 0000000..3686436 --- /dev/null +++ b/src/bots/wander_bot.gd.uid @@ -0,0 +1 @@ +uid://cfc5ckk0du602 diff --git a/src/server/error_counter.gd b/src/server/error_counter.gd new file mode 100644 index 0000000..b7eae15 --- /dev/null +++ b/src/server/error_counter.gd @@ -0,0 +1,44 @@ +## Counts engine and script errors, so the server can exit non-zero after any of them. +## Godot may call a Logger from any thread, so the state is guarded by a mutex, and +## nothing here prints (printing from a logger callback would recurse). +class_name ErrorCounter +extends Logger + +var _mutex: Mutex = Mutex.new() +var _count: int = 0 +var _first: String = "" + + +func _log_error( + function: String, + file: String, + line: int, + code: String, + rationale: String, + _editor_notify: bool, + error_type: int, + _script_backtrace: Array[ScriptBacktrace], +) -> void: + if error_type == Logger.ERROR_TYPE_WARNING: + return + _mutex.lock() + if _count == 0: + var what: String = rationale if not rationale.is_empty() else code + _first = "%s (at %s:%d in %s)" % [what, file, line, function] + _count += 1 + _mutex.unlock() + + +func count() -> int: + _mutex.lock() + var value: int = _count + _mutex.unlock() + return value + + +## The first error seen, or an empty string. +func first() -> String: + _mutex.lock() + var value: String = _first + _mutex.unlock() + return value diff --git a/src/server/error_counter.gd.uid b/src/server/error_counter.gd.uid new file mode 100644 index 0000000..d15474f --- /dev/null +++ b/src/server/error_counter.gd.uid @@ -0,0 +1 @@ +uid://l3us7ygwk4vr diff --git a/src/server/headless_server.gd b/src/server/headless_server.gd new file mode 100644 index 0000000..ae2d051 --- /dev/null +++ b/src/server/headless_server.gd @@ -0,0 +1,130 @@ +## A headless dedicated server. It runs the match at the real tick rate, or back to back +## with `--unthrottled`, and writes structured logs: one JSON object per line on stdout, +## each carrying the match seed so any run can be reproduced. +## +## Exits 0 after a clean run, 1 if any engine or script error happened, and 2 if the +## command line was invalid. +class_name HeadlessServer +extends Node + +## Catch-up limit from docs/ARCHITECTURE.md. +const MAX_TICKS_PER_FRAME: int = 5 +## Ticks per frame when unthrottled. Keeps the engine's main loop responsive. +const UNTHROTTLED_BATCH: int = 250 +## Engine frame cap in real-time mode, so an idle server doesn't spin a core. +const REALTIME_MAX_FPS: int = 120 +const FAULT_TICK: int = 10 + +var config: ServerConfig +var loop: ServerLoop + +var _errors: ErrorCounter = ErrorCounter.new() +var _accumulator: float = 0.0 +var _started_usec: int = 0 +var _finished: bool = false + + +func _init(server_config: ServerConfig) -> void: + config = server_config + + +func _ready() -> void: + OS.add_logger(_errors) + _started_usec = Time.get_ticks_usec() + if not config.is_valid(): + for problem: String in config.errors: + _log("config_error", {"message": problem}) + _finish(2) + return + if not config.has_seed: + config.match_seed = _fresh_seed() + Engine.max_fps = 0 if config.unthrottled else REALTIME_MAX_FPS + loop = ServerLoop.new(config.match_seed, config.players, config.bots) + var started: Dictionary = config.to_dictionary() + started["engine"] = Engine.get_version_info()["string"] + started["tick_rate_hz"] = Tuning.TICK_RATE_HZ + started["listening"] = false + started["note"] = "networking arrives in v0.5; players join as idle slots until then" + _log("start", started) + + +func _process(delta: float) -> void: + if _finished or loop == null: + return + var due: int = UNTHROTTLED_BATCH + if not config.unthrottled: + _accumulator += delta + due = mini(floori(_accumulator * Tuning.TICK_RATE_HZ), MAX_TICKS_PER_FRAME) + _accumulator = maxf(_accumulator - float(due) / Tuning.TICK_RATE_HZ, 0.0) + if due == MAX_TICKS_PER_FRAME: + # Too far behind to catch up: drop the backlog rather than spiral. + _accumulator = minf(_accumulator, 1.0 / Tuning.TICK_RATE_HZ) + for _index: int in due: + if config.ticks > 0 and loop.sim.tick >= config.ticks: + break + _tick() + if config.ticks > 0 and loop.sim.tick >= config.ticks: + _finish(0) + + +func _exit_tree() -> void: + # Stopped from outside, for example by SIGTERM: still report what happened. + if not _finished and loop != null: + _finish(0) + + +func _tick() -> void: + if config.fault == "script-error" and loop.sim.tick == FAULT_TICK: + _deliberate_script_error() + loop.run_tick() + if config.log_every > 0 and loop.sim.tick % config.log_every == 0: + var stats: Dictionary = loop.timing_stats() + _log("progress", {"tick": loop.sim.tick, "p95_us": stats["p95_us"], "max_us": stats["max_us"]}) + + +func _finish(requested_code: int) -> void: + _finished = true + var code: int = requested_code + if loop != null: + var stats: Dictionary = loop.timing_stats() + stats["state_hash"] = loop.sim.state_hash() + stats["entities"] = loop.sim.entities.count() + _log("stats", stats) + if code == 0 and _errors.count() > 0: + code = 1 + var stopped: Dictionary = { + "exit_code": code, + "errors": _errors.count(), + "wall_ms": roundi((Time.get_ticks_usec() - _started_usec) / 1000.0), + } + if _errors.count() > 0: + stopped["first_error"] = _errors.first() + _log("stop", stopped) + OS.remove_logger(_errors) + if is_inside_tree(): + get_tree().quit(code) + + +func _log(event: String, fields: Dictionary) -> void: + var record: Dictionary = { + "t_ms": roundi((Time.get_ticks_usec() - _started_usec) / 1000.0), + "event": event, + "seed": config.match_seed, + } + if loop != null: + record["tick"] = loop.sim.tick + record.merge(fields, false) + print(JSON.stringify(record)) + + +## A non-negative 63-bit seed. Only the server picks seeds; the simulation never does. +static func _fresh_seed() -> int: + var rng: RandomNumberGenerator = RandomNumberGenerator.new() + rng.randomize() + return (rng.randi() << 31 | rng.randi()) & 0x7FFFFFFFFFFFFFFF + + +## Calls a method on a null object, the most common real script error. +func _deliberate_script_error() -> void: + var missing: Object = null + missing.call("deliberate_fault") diff --git a/src/server/headless_server.gd.uid b/src/server/headless_server.gd.uid new file mode 100644 index 0000000..03d2cff --- /dev/null +++ b/src/server/headless_server.gd.uid @@ -0,0 +1 @@ +uid://bv6bvpw5ooxwv diff --git a/src/server/server_config.gd b/src/server/server_config.gd new file mode 100644 index 0000000..b85c5bb --- /dev/null +++ b/src/server/server_config.gd @@ -0,0 +1,125 @@ +## Command-line configuration for a headless server. +## +## tools/godot.sh -- --server [--seed=N] [--port=N] [--players=N] [--bots=N] +## [--ticks=N] [--unthrottled] [--log-every=N] +## +## Unknown or malformed arguments are errors, so a typo can't silently run the wrong +## match. See `parse()`. +class_name ServerConfig +extends RefCounted + +const DEFAULT_PORT: int = 24650 +## The `--fault` names `HeadlessServer` understands. They exist so tools/smoke.sh can +## prove a script error really produces a non-zero exit. +const FAULTS: Array[String] = ["", "script-error"] + +## True when `--server` was given. +var server: bool = false +## The match seed. `has_seed` is false when none was given and the server must pick one. +var match_seed: int = 0 +var has_seed: bool = false +## UDP port the server will listen on once networking lands (v0.5). +var port: int = DEFAULT_PORT +## Player slots for human clients. Until v0.5 they join as idle players. +var players: int = 0 +var bots: int = 0 +## Ticks to run before exiting. 0 runs until the process is stopped. +var ticks: int = 0 +## Runs ticks back to back instead of at the real tick rate. For tests and smoke runs. +var unthrottled: bool = false +## Logs a progress line every this many ticks. 0 turns progress lines off. +var log_every: int = 0 +var fault: String = "" +## Human-readable problems found while parsing. Empty when the config is usable. +var errors: Array[String] = [] + + +## Parses arguments. Arguments before `--server` are ignored unless `--server` is absent, +## in which case nothing is parsed and `server` stays false. +static func parse(args: PackedStringArray) -> ServerConfig: + var config: ServerConfig = ServerConfig.new() + var start: int = args.find("--server") + if start < 0: + return config + config.server = true + for index: int in range(start + 1, args.size()): + config._parse_one(args[index]) + config._validate() + return config + + +func is_valid() -> bool: + return errors.is_empty() + + +## The values that matter for reproducing a run, for the start log line. +func to_dictionary() -> Dictionary: + return { + "seed": match_seed, + "port": port, + "players": players, + "bots": bots, + "ticks": ticks, + "unthrottled": unthrottled, + } + + +func _parse_one(arg: String) -> void: + if arg == "--unthrottled": + unthrottled = true + return + var parts: PackedStringArray = arg.split("=", true, 1) + if parts.size() != 2 or not arg.begins_with("--"): + errors.append("unknown argument '%s'" % arg) + return + var key: String = parts[0] + var value: String = parts[1] + match key: + "--seed": + if _check_int(key, value): + match_seed = value.to_int() + has_seed = true + "--port": + if _check_int(key, value): + port = value.to_int() + "--players": + if _check_int(key, value): + players = value.to_int() + "--bots": + if _check_int(key, value): + bots = value.to_int() + "--ticks": + if _check_int(key, value): + ticks = value.to_int() + "--log-every": + if _check_int(key, value): + log_every = value.to_int() + "--fault": + fault = value + _: + errors.append("unknown argument '%s'" % arg) + + +func _check_int(key: String, value: String) -> bool: + if value.is_valid_int(): + return true + errors.append("%s needs a whole number, got '%s'" % [key, value]) + return false + + +func _validate() -> void: + if port < 1 or port > 65535: + errors.append("--port must be between 1 and 65535, got %d" % port) + if players < 0 or bots < 0: + errors.append("--players and --bots can't be negative") + if players + bots > Tuning.SOLO_PLAYERS: + errors.append( + "--players plus --bots is %d, more than the match size of %d" + % [players + bots, Tuning.SOLO_PLAYERS] + ) + if ticks < 0: + errors.append("--ticks can't be negative") + if log_every < 0: + errors.append("--log-every can't be negative") + if fault not in FAULTS: + errors.append("unknown --fault '%s'" % fault) diff --git a/src/server/server_config.gd.uid b/src/server/server_config.gd.uid new file mode 100644 index 0000000..bea1bb1 --- /dev/null +++ b/src/server/server_config.gd.uid @@ -0,0 +1 @@ +uid://dbnjplsnqeona diff --git a/src/server/server_loop.gd b/src/server/server_loop.gd new file mode 100644 index 0000000..cd5faa4 --- /dev/null +++ b/src/server/server_loop.gd @@ -0,0 +1,65 @@ +## Drives one match's simulation for a server: adds players and bots, gathers their +## commands each tick, steps the simulation and times every tick. +## It has no scene-tree dependency, so tests can run it directly. +class_name ServerLoop +extends RefCounted + +## The server-tick budget from docs/ARCHITECTURE.md, in microseconds. +const TICK_BUDGET_USEC: int = 12_000 + +var sim: Simulation +var player_ids: Array[int] = [] +var bots: Array[WanderBot] = [] +## Wall-clock duration of every tick run, in microseconds. +var tick_usec: PackedInt64Array = PackedInt64Array() + + +func _init(match_seed: int, human_players: int, bot_count: int) -> void: + sim = Simulation.new(match_seed) + for _index: int in human_players: + player_ids.append(sim.add_player()) + for _index: int in bot_count: + var id: int = sim.add_player() + player_ids.append(id) + bots.append(WanderBot.new(id)) + + +## Runs one tick and records how long it took. +func run_tick() -> void: + var started: int = Time.get_ticks_usec() + for bot: WanderBot in bots: + sim.submit_command(bot.player_id, bot.think(sim)) + sim.step() + tick_usec.append(Time.get_ticks_usec() - started) + + +## Timing statistics over every tick run so far, in microseconds. +func timing_stats() -> Dictionary: + var count: int = tick_usec.size() + if count == 0: + return {"ticks": 0} + var sorted: PackedInt64Array = tick_usec.duplicate() + sorted.sort() + var total: int = 0 + var over_budget: int = 0 + for value: int in sorted: + total += value + if value > TICK_BUDGET_USEC: + over_budget += 1 + return { + "ticks": count, + "min_us": sorted[0], + "mean_us": roundi(float(total) / count), + "p50_us": _percentile(sorted, 0.50), + "p95_us": _percentile(sorted, 0.95), + "p99_us": _percentile(sorted, 0.99), + "max_us": sorted[count - 1], + "budget_us": TICK_BUDGET_USEC, + "over_budget": over_budget, + } + + +## Nearest-rank percentile of an ascending array. +static func _percentile(sorted: PackedInt64Array, fraction: float) -> int: + var rank: int = clampi(ceili(fraction * sorted.size()) - 1, 0, sorted.size() - 1) + return sorted[rank] diff --git a/src/server/server_loop.gd.uid b/src/server/server_loop.gd.uid new file mode 100644 index 0000000..988daa7 --- /dev/null +++ b/src/server/server_loop.gd.uid @@ -0,0 +1 @@ +uid://cr14bfd1283vt diff --git a/test/server/test_server_config.gd b/test/server/test_server_config.gd new file mode 100644 index 0000000..aedc890 --- /dev/null +++ b/test/server/test_server_config.gd @@ -0,0 +1,68 @@ +extends TestCase + + +func _parse(args: Array[String]) -> ServerConfig: + return ServerConfig.parse(PackedStringArray(args)) + + +func test_without_server_flag_nothing_is_parsed() -> void: + var config: ServerConfig = _parse(["--seed=5", "--bots=3"]) + assert_false(config.server) + assert_eq(config.bots, 0) + assert_true(config.is_valid()) + + +func test_defaults() -> void: + var config: ServerConfig = _parse(["--server"]) + assert_true(config.server) + assert_true(config.is_valid()) + assert_false(config.has_seed) + assert_eq(config.port, ServerConfig.DEFAULT_PORT) + assert_eq(config.ticks, 0) + assert_false(config.unthrottled) + + +func test_all_options() -> void: + var config: ServerConfig = _parse([ + "--path", "/ignored", "--server", "--seed=-17", "--port=4000", "--players=3", + "--bots=20", "--ticks=900", "--unthrottled", "--log-every=30", + ]) + assert_true(config.is_valid(), str(config.errors)) + assert_true(config.has_seed) + assert_eq(config.match_seed, -17) + assert_eq(config.port, 4000) + assert_eq(config.players, 3) + assert_eq(config.bots, 20) + assert_eq(config.ticks, 900) + assert_true(config.unthrottled) + assert_eq(config.log_every, 30) + + +func test_large_seed_round_trips() -> void: + var config: ServerConfig = _parse(["--server", "--seed=9223372036854775807"]) + assert_eq(config.match_seed, 9223372036854775807) + assert_eq(config.to_dictionary()["seed"], 9223372036854775807) + + +func test_unknown_argument_is_an_error() -> void: + var config: ServerConfig = _parse(["--server", "--bot=3"]) + assert_false(config.is_valid()) + assert_true(config.errors[0].contains("--bot=3")) + + +func test_non_numeric_value_is_an_error() -> void: + var config: ServerConfig = _parse(["--server", "--seed=abc"]) + assert_false(config.is_valid()) + assert_false(config.has_seed) + + +func test_ranges_are_checked() -> void: + assert_false(_parse(["--server", "--port=0"]).is_valid()) + assert_false(_parse(["--server", "--port=70000"]).is_valid()) + assert_false(_parse(["--server", "--bots=-1"]).is_valid()) + assert_false(_parse(["--server", "--ticks=-1"]).is_valid()) + assert_false(_parse(["--server", "--fault=meteor"]).is_valid()) + var too_many: String = "--bots=%d" % (Tuning.SOLO_PLAYERS + 1) + assert_false(_parse(["--server", too_many]).is_valid()) + var full: Array[String] = ["--server", "--players=4", "--bots=%d" % (Tuning.SOLO_PLAYERS - 4)] + assert_true(_parse(full).is_valid()) diff --git a/test/server/test_server_config.gd.uid b/test/server/test_server_config.gd.uid new file mode 100644 index 0000000..ee5d380 --- /dev/null +++ b/test/server/test_server_config.gd.uid @@ -0,0 +1 @@ +uid://cmngmhd4ivq50 diff --git a/test/server/test_server_loop.gd b/test/server/test_server_loop.gd new file mode 100644 index 0000000..2f56cac --- /dev/null +++ b/test/server/test_server_loop.gd @@ -0,0 +1,59 @@ +extends TestCase + + +func _run(match_seed: int, players: int, bots: int, ticks: int) -> ServerLoop: + var loop: ServerLoop = ServerLoop.new(match_seed, players, bots) + for _index: int in ticks: + loop.run_tick() + return loop + + +func test_creates_players_then_bots() -> void: + var loop: ServerLoop = ServerLoop.new(1, 2, 3) + assert_eq(loop.sim.entities.count(), 5) + assert_eq(loop.player_ids, [1, 2, 3, 4, 5] as Array[int]) + assert_eq(loop.bots.size(), 3) + assert_eq(loop.bots[0].player_id, 3) + + +func test_records_one_timing_sample_per_tick() -> void: + var loop: ServerLoop = _run(7, 0, 4, 120) + assert_eq(loop.sim.tick, 120) + var stats: Dictionary = loop.timing_stats() + assert_eq(stats["ticks"], 120) + assert_true(stats["min_us"] <= stats["p50_us"]) + assert_true(stats["p50_us"] <= stats["p95_us"]) + assert_true(stats["p95_us"] <= stats["p99_us"]) + assert_true(stats["p99_us"] <= stats["max_us"]) + assert_eq(stats["budget_us"], ServerLoop.TICK_BUDGET_USEC) + + +func test_empty_stats() -> void: + assert_eq(ServerLoop.new(1, 0, 0).timing_stats(), {"ticks": 0}) + + +func test_percentile_uses_nearest_rank() -> void: + var values: PackedInt64Array = PackedInt64Array(range(1, 101)) + assert_eq(ServerLoop._percentile(values, 0.5), 50) + assert_eq(ServerLoop._percentile(values, 0.95), 95) + assert_eq(ServerLoop._percentile(values, 1.0), 100) + assert_eq(ServerLoop._percentile(PackedInt64Array([9]), 0.99), 9) + + +func test_bots_move_and_humans_without_commands_stay_put() -> void: + var loop: ServerLoop = ServerLoop.new(99, 1, 1) + var human: SimEntity = loop.sim.entities.find(1) + var bot: SimEntity = loop.sim.entities.find(2) + var human_start: Vector3 = human.position + var bot_start: Vector3 = bot.position + for _index: int in 90: + loop.run_tick() + assert_eq(human.position, human_start) + assert_true(bot.position.distance_to(bot_start) > 1.0, "the bot should have walked") + + +func test_same_seed_gives_the_same_match() -> void: + var first: String = _run(2026, 2, 22, 300).sim.state_hash() + var second: String = _run(2026, 2, 22, 300).sim.state_hash() + assert_eq(first, second) + assert_ne(first, _run(2027, 2, 22, 300).sim.state_hash()) diff --git a/test/server/test_server_loop.gd.uid b/test/server/test_server_loop.gd.uid new file mode 100644 index 0000000..fa219ee --- /dev/null +++ b/test/server/test_server_loop.gd.uid @@ -0,0 +1 @@ +uid://b8lr7u4qaduqp diff --git a/tools/smoke.sh b/tools/smoke.sh new file mode 100755 index 0000000..043f302 --- /dev/null +++ b/tools/smoke.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash +# Boots the headless server, runs a full-size match for 1,000 ticks, prints the timing +# statistics, and checks that the server: +# - exits 0 and logs start, stats and stop lines that all carry the seed, +# - produces the same state hash when run again with the same seed, +# - exits non-zero when a script error happens (a deliberate --fault), +# - exits non-zero on a bad command line. +# +# tools/smoke.sh unthrottled, so it finishes in seconds +# SMOKE_REALTIME=1 tools/smoke.sh at the real 30 Hz tick rate (about 34 seconds) +set -uo pipefail +cd "$(dirname "${BASH_SOURCE[0]}")/.." + +seed="${SMOKE_SEED:-20261109}" +ticks="${SMOKE_TICKS:-1000}" +pace="--unthrottled" +[[ "${SMOKE_REALTIME:-0}" == "1" ]] && pace="" +status=0 +code=0 + +# run : runs the server, sets $output (JSON lines only) and $code. +run() { + local raw + raw="$(timeout --signal=KILL 300 tools/godot.sh -- --server "$@" 2>&1)" + code=$? + output="$(grep '^{' <<<"$raw")" + stderr_tail="$(grep -v '^{' <<<"$raw" | tail -n 15)" +} + +# field : prints a value from the first log line of that event. +field() { + grep -m1 "\"event\":\"$1\"" <<<"$output" | sed -E "s/.*\"$2\":(\"[^\"]*\"|[^,}]*).*/\1/" | tr -d '"' +} + +fail() { + echo "smoke FAIL: $*" + status=1 +} + +run --seed="$seed" --bots=24 --ticks="$ticks" --log-every=250 $pace +if [[ "$code" -ne 0 ]]; then + fail "server exited $code" + echo "$output" + echo "$stderr_tail" +fi +for event in start stats stop; do + grep -q "\"event\":\"$event\"" <<<"$output" || fail "no '$event' log line" +done +lines="$(wc -l <<<"$output")" +seeded="$(grep -c "\"seed\":$seed[,}]" <<<"$output")" +[[ "$lines" -eq "$seeded" ]] || fail "only $seeded of $lines log lines carry the seed" +[[ "$(field stats ticks)" == "$ticks" ]] || fail "ran $(field stats ticks) ticks, expected $ticks" +hash="$(field stats state_hash)" +echo "smoke: $ticks ticks, 24 bots, seed $seed" +echo "smoke: tick us min $(field stats min_us) mean $(field stats mean_us) p50 $(field stats p50_us)" \ + "p95 $(field stats p95_us) p99 $(field stats p99_us) max $(field stats max_us)," \ + "$(field stats over_budget) over the $(field stats budget_us) us budget" +echo "smoke: state hash $hash" + +run --seed="$seed" --bots=24 --ticks="$ticks" --unthrottled +[[ "$code" -eq 0 && "$(field stats state_hash)" == "$hash" ]] \ + || fail "a second run with the same seed gave a different state hash" + +run --seed="$seed" --bots=2 --ticks=50 --unthrottled --fault=script-error +[[ "$code" -eq 1 ]] || fail "a script error exited $code, expected 1" +[[ "$(field stop errors)" -ge 1 ]] || fail "a script error wasn't counted in the stop line" + +run --bots=99 --nonsense +[[ "$code" -eq 2 ]] || fail "a bad command line exited $code, expected 2" +grep -q '"event":"config_error"' <<<"$output" || fail "a bad command line logged no config_error" + +if [[ "$status" -eq 0 ]]; then + echo "smoke: ok (clean run, reproducible hash, script error exits 1, bad config exits 2)" +fi +exit "$status"