Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Oct 12, 2023
1 parent 777706f commit edb1ecd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 65 deletions.
27 changes: 2 additions & 25 deletions packages/cli/src/cmds/runTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Environment } from "@moonwall/types";
import chalk from "chalk";
import path from "path";
import { fileURLToPath } from "url";
import type { UserConfig } from "vitest";
import { startVitest } from "vitest/node";
import { clearNodeLogs } from "../internal/cmdFunctions/tempLogs";
Expand Down Expand Up @@ -90,8 +89,7 @@ export async function executeTests(env: Environment, additionalArgs?: object) {
} satisfies UserConfig;

// TODO: Create options builder class
const optionsWithThreads = addThreadConfig(baseOptions, env.multiThreads);
const options = addGlobalsConfig(optionsWithThreads);
const options = addThreadConfig(baseOptions, env.multiThreads);

if (
globalConfig.environments.find((env) => env.name === process.env.MOON_TEST_ENV)?.foundation
Expand Down Expand Up @@ -130,7 +128,7 @@ function addThreadConfig(
},
};

if (threads == true || process.env.MOON_RECYCLE !== "true") {
if (threads == true && process.env.MOON_RECYCLE !== "true") {
configWithThreads.poolOptions.threads = {
isolate: false,
minThreads: 1,
Expand All @@ -149,26 +147,5 @@ function addThreadConfig(
configWithThreads.pool = Object.keys(threads)[0];
configWithThreads.poolOptions = Object.values(threads)[0];
}

return configWithThreads;
}
function addGlobalsConfig(config: UserConfig): UserConfig {
const configWithGlobals = {
...config,
};

if (process.env.MOON_RECYCLE !== "true") {
configWithGlobals.globals = true;
configWithGlobals.setupFiles = [path.resolve(getDirname(), "./internal/vitestSetup.js")];
}

return configWithGlobals;
}

function getDirname() {
try {
return __dirname;
} catch {
return path.dirname(fileURLToPath(import.meta.url));
}
}
19 changes: 0 additions & 19 deletions packages/cli/src/internal/singleThreadSetup.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/cli/src/internal/vitestSetup.ts

This file was deleted.

31 changes: 23 additions & 8 deletions packages/cli/src/lib/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Debug from "debug";
import fs from "fs";
import { ChildProcess, exec } from "node:child_process";
import readline from "readline";
import { setTimeout } from "timers/promises";
import { setTimeout as timer } from "timers/promises";
import { parseChopsticksRunCmd, parseRunCmd, parseZombieCmd } from "../internal/commandParsers";
import { checkZombieBins, getZombieConfig } from "../internal/foundations/zombieHelpers";
import { launchNode } from "../internal/localNode";
Expand Down Expand Up @@ -287,7 +287,7 @@ export class MoonwallContext {
await (provider.api as ApiPromise).rpc.chain.getBlock()
).block.header.number.toNumber() == 0
) {
await setTimeout(500);
await timer(500);
}
console.log(`✅ Chain ${provider.name} producing blocks, continuing`);
resolve("");
Expand Down Expand Up @@ -338,17 +338,32 @@ export class MoonwallContext {
public static async destroy() {
const ctx = MoonwallContext.getContext();
try {
ctx.disconnect();
await ctx.disconnect();
} catch {
console.log("🛑 All connections disconnected");
}
const promises = ctx.nodes.map((process) => {
return new Promise((resolve) => {
process.kill();
if (process.killed) {
resolve(`process ${process.pid} killed`);
}
return new Promise((resolve, reject) => {
process.kill(); // SIGTERM

// If the process hasn't exited after 5 seconds, we'll forcefully kill it
const timeout = 5000;
setTimeout(() => {
if (!process.killed) {
// If the process hasn't exited, forcefully kill it
process.kill("SIGKILL");
reject(`process ${process.pid} had to be forcefully killed`);
} else {
resolve(`process ${process.pid} killed gracefully`);
}
}, timeout);
});
// return new Promise((resolve) => {
// process.kill();
// if (process.killed) {
// resolve(`process ${process.pid} killed`);
// }
// });
});

await Promise.all(promises);
Expand Down
6 changes: 5 additions & 1 deletion packages/cli/src/lib/runnerContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ApiPromise } from "@polkadot/api";
import Bottleneck from "bottleneck";
import Debug from "debug";
import { Signer } from "ethers";
import { beforeAll, describe, it } from "vitest";
import { afterAll, beforeAll, describe, it } from "vitest";
import { Web3 } from "web3";
import { importAsyncConfig } from "./configReader";
import { MoonwallContext, contextCreator } from "./globalContext";
Expand Down Expand Up @@ -96,6 +96,10 @@ export function describeSuite<T extends FoundationType>({
}
});

afterAll(async () => {
await MoonwallContext.destroy();
});

const testCase = (params: ITestCase) => {
if (params.modifier) {
it[params.modifier](
Expand Down

0 comments on commit edb1ecd

Please sign in to comment.