Skip to content

Commit

Permalink
feat(server): Add stop method (#3153)
Browse files Browse the repository at this point in the history
Fix #3149
  • Loading branch information
filipesilva authored and johnjbarton committed Oct 5, 2018
1 parent 873e4f9 commit 6d96d8e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
26 changes: 19 additions & 7 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ class Server extends KarmaEventEmitter {
}
})

this.on('stop', function (done) {
this.log.debug('Received stop event, exiting.')
return disconnectBrowsers().then(done)
})

if (config.singleRun) {
this.on('browser_restart_failure', (completedBrowser) => {
singleRunDoneBrowsers[completedBrowser.id] = true
Expand Down Expand Up @@ -327,14 +332,17 @@ class Server extends KarmaEventEmitter {
done(code || 0)
}

this.emitAsync('exit').then(() => {
socketServer.sockets.removeAllListeners()
socketServer.close()
const closeTimeout = setTimeout(removeAllListeners, webServerCloseTimeout)
return this.emitAsync('exit').then(() => {
return new Promise((resolve, reject) => {
socketServer.sockets.removeAllListeners()
socketServer.close()
const closeTimeout = setTimeout(removeAllListeners, webServerCloseTimeout)

webServer.close(() => {
clearTimeout(closeTimeout)
removeAllListeners()
webServer.close(() => {
clearTimeout(closeTimeout)
removeAllListeners()
resolve()
})
})
})
}
Expand Down Expand Up @@ -373,6 +381,10 @@ class Server extends KarmaEventEmitter {
child.unref()
}

stop () {
return this.emitAsync('stop')
}

static start (cliOptions, done) {
console.warn('Deprecated static method to be removed in v3.0')
return new Server(cliOptions, done).start()
Expand Down
19 changes: 19 additions & 0 deletions test/unit/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,25 @@ describe('server', () => {
})
})

describe('start on watch mode', () => {
var config
beforeEach(() => {
config = { port: 9876, listenAddress: '127.0.0.1', singleRun: false }
sinon.spy(BundleUtils, 'bundleResourceIfNotExist')
sinon.stub(NetUtils, 'bindAvailablePort').resolves(mockBoundServer)
sinon.stub(mockBoundServer, 'address').returns({ port: 9877 })
sinon
.stub(server, 'get')
.withArgs('config').returns(config)
})

it('should exit gracefully', (done) => {
server.start()
.then(() => server.stop())
.then(() => done())
})
})

// ============================================================================
// server._start()
// ============================================================================
Expand Down

0 comments on commit 6d96d8e

Please sign in to comment.