-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
fix(client): prevent race condition in clear context #3425
Closed
nicojs
wants to merge
1
commit into
karma-runner:master
from
nicojs:fix/race-condition-in-clear-context
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,8 +89,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) { | ||
|
@@ -120,14 +120,33 @@ function Karma (socket, iframe, opener, navigator, location, document) { | |
} | ||
loadScript(0) | ||
} | ||
// run in iframe | ||
// run in iframe | ||
} else { | ||
iframe.src = policy.createURL(url) | ||
} | ||
} | ||
|
||
/** | ||
* Schedules te next execution after current context is cleared | ||
* (or is directly started if context is currently not being cleared) | ||
* @param execution {() => void} | ||
* @see https://github.com/karma-runner/karma/issues/3424 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add
to the PR description. |
||
*/ | ||
this.scheduleExecution = function (execution) { | ||
if (reloadingContext) { | ||
// A context reload is in progress. Wait for it to complete before executing. | ||
this.scheduledExecution = execution | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this one character member name difference is hard to read, could we use eg |
||
} else { | ||
execution() | ||
} | ||
} | ||
|
||
this.onbeforeunload = function () { | ||
if (!reloadingContext) { | ||
if (reloadingContext) { | ||
reloadingContext = false | ||
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!') | ||
} | ||
|
@@ -146,8 +165,6 @@ function Karma (socket, iframe, opener, navigator, location, document) { | |
this.stringify = stringify | ||
|
||
function clearContext () { | ||
reloadingContext = true | ||
|
||
navigateContextTo('about:blank') | ||
} | ||
|
||
|
@@ -234,6 +251,8 @@ function Karma (socket, iframe, opener, navigator, location, document) { | |
} | ||
|
||
if (self.config.clearContext) { | ||
reloadingContext = true | ||
|
||
// give the browser some time to breath, there could be a page reload, but because a bunch of | ||
// tests could run in the same event loop, we wouldn't notice. | ||
setTimeout(function () { | ||
|
@@ -259,24 +278,25 @@ 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' | ||
}) | ||
} | ||
self.scheduleExecution(() => { | ||
// reset startEmitted and reload the iframe | ||
startEmitted = false | ||
self.config = cfg | ||
|
||
// 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() | ||
} | ||
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() | ||
} | ||
}) | ||
}) | ||
socket.on('stop', function () { | ||
this.complete() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the