From d4237c0879f487557676df2389b9e31b3492de9e Mon Sep 17 00:00:00 2001 From: Jaco Date: Wed, 10 Aug 2022 11:47:46 +0200 Subject: [PATCH] Adjust endpoint checks (#7972) --- package.json | 4 +- packages/apps-config/src/ci/fetch.ts | 55 ++++++++++++++------------- packages/apps-config/src/ci/runner.ts | 2 +- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 957ec97ba4..3bdeb527bb 100644 --- a/package.json +++ b/package.json @@ -41,8 +41,8 @@ "build:release:www": "yarn polkadot-ci-ghact-build && yarn build:release:ghpages && yarn build:release:ipfs", "build:robohash": "node scripts/robohash.cjs", "build:www": "rm -rf packages/apps/build && mkdir -p packages/apps/build && yarn run build:i18n && cd packages/apps && yarn webpack --config webpack.config.cjs", - "ci:chainEndpoints": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --detectOpenHandles packages/apps-config/src/ci/chainEndpoints", - "ci:chainTypes": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --detectOpenHandles packages/apps-config/src/ci/chainTypes", + "ci:chainEndpoints": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --forceExit packages/apps-config/src/ci/chainEndpoints", + "ci:chainTypes": "NODE_OPTIONS=--experimental-vm-modules polkadot-dev-run-test --config ./jest-ci.config.cjs --runInBand --forceExit packages/apps-config/src/ci/chainTypes", "clean": "polkadot-dev-clean-build", "clean:electronBuild": "cd packages/apps-electron && polkadot-dev-clean-build", "clean:electronRelease": "cd packages/apps-electron && rm -rf release", diff --git a/packages/apps-config/src/ci/fetch.ts b/packages/apps-config/src/ci/fetch.ts index 530bb7c057..e5815b8682 100644 --- a/packages/apps-config/src/ci/fetch.ts +++ b/packages/apps-config/src/ci/fetch.ts @@ -3,39 +3,42 @@ import { fetch } from '@polkadot/x-fetch'; -// this is also in @polkadot/phishing - we may want to expose it -// inside x-fetch at some point in the future... - -// a fetch with a 2s timeout -async function fetchWithTimeout (url: string, timeout = 2000): Promise { +function fetchWithTimeout (url: string, timeout = 2000): Promise { const controller = new AbortController(); - let isAborted = false; - const id = setTimeout((): void => { - console.log(`Timeout on ${url}`); + let id: null | ReturnType = null; - isAborted = true; - controller.abort(); - }, timeout); + // This is a weird mess, however we seem to have issues with Jest & hanging connections + // in the case where things are (possibly) aborted. So we just swallow/log everything + // and return null in the cases where things don't quite go as planned + return Promise + .race([ + fetch(url, { signal: controller.signal }).catch((error): null => { + console.error(error); - try { - const response = await fetch(url, { signal: controller.signal }); + return null; + }), + new Promise((resolve): void => { + id = setTimeout((): void => { + id = null; + controller.abort(); - clearTimeout(id); + resolve(null); + }, timeout); + }) + ]) + .then((response): Response | null => { + if (id) { + clearTimeout(id); + } - return response; - } catch (error) { - if (!isAborted) { - clearTimeout(id); - } - - throw error; - } + return response; + }); } -export function fetchJson (url: string, timeout?: number): Promise { - return fetchWithTimeout(url, timeout).then((r) => r.json()); +export function fetchJson (url: string, timeout?: number): Promise { + return fetchWithTimeout(url, timeout).then((r) => r && r.json()); } -export function fetchText (url: string, timeout?: number): Promise { - return fetchWithTimeout(url, timeout).then((r) => r.text()); +export function fetchText (url: string, timeout?: number): Promise { + return fetchWithTimeout(url, timeout).then((r) => r && r.text()); } diff --git a/packages/apps-config/src/ci/runner.ts b/packages/apps-config/src/ci/runner.ts index 586f78080f..92c43bb972 100644 --- a/packages/apps-config/src/ci/runner.ts +++ b/packages/apps-config/src/ci/runner.ts @@ -34,7 +34,7 @@ function createChecker (issueFile: string, failures: string[]): (name: string, w let isOk = true; try { - assert(json.Answer, `No DNS entry for ${host}`); + assert(json && json.Answer, `No DNS entry for ${host}`); provider = new WsProvider(ws, false); api = new ApiPromise({