Skip to content

Commit

Permalink
fix(middleware/runner): handle file list rejections
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Nov 21, 2019
1 parent adc6a66 commit 3edf9ce
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 106 deletions.
30 changes: 30 additions & 0 deletions lib/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Executor {
this.emitter = emitter

this.executionScheduled = false
this.errorsScheduled = []
this.pendingCount = 0
this.runningBrowsers = null

Expand Down Expand Up @@ -37,10 +38,39 @@ class Executor {
}
}

/**
* Schedule an error to be reported
* @param {string} errorMessage
* @returns {boolean} a boolean indicating whether or not the error was handled synchronously
*/
scheduleError (errorMessage) {
if (this.capturedBrowsers.areAllReady()) {
log.warn(errorMessage)
const errorResult = {
success: 0,
failed: 0,
skipped: 0,
error: errorMessage,
exitCode: 1
}
this.emitter.emit('run_start', []) // A run cannot complete without being started
this.emitter.emit('run_complete', [] /* No browsers ran actual tests */, errorResult)
return true
} else {
this.errorsScheduled.push(errorMessage)
return false
}
}

onRunComplete () {
if (this.executionScheduled) {
this.schedule()
}
if (this.errorsScheduled.length) {
const errorsToReport = this.errorsScheduled
this.errorsScheduled = []
errorsToReport.forEach((error) => this.scheduleError(error))
}
}

