Skip to content

Commit

Permalink
fix: Prevent "Connection closed" errors when deleting the session for…
Browse files Browse the repository at this point in the history
… @wdio/cucumber-framework (#116)

* fix: Prevent "Connection closed" errors when deleting the session for @wdio/cucumber-framework

* Update src/service.ts

Co-authored-by: Christian Bromann <[email protected]>

* Add comment and guard for Cucumber

---------

Co-authored-by: Christian Bromann <[email protected]>
  • Loading branch information
seanpoulter and christian-bromann authored Feb 27, 2024
1 parent f57cd0b commit efb6b1b
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export default class VSCodeWorkerService implements Services.ServiceInstance {
private _proxyOptions: VSCodeProxyOptions
private _vscodeOptions: VSCodeOptions
private _isWebSession = false
private _isCucumberSession = false
private _deletingSession = false

constructor (_: never, private _capabilities: VSCodeCapabilities) {
this._vscodeOptions = this._capabilities[VSCODE_CAPABILITY_KEY] || <VSCodeOptions>{}
Expand All @@ -56,13 +58,22 @@ export default class VSCodeWorkerService implements Services.ServiceInstance {
}

private _handleSocketClose (code: number, reason: Buffer) {
/*
* Prevent this block from running when deleting a session using the Cucumber framework.
* Otherwise the specs fail with the "Connection closed" error.
*/
if (this._isCucumberSession && this._deletingSession) {
return
}

const msg = `Connection closed. Code: ${code}, reason: ${reason.toString()}`
this._promisedSocket = Promise.reject(new Error(msg))
this._pendingMessages.forEach((resolver) => resolver(msg, null))
}

async beforeSession (option: Options.Testrunner, capabilities: VSCodeCapabilities) {
this._isWebSession = capabilities.browserName !== 'vscode'
this._isCucumberSession = option.framework === 'cucumber'

/**
* only run setup for VSCode capabilities
Expand Down Expand Up @@ -102,6 +113,7 @@ export default class VSCodeWorkerService implements Services.ServiceInstance {
userSettings[SETTINGS_KEY].port = port
log.info(`Start VSCode proxy server on port ${port}`)
const wss = this._wss = new WebSocketServer({ port })
this._deletingSession = false
this._promisedSocket = new Promise((resolve, reject) => {
const socketTimeout = setTimeout(
() => reject(new Error('Connection timeout exceeded')),
Expand Down Expand Up @@ -163,6 +175,12 @@ export default class VSCodeWorkerService implements Services.ServiceInstance {
log.info(`Start VSCode: ${binary} ${args.join(' ')}`)
}

beforeCommand (commandName: string): void {
if (commandName === 'deleteSession') {
this._deletingSession = true
}
}

async before (capabilities: VSCodeCapabilities, __: never, browser: WebdriverIO.Browser) {
/**
* only run setup for VSCode capabilities
Expand Down

0 comments on commit efb6b1b

Please sign in to comment.