Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions packages/core/lib/utils/waitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const waitAndRetry = <T>(
function performCheck() {
if (maxRetryCount !== 0 && retryCount > maxRetryCount) {
resolve(predicateFn())

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

predicateFn() runs synchronously here, inside a setTimeout callback and outside the promise chain. If it throws, the exception is uncaught and the returned promise never settles. Earlier checks only reach reject because they run inside Promise.resolve().then().

Example: () => { if (!client) throw new Error('closed'); return client.ready } returns false on every retry and then throws on the final check. Node raises uncaughtException, Vitest reports an unhandled error, and the caller's await hangs. The new test only covers an async rejection, which goes through resolve(promise) and is handled.

return
}
Promise.resolve()
.then(() => {
Expand Down
57 changes: 57 additions & 0 deletions packages/core/test/utils/waitUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { setTimeout } from 'node:timers/promises'
import { describe, expect, it } from 'vitest'

import { waitAndRetry } from '../../lib/utils/waitUtils.ts'

describe('waitUtils', () => {
describe('waitAndRetry', () => {
it('resolves with the predicate result once it is truthy', async () => {
let calls = 0

const result = await waitAndRetry(() => {
calls++
return calls === 3 ? 'done' : undefined
}, 1)

expect(result).toBe('done')
expect(calls).toBe(3)
})

it('stops polling after the retries are exhausted', async () => {
let calls = 0

const result = await waitAndRetry(
() => {
calls++
return false
},
1,
3,
)

expect(result).toBe(false)
// 4 checks that count as retries, plus the final check whose result is returned
expect(calls).toBe(5)

await setTimeout(50)
expect(calls).toBe(5)
})

it('rejects when the final check after the retries are exhausted rejects', async () => {
let calls = 0

const promise = waitAndRetry(
() => {
calls++
return calls > 4 ? Promise.reject(new Error('unreachable')) : Promise.resolve(false)
},
1,
3,
)

await expect(promise).rejects.toThrow('unreachable')
await setTimeout(50)
expect(calls).toBe(5)
})
})
})