Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
nedsalk committed Nov 22, 2024
1 parent 05f3dec commit 0c11d3b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
6 changes: 3 additions & 3 deletions packages/fuels/test/features/dev-2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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 }
);
Expand Down
25 changes: 0 additions & 25 deletions packages/fuels/test/utils/findChildProcessPid.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/fuels/test/utils/isProcessRunning.ts

This file was deleted.

52 changes: 52 additions & 0 deletions packages/fuels/test/utils/processUtils.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 0c11d3b

Please sign in to comment.