diff --git a/tests/integration/helpers.js b/tests/integration/helpers.js index 064f4e6..be3b18d 100644 --- a/tests/integration/helpers.js +++ b/tests/integration/helpers.js @@ -18,11 +18,22 @@ const SSO = { }; async function api(method, url, { headers = {}, body } = {}) { - const res = await fetch(BASE + url, { + const send = () => fetch(BASE + url, { method, headers: { ...(body !== undefined ? { 'Content-Type': 'application/json' } : {}), ...headers }, body: body !== undefined ? JSON.stringify(body) : undefined, }); + let res; + try { + res = await send(); + } catch (e) { + // A pooled keep-alive connection can be closed by the server (5 s idle timeout) just + // as the client reuses it: ECONNRESET. Retry once, but only reads; a write might + // already have taken effect. + const reset = e && e.cause && e.cause.code === 'ECONNRESET'; + if (method !== 'GET' || !reset) throw e; + res = await send(); + } const text = await res.text(); let json = null; try { json = JSON.parse(text); } catch { /* not JSON */ } diff --git a/tests/utils.test.js b/tests/utils.test.js index 49dc18a..9a0d40b 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -123,10 +123,13 @@ describe('evalCondition', () => { describe('calculateNextRun', () => { test('interval: returns a date ~N minutes in the future', () => { + // Bounded by the clock before and after the call: its first use loads cron-parser, + // which took 30 s once on a loaded CI runner and broke a fixed 1 s tolerance. const before = Date.now(); const result = calculateNextRun('interval', '30'); - expect(result.getTime()).toBeGreaterThanOrEqual(before + 30 * 60000 - 100); - expect(result.getTime()).toBeLessThanOrEqual(before + 30 * 60000 + 1000); + const after = Date.now(); + expect(result.getTime()).toBeGreaterThanOrEqual(before + 30 * 60000); + expect(result.getTime()).toBeLessThanOrEqual(after + 30 * 60000); }); test('interval: throws on non-positive value', () => {