This commit is contained in:
Dmitry Vasilev
2023-05-18 23:15:10 +03:00
parent db3166154d
commit 932baf3eed
6 changed files with 35 additions and 65 deletions

View File

@@ -2882,7 +2882,7 @@ const y = x()`
test('async/await logs out of order timeout', async () => {
const i = await test_initial_state_async(`
const delay = async time => {
await new Promise(res => globalThis.setTimeout(res, time*10))
await new Promise(res => setTimeout(res, time*10))
console.log(time)
}
@@ -3091,10 +3091,8 @@ const y = x()`
}),
test('record io fetch rejects', async () => {
const original_fetch = globalThis.run_window.fetch
// Patch fetch
Object.assign(globalThis.run_window, {fetch: () => Promise.reject('fail')})
patch_builtin('fetch', () => Promise.reject('fail'))
const initial = await test_initial_state_async(`
await fetch('url', {method: 'GET'})
@@ -3102,7 +3100,7 @@ const y = x()`
assert_equal(root_calltree_node(initial).error, 'fail')
// Patch fetch again
Object.assign(globalThis.run_window, {fetch: async () => 'result'})
patch_builtin('fetch', () => async () => 'result')
const with_cache = await command_input_async(initial, `
await fetch('url', {method: 'GET'})
@@ -3110,7 +3108,7 @@ const y = x()`
assert_equal(root_calltree_node(initial).error, 'fail')
// Remove patch
Object.assign(globalThis.run_window, {fetch: original_fetch})
patch_builtin('fetch', null)
}),
test('record io preserve promise resolution order', async () => {

View File

@@ -12,36 +12,39 @@ Object.assign(globalThis,
}
)
export const original_setTimeout = globalThis.setTimeout
export const patch_builtin = new Function(`
// Substitute some builtin functions: fetch, setTimeout, Math.random to be
// able to patch them in tests
let originals = globalThis.run_window.__builtins_originals
let patched = globalThis.run_window.__builtins_patched
if(originals == null) {
globalThis.run_window.__original_setTimeout = globalThis.setTimeout
// This code can execute twice when tests are run in self-hosted mode.
// Ensure that patches will be applied only once
originals = globalThis.run_window.__builtins_originals = {}
patched = globalThis.run_window.__builtins_patched = {}
const originals = {
random: Math.random,
fetch: globalThis.fetch,
setTimeout: globalThis.setTimeout,
const patch = (obj, name) => {
originals[name] = obj[name]
obj[name] = (...args) => {
return patched[name] == null
? originals[name].apply(null, args)
: patched[name].apply(null, args)
}
}
// Substitute some builtin functions: fetch, setTimeout, Math.random to be
// able to patch them in tests
patch(globalThis.run_window, 'fetch')
patch(globalThis.run_window, 'setTimeout')
patch(globalThis.run_window.Math, 'random')
}
const patched = {}
const patch = (obj, name) => {
originals[name] = obj[name]
obj[name] = (...args) => patched[name] == null
? originals[name](...args)
: patched[name](...args)
}
patch(globalThis, 'fetch')
patch(globalThis, 'setTimeout')
patch(Math, 'random')
return (name, fn) => {
patched[name] = fn
}
`)()
export const original_setTimeout = globalThis.run_window.__original_setTimeout
export const parse_modules = (entry, modules) =>
load_modules(entry, module_name => modules[module_name])