Files
wekan/tests/playwright/helpers/docker-host-proxy.cjs
T
Lauri Ojansivu ffe207e191 Tests: the Docker test proxy dials the host again when a connect is never answered.
Even on the bridge network, EVERYTHING's Firefox stage lost a test now and
then to a page whose document never came (RuleBleed's cross-board test,
Red Strings, shared templates). Inside the container the cause was in
plain sight: a connect to host.docker.internal:3000 in SYN_SENT after nine
retransmits, while the next connection went straight through. Through the
container's proxy 9 of 1000 requests went unanswered; from the Mac itself
none did.

The proxy no longer waits on such a connect. It reads nothing from the
browser until a connection to the host is up (pauseOnConnect), drops a
dial that has not connected in 1.5 s - a normal one takes milliseconds -
and dials again, five times, so no byte is lost or sent twice. If none
connects the browser's connection is closed rather than left open.

playwrightDockerHostProxy.test.cjs: two unanswered dials, then one that
connects - the request arrives once and is answered, and the abandoned
dials are closed; when every dial is unanswered the client is closed after
exactly the attempts, in well under the old 30-60 s wait.

Thanks to xet7 !
2026-11-01 06:23:34 +02:00

68 lines
2.2 KiB
JavaScript

'use strict';
// Docker Desktop on macOS does not share the host loopback with containers.
// Keep the test browser's origin equal to the bundle's ROOT_URL while sending
// HTTP and WebSocket traffic to the host through Docker's gateway name.
//
// Docker Desktop's gateway now and then never answers one connect: under a
// whole-suite run a SYN to host.docker.internal sat unanswered through nine
// retransmits while the next connection went through at once. Waiting on it
// left a page with no document for the test's whole timeout. So a connect that
// has not completed in CONNECT_TIMEOUT_MS is dropped and dialled again. The
// browser's bytes are not read until a connection is up (pauseOnConnect), so
// nothing is lost or sent twice.
const net = require('node:net');
const CONNECT_TIMEOUT_MS = 1500;
const ATTEMPTS = 5;
function createProxy({
port,
dial = () => net.connect(port, 'host.docker.internal'),
connectTimeoutMs = CONNECT_TIMEOUT_MS,
attempts = ATTEMPTS,
}) {
return net.createServer({ pauseOnConnect: true }, client => {
let upstream = null;
client.on('error', () => upstream && upstream.destroy());
client.on('close', () => upstream && upstream.destroy());
const attempt = n => {
if (client.destroyed) return;
const socket = dial();
upstream = socket;
const timer = setTimeout(() => {
socket.destroy();
if (n < attempts) attempt(n + 1);
else client.destroy();
}, connectTimeoutMs);
socket.once('connect', () => {
clearTimeout(timer);
socket.on('close', () => client.destroy());
client.pipe(socket);
socket.pipe(client);
});
socket.on('error', () => {
clearTimeout(timer);
client.destroy();
});
};
attempt(1);
});
}
if (require.main === module) {
const port = Number(process.env.WEKAN_DOCKER_HOST_PROXY_PORT);
if (!Number.isInteger(port) || port < 1 || port > 65535) {
console.error('Invalid WEKAN_DOCKER_HOST_PROXY_PORT');
process.exit(2);
}
const server = createProxy({ port });
server.on('error', error => {
console.error('WeKan Docker test proxy:', error);
process.exit(1);
});
server.listen(port, '127.0.0.1');
}
module.exports = { createProxy, CONNECT_TIMEOUT_MS, ATTEMPTS };