Print the net corpus's server and client lines in a fixed order

The server's 'connection' and the client's 'connect' race, and macOS runs
them in the other order from Linux. 5644 now prints both sides once both
have run, and 5645 keeps the server's and the client's events in separate
lists printed one after the other.
This commit is contained in:
Chris Tate
2026-10-15 16:42:32 -05:00
parent a5a3588d3d
commit bfb0ca8ffc
2 changed files with 33 additions and 25 deletions
+22 -21
View File
@@ -4,17 +4,25 @@
// the open halves.
import * as net from "node:net";
// The server's 'connection' and the client's 'connect' race (either can
// run first), so each side records its line and both print once both
// have run.
let serverLine: string | null = null;
let clientLine: string | null = null;
let startClient: (() => void) | null = null;
function bothConnected(): void {
if (serverLine === null || clientLine === null || startClient === null) return;
console.log(serverLine);
console.log(clientLine);
startClient();
}
const server = net.createServer((sock) => {
const port = (server.address() as net.AddressInfo).port;
console.log(
"server side",
sock.remoteAddress,
sock.remoteFamily,
typeof sock.remotePort,
sock.localAddress,
sock.localPort === port,
sock.localFamily,
);
serverLine =
`server side ${sock.remoteAddress} ${sock.remoteFamily} ${typeof sock.remotePort} ` +
`${sock.localAddress} ${sock.localPort === port} ${sock.localFamily}`;
bothConnected();
sock.on("data", (d: Buffer) => {
console.log("server got", d.toString(), sock.bytesRead);
sock.end("bye");
@@ -36,18 +44,11 @@ server.listen(0, "127.0.0.1", () => {
c.bytesRead,
);
c.on("connect", () => {
console.log(
"connected",
c.readyState,
c.connecting,
c.pending,
c.remotePort === port,
c.remoteFamily,
c.localAddress,
typeof c.localPort,
c.localFamily,
);
c.write("hello");
clientLine =
`connected ${c.readyState} ${c.connecting} ${c.pending} ${c.remotePort === port} ` +
`${c.remoteFamily} ${c.localAddress} ${typeof c.localPort} ${c.localFamily}`;
startClient = () => c.write("hello");
bothConnected();
});
c.on("data", (d: Buffer) => console.log("client got", d.toString(), c.bytesRead));
c.on("end", () => console.log("end", c.readyState, c.bytesRead, c.bytesWritten));
+11 -4
View File
@@ -3,18 +3,22 @@
// setKeepAlive's chaining.
import * as net from "node:net";
// Each side keeps its own list: the client's callbacks and the server's
// events can interleave either way, so the lists print one after the
// other at the end.
const events: string[] = [];
const serverEvents: string[] = [];
let serverSockets = 0;
const server = net.createServer((sock) => {
sock.setEncoding("utf8");
sock.on("data", (d: string) => {
events.push(`server data ${typeof d} ${d}`);
sock.end("pong", () => events.push("server end callback"));
serverEvents.push(`server data ${typeof d} ${d}`);
sock.end("pong", () => serverEvents.push("server end callback"));
});
// the first connection's server side (the timeout connection's close
// order against the client's depends on the event loop phase)
if (serverSockets++ === 0) {
sock.on("close", (hadError: boolean) => events.push(`server close ${hadError}`));
sock.on("close", (hadError: boolean) => serverEvents.push(`server close ${hadError}`));
}
});
@@ -59,7 +63,10 @@ server.listen(0, "127.0.0.1", () => {
t.on("timeout", () => events.push("timeout event"));
t.on("close", (hadError: boolean) => {
events.push(`t close ${hadError}`);
server.close(() => console.log(events.join("\n")));
server.close(() => {
console.log(serverEvents.join("\n"));
console.log(events.join("\n"));
});
});
});
});