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

feat(async): frameworks can be loaded asynchronously #3297

Merged
merged 1 commit into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Server extends KarmaEventEmitter {
])
this._boundServer = await NetUtils.bindAvailablePort(config.port, config.listenAddress)
config.port = this._boundServer.address().port
this._injector.invoke(this._start, this)
await this._injector.invoke(this._start, this)
} catch (err) {
this.dieOnError(`Server start failed on port ${config.port}: ${err}`)
}
Expand All @@ -142,15 +142,17 @@ class Server extends KarmaEventEmitter {
return this._fileList ? this._fileList.changeFile(path) : Promise.resolve()
}

_start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
async _start (config, launcher, preprocess, fileList, capturedBrowsers, executor, done) {
if (config.detached) {
this._detach(config, done)
return
}

this._fileList = fileList

config.frameworks.forEach((framework) => this._injector.get('framework:' + framework))
await Promise.all(
config.frameworks.map((framework) => this._injector.get('framework:' + framework))
)

const webServer = this._injector.get('webServer')
const socketServer = this._injector.get('socketServer')
Expand Down
33 changes: 16 additions & 17 deletions test/unit/server.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ describe('server', () => {
server._boundServer = mockBoundServer
})

it('should start the web server after all files have been preprocessed successfully', () => {
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
it('should start the web server after all files have been preprocessed successfully', async () => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

expect(mockFileList.refresh).to.have.been.called
expect(fileListOnResolve).not.to.be.null
Expand All @@ -203,8 +203,8 @@ describe('server', () => {
expect(server._injector.invoke).to.have.been.calledWith(mockLauncher.launch, mockLauncher)
})

it('should start the web server after all files have been preprocessed with an error', () => {
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
it('should start the web server after all files have been preprocessed with an error', async () => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

expect(mockFileList.refresh).to.have.been.called
expect(fileListOnReject).not.to.be.null
Expand All @@ -217,8 +217,8 @@ describe('server', () => {
expect(server._injector.invoke).to.have.been.calledWith(mockLauncher.launch, mockLauncher)
})

it('should launch browsers after the web server has started', () => {
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
it('should launch browsers after the web server has started', async () => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

expect(mockWebServer.listen).not.to.have.been.called
expect(webServerOnError).not.to.be.null
Expand All @@ -229,8 +229,8 @@ describe('server', () => {
expect(server._injector.invoke).to.have.been.calledWith(mockLauncher.launch, mockLauncher)
})

it('should emit a listening event once server begin accepting connections', () => {
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
it('should emit a listening event once server begin accepting connections', async () => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

const listening = sinon.spy()
server.on('listening', listening)
Expand All @@ -241,8 +241,8 @@ describe('server', () => {
expect(listening).to.have.been.calledWith(9876)
})

it('should emit a browsers_ready event once all the browsers are captured', () => {
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
it('should emit a browsers_ready event once all the browsers are captured', async () => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

const browsersReady = sinon.spy()
server.on('browsers_ready', browsersReady)
Expand All @@ -256,8 +256,8 @@ describe('server', () => {
expect(browsersReady).to.have.been.called
})

it('should emit a browser_register event for each browser added', () => {
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
it('should emit a browser_register event for each browser added', async () => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

const browsersReady = sinon.spy()
server.on('browsers_ready', browsersReady)
Expand All @@ -271,12 +271,11 @@ describe('server', () => {
expect(browsersReady).to.have.been.called
})

it('should exit with error exit code on load errors', (done) => {
it('should exit with error exit code on load errors', async () => {
mockProcess(process)

server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, (exitCode) => {
expect(exitCode).to.have.equal(1)
done()
})

server.loadErrors.push(['TestError', 'Test'])
Expand All @@ -291,9 +290,9 @@ describe('server', () => {
describe('reconnecting browser', () => {
let mockBrowserSocket

beforeEach(() => {
beforeEach(async () => {
browserCollection = new BrowserCollection(server)
server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)
await server._start(mockConfig, mockLauncher, null, mockFileList, browserCollection, mockExecutor, doneSpy)

mockBrowserSocket = {
id: 'browser-socket-id',
Expand Down