From becbb1c6c5d4459eb6f880537f5fe4258bb7d72f Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Mon, 19 Oct 2026 11:42:07 +0200 Subject: [PATCH] THRIFT-6435: Wait until the browser-test servers listen instead of sleeping Client: js,ts The test task in lib/ts and lib/js starts the Node test servers asynchronously and then gave them one fixed second before headless Chrome loaded the first page. On a slow CI runner that is not always enough, and lib-ts failed with net::ERR_CONNECTION_REFUSED at http://localhost:8089/test.html. The wait task now tries to connect to each server port every 100 ms and continues once all of them accept connections. A server that is still not listening after 30 seconds fails the task with a message naming its port, instead of surfacing as a refused connection in the browser test. Co-Authored-By: Claude Opus 5.5 (1M context) --- lib/js/Gruntfile.js | 35 +++++++++++++++++++++++++++++++---- lib/ts/Gruntfile.js | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lib/js/Gruntfile.js b/lib/js/Gruntfile.js index a2211fef94e..a18d090fdfd 100644 --- a/lib/js/Gruntfile.js +++ b/lib/js/Gruntfile.js @@ -307,12 +307,39 @@ module.exports = function (grunt) { grunt.registerTask( "wait", - "Wait just one second for the server to start", + "Wait until the test servers accept connections", function () { var done = this.async(); - setTimeout(function () { - done(true); - }, 1000); + var net = require("net"); + var ports = [8088, 8089, 8090, 8091]; + var deadline = Date.now() + 30000; + var probe = function (index) { + if (index === ports.length) { + done(true); + return; + } + var socket = net.connect(ports[index], "localhost"); + socket.on("connect", function () { + socket.destroy(); + probe(index + 1); + }); + socket.on("error", function () { + socket.destroy(); + if (Date.now() < deadline) { + setTimeout(function () { + probe(index); + }, 100); + } else { + grunt.log.error( + "Nothing is listening on port " + + ports[index] + + " after 30 seconds", + ); + done(false); + } + }); + }; + probe(0); }, ); diff --git a/lib/ts/Gruntfile.js b/lib/ts/Gruntfile.js index a13a6c4acce..49f85dc4127 100644 --- a/lib/ts/Gruntfile.js +++ b/lib/ts/Gruntfile.js @@ -152,12 +152,39 @@ module.exports = function (grunt) { grunt.registerTask( "wait", - "Wait just one second for the server to start", + "Wait until the test server accepts connections", function () { var done = this.async(); - setTimeout(function () { - done(true); - }, 1000); + var net = require("net"); + var ports = [8089]; + var deadline = Date.now() + 30000; + var probe = function (index) { + if (index === ports.length) { + done(true); + return; + } + var socket = net.connect(ports[index], "localhost"); + socket.on("connect", function () { + socket.destroy(); + probe(index + 1); + }); + socket.on("error", function () { + socket.destroy(); + if (Date.now() < deadline) { + setTimeout(function () { + probe(index); + }, 100); + } else { + grunt.log.error( + "Nothing is listening on port " + + ports[index] + + " after 30 seconds", + ); + done(false); + } + }); + }; + probe(0); }, );