Skip to content

Commit

Permalink
fix(test): clear up clearContext
Browse files Browse the repository at this point in the history
emit the complete event after the navigation events, if any.
The complete event on the client triggers the server to begin shutdown.
The shutdown can race with the navigate context.

Use navigateContetTo() for the returnUrl and fix up the test.
  • Loading branch information
johnjbarton committed Dec 21, 2020
1 parent 1a65bf1 commit 9ba9dec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 77 deletions.
56 changes: 23 additions & 33 deletions client/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ function Karma (socket, iframe, opener, navigator, location, document) {
}
}

function clearContext () {
navigateContextTo('about:blank')
}

this.log = function (type, args) {
var values = []

Expand Down Expand Up @@ -234,18 +230,15 @@ function Karma (socket, iframe, opener, navigator, location, document) {
socket.emit('result', resultsBuffer)
resultsBuffer = []
}

if (self.config.clearContext) {
// A test could have incorrectly issued a navigate. To clear the context
// we will navigate the iframe. Delay ours to ensure the error from an
// incorrect navigate is processed.
setTimeout(clearContext)
}

socket.emit('complete', result || {}, function () {
// A test could have incorrectly issued a navigate. Wait one turn
// to ensure the error from an incorrect navigate is processed.
setTimeout(() => {
if (returnUrl) {
location.href = returnUrl
navigateContextTo(returnUrl)
} else if (this.config.clearContext) {
navigateContextTo('about:blank')
}
socket.emit('complete', result || {})
})
}

Expand All @@ -260,26 +253,23 @@ function Karma (socket, iframe, opener, navigator, location, document) {
}

socket.on('execute', function (cfg) {
// Delay our navigation to the next event in case the clearContext has not completed.
setTimeout(function allowClearContextToComplete () {
// reset startEmitted and reload the iframe
startEmitted = false
self.config = cfg

navigateContextTo(constant.CONTEXT_URL)

if (self.config.clientDisplayNone) {
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
el.style.display = 'none'
})
}
// 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()
Expand Down
2 changes: 0 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ class Server extends KarmaEventEmitter {

const replySocketEvents = events.bufferEvents(socket, ['start', 'info', 'karma_error', 'result', 'complete'])

socket.on('complete', (data, ack) => ack())

socket.on('error', (err) => {
this.log.debug('karma server socket error: ' + err)
})
Expand Down
56 changes: 23 additions & 33 deletions static/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,6 @@ function Karma (socket, iframe, opener, navigator, location, document) {
}
}

function clearContext () {
navigateContextTo('about:blank')
}

this.log = function (type, args) {
var values = []

Expand Down Expand Up @@ -244,18 +240,15 @@ function Karma (socket, iframe, opener, navigator, location, document) {
socket.emit('result', resultsBuffer)
resultsBuffer = []
}

if (self.config.clearContext) {
// A test could have incorrectly issued a navigate. To clear the context
// we will navigate the iframe. Delay ours to ensure the error from an
// incorrect navigate is processed.
setTimeout(clearContext)
}

socket.emit('complete', result || {}, function () {
// A test could have incorrectly issued a navigate. Wait one turn
// to ensure the error from an incorrect navigate is processed.
setTimeout(() => {
if (returnUrl) {
location.href = returnUrl
navigateContextTo(returnUrl)
} else if (this.config.clearContext) {
navigateContextTo('about:blank')
}
socket.emit('complete', result || {})
})
}

Expand All @@ -270,26 +263,23 @@ function Karma (socket, iframe, opener, navigator, location, document) {
}

socket.on('execute', function (cfg) {
// Delay our navigation to the next event in case the clearContext has not completed.
setTimeout(function allowClearContextToComplete () {
// reset startEmitted and reload the iframe
startEmitted = false
self.config = cfg

navigateContextTo(constant.CONTEXT_URL)

if (self.config.clientDisplayNone) {
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
el.style.display = 'none'
})
}
// 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()
Expand Down
17 changes: 8 additions & 9 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,24 +437,23 @@ describe('Karma', function () {
it('should navigate the client to return_url if specified', function (done) {
windowLocation.search = '?id=567&return_url=http://return.com'
socket = new MockSocket()
k = new ClientKarma(socket, {}, windowStub, windowNavigator, windowLocation)
k = new ClientKarma(socket, iframe, windowStub, windowNavigator, windowLocation)
clientWindow = { karma: k }
ck = new ContextKarma(ContextKarma.getDirectCallParentKarmaMethod(clientWindow))
ck.config = {}

sinon.spy(socket, 'disconnect')
clock.tick(500)

socket.on('complete', function (data, ack) {
ack()
socket.on('complete', function (data) {
// Wait one turn to match the wait in the client
setTimeout(() => {
assert(iframe.src === 'http://return.com')
done()
})
})

ck.complete()

clock.tick(500)
setTimeout(function () {
assert(windowLocation.href === 'http://return.com')
done()
}, 5)
clock.tick(10)
})

Expand Down

0 comments on commit 9ba9dec

Please sign in to comment.