diff --git a/lib/js/Gruntfile.js b/lib/js/Gruntfile.js index a2211fef94..a18d090fdf 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 a13a6c4acc..49f85dc412 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); }, );