Skip to content

Commit

Permalink
feat(config): Add config option for browser socket timeout (#3102)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ndcornelius authored and johnjbarton committed Aug 13, 2018
1 parent 33ed285 commit 11e3a9d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions client/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
VERSION: '%KARMA_VERSION%',
KARMA_URL_ROOT: '%KARMA_URL_ROOT%',
KARMA_PROXY_PATH: '%KARMA_PROXY_PATH%',
BROWSER_SOCKET_TIMEOUT: '%BROWSER_SOCKET_TIMEOUT%',
CONTEXT_URL: 'context.html'
}
3 changes: 2 additions & 1 deletion client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ var constants = require('./constants')

var KARMA_URL_ROOT = constants.KARMA_URL_ROOT
var KARMA_PROXY_PATH = constants.KARMA_PROXY_PATH
var BROWSER_SOCKET_TIMEOUT = constants.BROWSER_SOCKET_TIMEOUT

// Connect to the server using socket.io http://socket.io
var socket = io(location.host, {
reconnectionDelay: 500,
reconnectionDelayMax: Infinity,
timeout: 20000,
timeout: BROWSER_SOCKET_TIMEOUT,
path: KARMA_PROXY_PATH + KARMA_URL_ROOT.substr(1) + 'socket.io',
'sync disconnect on unload': true
})
Expand Down
23 changes: 23 additions & 0 deletions docs/config/01-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,29 @@ If set then the following fields will be defined and can be overriden:
All of Karma's urls get prefixed with the `urlRoot`. This is helpful when using proxies, as
sometimes you might want to proxy a url that is already taken by Karma.

## browserSocketTimeout
**Type:** Number

**Default:** `20000`

**Description:** Timeout for the client socket connection (in ms).

This configuration represents the amount of time that the client will wait for the socket
to connect.

When running a browser in different environments, it can take different amounts of time for the
client socket to connect. If Karma cannot connect within the default timeout, you may see an
error similar to the following:
```
ChromeHeadless have not captured in 60000ms, killing.
Trying to start ChromeHeadless again (1/2).
ChromeHeadless have not captured in 60000ms, killing.
Trying to start ChromeHeadless again (2/2).
ChromeHeadless have not captured in 60000ms, killing.
ChromeHeadless failed 2 times(timeout). Giving up.
```
If you see this error, you can try increasing the socket connection timeout.


[plugins]: plugins.html
[config/files]: files.html
Expand Down
5 changes: 5 additions & 0 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ function normalizeConfig (config, configFilePath) {
throw new TypeError('Invalid configuration: processKillTimeout option must be a number.')
}

if (config.browserSocketTimeout && !helper.isNumber(config.browserSocketTimeout)) {
throw new TypeError('Invalid configuration: browserSocketTimeout option must be a number.')
}

const defaultClient = config.defaultClient || {}
Object.keys(defaultClient).forEach(function (key) {
const option = config.client[key]
Expand Down Expand Up @@ -350,6 +354,7 @@ class Config {
this.retryLimit = 2
this.detached = false
this.crossOriginAttribute = true
this.browserSocketTimeout = 20000
}

set (newConfig) {
Expand Down
7 changes: 5 additions & 2 deletions lib/middleware/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ function createKarmaMiddleware (
injector,
basePath,
urlRoot,
upstreamProxy
upstreamProxy,
browserSocketTimeout
) {
var proxyPath = upstreamProxy ? upstreamProxy.path : '/'
return function (request, response, next) {
Expand Down Expand Up @@ -129,6 +130,7 @@ function createKarmaMiddleware (
return data.replace('%KARMA_URL_ROOT%', urlRoot)
.replace('%KARMA_VERSION%', VERSION)
.replace('%KARMA_PROXY_PATH%', proxyPath)
.replace('%BROWSER_SOCKET_TIMEOUT%', browserSocketTimeout)
})
}

Expand Down Expand Up @@ -260,7 +262,8 @@ createKarmaMiddleware.$inject = [
'injector',
'config.basePath',
'config.urlRoot',
'config.upstreamProxy'
'config.upstreamProxy',
'config.browserSocketTimeout'
]

// PUBLIC API
Expand Down

0 comments on commit 11e3a9d

Please sign in to comment.