Skip to content

Commit 11e3a9d

Browse files
ndcorneliusjohnjbarton
authored andcommitted
feat(config): Add config option for browser socket timeout (#3102)
Add a configuration option that allows modification of the default timeout value for the client socket connection. The previous hardcoded value of 2000 (ms) was insufficent for some environments. Closes #2927
1 parent 33ed285 commit 11e3a9d

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

client/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ module.exports = {
22
VERSION: '%KARMA_VERSION%',
33
KARMA_URL_ROOT: '%KARMA_URL_ROOT%',
44
KARMA_PROXY_PATH: '%KARMA_PROXY_PATH%',
5+
BROWSER_SOCKET_TIMEOUT: '%BROWSER_SOCKET_TIMEOUT%',
56
CONTEXT_URL: 'context.html'
67
}

client/main.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ var constants = require('./constants')
99

1010
var KARMA_URL_ROOT = constants.KARMA_URL_ROOT
1111
var KARMA_PROXY_PATH = constants.KARMA_PROXY_PATH
12+
var BROWSER_SOCKET_TIMEOUT = constants.BROWSER_SOCKET_TIMEOUT
1213

1314
// Connect to the server using socket.io http://socket.io
1415
var socket = io(location.host, {
1516
reconnectionDelay: 500,
1617
reconnectionDelayMax: Infinity,
17-
timeout: 20000,
18+
timeout: BROWSER_SOCKET_TIMEOUT,
1819
path: KARMA_PROXY_PATH + KARMA_URL_ROOT.substr(1) + 'socket.io',
1920
'sync disconnect on unload': true
2021
})

docs/config/01-configuration-file.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,29 @@ If set then the following fields will be defined and can be overriden:
793793
All of Karma's urls get prefixed with the `urlRoot`. This is helpful when using proxies, as
794794
sometimes you might want to proxy a url that is already taken by Karma.
795795

796+
## browserSocketTimeout
797+
**Type:** Number
798+
799+
**Default:** `20000`
800+
801+
**Description:** Timeout for the client socket connection (in ms).
802+
803+
This configuration represents the amount of time that the client will wait for the socket
804+
to connect.
805+
806+
When running a browser in different environments, it can take different amounts of time for the
807+
client socket to connect. If Karma cannot connect within the default timeout, you may see an
808+
error similar to the following:
809+
```
810+
ChromeHeadless have not captured in 60000ms, killing.
811+
Trying to start ChromeHeadless again (1/2).
812+
ChromeHeadless have not captured in 60000ms, killing.
813+
Trying to start ChromeHeadless again (2/2).
814+
ChromeHeadless have not captured in 60000ms, killing.
815+
ChromeHeadless failed 2 times(timeout). Giving up.
816+
```
817+
If you see this error, you can try increasing the socket connection timeout.
818+
796819

797820
[plugins]: plugins.html
798821
[config/files]: files.html

lib/config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ function normalizeConfig (config, configFilePath) {
229229
throw new TypeError('Invalid configuration: processKillTimeout option must be a number.')
230230
}
231231

232+
if (config.browserSocketTimeout && !helper.isNumber(config.browserSocketTimeout)) {
233+
throw new TypeError('Invalid configuration: browserSocketTimeout option must be a number.')
234+
}
235+
232236
const defaultClient = config.defaultClient || {}
233237
Object.keys(defaultClient).forEach(function (key) {
234238
const option = config.client[key]
@@ -350,6 +354,7 @@ class Config {
350354
this.retryLimit = 2
351355
this.detached = false
352356
this.crossOriginAttribute = true
357+
this.browserSocketTimeout = 20000
353358
}
354359

355360
set (newConfig) {

lib/middleware/karma.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ function createKarmaMiddleware (
7878
injector,
7979
basePath,
8080
urlRoot,
81-
upstreamProxy
81+
upstreamProxy,
82+
browserSocketTimeout
8283
) {
8384
var proxyPath = upstreamProxy ? upstreamProxy.path : '/'
8485
return function (request, response, next) {
@@ -129,6 +130,7 @@ function createKarmaMiddleware (
129130
return data.replace('%KARMA_URL_ROOT%', urlRoot)
130131
.replace('%KARMA_VERSION%', VERSION)
131132
.replace('%KARMA_PROXY_PATH%', proxyPath)
133+
.replace('%BROWSER_SOCKET_TIMEOUT%', browserSocketTimeout)
132134
})
133135
}
134136

@@ -260,7 +262,8 @@ createKarmaMiddleware.$inject = [
260262
'injector',
261263
'config.basePath',
262264
'config.urlRoot',
263-
'config.upstreamProxy'
265+
'config.upstreamProxy',
266+
'config.browserSocketTimeout'
264267
]
265268

266269
// PUBLIC API

0 commit comments

Comments
 (0)