onBrowserComplete () {
Expand Down
67 changes: 38 additions & 29 deletions lib/middleware/runner.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
/**
* Runner middleware is responsible for communication with `karma run`.
*
Expand Down Expand Up @@ -35,19 +36,18 @@ function createRunnerMiddleware (emitter, fileList, capturedBrowsers, reporter,
}

const data = request.body
emitter.once('run_start', function () {
const responseWrite = response.write.bind(response)
responseWrite.colors = data.colors
reporter.addAdapter(responseWrite)

// clean up, close runner response
emitter.once('run_complete', function (browsers, results) {
reporter.removeAdapter(responseWrite)
const emptyTestSuite = (results.failed + results.success) === 0 ? 0 : 1
response.end(constant.EXIT_CODE + emptyTestSuite + results.exitCode)
})
updateClientArgs(data)
handleRun(data)
refreshFileList(data).then(() => {
executor.schedule()
}).catch((error) => {
const errorMessage = `Error during refresh file list. ${error.stack || error}`
executor.scheduleError(errorMessage)
})
})

function updateClientArgs (data) {
helper.restoreOriginalArgs(config)
if (_.isEmpty(data.args)) {
log.debug('Ignoring empty client.args from run command')
Expand All @@ -59,43 +59,52 @@ function createRunnerMiddleware (emitter, fileList, capturedBrowsers, reporter,
log.warn('Replacing client.args with ', data.args, ' as their types do not match.')
config.client.args = data.args
}
}

async function refreshFileList (data) {
let fullRefresh = true

if (helper.isArray(data.changedFiles)) {
data.changedFiles.forEach(function (filepath) {
fileList.changeFile(path.resolve(config.basePath, filepath))
await Promise.all(data.changedFiles.map(async function (filepath) {
await fileList.changeFile(path.resolve(config.basePath, filepath))
fullRefresh = false
})
}))
}

if (helper.isArray(data.addedFiles)) {
data.addedFiles.forEach(function (filepath) {
fileList.addFile(path.resolve(config.basePath, filepath))
await Promise.all(data.addedFiles.map(async function (filepath) {
await fileList.addFile(path.resolve(config.basePath, filepath))
fullRefresh = false
})
}))
}

if (helper.isArray(data.removedFiles)) {
data.removedFiles.forEach(function (filepath) {
fileList.removeFile(path.resolve(config.basePath, filepath))
await Promise.all(data.removedFiles.map(async function (filepath) {
await fileList.removeFile(path.resolve(config.basePath, filepath))
fullRefresh = false
})
}))
}

if (fullRefresh && data.refresh !== false) {
log.debug('Refreshing all the files / patterns')
fileList.refresh().then(function () {
// Wait for the file list refresh to complete before starting test run,
// otherwise the context.html generation might not see new/updated files.
if (!config.autoWatch) {
executor.schedule()
}
})
} else {
executor.schedule()
await fileList.refresh()
}
})
}

function handleRun (data) {
emitter.once('run_start', function () {
const responseWrite = response.write.bind(response)
responseWrite.colors = data.colors
reporter.addAdapter(responseWrite)

// clean up, close runner response
emitter.once('run_complete', function (_browsers, results) {
reporter.removeAdapter(responseWrite)
const emptyTestSuite = (results.failed + results.success) === 0 ? 0 : 1
response.end(constant.EXIT_CODE + emptyTestSuite + results.exitCode)
})
})
}
}
}

Expand Down
92 changes: 72 additions & 20 deletions test/unit/executor.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
'use strict'
// @ts-check

const Browser = require('../../lib/browser')
const BrowserCollection = require('../../lib/browser_collection')
const EventEmitter = require('../../lib/events').EventEmitter
const Executor = require('../../lib/executor')

const log = require('../../lib/logger').create()

describe('executor', () => {
let emitter
let capturedBrowsers
Expand All @@ -21,36 +24,85 @@ describe('executor', () => {
executor.socketIoSockets = new EventEmitter()

spy = {
onRunStart: () => null,
onSocketsExecute: () => null
onRunStart: sinon.stub(),
onSocketsExecute: sinon.stub(),
onRunComplete: sinon.stub()
}

sinon.spy(spy, 'onRunStart')
sinon.spy(spy, 'onSocketsExecute')
sinon.stub(log, 'warn')

emitter.on('run_start', spy.onRunStart)
emitter.on('run_complete', spy.onRunComplete)
executor.socketIoSockets.on('execute', spy.onSocketsExecute)
})

it('should start the run and pass client config', () => {
capturedBrowsers.areAllReady = () => true
describe('schedule', () => {
it('should start the run and pass client config', () => {
capturedBrowsers.areAllReady = () => true

executor.schedule()
expect(spy.onRunStart).to.have.been.called
expect(spy.onSocketsExecute).to.have.been.calledWith(config.client)
})

it('should wait for all browsers to finish', () => {
capturedBrowsers.areAllReady = () => false

executor.schedule()
expect(spy.onRunStart).to.have.been.called
expect(spy.onSocketsExecute).to.have.been.calledWith(config.client)
// they are not ready yet
executor.schedule()
expect(spy.onRunStart).not.to.have.been.called
expect(spy.onSocketsExecute).not.to.have.been.called

capturedBrowsers.areAllReady = () => true
emitter.emit('run_complete')
expect(spy.onRunStart).to.have.been.called
expect(spy.onSocketsExecute).to.have.been.called
})
})

it('should wait for all browsers to finish', () => {
capturedBrowsers.areAllReady = () => false
describe('scheduleError', () => {
it('should return `true` if scheduled synchronously', () => {
const result = executor.scheduleError('expected error')
expect(result).to.be.true
})

it('should emit both "run_start" and "run_complete"', () => {
executor.scheduleError('expected error')
expect(spy.onRunStart).to.have.been.called
expect(spy.onRunComplete).to.have.been.called
expect(spy.onRunStart).to.have.been.calledBefore(spy.onRunComplete)
})

it('should report the error', () => {
const expectedError = 'expected error'
executor.scheduleError(expectedError)
expect(spy.onRunComplete).to.have.been.calledWith([], {
success: 0,
failed: 0,
skipped: 0,
error: expectedError,
exitCode: 1
})
})

it('should wait for scheduled runs to end before reporting the error', () => {
// Arrange
let browsersAreReady = true
const expectedError = 'expected error'
capturedBrowsers.areAllReady = () => browsersAreReady
executor.schedule()
browsersAreReady = false

// they are not ready yet
executor.schedule()
expect(spy.onRunStart).not.to.have.been.called
expect(spy.onSocketsExecute).not.to.have.been.called
// Act
const result = executor.scheduleError(expectedError)
browsersAreReady = true

capturedBrowsers.areAllReady = () => true
emitter.emit('run_complete')
expect(spy.onRunStart).to.have.been.called
expect(spy.onSocketsExecute).to.have.been.called
// Assert
expect(result).to.be.false
expect(spy.onRunComplete).to.not.have.been.called
emitter.emit('run_complete')
expect(spy.onRunComplete).to.have.been.calledWith([], sinon.match({
error: expectedError
}))
})
})
})
Loading

0 comments on commit 3edf9ce

Please sign in to comment.