sap-aicore: the token deadline test fires the exchange's deadline once fetch is entered (#984)
"token deadline bounds fetch that ignores abort" rewrote every 15000ms timer to 20ms. The credential read and the token exchange each arm their own 15000ms deadline through bounded(), so the read's deadline was cut to 20ms too. When the read took longer than that on a loaded machine, it timed out with the same "token acquisition timed out" message before fetch was ever called, and `upstream.aborted` threw on undefined. The test now keeps both timers at 15000ms, records each deadline's callback, and fires the exchange's (the second) from inside the fake fetch. It checks that two deadlines were armed and that fetch was entered before it checks the abort. The source is unchanged: a separate 15s bound on the read is intended. Run (wsl-run, magpie-desktop, Bun 1.4.2, HOME=$(mktemp -d)): - bun test packages/sap-aicore: 343 pass, 0 fail. - bun scripts/check.mjs: exit 0. Injected by hand: - A 50ms wait at the start of readCredentials' operation. The old test failed with "TypeError: undefined is not an object (evaluating 'upstream.aborted')" at 28ms, the issue's case. The new test passes with the same wait. - The exchange run without bounded() (no deadline). The new test fails. Not tried: the full root bun test run under load where it first failed.
This commit is contained in:
@@ -170,14 +170,27 @@ it("late OAuth refusal cannot expire an explicitly replaced slot", () => sandbox
|
||||
}))
|
||||
|
||||
it("token deadline bounds fetch that ignores abort", () => sandbox(async ({ file }) => {
|
||||
// The credential read and the token exchange each arm their own 15000ms deadline.
|
||||
// Fire the exchange's once fetch has been entered, not after a wall-clock wait: a
|
||||
// shortened timer also cut the read short on a busy machine, before fetch ran (#984).
|
||||
const original = setTimeout
|
||||
globalThis.setTimeout = (fn, ms, ...args) => original(fn, ms === 15000 ? 20 : ms, ...args)
|
||||
const deadlines = []
|
||||
globalThis.setTimeout = (fn, ms, ...args) => {
|
||||
if (ms === 15000) deadlines.push(fn)
|
||||
return original(fn, ms, ...args)
|
||||
}
|
||||
let upstream
|
||||
globalThis.fetch = async (_, init) => { upstream = init.signal; return new Promise(() => {}) }
|
||||
globalThis.fetch = async (_, init) => {
|
||||
upstream = init.signal
|
||||
assert.equal(deadlines.length, 2)
|
||||
queueMicrotask(deadlines.at(-1))
|
||||
return new Promise(() => {})
|
||||
}
|
||||
const plugin = await SAPAICorePlugin()
|
||||
const result = await plugin.auth.methods[0].authorize({ credentialFile: file })
|
||||
assert.equal(result.type, "failed")
|
||||
assert.match(result.error, /timed out/)
|
||||
assert.ok(upstream, "the deadline fired before fetch was called")
|
||||
assert.equal(upstream.aborted, true)
|
||||
}))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user