Skip to content

Commit

Permalink
chore: fix flaky structured-logging tests (#33664)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladar authored Oct 26, 2021
1 parent c1488d6 commit 5f38bd9
Showing 1 changed file with 40 additions and 18 deletions.
58 changes: 40 additions & 18 deletions integration-tests/structured-logging/__tests__/to-do.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ const collectEventsForDevelop = (events, env = {}) => {
},
})

let startedPromiseResolve = () => {}
const startedPromise = new Promise(resolve => {
startedPromiseResolve = resolve
})

const finishedPromise = new Promise((resolve, reject) => {
let listening = true

gatsbyProcess.on(`message`, msg => {
if (!listening) {
return
}
startedPromiseResolve()

events.push(msg)
// we are ready for tests
Expand All @@ -52,15 +58,15 @@ const collectEventsForDevelop = (events, env = {}) => {
setTimeout(() => {
listening = false
gatsbyProcess.kill()

setTimeout(resolve, 1000)
waitChildProcessExit(gatsbyProcess.pid, resolve, reject)
}, 5000)
}
})
})

return {
finishedPromise,
startedPromise,
gatsbyProcess,
}
}
Expand Down Expand Up @@ -305,20 +311,20 @@ describe(`develop`, () => {
let events = []

beforeAll(done => {
const { finishedPromise, gatsbyProcess } =
const { startedPromise, gatsbyProcess } =
collectEventsForDevelop(events)

setTimeout(() => {
gatsbyProcess.kill(`SIGTERM`)
startedPromise.then(() => {
setTimeout(() => {
done()
gatsbyProcess.kill(`SIGTERM`)
waitChildProcessExit(gatsbyProcess.pid, done, done.fail)
}, 5000)
}, 5000)

finishedPromise.then(done)
})
})

commonAssertionsForFailure(events)

// Note: this will fail on windows because it doesn't support POSIX signals (i.e. SIGTERM)
it(`emit final SET_STATUS with INTERRUPTED - last message`, () => {
const event = last(events)
expect(event).toHaveProperty(`action.type`, `SET_STATUS`)
Expand Down Expand Up @@ -365,7 +371,7 @@ describe(`develop`, () => {

afterAll(done => {
gatsbyProcess.kill()
setTimeout(done, 1000)
waitChildProcessExit(gatsbyProcess.pid, done, done.fail)
})

describe(`code change`, () => {
Expand Down Expand Up @@ -638,18 +644,19 @@ describe(`build`, () => {
},
})

await new Promise(resolve => {
await new Promise((resolve, reject) => {
let killing = false
gatsbyProcess.on(`message`, msg => {
events.push(msg)
})

gatsbyProcess.on(`exit`, exitCode => {
resolve()
if (!killing) {
killing = true
setTimeout(() => {
gatsbyProcess.kill(`SIGTERM`)
waitChildProcessExit(gatsbyProcess.pid, resolve, reject)
}, 2000)
}
})

setTimeout(() => {
gatsbyProcess.kill(`SIGTERM`)
}, 1000)
})
})
commonAssertionsForFailure(events)
Expand All @@ -662,4 +669,19 @@ describe(`build`, () => {
})
})

function waitChildProcessExit(pid, resolve, reject, attempt = 0) {
try {
process.kill(pid, 0) // check if process is still running
if (attempt > 15) {
reject(new Error("Gatsby process hasn't exited in 15 seconds"))
return
}
setTimeout(() => {
waitChildProcessExit(pid, resolve, reject, attempt + 1)
}, 1000)
} catch (e) {
resolve()
}
}

//TO-DO: add api running activity

0 comments on commit 5f38bd9

Please sign in to comment.