Skip to content

Commit

Permalink
feat: add killAll function (GoogleChrome#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
o0101 authored and patrickhulce committed Jan 14, 2020
1 parent b8c89f8 commit 83da1e4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ Returns an `Array<string>` of paths to available Chrome installations. When `chr

Note: This method performs synchronous I/O operations.

### `.killAll()`

Attempts to kill all Chrome instances created with [`.launch([opts])`](#launchopts). Returns a Promise that resolves to an array of errors that occurred while killing instances. If all instances were killed successfully, the array will be empty.

```js
const ChromeLauncher = require('chrome-launcher');

async function cleanup() {
await ChromeLauncher.killAll();
}
```

## Examples

#### Launching chrome:
Expand Down
24 changes: 17 additions & 7 deletions src/chrome-launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@ export interface ModuleOverrides {
}

const sigintListener = async () => {
for (const instance of instances) {
try {
await instance.kill();
} catch (err) {
}
}
await killAll();
process.exit(_SIGINT_EXIT_CODE);
};

Expand Down Expand Up @@ -90,6 +85,21 @@ async function launch(opts: Options = {}): Promise<LaunchedChrome> {
return {pid: instance.pid!, port: instance.port!, kill, process: instance.chrome!};
}

async function killAll(): Promise<Array<Error>> {
let errors = [];
for (const instance of instances) {
try {
await instance.kill();
// only delete if kill did not error
// this means erroring instances remain in the Set
instances.delete(instance);
} catch (err) {
errors.push(err);
}
}
return errors;
}

class Launcher {
private tmpDirandPidFileReady = false;
private pidFile: string;
Expand Down Expand Up @@ -373,4 +383,4 @@ class Launcher {
};

export default Launcher;
export {Launcher, launch};
export {Launcher, launch, killAll};
9 changes: 8 additions & 1 deletion test/chrome-launcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
'use strict';

import {Launcher, Options} from '../src/chrome-launcher';
import {Launcher, launch, killAll, Options} from '../src/chrome-launcher';
import {DEFAULT_FLAGS} from '../src/flags';

import {spy, stub} from 'sinon';
Expand Down Expand Up @@ -98,6 +98,13 @@ describe('Launcher', () => {
await chromeInstance.kill();
});

it('doesn\'t fail when killing all instances', async () => {
await launch();
await launch();
const errors = await killAll();
assert.strictEqual(errors.length, 0);
});

it('doesn\'t launch multiple chrome processes', async () => {
const chromeInstance = new Launcher();
await chromeInstance.launch();
Expand Down

0 comments on commit 83da1e4

Please sign in to comment.