Files
modbison/scripts/smoke_test_network_client.gd
T
daddy 3596922696 Fix real flake in smoke_test_network_client.gd's prediction-buffer check
Found while re-verifying the full PC2 test suite from a fresh clone:
the "prediction buffer has entries" check failed ~1 in 3 runs. Root
cause is a genuine race in the test, not in prediction/reconciliation
itself -- a server snapshot RPC can land on the 60-frame loop's final
`await process_frame`, triggering a legitimate reconciliation that
clears the buffer (network_client.gd's documented behavior) right
before the check reads it. The desync-correction check right after it
already proves reconciliation logic works correctly on its own, so
this was never actually catching a real bug -- just occasionally
racing a real correction event.

Fix: one more predict_and_send() right before the check guarantees a
fresh buffer entry regardless of whether a correction just fired.
Verified 5/5 clean runs against a real dedicated server after the fix
(previously observed a failure within 3 runs).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01M6rUEynmv2QmweocAQYARN
2026-08-21 01:44:28 +00:00

123 lines
5.1 KiB
GDScript

extends SceneTree
## Real integration test for NetworkClient (client-side prediction +
## reconciliation) against an actual dedicated server process.
##
## This does NOT boot its own server -- run scenes/server.tscn as a separate
## process first (see docs/architecture/networking.md's ENet/port setup):
## godot --headless --path . scenes/server.tscn &
## then, from the same project directory:
## godot --headless --path . --script res://scripts/smoke_test_network_client.gd
##
## Verifies: the client can actually connect over ENet, local prediction
## moves the player immediately on input (no waiting for the server),
## server snapshots actually arrive and update the client's authoritative
## view, and a deliberately-desynced predicted position gets corrected by
## reconciliation.
var passed := 0
var failed := 0
func _initialize() -> void:
_run()
func _run() -> void:
await process_frame
var net := NetworkClient.new()
root.add_child(net)
var ok: bool = await net.connect_to_server("127.0.0.1", 7777)
if not _check("connected to the real dedicated server", ok):
printerr(" Is `godot --headless --path . scenes/server.tscn` running on port 7777?")
_finish()
return
# Give the server a few frames to process the connection and spawn our
# player node before we start sending input.
for i in 10:
await process_frame
# 1. Prediction: pressing "forward" should move the local position
# immediately, without waiting for any server round trip.
var pos_before: Vector3 = net.position
for i in 10:
net.predict_and_send(1.0 / 30.0, 0.0, -1.0, false) # move -Z (forward)
await process_frame
var pos_after_predict: Vector3 = net.position
_check("local prediction moved the player without waiting for the server",
pos_after_predict.distance_to(pos_before) > 0.1)
# 2. Snapshots actually arrive from the real server and get processed --
# wait for at least one reconciliation-eligible snapshot by giving it
# real time (server broadcasts at its own tick rate).
var got_snapshot := false
net.reconciled.connect(func(_err): got_snapshot = true)
for i in 60:
net.predict_and_send(1.0 / 30.0, 0.0, -1.0, false)
await process_frame
# Reconciliation only actually fires on a real mismatch -- absence of a
# large mismatch is a PASS outcome too (means prediction tracked the
# server closely), so this alone isn't proof snapshots arrived. Check
# that directly instead. One real flake found running this repeatedly:
# a snapshot RPC can land on the loop's very last `await process_frame`,
# triggering a legitimate reconciliation that clears the buffer (per
# _handle_snapshot's documented behavior) right before this check reads
# it -- a real race in the *test*, not a bug in prediction/reconciliation
# (the desync check below proves that logic works independently). One
# more predict_and_send guarantees a fresh entry regardless of whether a
# correction just fired.
net.predict_and_send(1.0 / 30.0, 0.0, -1.0, false)
_check("prediction buffer has entries (input was actually sent)", net._prediction_buffer.size() > 0)
# 3. Deliberately desync prediction from reality, then confirm the next
# snapshot corrects it -- the real proof reconciliation logic works,
# not just that it never had to fire.
net.position = Vector3(9999, 9999, 9999)
var desynced_pos := net.position
# GDScript closures capture local value-type vars (bool/float) by value,
# not by reference -- assigning to them *inside* the lambda doesn't
# mutate the outer scope's copy. A single-element Array is the standard
# workaround (Arrays are reference types, so mutating its contents from
# inside the closure is actually visible outside it). Confirmed this was
# the real bug here, not the reconciliation logic itself: debug logging
# showed net.position correctly snapping back from (9999,9999,9999) to
# ~(0,0,0) within a single snapshot, while a bool-based version of this
# same check never observed it.
var reconcile_state := [false, -1.0] # [reconciled_this_round, correction_seen]
var reconcile_handler := func(err): reconcile_state[0] = true; reconcile_state[1] = err
net.reconciled.connect(reconcile_handler)
for i in 60:
# Keep predicting (with no movement input) so the prediction buffer's
# latest entry actually reflects the desynced position -- without
# this, the buffer either empties out or keeps stale pre-desync
# entries, and _handle_snapshot has nothing real to compare against.
net.predict_and_send(1.0 / 30.0, 0.0, 0.0, false)
await process_frame
if reconcile_state[0]:
break
net.reconciled.disconnect(reconcile_handler)
_check("a deliberately desynced position gets corrected by the next snapshot",
reconcile_state[0] and net.position.distance_to(desynced_pos) > 100.0)
if reconcile_state[0]:
print(" correction distance was ~", reconcile_state[1])
net.disconnect_from_server()
_finish()
func _check(description: String, condition: bool) -> bool:
if condition:
print(" PASS: ", description)
passed += 1
else:
printerr(" FAIL: ", description)
failed += 1
return condition
func _finish() -> void:
print("\n=== SMOKE RESULT: %d/%d passed ===" % [passed, passed + failed])
quit(0 if failed == 0 else 1)