catch errors starting havenods and shut down gracefully

This commit is contained in:
woodser 2023-02-26 10:36:14 -05:00
parent 490f1e8c22
commit 9f3ff5c2ce

View File

@ -297,6 +297,7 @@ const OFFLINE_ERR_MSG = "Http response at 400 or 500 level";
jest.setTimeout(TestConfig.testTimeout);
beforeAll(async () => {
try {
// set log level for tests
HavenoUtils.setLogLevel(TestConfig.logLevel);
@ -317,11 +318,13 @@ beforeAll(async () => {
// start configured haveno daemons
const promises: Promise<HavenoClient>[] = [];
let err;
for (const config of TestConfig.startupHavenods) promises.push(initHaveno(config));
for (const settledPromise of await Promise.allSettled(promises)) {
if (settledPromise.status !== "fulfilled") throw new Error((settledPromise as PromiseRejectedResult).reason);
startupHavenods.push((settledPromise as PromiseFulfilledResult<HavenoClient>).value);
if (settledPromise.status === "fulfilled") startupHavenods.push((settledPromise as PromiseFulfilledResult<HavenoClient>).value);
else if (!err) err = new Error((settledPromise as PromiseRejectedResult).reason);
}
if (err) throw err;
// assign arbitrator, user1, user2
arbitrator = startupHavenods[0];
@ -340,6 +343,10 @@ beforeAll(async () => {
// create test data directory if it doesn't exist
if (!fs.existsSync(TestConfig.testDataDir)) fs.mkdirSync(TestConfig.testDataDir);
} catch (err) {
await shutDown();
throw err;
}
});
beforeEach(async () => {
@ -347,6 +354,10 @@ beforeEach(async () => {
});
afterAll(async () => {
await shutDown();
});
async function shutDown() {
// release haveno processes
const promises: Promise<void>[] = [];
@ -357,7 +368,7 @@ afterAll(async () => {
// terminate monero-javascript worker
(await monerojs.LibraryUtils.getWorker()).terminate();
});
}
// ----------------------------------- TESTS ----------------------------------
@ -2733,7 +2744,14 @@ async function initHaveno(ctx?: HavenodContext): Promise<HavenoClient> {
}
// open account if configured
if (ctx.autoLogin) await initHavenoAccount(havenod, ctx.accountPassword!);
if (ctx.autoLogin) {
try {
await initHavenoAccount(havenod, ctx.accountPassword!);
} catch (err) {
await releaseHavenoProcess(havenod);
throw err;
}
}
return havenod;
async function getAvailablePort(): Promise<number> {