Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion graphify-out/graph.json

Large diffs are not rendered by default.

37 changes: 21 additions & 16 deletions lib/hosts/daemon.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ function scriptedServer({ refuse = () => null, validToken = null, version = 6 }
const sendAck = (ws) => ws.send(JSON.stringify({ type: "ack", ack }));
const server = {
refuse, validToken, hellos: [], helloTimes: [], upAtHello: [], up: [], acks: [], blobs: [], frames: [], textFrameBytes: [], gets: 0, diagnoses: 0,
/** Runs when a hello arrives, before its welcome goes. */
/** Runs when a hello arrives, before its welcome goes; the messages it returns go in the welcome's frame, after it,
* so the daemon applies them in the same event as the welcome. */
onHello: null,
get holdAcks() { return holdAcks; },
set holdAcks(value) {
Expand Down Expand Up @@ -216,9 +217,9 @@ function scriptedServer({ refuse = () => null, validToken = null, version = 6 }
server.hellos.push(JSON.parse(data));
server.helloTimes.push(Date.now());
server.upAtHello.push(server.up.length);
server.onHello?.();
const after = server.onHello?.() ?? [];
// These delivery/metrics assertions exercise the negotiated legacy spawn path.
server.raw({ type: "welcome", v: version, hostId: "h1" });
socket.send([{ type: "welcome", v: version, hostId: "h1" }, ...after].map((m) => JSON.stringify(m)).join("\n"));
return;
}
server.textFrameBytes.push(Buffer.byteLength(data));
Expand Down Expand Up @@ -1899,32 +1900,36 @@ test("a text frame batches lines up to one piece of UTF-8 bytes, also for multi-
}
});

/** About 20,000 unacked "1" lines on c1 (with a 1 MiB window for the run), then a redial whose resend stalls in the
* socket after about three frames (the uplink pauses as the hello arrives); `stalled(maxSeq)` runs then, before the
* uplink resumes. Acks stay held. Returns how many of the lines sent before the drop went again, and how many there
* were. */
async function stalledRedial(server, stalled) {
/** About 20,000 unacked "1" lines on c1 (a full 1 MiB window for the run), then a redial whose resend stalls in the
* socket after about three frames (the uplink pauses as the hello arrives). `afterWelcome(maxSeq)` returns messages
* that go in the welcome's frame, so the daemon applies them right after the stalled flush and before the uplink
* resumes; `stalled(maxSeq)` runs once the redial is welcomed, also before it resumes. Acks stay held. Returns how
* many of the lines sent before the drop went again, and how many there were. */
async function stalledRedial(server, { afterWelcome = () => [], stalled = async () => {} }) {
const savedWindow = LINK_LIMITS.unackedBytes;
LINK_LIMITS.unackedBytes = 1024 * 1024;
try {
await waitFor(() => server.hellos.length === 1, "the hello");
await spawnChannel(server, "c1");
server.holdAcks = true;
const heldFrom = server.textFrameBytes.length;
const ones = () => server.up.filter((m) => m.ch === "c1" && m.line === "1");
server.send({ type: "stdin", ch: "c1", line: '{"type":"many"}\n' });
await waitFor(() => ones().length >= 15_000, "most lines sent once");
await sleep(300);
// The daemon stops at a full unacked window, and every frame since the hold is unacked: nothing more goes up.
await waitFor(() => server.textFrameBytes.slice(heldFrom).reduce((sum, bytes) => sum + bytes, 0) >= LINK_LIMITS.unackedBytes,
"a full unacked window of lines");
const unacked = ones().length;
const maxSeq = Math.max(...ones().map((m) => m.seq));
server.onHello = () => {
uplink.paused = true;
return afterWelcome(maxSeq);
};
server.drop();
await waitFor(() => server.hellos.length === 2, "the redial");
await sleep(100);
await stalled(maxSeq);
uplink.paused = false;
await sleep(1_000);
// The resend is done once a message sequenced after the old lines arrives: new lines come after the snapshot.
await waitFor(() => server.up.slice(server.upAtHello[1]).some((m) => m.seq > maxSeq), "the resend to finish");
const resent = server.up.slice(server.upAtHello[1]).filter((m) => m.ch === "c1" && m.line === "1" && m.seq <= maxSeq).length;
return { resent, unacked };
} finally {
Expand All @@ -1937,8 +1942,8 @@ test("a resend skips what main acked after the welcome", async () => {
const server = scriptedServer();
const daemon = start(server);
try {
// Main had applied every line before the drop; its ack says so on the new socket.
const { resent, unacked } = await stalledRedial(server, (maxSeq) => server.raw({ type: "ack", ack: maxSeq }));
// Main had applied every line before the drop; its ack says so on the new socket, right after the welcome.
const { resent, unacked } = await stalledRedial(server, { afterWelcome: (maxSeq) => [{ type: "ack", ack: maxSeq }] });
// The resend stalled after about three frames; the fourth frame's lines must not go again.
assert.ok(unacked - resent >= 2_000, `${resent} of ${unacked} lines resent`);
} finally {
Expand All @@ -1952,11 +1957,11 @@ test("a resend skips a channel's lines dropped by an overflow after the welcome"
const server = scriptedServer();
const daemon = start(server);
try {
const { resent, unacked } = await stalledRedial(server, async () => {
const { resent, unacked } = await stalledRedial(server, { stalled: async () => {
server.send({ type: "stdin", ch: "c1", line: '{"type":"flood"}\n' });
// 40 MB of output, past the channel buffer: wait for the daemon to drop it.
await waitForDropped(log, FLOOD_BYTES - LINK_LIMITS.channelBufferBytes);
});
} });
// The resend stalled after about three frames; the fourth frame's lines must not go again.
assert.ok(unacked - resent >= 2_000, `${resent} of ${unacked} lines resent`);
} finally {
Expand Down
Loading