From 0c11d3bd926744d82a2d4625853ba0053d5a0fcc Mon Sep 17 00:00:00 2001 From: nedsalk Date: Fri, 22 Nov 2024 14:28:04 +0100 Subject: [PATCH] fix test --- packages/fuels/test/features/dev-2.test.ts | 6 +-- .../fuels/test/utils/findChildProcessPid.ts | 25 --------- packages/fuels/test/utils/isProcessRunning.ts | 19 ------- packages/fuels/test/utils/processUtils.ts | 52 +++++++++++++++++++ 4 files changed, 55 insertions(+), 47 deletions(-) delete mode 100644 packages/fuels/test/utils/findChildProcessPid.ts delete mode 100644 packages/fuels/test/utils/isProcessRunning.ts create mode 100644 packages/fuels/test/utils/processUtils.ts diff --git a/packages/fuels/test/features/dev-2.test.ts b/packages/fuels/test/features/dev-2.test.ts index 51215d2b04a..ffbfdc4f780 100644 --- a/packages/fuels/test/features/dev-2.test.ts +++ b/packages/fuels/test/features/dev-2.test.ts @@ -4,8 +4,7 @@ import { tmpdir } from 'os'; import path from 'path'; import { deferPromise, randomUUID } from '../../src'; -import { findChildProcessPid } from '../utils/findChildProcessPid'; -import { isProcessRunning } from '../utils/isProcessRunning'; +import { findChildProcessPid, waitProcessEnd } from '../utils/processUtils'; function runInit() { const fuelsPath = path.join(process.cwd(), 'packages/fuels'); @@ -72,7 +71,8 @@ describe('dev', () => { await devExited.promise; - expect(isProcessRunning(fuelCorePid)).toBe(false); + // if it finishes before timeout, it means the process was killed successfully + await waitProcessEnd(fuelCorePid); }, { timeout: 15000 } ); diff --git a/packages/fuels/test/utils/findChildProcessPid.ts b/packages/fuels/test/utils/findChildProcessPid.ts deleted file mode 100644 index 69b35a1fdb1..00000000000 --- a/packages/fuels/test/utils/findChildProcessPid.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { execSync } from 'child_process'; - -export function findChildProcessPid( - parentPid: number, - childProcessName: string -): number | undefined { - const childProcesses = execSync(`ps --ppid ${parentPid} -o pid,cmd --no-headers || true`) - .toString() - .split('\n') - .map((s) => s.trim()) - .filter((s) => s !== ''); - - for (const cp of childProcesses) { - const [pid, name] = cp.split(' '); - if (name.indexOf(childProcessName) !== -1) { - return +pid; - } - const childPid = findChildProcessPid(+pid, childProcessName); - if (childPid) { - return childPid; - } - } - - return undefined; -} diff --git a/packages/fuels/test/utils/isProcessRunning.ts b/packages/fuels/test/utils/isProcessRunning.ts deleted file mode 100644 index eb7ec7be39c..00000000000 --- a/packages/fuels/test/utils/isProcessRunning.ts +++ /dev/null @@ -1,19 +0,0 @@ -export function isProcessRunning(pid: number) { - try { - // Check if the process exists - process.kill(pid, 0); - return true; // If no error, the process is running - } catch (e) { - const error = e as Error & { code: string }; - // Error codes: - // ESRCH: No such process - // EPERM: Permission denied (you don't have permissions to check) - if (error.code === 'ESRCH') { - return false; // No such process - } - if (error.code === 'EPERM') { - return true; // Process exists, but we don't have permission to send a signal - } - throw error; // Some other unexpected error - } -} diff --git a/packages/fuels/test/utils/processUtils.ts b/packages/fuels/test/utils/processUtils.ts new file mode 100644 index 00000000000..1ebc049a1bb --- /dev/null +++ b/packages/fuels/test/utils/processUtils.ts @@ -0,0 +1,52 @@ +import { sleep } from '@fuel-ts/utils'; +import { execSync } from 'child_process'; + +export function findChildProcessPid( + parentPid: number, + childProcessName: string +): number | undefined { + const childProcesses = execSync(`ps --ppid ${parentPid} -o pid,cmd --no-headers || true`) + .toString() + .split('\n') + .map((s) => s.trim()) + .filter((s) => s !== ''); + + for (const cp of childProcesses) { + const [pid, name] = cp.split(' '); + if (name.indexOf(childProcessName) !== -1) { + return +pid; + } + const childPid = findChildProcessPid(+pid, childProcessName); + if (childPid) { + return childPid; + } + } + + return undefined; +} + +function isProcessRunning(pid: number) { + try { + // Check if the process exists + process.kill(pid, 0); + return true; // If no error, the process is running + } catch (e) { + const error = e as Error & { code: string }; + // Error codes: + // ESRCH: No such process + // EPERM: Permission denied (you don't have permissions to check) + if (error.code === 'ESRCH') { + return false; // No such process + } + if (error.code === 'EPERM') { + return true; // Process exists, but we don't have permission to send a signal + } + throw error; // Some other unexpected error + } +} + +export async function waitProcessEnd(pid: number) { + while (isProcessRunning(pid)) { + await sleep(100); + } +}