From 30ce9f8af52f019fd9b06079481c7bb16652a7db Mon Sep 17 00:00:00 2001 From: Wesley Nelson Date: Wed, 11 Nov 2026 15:54:06 -0800 Subject: [PATCH] test: retry requests refused while a TCP endpoint comes online The online tests send requests to an endpoint right after creating it. An HTTP endpoint that ngrok has not routed yet answers 404, which retry-config.mjs retries. A TCP endpoint refuses the connection instead, and the retry condition gave up on any error without a response, so a test that requested its TCP endpoint too soon failed with ECONNREFUSED, as "listen and forward multipass" did on one CI run while passing on another of the same code. Retry a refused connection with the same delays. A new test refuses the first connection and listens from then on, and checks the config retries until it gets the answer. --- __test__/retry-config.mjs | 5 +++++ __test__/retry-config.spec.mjs | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 __test__/retry-config.spec.mjs diff --git a/__test__/retry-config.mjs b/__test__/retry-config.mjs index b351fcec..7beb4aaa 100644 --- a/__test__/retry-config.mjs +++ b/__test__/retry-config.mjs @@ -5,6 +5,11 @@ export const retryConfig = { return retryCount * 2000; // time interval between retries }, retryCondition: (error) => { + // A TCP endpoint refuses connections until ngrok routes it, as an HTTP + // endpoint answers 404 until then. + if (error.code === "ECONNREFUSED") { + return true; + } // if retry condition is not specified, by default idempotent requests are retried if (!error.hasOwnProperty("response")) { return false; diff --git a/__test__/retry-config.spec.mjs b/__test__/retry-config.spec.mjs new file mode 100644 index 00000000..47c97372 --- /dev/null +++ b/__test__/retry-config.spec.mjs @@ -0,0 +1,34 @@ +import axios from "axios"; +import axiosRetry from "axios-retry"; +import * as http from "http"; +import * as retry from "./retry-config.mjs"; + +// A TCP endpoint refuses connections until ngrok routes it, so a request sent +// right after the listener is created can be refused; the online tests must +// retry it as they retry an HTTP endpoint's "offline" 404. +test("retry config retries a connection refused until the server listens", async () => { + const server = http.createServer((req, res) => res.end("Hello")); + await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve)); + const port = server.address().port; + await new Promise((resolve) => server.close(resolve)); + + const client = axios.create(); + let refused = 0; + // Registered before axios-retry, so it sees each failed attempt first: the + // first refusal brings the server up for the retry. + client.interceptors.response.use(undefined, (error) => { + if (error.code === "ECONNREFUSED" && refused++ === 0) { + server.listen(port, "127.0.0.1"); + } + return Promise.reject(error); + }); + axiosRetry(client, retry.retryConfig); + + try { + const response = await client.get(`http://127.0.0.1:${port}/`); + expect(refused).toBe(1); + expect(response.data).toBe("Hello"); + } finally { + server.close(); + } +});