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

✅👷 kill browserstack execution early #3096

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions scripts/test/bs-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
// after killing it. There might be a better way of prematurely aborting the test command if we need
// to in the future.

const { spawnCommand, printLog, runMain, timeout } = require('../lib/executionUtils')
const spawn = require('child_process').spawn
const { printLog, runMain, timeout, printError } = require('../lib/executionUtils')
const { command } = require('../lib/command')
const { browserStackRequest } = require('../lib/bsUtils')

const AVAILABILITY_CHECK_DELAY = 30_000
const NO_OUTPUT_TIMEOUT = 5 * 60_000
const BS_BUILD_URL = 'https://api.browserstack.com/automate/builds.json?status=running'

runMain(async () => {
Expand All @@ -27,8 +29,8 @@ runMain(async () => {
return
}
await waitForAvailability()
const exitCode = await runTests()
process.exit(exitCode)
const isSuccess = await runTests()
process.exit(isSuccess ? 0 : 1)
})

async function waitForAvailability() {
Expand All @@ -43,6 +45,46 @@ async function hasRunningBuild() {
}

function runTests() {
const [command, ...args] = process.argv.slice(2)
return spawnCommand(command, args)
return new Promise((resolve) => {
const [command, ...args] = process.argv.slice(2)

const child = spawn(command, args, {
stdio: ['inherit', 'pipe', 'pipe'],
env: { ...process.env, FORCE_COLOR: true },
})

let output = ''
let timeoutId

child.stdout.pipe(process.stdout)
child.stdout.on('data', onOutput)

child.stderr.pipe(process.stderr)
child.stderr.on('data', onOutput)

child.on('exit', (code, signal) => {
resolve(!signal && code === 0)
})

function onOutput(data) {
output += data

clearTimeout(timeoutId)

if (hasUnrecoverableFailure(output)) {
killIt('unrecoverable failure')
} else {
timeoutId = setTimeout(() => killIt('no output timeout'), NO_OUTPUT_TIMEOUT)
}
}

function killIt(message) {
printError(`Killing the browserstack job because of ${message}`)
child.kill('SIGTERM')
}
})
}

function hasUnrecoverableFailure(stdout) {
return stdout.includes('is set to true but local testing through BrowserStack is not connected.')
}