Skip to content

Commit

Permalink
fix(client): prevent race condition in clear context
Browse files Browse the repository at this point in the history
  • Loading branch information
nicojs committed Feb 17, 2020
1 parent ad777f0 commit 4f47465
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions client/karma.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
var stringify = require('../common/stringify')
var constant = require('./constants')
var util = require('../common/util')
Expand Down Expand Up @@ -89,8 +90,8 @@ function Karma (socket, iframe, opener, navigator, location, document) {
childWindow.close()
}
childWindow = opener(url)
// run context on parent element (client_with_context)
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
// run context on parent element (client_with_context)
// using window.__karma__.scriptUrls to get the html element strings and load them dynamically
} else if (url !== 'about:blank') {
var loadScript = function (idx) {
if (idx < window.__karma__.scriptUrls.length) {
Expand Down Expand Up @@ -120,14 +121,25 @@ function Karma (socket, iframe, opener, navigator, location, document) {
}
loadScript(0)
}
// run in iframe
// run in iframe
} else {
iframe.src = policy.createURL(url)
}
}

this.scheduleExecution = function (execution) {
if (reloadingContext) {
this.scheduledExecution = execution
} else {
execution()
}
}

this.onbeforeunload = function () {
if (!reloadingContext) {
if (reloadingContext) {
self.scheduledExecution && self.scheduledExecution()
self.scheduledExecution = undefined
} else {
// TODO(vojta): show what test (with explanation about jasmine.UPDATE_INTERVAL)
self.error('Some of your tests did a full page reload!')
}
Expand Down Expand Up @@ -259,24 +271,29 @@ function Karma (socket, iframe, opener, navigator, location, document) {
}

socket.on('execute', function (cfg) {
// reset startEmitted and reload the iframe
startEmitted = false
self.config = cfg
// if not clearing context, reloadingContext always true to prevent beforeUnload error
reloadingContext = !self.config.clearContext
navigateContextTo(constant.CONTEXT_URL)

if (self.config.clientDisplayNone) {
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
el.style.display = 'none'
})
}
// clearing context can be in progress, make sure to schedule execution after it.
// See https://github.com/karma-runner/karma/issues/3424
self.scheduleExecution(() => {
// reset startEmitted and reload the iframe
startEmitted = false
self.config = cfg

// if not clearing context, reloadingContext always true to prevent beforeUnload error
reloadingContext = !self.config.clearContext
navigateContextTo(constant.CONTEXT_URL)

if (self.config.clientDisplayNone) {
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
el.style.display = 'none'
})
}

// clear the console before run
// works only on FF (Safari, Chrome do not allow to clear console from js source)
if (window.console && window.console.clear) {
window.console.clear()
}
// clear the console before run
// works only on FF (Safari, Chrome do not allow to clear console from js source)
if (window.console && window.console.clear) {
window.console.clear()
}
})
})
socket.on('stop', function () {
this.complete()
Expand Down

0 comments on commit 4f47465

Please sign in to comment.