forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
186 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2017-2022 @polkadot/apps authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
const config = require('@polkadot/dev/config/jest.cjs'); | ||
|
||
module.exports = { | ||
...config, | ||
moduleNameMapper: {}, | ||
testTimeout: 2 * 60 * 1000 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2017-2022 @polkadot/apps-config authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import fs from 'fs'; | ||
|
||
import { ApiPromise, WsProvider } from '@polkadot/api'; | ||
import { assert, isError } from '@polkadot/util'; | ||
|
||
import { typesBundle, typesChain } from '../api'; | ||
import { fetchJson } from './fetch'; | ||
|
||
interface DnsResponse { | ||
Answer?: { name: string }[]; | ||
Question: { name: string }[]; | ||
} | ||
|
||
const TICK = '`'; | ||
|
||
export function checkEndpoint (issueFile: string, failures: string[]): (name: string, ws: string) => Promise<boolean> { | ||
return async (name: string, ws: string): Promise<boolean> => { | ||
const [,, hostWithPort] = ws.split('/'); | ||
const [host] = hostWithPort.split(':'); | ||
const json = await fetchJson<DnsResponse>(`https://dns.google/resolve?name=${host}`); | ||
|
||
let provider: WsProvider | null = null; | ||
let api: ApiPromise | null = null; | ||
let timerId: NodeJS.Timeout | null = null; | ||
|
||
try { | ||
assert(json.Answer, `No DNS entry for ${host}`); | ||
|
||
provider = new WsProvider(ws, false); | ||
api = new ApiPromise({ | ||
provider, | ||
throwOnConnect: true, | ||
throwOnUnknown: false, | ||
typesBundle, | ||
typesChain | ||
}); | ||
|
||
setTimeout((): void => { | ||
provider && | ||
provider | ||
.connect() | ||
.catch(() => undefined); | ||
}, 1000); | ||
|
||
await Promise.race([ | ||
// eslint-disable-next-line promise/param-names | ||
new Promise((_, reject): void => { | ||
timerId = setTimeout((): void => { | ||
timerId = null; | ||
reject(new Error(`Timeout connecting to ${ws}`)); | ||
}, 30_000); | ||
}), | ||
api.isReadyOrError | ||
.then((a) => a.rpc.chain.getBlock()) | ||
.then((b) => console.log(b.toHuman())) | ||
]); | ||
} catch (error) { | ||
if (isError(error) && failures.some((f) => (error as Error).message.includes(f))) { | ||
process.env.CI_LOG && fs.appendFileSync(issueFile, `\n${TICK}${name} @ ${ws} ${error.message}${TICK}\n`); | ||
|
||
throw error; | ||
} | ||
|
||
console.error(JSON.stringify(error)); | ||
} finally { | ||
if (timerId) { | ||
clearTimeout(timerId); | ||
} | ||
|
||
if (provider) { | ||
try { | ||
if (api) { | ||
await api.disconnect(); | ||
} else { | ||
await provider.disconnect(); | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
} | ||
|
||
return Promise.resolve(true); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2017-2022 @polkadot/apps authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { fetch } from '@polkadot/x-fetch'; | ||
|
||
// a fetch with a 2s timeout | ||
async function fetchWithTimeout (url: string, timeout = 2000): Promise<Response> { | ||
const controller = new AbortController(); | ||
let isAborted = false; | ||
const id = setTimeout((): void => { | ||
console.log(`Timeout on ${url}`); | ||
|
||
isAborted = true; | ||
controller.abort(); | ||
}, timeout); | ||
|
||
try { | ||
const response = await fetch(url, { signal: controller.signal }); | ||
|
||
clearTimeout(id); | ||
|
||
return response; | ||
} catch (error) { | ||
if (!isAborted) { | ||
clearTimeout(id); | ||
} | ||
|
||
throw error; | ||
} | ||
} | ||
|
||
export function fetchJson <T> (url: string, timeout = 2000): Promise<T> { | ||
return fetchWithTimeout(url, timeout).then<T>((r) => r.json()); | ||
} | ||
|
||
export function fetchText (url: string, timeout = 2000): Promise<string> { | ||
return fetchWithTimeout(url, timeout).then((r) => r.text()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2017-2022 @polkadot/apps-config authors & contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { isString } from '@polkadot/util'; | ||
|
||
import { createWsEndpoints } from '../endpoints'; | ||
import { checkEndpoint } from './check'; | ||
|
||
interface Endpoint { | ||
name: string; | ||
ws: string; | ||
} | ||
|
||
export function checkEndpoints (issueFile: string, failures: string[]): void { | ||
const checker = checkEndpoint(issueFile, failures); | ||
|
||
it.each( | ||
createWsEndpoints() | ||
.filter(({ isDisabled, isUnreachable, value }) => | ||
!isDisabled && | ||
!isUnreachable && | ||
value && | ||
isString(value) && | ||
!value.includes('127.0.0.1') && | ||
!value.startsWith('light://') | ||
) | ||
.map(({ text, value }): Partial<Endpoint> => ({ | ||
name: text as string, | ||
ws: value | ||
})) | ||
.filter((v): v is Endpoint => !!v.ws) | ||
)('%name @ %$ws', ({ name, ws }): Promise<void> => { | ||
console.error(`>>> ${name} @ ${ws}`); | ||
|
||
return checker(name, ws).then((r) => expect(r).toBe(true)); | ||
}); | ||
} |
This file was deleted.
Oops, something went wrong.