Files
tidewall/test/server/test_client_game.gd
T
claudeandClaude Opus 5.5 09d663e647 N8: Keep connections serviced while islands are made, and stop servers gracefully
The soak's drops under loss came from stalls: the server's island (#79) and
each client process making islands on its main thread fed ENet second-long
round-trip samples, its retransmission timer grew, and two losses of one
reliable message in the busy drop that followed reached the 15 s timeout.

- ClientGame.build_in_background makes a new match's island on a
  WorkerThreadPool task and holds the packets that arrive meanwhile, then
  takes them in order. The networked client and the soak's clients use it;
  the soak's own one-island-a-frame holding is gone.
- NetServer.close(grace_msec) sends the shutdown notice reliably and
  services the socket while clients acknowledge it. The headless server
  waits up to 2 s. A notice sent once was lost under 2% loss, and that
  client timed out instead of hearing the server stop.
- Tests: background island holding and ordering, freeing mid-build, a
  graceful shutdown reaching four clients behind 50% loss (it fails with
  the old one-shot notice), and the scripted UDP match and client capture
  wait for the client's island the way a real 20 s lobby would.

The soak at 2% loss and +/-20 ms now keeps all 24 clients for both matches,
with 0 of 3,758 world hashes differing and every client told of the stop.
It still fails on client memory growth between matches in two of three
processes (17 and 20 MB), which is not yet explained, so #39 stays open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 01:28:08 -05:00

208 lines
8.6 KiB
GDScript

extends TestCase
## Issue N3: ClientGame against a real server. Over loopback UDP with a NetClient, and
## by hand, packet by packet.
const HOST: String = "127.0.0.1"
const DT: float = 1.0 / Tuning.TICK_RATE_HZ
var loop: ServerLoop
var net: NetServer
var game: NetGame
func _rules() -> MatchRules:
var rules: MatchRules = MatchRules.new()
rules.max_players = 3
rules.min_players = 1
rules.lobby_ticks = 6
rules.countdown_ticks = 6
rules.results_ticks = 6
return rules
func before_each() -> void:
loop = ServerLoop.new(31, 0, 2, _rules())
net = NetServer.new()
game = NetGame.new(loop, net)
func after_each() -> void:
net.close()
func test_a_client_joins_over_udp_generates_the_island_and_plays() -> void:
assert_eq(net.listen(0, HOST), OK)
var link: NetClient = NetClient.new()
var client: ClientGame = ClientGame.new()
client.attach(link)
client.sample_input = func(tick: int) -> PlayerCommand:
return PlayerCommand.from_input(tick, Vector2(0.0, 1.0), 2.0, 0.1)
assert_eq(link.connect_to(HOST, net.port(), "Mara"), OK)
var started: int = Time.get_ticks_msec()
var played: int = 0
# A server tick and a client frame of the same length each round, so both clocks
# agree however fast the test machine is.
while Time.get_ticks_msec() - started < 20000 and played < 150:
game.poll()
loop.run_tick()
game.after_tick()
client.poll()
client.advance(DT)
if client.playing():
played += 1
OS.delay_msec(1)
# Let the last reliable messages arrive, then compare the two worlds.
for _wait: int in 20:
game.poll()
client.poll()
OS.delay_msec(1)
client.replica.sync(loop.sim.tick)
assert_true(client.replica.applied >= 2, "the whole state on joining, then the match starting: %d WORLD messages" % client.replica.applied)
assert_eq(client.replica.seq, game.world.seq)
assert_eq(NetWorld.world_hash(client.world), NetWorld.world_hash(loop.sim), "the world arrived on the reliable channel")
assert_not_null(client.world.tide)
assert_false(client.replica.own.is_empty(), "and what the player carries")
link.leave()
assert_eq(played, 150, "the client got into the match and played")
var flow: Match = loop.match_flow
assert_true(flow.in_match())
assert_eq(client.world_seed, flow.current_seed())
assert_eq(client.world.island.heightmap_hash(), loop.sim.island.heightmap_hash(), "the same island on both ends")
assert_eq(client.match_phase, flow.phase)
var mine: SimEntity = loop.sim.entities.find(flow.entity_of[link.handle])
assert_eq(client.prediction.player.id, mine.id)
assert_eq(mine.yaw, PlayerCommand.from_input(0, Vector2.ZERO, 2.0, 0.0).yaw, "the server is applying this client's commands")
assert_eq(client.prediction.player.drop_state, mine.drop_state)
assert_true(client.prediction.reconciles > 50, "%d snapshots compared" % client.prediction.reconciles)
assert_true(client.prediction.max_correction < 2.0, "largest correction %.3f m" % client.prediction.max_correction)
assert_true(client.own_position().distance_to(mine.position) < 10.0, "drawn %.1f m from where the server has it" % client.own_position().distance_to(mine.position))
# The two bots ride the same barge.
assert_eq(flow.entity_of.size(), 3)
for handle: int in flow.entity_of:
assert_true(client.remote.has(flow.entity_of[handle]), "player %d is drawn" % handle)
assert_eq(client.bad_packets, 0)
assert_eq(game.bad_inputs, 0)
func test_in_the_lobby_it_only_says_which_snapshot_it_holds() -> void:
var worlds: Array[int] = []
var client: ClientGame = ClientGame.new()
client.make_world = func(match_seed: int) -> Simulation:
worlds.append(match_seed)
return Simulation.new(match_seed, SimGround.flat())
loop.match_flow.rules.min_players = 5
assert_true(loop.match_flow.join(7))
game.adopt(7)
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
assert_eq(worlds, [loop.match_flow.current_seed()], "the island is made as soon as the seed is known")
for _round: int in 4:
loop.run_tick()
game.after_tick()
if game.snapshot_due():
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
client.advance(DT)
assert_false(client.playing())
assert_eq(client.match_phase, Match.LOBBY)
assert_eq(client.outbox.size(), 2, "one answer per snapshot")
var answer: Dictionary = NetSnapshot.decode_input(client.outbox[1])
assert_eq(answer["ack"], client.receiver.tick)
assert_eq((answer["commands"] as Array).size(), 0)
# Junk is counted and changes nothing.
client.receive(NetProtocol.CHANNEL_CONTROL, PackedByteArray([NetSnapshot.MSG_MATCH, 1, 2]))
client.receive(NetProtocol.CHANNEL_STATE, PackedByteArray([NetSnapshot.MSG_SNAPSHOT, 9, 9]))
assert_eq(client.bad_packets, 1)
assert_eq(client.receiver.malformed, 1)
assert_eq(worlds.size(), 1)
func test_a_new_match_starts_everything_again() -> void:
var worlds: Array[int] = []
var client: ClientGame = ClientGame.new()
client.make_world = func(match_seed: int) -> Simulation:
worlds.append(match_seed)
return Simulation.new(match_seed, SimGround.flat())
assert_true(loop.match_flow.join(7))
game.adopt(7)
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
var first_seed: int = loop.match_flow.current_seed()
var sent: int = 0
for _round: int in 60:
for bytes: PackedByteArray in client.outbox:
game.take_input(7, bytes)
sent += 1
client.outbox.clear()
loop.run_tick()
game.after_tick()
for event: Dictionary in loop.match_flow.events:
if event["type"] == "phase":
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
if game.snapshot_due():
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
client.advance(DT)
assert_true(client.playing(), "phase %s" % loop.match_flow.phase_name())
assert_true(sent > 30)
assert_true(client.receiver.command_tick >= 0, "the server echoes the commands it gets")
assert_eq(worlds, [first_seed], "one world for the whole match")
# The server moves on to another match: a notice with the next index and seed.
client.receive(NetProtocol.CHANNEL_CONTROL, NetSnapshot.encode_match(Match.LOBBY, 1, first_seed + 1))
assert_false(client.playing())
assert_false(client.clock.running)
assert_false(client.remote.started)
assert_eq(worlds, [first_seed, first_seed + 1])
assert_eq(client.match_index, 1)
## N8: with build_in_background the island is made on a worker thread, and the packets
## that arrive meanwhile are held, then taken in order once it's made.
func test_a_background_island_holds_packets_and_takes_them_in_order() -> void:
var worlds: Array[int] = []
var client: ClientGame = ClientGame.new()
client.build_in_background = true
client.make_world = func(match_seed: int) -> Simulation:
# Slow enough that the packets below certainly arrive while it runs.
OS.delay_msec(300)
worlds.append(match_seed)
return Simulation.new(match_seed, SimGround.flat())
loop.match_flow.rules.min_players = 5
assert_true(loop.match_flow.join(7))
game.adopt(7)
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
assert_true(client.building_world(), "the notice starts the island in the background")
assert_null(client.world, "and nothing waits for it")
client.receive(NetProtocol.CHANNEL_CONTROL, game.world_notice())
loop.run_tick()
game.after_tick()
client.receive(NetProtocol.CHANNEL_STATE, game.snapshot_for(7))
assert_eq(client.held_count(), 3, "the notice, the world and the snapshot are held")
assert_eq(client.receiver.malformed, 0)
var started: int = Time.get_ticks_msec()
while client.building_world() and Time.get_ticks_msec() - started < 5000:
client.advance(DT)
OS.delay_msec(5)
assert_false(client.building_world())
assert_eq(client.held_count(), 0, "everything held was taken in")
assert_eq(worlds, [loop.match_flow.current_seed()], "one island, for the notice's seed")
assert_not_null(client.world)
assert_eq(client.world_seed, loop.match_flow.current_seed())
assert_eq(client.match_index, 0)
assert_true(client.replica.applied >= 1, "the WORLD message went on the new island")
assert_eq(client.outbox.size(), 1, "and the snapshot was answered")
assert_eq(client.bad_packets, 0)
# The same seed again doesn't make another island.
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
assert_false(client.building_world())
assert_eq(worlds.size(), 1)
func test_a_client_freed_while_its_island_is_made_waits_for_it() -> void:
var client: ClientGame = ClientGame.new()
client.build_in_background = true
client.make_world = func(match_seed: int) -> Simulation:
OS.delay_msec(100)
return Simulation.new(match_seed, SimGround.flat())
client.receive(NetProtocol.CHANNEL_CONTROL, game.match_notice())
assert_true(client.building_world())
client = null
assert_true(true, "freeing it with a task running raises no error")