-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
528ed85
commit c4ff62d
Showing
5 changed files
with
130 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
import { kill, lookup } from './ps.ts' | ||
|
||
export type * from './ps.ts' | ||
export { kill, lookup } from './ps.ts' | ||
export default { lookup, kill } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as assert from 'node:assert' | ||
import { describe, it, before, after } from 'node:test' | ||
import process from 'node:process' | ||
import * as cp from 'node:child_process' | ||
import * as path from 'node:path' | ||
import { kill, lookup } from '../../main/ts/ps.ts' | ||
|
||
const __dirname = new URL('.', import.meta.url).pathname | ||
const testScript = path.resolve(__dirname, '../legacy/node_process_for_test.cjs') | ||
const testScriptArgs = ['--foo', '--bar', Math.random().toString(16).slice(2)] | ||
|
||
describe('lookup()', () => { | ||
let pid: number | ||
before(() => { | ||
pid = cp.fork(testScript, testScriptArgs).pid as number | ||
}) | ||
|
||
after(() => { | ||
try { | ||
process.kill(pid) | ||
} catch (err) { void err } | ||
}) | ||
|
||
it('returns a process list', async () => { | ||
const list = await lookup() | ||
assert.ok(list.length > 0) | ||
}) | ||
|
||
it('searches process by pid', async () => { | ||
const list = await lookup({ pid }) | ||
const found = list[0] | ||
|
||
assert.equal(list.length, 1) | ||
assert.equal(found.pid, pid) | ||
}) | ||
|
||
it('filters by args', async () => { | ||
const list = await lookup({ arguments: testScriptArgs[2] }) | ||
|
||
assert.equal(list.length, 1) | ||
assert.equal(list[0].pid, pid) | ||
}) | ||
}) | ||
|
||
describe('kill()', () => { | ||
it('kills a process', async () => { | ||
const pid = cp.fork(testScript, testScriptArgs).pid as number | ||
assert.equal((await lookup({ pid })).length, 1) | ||
await kill(pid) | ||
assert.equal((await lookup({ pid })).length, 0) | ||
}) | ||
|
||
it('kills with check', async () => { | ||
let cheked = false | ||
const cb = () => cheked = true | ||
|
||
const pid = cp.fork(testScript, testScriptArgs).pid as number | ||
assert.equal((await lookup({ pid })).length, 1) | ||
|
||
await kill(pid, {timeout: 1}, cb) | ||
assert.equal((await lookup({ pid })).length, 0) | ||
assert.equal(cheked, true) | ||
}) | ||
}) |