Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
wait until all events has been sent to the main process - refs webdri…
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Dec 7, 2016
1 parent 780bdfc commit 865480b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class MochaAdapter {
mochaOpts: {}
}, config)
this.runner = {}

this.sentMessages = 0 // number of messages sent to the parent
this.receivedMessages = 0 // number of messages received by the parent
}

options (options, context) {
Expand Down Expand Up @@ -104,6 +107,8 @@ class MochaAdapter {
this.runner.suite.afterAll(this.wrapHook('afterSuite'))
})
await executeHooksWithArgs(this.config.after, [result, this.capabilities, this.specs])
await this.waitUntilSettled()

return result
}

Expand Down Expand Up @@ -219,7 +224,8 @@ class MochaAdapter {
this.sendInternal(event, message)
}

this.send(message)
this.sentMessages++
this.send(message, null, {}, () => ++this.receivedMessages)
}

sendInternal (event, message) {
Expand All @@ -233,6 +239,19 @@ class MochaAdapter {
return process.send.apply(process, args)
}

/**
* wait until all messages were sent to parent
*/
waitUntilSettled () {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (this.sentMessages !== this.receivedMessages) return
clearInterval(interval)
resolve()
}, 100)
})
}

load (name, context) {
try {
module.context = context
Expand Down
19 changes: 19 additions & 0 deletions test/adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ describe('mocha adapter', () => {
})
})

it('should wait until all events were sent', () => {
const start = (new Date()).getTime()
let promise = adapter.run().then((failures) => {
const end = (new Date()).getTime();
(end - start).should.be.greaterThan(500)
failures.should.be.exactly(1234)
})
adapter.emit('foobar', {}, {})
adapter.emit('foobar2', {}, {})
process.nextTick(() => run.callArgWith(0, 1234))

setTimeout(() => {
send.args[0][3]()
send.args[1][3]()
}, 500)

return promise
})

after(() => {
Object.defineProperty(process, 'cwd', {
value: originalCWD
Expand Down

0 comments on commit 865480b

Please sign in to comment.