Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: simplify stop port forward function #240

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 3 additions & 43 deletions src/core/k8.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -701,15 +701,15 @@ export class K8 {
* @param timeout the delay between checks in milliseconds
* @return {Promise<void>}
*/
async stopPortForward (server, maxAttempts = 20, timeout = 500) {
stopPortForward (server, maxAttempts = 20, timeout = 500) {
if (!server) {
return
return Promise.resolve()
}

this.logger.debug(`Stopping port-forwarder [${server.info}]`)

// try to close the websocket server
await new Promise((resolve, reject) => {
return new Promise((resolve, reject) => {
server.close((e) => {
if (e) {
if (e.message.contains('Server is not running')) {
Expand All @@ -725,46 +725,6 @@ export class K8 {
}
})
})

// test to see if the port has been closed or if it is still open
let attempts = 0
while (attempts < maxAttempts) {
let hasError = 0
attempts++

try {
const isPortOpen = await new Promise((resolve, reject) => {
const testServer = net.createServer()
.once('error', err => {
if (err) {
resolve(false)
}
})
.once('listening', () => {
testServer
.once('close', () => {
hasError++
if (hasError > 1) {
resolve(false)
} else {
resolve(true)
}
})
.close()
})
.listen(server.localPort, '0.0.0.0')
})
if (isPortOpen) {
return
}
} catch (e) {
return
}
await sleep(timeout)
}
if (attempts >= maxAttempts) {
throw new FullstackTestingError(`failed to stop port-forwarder [${server.info}]`)
}
}

async recyclePodByLabels (podLabels, maxAttempts = 50) {
Expand Down