server_player_targets harness: end each step by the server's clock

Each step waited a fixed 2.5 s. When the machine is loaded the vanilla server stalls for longer than that (CI on
#1571 and #1585: after Bob's trip to the Nether, vanilla's `tp Bob 4.5 -60 0.5` from the console ran a step late),
so its packets were filed under the next step and the transcripts differed although both servers sent the same.

A step now ends once the server has done its work:
- the console's own `time query gametime` comes back, so the step's console commands have run (the console runs
  its queue in order);
- LocatorBarClient's new `sync NAME` runs `time query gametime` as that player and waits for the reply (not
  recorded), so that player's commands have run and every packet sent before the reply is recorded;
- the server's game time has advanced STEP_TICKS (40) more ticks, then each player sends its position reminder
  and syncs once more.

game_time() moves out of settle() in test_dedicated_server_parity.py so both use it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-25 05:20:55 +01:00
parent 4a675206b1
commit 6acf7fb6ab
3 changed files with 91 additions and 24 deletions
@@ -5,9 +5,12 @@ import io.netty.buffer.Unpooled;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
@@ -78,7 +81,10 @@ import net.minecraft.world.phys.Vec3;
* on|off} (the shift key, as ServerboundPlayerInputPacket), {@code fly NAME
* on|off} (ServerboundPlayerAbilitiesPacket), {@code still NAME} (the
* position reminder LocalPlayer.sendPosition sends every 20 ticks standing
* still), {@code quit NAME}, {@code
* still), {@code sync NAME} (runs {@code time query gametime}, whose reply is
* not recorded, and prints {@code synced NAME} once it is back: the server has
* handled everything this player sent before, and everything the server sent
* this player before the reply has been recorded), {@code quit NAME}, {@code
* players on|off} (whether each player also records the other players added
* to and removed from its level, and its own respawns into a dimension),
* {@code chat on|off} (whether the system chat each player receives is
@@ -95,6 +101,8 @@ public final class LocatorBarClient {
private static final Map<UUID, String> NAMES = new LinkedHashMap<>();
public static void main(String[] args) throws Exception {
// Bootstrap sends System.out to the logger: `synced` lines go to the real stdout.
PrintStream stdout = new PrintStream(new FileOutputStream(FileDescriptor.out), true, StandardCharsets.UTF_8);
ParityData.init();
String host = args[0];
int port = Integer.parseInt(args[1]);
@@ -122,6 +130,13 @@ public final class LocatorBarClient {
players.get(w[1]).send(new net.minecraft.network.protocol.game.ServerboundPlayerAbilitiesPacket(abilities));
}
case "still" -> players.get(w[1]).still();
case "sync" -> {
try {
players.get(w[1]).sync();
} finally {
stdout.println("synced " + w[1]);
}
}
case "quit" -> players.get(w[1]).close();
case "players" -> LocatorBarClient.players = w[1].equals("on");
case "chat" -> chat = w[1].equals("on");
@@ -189,6 +204,7 @@ public final class LocatorBarClient {
private final Map<Integer, String> seen = new java.util.HashMap<>();
private final Object replyLock = new Object();
private int replies;
private int syncs;
Player(String name) {
this.name = name;
@@ -211,6 +227,23 @@ public final class LocatorBarClient {
}
}
/** A round trip through the server thread, its reply left out of the transcript. */
void sync() throws Exception {
synchronized (replyLock) {
int before = syncs;
send(new ServerboundChatCommandPacket("time query gametime"));
long until = System.nanoTime() + 30_000_000_000L;
while (syncs == before) {
long left = (until - System.nanoTime()) / 1_000_000L;
if (left <= 0) {
record("error no reply to sync");
return;
}
replyLock.wait(left);
}
}
}
void record(String line) {
synchronized (lines) {
lines.add("step=" + step + " " + line);
@@ -312,6 +345,13 @@ public final class LocatorBarClient {
seen.clear();
} else if (p instanceof ClientboundTrackedWaypointPacket) {
record(waypoint(r.payload));
} else if (p instanceof ClientboundSystemChatPacket c
&& c.content().getContents() instanceof net.minecraft.network.chat.contents.TranslatableContents t
&& t.getKey().equals("commands.time.query.gametime")) {
synchronized (replyLock) {
syncs++;
replyLock.notifyAll();
}
} else if (p instanceof ClientboundSystemChatPacket c) {
if (chat) {
record("chat " + (c.overlay() ? "overlay " : "") + net.minecraft.network.chat.ComponentSerialization.CODEC
+16 -11
View File
@@ -181,20 +181,25 @@ def settle(server, ticks=600, timeout=600):
break
time.sleep(2)
def game_time():
start = len(server.lines)
server.console('time query gametime')
while time.time() < deadline:
for line in server.lines[start:]:
if m := re.search(r'The (?:game )?time is (\d+)', line):
return int(m.group(1))
time.sleep(0.2)
raise AssertionError(f'{server.log_path.name}: no game time in {timeout} s')
until = game_time() + ticks
while game_time() < until:
until = game_time(server, deadline - time.time()) + ticks
while game_time(server, deadline - time.time()) < until:
time.sleep(2)
def game_time(server, timeout=300):
"""The server's game time, asked from its console: the console runs its queue in order, so every console
command sent before has run."""
deadline = time.time() + timeout
start = len(server.lines)
server.console('time query gametime')
while time.time() < deadline:
for line in server.lines[start:]:
if m := re.search(r'The (?:game )?time is (\d+)', line):
return int(m.group(1))
time.sleep(0.05)
raise AssertionError(f'{server.log_path.name}: no game time in {timeout:.0f} s')
def legacy_ping(port):
"""The reply to a 1.6 client's 0xFE 0x01 0xFA MC|PingHost query (LegacyQueryHandler)."""
host = 'localhost'
+34 -12
View File
@@ -10,7 +10,11 @@ A vanilla dedicated server and Horizon's (`horizon-client --dedicated --nogui`)
vanilla generated first, with Alice and Bob as operators in creative. LocatorBarClient joins them both (players made
of vanilla's protocol code) and records, step by step, the system chat each receives and what the server changes
about it: game mode events, teleports, inventory slots, effects, its death (with the death message), the other
players it is sent or loses and the levels it respawns into. Each player's lines in each step are compared exactly,
players it is sent or loses and the levels it respawns into. A step ends by the server's clock, not the wall's: its
console commands have run (the console's `time query gametime` comes back), each player's commands have run (a
`sync` round trip), the server has ticked STEP_TICKS more times, and each client holds what was sent until then (a
last `sync`), so a server that stalls under load still has each packet recorded in its own step (a fixed 2.5 s
filed a stalled vanilla's teleports under the next step). Each player's lines in each step are compared exactly,
in sorted order (the packets of one step may leave in another order within the tick), with the locator bar's
waypoints as each step leaves them (one manager per level: a player in another level is untracked). What a death does besides its messages
is left to its own harness.
@@ -25,12 +29,12 @@ import time
try:
from . import lab, test_rust_parity as base
from .test_dedicated_server_parity import Server
from .test_dedicated_server_parity import Server, game_time
from .test_locator_bar_parity import generate_world, server_dir
except ImportError:
import lab
import test_rust_parity as base
from test_dedicated_server_parity import Server
from test_dedicated_server_parity import Server, game_time
from test_locator_bar_parity import generate_world, server_dir
NAME = 'server-player-targets-parity'
@@ -70,7 +74,8 @@ SCRIPT = [
# What the players' locator bars show has its own harness (test_locator_bar_parity), and what a death does
# besides its messages (drops, the body leaving) the player combat ones: here only the messages of `kill`.
DEATH_STEP = 'step=console-kill'
STEP_SECONDS = 2.5
STEP_TICKS = 40
PLAYERS = ('Alice', 'Bob')
def run_side(label, server_command, client_command, build, world):
@@ -83,32 +88,49 @@ def run_side(label, server_command, client_command, build, world):
server.wait_for(r'Done \(')
with (build / f'{label}-client.log').open('w') as log:
client = subprocess.Popen([*map(str, client_command), '127.0.0.1', str(port), str(out)],
cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT,
text=True)
cwd=lab.ROOT, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, text=True)
def tell(line):
client.stdin.write(line + '\n')
client.stdin.flush()
def sync():
for name in PLAYERS:
tell(f'sync {name}')
for name in PLAYERS:
while (line := client.stdout.readline()) != f'synced {name}\n':
if not line:
raise AssertionError(f'{label}: the client exited (see {build}/{label}-client.log)')
log.write(line)
def finish_step():
sync()
until = game_time(server) + STEP_TICKS
while game_time(server) < until:
time.sleep(0.2)
for name in PLAYERS:
tell(f'still {name}')
sync()
try:
for name, x in (('Alice', 0.5), ('Bob', 4.5)):
for name, x in zip(PLAYERS, (0.5, 4.5)):
tell(f'join {name}')
server.wait_for(rf'{name} joined the game', timeout=120)
tell(f'fly {name} on')
tell(f'cmd {name} tp @s {x} -60 0.5 0 0')
time.sleep(STEP_SECONDS)
finish_step()
for flag in ('chat', 'state', 'players'):
tell(f'{flag} on')
for step, console, actions in SCRIPT:
tell(f'step {step}')
for command in console:
server.console(command)
game_time(server) # the console runs its queue in order: these commands have run
for action in actions:
tell(action)
time.sleep(STEP_SECONDS)
for name in ('Alice', 'Bob'):
tell(f'still {name}')
finish_step()
tell('end')
client.wait(timeout=60)
log.write(client.communicate(timeout=60)[0])
finally:
if client.poll() is None:
client.kill()