This commit is contained in:
Andras Schmelczer 2026-08-22 18:35:53 +01:00
commit 5017ff168a

View file

@ -1,7 +1,4 @@
// What a Player sheds when its socket stops draining. The guard used to read
// `socket.conn.bufferedAmount`, which engine.io's Socket does not have, so it
// never once ran — and had it run, it dropped the whole batch, including the
// create/delete that handleViewAreaUpdate had already committed to having sent.
// Slow-client backpressure: what a Player sheds when its socket stops draining.
import { describe, it, expect, beforeEach } from 'vitest';
import { createRequire } from 'node:module';
@ -26,8 +23,7 @@ const {
applyArrayPlugins();
// A socket whose write buffer we control, shaped the way engine.io really nests
// it: the byte count lives on the ws socket under the transport.
// Shaped like engine.io: the byte count lives on the ws socket under the transport.
class FakeSocket {
public readonly sent: Array<{ event: string; payload: unknown }> = [];
public readonly conn = { transport: { socket: { bufferedAmount: 0 } } };
@ -58,15 +54,11 @@ const makePlayer = () => {
const players = new PlayerContainer(objects, 4, 0);
const socket = new FakeSocket();
const player = players.createPlayer({ name: 'player' }, socket as never);
// The constructor already queues this player's own CreatePlayerCommand; flush
// it so each case starts from an empty queue.
player.sendQueuedCommandsToClient();
socket.sent.length = 0;
return { player, socket };
};
// One snapshot's worth of queued traffic: the bulky repeated state, and the
// one-shot bookkeeping that is never regenerated.
const queueOneSnapshot = (player: ReturnType<typeof makePlayer>['player']) => {
player.queueCommandSend(new CreateObjectsCommand([]));
player.queueCommandSend(new DeleteObjectsCommand([1234]));
@ -102,8 +94,6 @@ describe('slow-client backpressure', () => {
queueOneSnapshot(player);
player.sendQueuedCommandsToClient();
// If the guard cannot see the buffer it degrades to sending everything, so
// anything shed at all proves it is reading the right property.
expect(typesIn(socket.lastBatch)).not.toContain(PropertyUpdatesForObjects.name);
});
@ -114,12 +104,8 @@ describe('slow-client backpressure', () => {
player.sendQueuedCommandsToClient();
const types = typesIn(socket.lastBatch);
// Repeated in full every tick — safe to drop.
expect(types).not.toContain(PropertyUpdatesForObjects.name);
expect(types).not.toContain(UpdateMinimap.name);
// One-shot. handleViewAreaUpdate has already advanced its record of what
// this client knows, so these are never re-derived: dropping them leaves the
// client permanently missing objects and holding ghosts.
expect(types).toContain(CreateObjectsCommand.name);
expect(types).toContain(DeleteObjectsCommand.name);
expect(types).toContain(ServerAnnouncement.name);