Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot exit Karma server gracefully #3149

Closed
filipesilva opened this issue Sep 28, 2018 · 3 comments · Fixed by JetBrains/ring-ui#461, Narshe1412/nuarca-sql#25 or karronoli/redpen#10
Closed

Comments

@filipesilva
Copy link
Contributor

When the Karma server is run programmatically, there is no way to request it to stop and be informed of when that has happened:

const karma = require('karma');
const karmaServer = new karma.Server({ singleRun: false, configFile: __dirname + '/karma.conf.js' });
karmaServer.start();
// How to stop the server, and be informed when cleanup is finished?
// karma.stop(() => console.log('server exited gracefully'));

To reproduce, follow these steps:

git clone https://github.com/filipesilva/karma-exit-gracefully
cd karma-exit-gracefully
npm install
npm test

Having a way to gracefully stop Karma is useful when the Karma run is part of more complex orchestration, like in gulp or @angular/cli.

There is a way to stop the server by calling karma stop, but that will kill the process altogether:

log.info('Stopping server')
response.end('OK')
process.kill(process.pid, 'SIGINT')

There's some cleanup logic as well, but it only runs on --single-run:

karma/lib/server.js

Lines 289 to 292 in 6742ecf

this.on('run_complete', function (browsers, results) {
this.log.debug('Run complete, exiting.')
disconnectBrowsers(results.exitCode)
})

Related to #2867, ampproject/amphtml#14814

Environment Details

  • Karma version (output of karma --version): 3.0.0
  • Relevant part of your karma.config.js file: included in repro, but nothing relevant in the config really.
@johnjbarton
Copy link
Contributor

What is the purpose of stopping the server without exiting the process?

@filipesilva
Copy link
Contributor Author

@johnjbarton running Karma as part of a larger pipeline. In a gulp task, you might want to do something after the server is stopped.

Inside Angular CLI, we orchestrate build tasks as well. We cannot test karma plugin behavior on rebuilds because we are unable to gracefully stop karma.

A specific usecase is that we want to test that our karma setup works on rebuilds, recovers from compilation errors, and does not trigger tests after a compilation with no changes. We set it up in https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/build_angular/test/karma/rebuilds_spec_large.ts.

While we can test other toolchains this way, we cannot test karma because it would cause our test runner to exit.

@filipesilva
Copy link
Contributor Author

@johnjbarton I added a tentative feature of how this could work in #3153.

A user could call use it this way:

const karma = require('karma');
const karmaServer = new karma.Server({ singleRun: false, configFile: __dirname + '/karma.conf.js' });

karmaServer.start()
  // do things
  .then(() => karmaServer.stop())
  .then(() => console.log('server exited gracefully'))

WDYT?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants