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

Commit

Permalink
improve waitUntilSettled logic
Browse files Browse the repository at this point in the history
1) introduce a timeout so the process shuts down even tho it hasn't send all messages to the parent
2) don't make sentMessages bump dependent on whether or not the send method returned with true or false, Mocha sometimes sends to many events at once so the buffer "exceeds a threshold that makes it unwise to send more" (https://nodejs.org/api/child_process.html#child_process_child_send_message_sendhandle_options_callback). These messages didn't fail to get sent (see nodejs/node#7657 (comment))
  • Loading branch information
christian-bromann committed Dec 12, 2016
1 parent 4b0a806 commit e4000cf
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const EVENTS = {
}

const NOOP = function () {}
const SETTLE_TIMEOUT = 5000

/**
* Mocha runner
Expand Down Expand Up @@ -234,9 +235,8 @@ class MochaAdapter {
this.sendInternal(event, message)
}

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

generateUID (message) {
Expand Down Expand Up @@ -314,8 +314,11 @@ class MochaAdapter {
*/
waitUntilSettled () {
return new Promise((resolve) => {
const start = (new Date()).getTime()
const interval = setInterval(() => {
if (this.sentMessages !== this.receivedMessages) return
const now = (new Date()).getTime()

if (this.sentMessages !== this.receivedMessages && now - start < SETTLE_TIMEOUT) return
clearInterval(interval)
resolve()
}, 100)
Expand Down

0 comments on commit e4000cf

Please sign in to comment.