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,49 +297,56 @@ const OFFLINE_ERR_MSG = "Http response at 400 or 500 level";
jest.setTimeout(TestConfig.testTimeout); jest.setTimeout(TestConfig.testTimeout);
beforeAll(async () => { beforeAll(async () => {
try {
// set log level for tests // set log level for tests
HavenoUtils.setLogLevel(TestConfig.logLevel); HavenoUtils.setLogLevel(TestConfig.logLevel);
// initialize funding wallet // initialize funding wallet
await initFundingWallet(); await initFundingWallet();
HavenoUtils.log(0, "Funding wallet balance: " + await fundingWallet.getBalance()); HavenoUtils.log(0, "Funding wallet balance: " + await fundingWallet.getBalance());
HavenoUtils.log(0, "Funding wallet unlocked balance: " + await fundingWallet.getUnlockedBalance()); HavenoUtils.log(0, "Funding wallet unlocked balance: " + await fundingWallet.getUnlockedBalance());
const subaddress = await fundingWallet.createSubaddress(0); const subaddress = await fundingWallet.createSubaddress(0);
HavenoUtils.log(0, "Funding wallet height: " + await fundingWallet.getHeight()); HavenoUtils.log(0, "Funding wallet height: " + await fundingWallet.getHeight());
HavenoUtils.log(0, "Funding wallet mnemonic: " + await fundingWallet.getMnemonic()); HavenoUtils.log(0, "Funding wallet mnemonic: " + await fundingWallet.getMnemonic());
HavenoUtils.log(0, "Funding wallet primary address: " + await fundingWallet.getPrimaryAddress()); HavenoUtils.log(0, "Funding wallet primary address: " + await fundingWallet.getPrimaryAddress());
HavenoUtils.log(0, "Funding wallet new subaddress: " + subaddress.getAddress()); HavenoUtils.log(0, "Funding wallet new subaddress: " + subaddress.getAddress());
// initialize monerod // initialize monerod
monerod = await monerojs.connectToDaemonRpc(TestConfig.monerod.url, TestConfig.monerod.username, TestConfig.monerod.password); monerod = await monerojs.connectToDaemonRpc(TestConfig.monerod.url, TestConfig.monerod.username, TestConfig.monerod.password);
await mineToHeight(160); // initialize blockchain to latest block type await mineToHeight(160); // initialize blockchain to latest block type
// start configured haveno daemons // start configured haveno daemons
const promises: Promise<HavenoClient>[] = []; const promises: Promise<HavenoClient>[] = [];
for (const config of TestConfig.startupHavenods) promises.push(initHaveno(config)); let err;
for (const settledPromise of await Promise.allSettled(promises)) { for (const config of TestConfig.startupHavenods) promises.push(initHaveno(config));
if (settledPromise.status !== "fulfilled") throw new Error((settledPromise as PromiseRejectedResult).reason); for (const settledPromise of await Promise.allSettled(promises)) {
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];
user1 = startupHavenods[1];
user2 = startupHavenods[2];
TestConfig.trade.arbitrator = arbitrator;
TestConfig.trade.maker = user1;
TestConfig.trade.taker = user2;
// connect client wallets
user1Wallet = await monerojs.connectToWalletRpc(TestConfig.startupHavenods[1].walletUrl, TestConfig.defaultHavenod.walletUsername, TestConfig.startupHavenods[1].accountPasswordRequired ? TestConfig.startupHavenods[1].accountPassword : TestConfig.defaultHavenod.walletDefaultPassword);
user2Wallet = await monerojs.connectToWalletRpc(TestConfig.startupHavenods[2].walletUrl, TestConfig.defaultHavenod.walletUsername, TestConfig.startupHavenods[2].accountPasswordRequired ? TestConfig.startupHavenods[2].accountPassword : TestConfig.defaultHavenod.walletDefaultPassword);
// register arbitrator dispute agent
await arbitrator.registerDisputeAgent("arbitrator", getArbitratorPrivKey(0));
// create test data directory if it doesn't exist
if (!fs.existsSync(TestConfig.testDataDir)) fs.mkdirSync(TestConfig.testDataDir);
} catch (err) {
await shutDown();
throw err;
} }
// assign arbitrator, user1, user2
arbitrator = startupHavenods[0];
user1 = startupHavenods[1];
user2 = startupHavenods[2];
TestConfig.trade.arbitrator = arbitrator;
TestConfig.trade.maker = user1;
TestConfig.trade.taker = user2;
// connect client wallets
user1Wallet = await monerojs.connectToWalletRpc(TestConfig.startupHavenods[1].walletUrl, TestConfig.defaultHavenod.walletUsername, TestConfig.startupHavenods[1].accountPasswordRequired ? TestConfig.startupHavenods[1].accountPassword : TestConfig.defaultHavenod.walletDefaultPassword);
user2Wallet = await monerojs.connectToWalletRpc(TestConfig.startupHavenods[2].walletUrl, TestConfig.defaultHavenod.walletUsername, TestConfig.startupHavenods[2].accountPasswordRequired ? TestConfig.startupHavenods[2].accountPassword : TestConfig.defaultHavenod.walletDefaultPassword);
// register arbitrator dispute agent
await arbitrator.registerDisputeAgent("arbitrator", getArbitratorPrivKey(0));
// create test data directory if it doesn't exist
if (!fs.existsSync(TestConfig.testDataDir)) fs.mkdirSync(TestConfig.testDataDir);
}); });
beforeEach(async () => { beforeEach(async () => {
@ -347,6 +354,10 @@ beforeEach(async () => {
}); });
afterAll(async () => { afterAll(async () => {
await shutDown();
});
async function shutDown() {
// release haveno processes // release haveno processes
const promises: Promise<void>[] = []; const promises: Promise<void>[] = [];
@ -357,7 +368,7 @@ afterAll(async () => {
// terminate monero-javascript worker // terminate monero-javascript worker
(await monerojs.LibraryUtils.getWorker()).terminate(); (await monerojs.LibraryUtils.getWorker()).terminate();
}); }
// ----------------------------------- TESTS ---------------------------------- // ----------------------------------- TESTS ----------------------------------
@ -2733,7 +2744,14 @@ async function initHaveno(ctx?: HavenodContext): Promise<HavenoClient> {
} }
// open account if configured // 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; return havenod;
async function getAvailablePort(): Promise<number> { async function getAvailablePort(): Promise<number> {