Skip to content

Commit

Permalink
Log most recent error when OpenWhisk readiness times out (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
hparra committed Apr 9, 2024
1 parent 6dd1af3 commit 53aa50e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
12 changes: 7 additions & 5 deletions src/lib/app-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,24 +314,26 @@ async function downloadOWJar (url, outFile) {
}

/** @private */
async function waitForOpenWhiskReadiness (host, endTime, period, timeout, waitFunc) {
async function waitForOpenWhiskReadiness (host, endTime, period, timeout, lastStatus, waitFunc) {
if (Date.now() > endTime) {
throw new Error(`local openwhisk stack startup timed out: ${timeout}ms`)
throw new Error(`local openwhisk stack startup timed out after ${timeout}ms due to ${lastStatus}`)
}

let ok
let ok, status

try {
const fetch = createFetch()
const response = await fetch(host + '/api/v1')
ok = response.ok
status = response.statusText
} catch (e) {
ok = false
status = e.toString()
}

if (!ok) {
await waitFunc(period)
return waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc)
return waitForOpenWhiskReadiness(host, endTime, period, timeout, status, waitFunc)
}
}

Expand All @@ -349,7 +351,7 @@ async function runOpenWhiskJar (jarFile, runtimeConfigFile, apihost, waitInitTim

const endTime = Date.now() + timeout
await waitFor(waitInitTime)
await waitForOpenWhiskReadiness(apihost, endTime, waitPeriodTime, timeout, waitFor)
await waitForOpenWhiskReadiness(apihost, endTime, waitPeriodTime, timeout, null, waitFor)

// must wrap in an object as execa return value is awaitable
return { proc }
Expand Down
8 changes: 5 additions & 3 deletions test/commands/lib/app-helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,13 +531,14 @@ test('waitForOpenWhiskReadiness timeout', async () => {
const period = 5000
const timeout = 5000
const endTime = Date.now() - 1000 // ends now
const status = 'FAIL'

const waitFunc = jest.fn((_period) => {
expect(_period).toEqual(period)
})
const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc)
const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, status, waitFunc)

await expect(result).rejects.toEqual(new Error(`local openwhisk stack startup timed out: ${timeout}ms`))
await expect(result).rejects.toEqual(new Error(`local openwhisk stack startup timed out after ${timeout}ms due to ${status}`))
expect(mockFetch).toHaveBeenCalledTimes(0)
expect(waitFunc).toHaveBeenCalledTimes(0)
})
Expand All @@ -547,6 +548,7 @@ test('waitForOpenWhiskReadiness (fail, retry, then success)', async () => {
const period = 5000
const timeout = 5000
const endTime = Date.now() + 5000
const status = null

const waitFunc = jest.fn((_period) => {
expect(_period).toEqual(period)
Expand All @@ -555,7 +557,7 @@ test('waitForOpenWhiskReadiness (fail, retry, then success)', async () => {
.mockRejectedValueOnce(new Error('some error')) // first fail (fetch exception)
.mockRejectedValueOnce({ ok: false }) // second fail (response not ok)
.mockResolvedValue({ ok: true }) // finally success
const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, waitFunc)
const result = appHelper.waitForOpenWhiskReadiness(host, endTime, period, timeout, status, waitFunc)

await expect(result).resolves.not.toBeDefined()
expect(mockFetch).toHaveBeenCalledTimes(3)
Expand Down

0 comments on commit 53aa50e

Please sign in to comment.