Skip to content

Commit 8997b74

Browse files
authoredDec 23, 2020
fix(test): clear up clearContext (#3597)
emit the 'complete' event after the navigation event, if any. The 'complete' event on the client triggers the server to begin shutdown. The shutdown can race with the navigate context. Simplify return_url implementation, assuming that we don't need any additional execution in the client after we send 'complete'.
1 parent fe0e24a commit 8997b74

File tree

4 files changed

+50
-70
lines changed

4 files changed

+50
-70
lines changed
 

‎client/karma.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,6 @@ function Karma (socket, iframe, opener, navigator, location, document) {
137137
}
138138
}
139139

140-
function clearContext () {
141-
navigateContextTo('about:blank')
142-
}
143-
144140
this.log = function (type, args) {
145141
var values = []
146142

@@ -234,15 +230,15 @@ function Karma (socket, iframe, opener, navigator, location, document) {
234230
socket.emit('result', resultsBuffer)
235231
resultsBuffer = []
236232
}
233+
// A test could have incorrectly issued a navigate. Wait one turn
234+
// to ensure the error from an incorrect navigate is processed.
235+
setTimeout(() => {
236+
if (this.config.clearContext) {
237+
navigateContextTo('about:blank')
238+
}
237239

238-
if (self.config.clearContext) {
239-
// A test could have incorrectly issued a navigate. To clear the context
240-
// we will navigate the iframe. Delay ours to ensure the error from an
241-
// incorrect navigate is processed.
242-
setTimeout(clearContext)
243-
}
240+
socket.emit('complete', result || {})
244241

245-
socket.emit('complete', result || {}, function () {
246242
if (returnUrl) {
247243
location.href = returnUrl
248244
}
@@ -260,26 +256,23 @@ function Karma (socket, iframe, opener, navigator, location, document) {
260256
}
261257

262258
socket.on('execute', function (cfg) {
263-
// Delay our navigation to the next event in case the clearContext has not completed.
264-
setTimeout(function allowClearContextToComplete () {
265-
// reset startEmitted and reload the iframe
266-
startEmitted = false
267-
self.config = cfg
268-
269-
navigateContextTo(constant.CONTEXT_URL)
270-
271-
if (self.config.clientDisplayNone) {
272-
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
273-
el.style.display = 'none'
274-
})
275-
}
259+
// reset startEmitted and reload the iframe
260+
startEmitted = false
261+
self.config = cfg
276262

277-
// clear the console before run
278-
// works only on FF (Safari, Chrome do not allow to clear console from js source)
279-
if (window.console && window.console.clear) {
280-
window.console.clear()
281-
}
282-
})
263+
navigateContextTo(constant.CONTEXT_URL)
264+
265+
if (self.config.clientDisplayNone) {
266+
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
267+
el.style.display = 'none'
268+
})
269+
}
270+
271+
// clear the console before run
272+
// works only on FF (Safari, Chrome do not allow to clear console from js source)
273+
if (window.console && window.console.clear) {
274+
window.console.clear()
275+
}
283276
})
284277
socket.on('stop', function () {
285278
this.complete()

‎lib/server.js

-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ class Server extends KarmaEventEmitter {
256256

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

259-
socket.on('complete', (data, ack) => ack())
260-
261259
socket.on('error', (err) => {
262260
this.log.debug('karma server socket error: ' + err)
263261
})

‎static/karma.js

+23-30
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,6 @@ function Karma (socket, iframe, opener, navigator, location, document) {
147147
}
148148
}
149149

150-
function clearContext () {
151-
navigateContextTo('about:blank')
152-
}
153-
154150
this.log = function (type, args) {
155151
var values = []
156152

@@ -244,15 +240,15 @@ function Karma (socket, iframe, opener, navigator, location, document) {
244240
socket.emit('result', resultsBuffer)
245241
resultsBuffer = []
246242
}
243+
// A test could have incorrectly issued a navigate. Wait one turn
244+
// to ensure the error from an incorrect navigate is processed.
245+
setTimeout(() => {
246+
if (this.config.clearContext) {
247+
navigateContextTo('about:blank')
248+
}
247249

248-
if (self.config.clearContext) {
249-
// A test could have incorrectly issued a navigate. To clear the context
250-
// we will navigate the iframe. Delay ours to ensure the error from an
251-
// incorrect navigate is processed.
252-
setTimeout(clearContext)
253-
}
250+
socket.emit('complete', result || {})
254251

255-
socket.emit('complete', result || {}, function () {
256252
if (returnUrl) {
257253
location.href = returnUrl
258254
}
@@ -270,26 +266,23 @@ function Karma (socket, iframe, opener, navigator, location, document) {
270266
}
271267

272268
socket.on('execute', function (cfg) {
273-
// Delay our navigation to the next event in case the clearContext has not completed.
274-
setTimeout(function allowClearContextToComplete () {
275-
// reset startEmitted and reload the iframe
276-
startEmitted = false
277-
self.config = cfg
278-
279-
navigateContextTo(constant.CONTEXT_URL)
280-
281-
if (self.config.clientDisplayNone) {
282-
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
283-
el.style.display = 'none'
284-
})
285-
}
269+
// reset startEmitted and reload the iframe
270+
startEmitted = false
271+
self.config = cfg
286272

287-
// clear the console before run
288-
// works only on FF (Safari, Chrome do not allow to clear console from js source)
289-
if (window.console && window.console.clear) {
290-
window.console.clear()
291-
}
292-
})
273+
navigateContextTo(constant.CONTEXT_URL)
274+
275+
if (self.config.clientDisplayNone) {
276+
[].forEach.call(document.querySelectorAll('#banner, #browsers'), function (el) {
277+
el.style.display = 'none'
278+
})
279+
}
280+
281+
// clear the console before run
282+
// works only on FF (Safari, Chrome do not allow to clear console from js source)
283+
if (window.console && window.console.clear) {
284+
window.console.clear()
285+
}
293286
})
294287
socket.on('stop', function () {
295288
this.complete()

‎test/client/karma.spec.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -437,24 +437,20 @@ describe('Karma', function () {
437437
it('should navigate the client to return_url if specified', function (done) {
438438
windowLocation.search = '?id=567&return_url=http://return.com'
439439
socket = new MockSocket()
440-
k = new ClientKarma(socket, {}, windowStub, windowNavigator, windowLocation)
440+
k = new ClientKarma(socket, iframe, windowStub, windowNavigator, windowLocation)
441441
clientWindow = { karma: k }
442442
ck = new ContextKarma(ContextKarma.getDirectCallParentKarmaMethod(clientWindow))
443443
ck.config = {}
444444

445445
sinon.spy(socket, 'disconnect')
446-
447-
socket.on('complete', function (data, ack) {
448-
ack()
449-
})
446+
clock.tick(500)
450447

451448
ck.complete()
452-
453-
clock.tick(500)
454-
setTimeout(function () {
449+
setTimeout(() => {
455450
assert(windowLocation.href === 'http://return.com')
456451
done()
457452
}, 5)
453+
458454
clock.tick(10)
459455
})
460456

0 commit comments

Comments
 (0)
Please sign in to comment.