ethers.js/lib.esm/_tests/utils.js

125 lines
3.7 KiB
JavaScript
Raw Normal View History

2022-09-05 23:57:11 +03:00
import fs from "fs";
import path from "path";
import zlib from 'zlib';
// Find the package root (based on the nyc output/ folder)
const root = (function () {
let root = process.cwd();
while (true) {
if (fs.existsSync(path.join(root, "output"))) {
return root;
}
const parent = path.join(root, "..");
if (parent === root) {
break;
}
root = parent;
}
throw new Error("could not find root");
})();
// Load the tests
export function loadTests(tag) {
const filename = path.resolve(root, "testcases", tag + ".json.gz");
return JSON.parse(zlib.gunzipSync(fs.readFileSync(filename)).toString());
}
export function log(context, text) {
if (context && context.test && typeof (context.test._ethersLog) === "function") {
context.test._ethersLog(text);
}
else {
console.log(text);
}
}
2022-12-31 00:35:04 +03:00
export async function stall(duration) {
2022-09-16 05:58:45 +03:00
return new Promise((resolve) => { setTimeout(resolve, duration); });
}
const ATTEMPTS = 5;
export async function retryIt(name, func) {
2022-11-09 10:57:02 +03:00
//const errors: Array<Error> = [ ];
2022-09-16 05:58:45 +03:00
it(name, async function () {
this.timeout(ATTEMPTS * 5000);
for (let i = 0; i < ATTEMPTS; i++) {
try {
await func.call(this);
return;
}
catch (error) {
if (error.message === "sync skip; aborting execution") {
// Skipping a test; let mocha handle it
throw error;
}
else if (error.code === "ERR_ASSERTION") {
// Assertion error; let mocha scold us
throw error;
}
else {
2022-11-09 10:57:02 +03:00
//errors.push(error);
2022-09-16 05:58:45 +03:00
if (i === ATTEMPTS - 1) {
2022-11-09 10:57:02 +03:00
throw error;
//stats.pushRetry(i, name, error);
2022-09-16 05:58:45 +03:00
}
else {
await stall(500 * (1 << i));
2022-11-09 10:57:02 +03:00
//stats.pushRetry(i, name, null);
2022-09-16 05:58:45 +03:00
}
}
}
}
// All hope is lost.
throw new Error(`Failed after ${ATTEMPTS} attempts; ${name}`);
});
}
2022-11-09 10:57:02 +03:00
/*
export interface StatSet {
name: string;
retries: Array<{ message: string, error: null | Error }>;
}
const _guard = { };
2022-09-16 05:58:45 +03:00
export class Stats {
2022-11-09 10:57:02 +03:00
// #stats: Array<StatSet>;
constructor(guard: any) {
if (guard !== _guard) { throw new Error("private constructor"); }
// this.#stats = [ ];
2022-09-16 05:58:45 +03:00
}
2022-11-09 10:57:02 +03:00
#currentStats(): StatSet {
if (this.#stats.length === 0) { throw new Error("no active stats"); }
2022-09-16 05:58:45 +03:00
return this.#stats[this.#stats.length - 1];
}
2022-11-09 10:57:02 +03:00
pushRetry(attempt: number, line: string, error: null | Error): void {
2022-09-16 05:58:45 +03:00
const { retries } = this.#currentStats();
2022-11-09 10:57:02 +03:00
if (attempt > 0) { retries.pop(); }
2022-09-16 05:58:45 +03:00
if (retries.length < 100) {
retries.push({
2022-11-09 10:57:02 +03:00
message: `${ attempt + 1 } failures: ${ line }`,
2022-09-16 05:58:45 +03:00
error
});
}
}
2022-11-09 10:57:02 +03:00
start(name: string): void {
this.#stats.push({ name, retries: [ ] });
2022-09-16 05:58:45 +03:00
}
2022-11-09 10:57:02 +03:00
end(context?: any): void {
2022-09-16 05:58:45 +03:00
let log = console.log.bind(console);
2022-11-09 10:57:02 +03:00
if (context && typeof(context._ethersLog) === "function") {
2022-09-16 05:58:45 +03:00
log = context._ethersLog;
}
const { name, retries } = this.#currentStats();
2022-11-09 10:57:02 +03:00
if (retries.length === 0) { return; }
log(`Warning: The following tests required retries (${ name })`);
2022-09-16 05:58:45 +03:00
retries.forEach(({ error, message }) => {
log(" " + message);
2022-11-09 10:57:02 +03:00
if (error) { log(error); }
2022-09-16 05:58:45 +03:00
});
}
}
2022-11-09 10:57:02 +03:00
2022-09-16 05:58:45 +03:00
export const stats = new Stats(_guard);
2022-11-09 10:57:02 +03:00
*/
2022-09-05 23:57:11 +03:00
//# sourceMappingURL=utils.js.map