2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-03-03 12:22:53 -05:00
|
|
|
import {
|
|
|
|
assert,
|
|
|
|
assertEquals,
|
2020-03-28 13:03:49 -04:00
|
|
|
assertNotEquals,
|
2021-04-11 19:40:42 -04:00
|
|
|
Deferred,
|
2020-11-26 11:22:36 -05:00
|
|
|
deferred,
|
2021-06-24 21:44:14 -04:00
|
|
|
delay,
|
2022-03-11 11:18:49 -05:00
|
|
|
execCode,
|
2021-12-07 07:39:58 -05:00
|
|
|
unreachable,
|
2020-03-03 12:22:53 -05:00
|
|
|
} from "./test_util.ts";
|
2018-09-16 16:35:16 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function functionParameterBindingSuccess() {
|
2021-01-06 08:53:30 -05:00
|
|
|
const promise = deferred();
|
|
|
|
let count = 0;
|
|
|
|
|
2021-08-05 07:08:58 -04:00
|
|
|
const nullProto = (newCount: number) => {
|
2021-01-06 08:53:30 -05:00
|
|
|
count = newCount;
|
|
|
|
promise.resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
Reflect.setPrototypeOf(nullProto, null);
|
|
|
|
|
|
|
|
setTimeout(nullProto, 500, 1);
|
|
|
|
await promise;
|
|
|
|
// count should be reassigned
|
|
|
|
assertEquals(count, 1);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function stringifyAndEvalNonFunctions() {
|
2021-01-06 08:53:30 -05:00
|
|
|
// eval can only access global scope
|
|
|
|
const global = globalThis as unknown as {
|
|
|
|
globalPromise: ReturnType<typeof deferred>;
|
|
|
|
globalCount: number;
|
|
|
|
};
|
|
|
|
global.globalPromise = deferred();
|
|
|
|
global.globalCount = 0;
|
|
|
|
|
|
|
|
const notAFunction =
|
|
|
|
"globalThis.globalCount++; globalThis.globalPromise.resolve();" as unknown as () =>
|
|
|
|
void;
|
|
|
|
|
|
|
|
setTimeout(notAFunction, 500);
|
|
|
|
|
|
|
|
await global.globalPromise;
|
|
|
|
|
|
|
|
// count should be incremented
|
|
|
|
assertEquals(global.globalCount, 1);
|
|
|
|
|
|
|
|
Reflect.deleteProperty(global, "globalPromise");
|
|
|
|
Reflect.deleteProperty(global, "globalCount");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutSuccess() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
setTimeout(() => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2018-09-16 16:35:16 -04:00
|
|
|
}, 500);
|
|
|
|
await promise;
|
|
|
|
// count should increment
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutEvalNoScopeLeak() {
|
2021-04-11 19:40:42 -04:00
|
|
|
// eval can only access global scope
|
|
|
|
const global = globalThis as unknown as {
|
|
|
|
globalPromise: Deferred<Error>;
|
|
|
|
};
|
|
|
|
global.globalPromise = deferred();
|
|
|
|
setTimeout(
|
|
|
|
`
|
|
|
|
try {
|
|
|
|
console.log(core);
|
|
|
|
globalThis.globalPromise.reject(new Error("Didn't throw."));
|
|
|
|
} catch (error) {
|
|
|
|
globalThis.globalPromise.resolve(error);
|
|
|
|
}` as unknown as () => void,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
const error = await global.globalPromise;
|
|
|
|
assertEquals(error.name, "ReferenceError");
|
|
|
|
Reflect.deleteProperty(global, "globalPromise");
|
|
|
|
});
|
|
|
|
|
2022-09-02 11:55:44 -04:00
|
|
|
Deno.test(async function evalPrimordial() {
|
|
|
|
const global = globalThis as unknown as {
|
|
|
|
globalPromise: ReturnType<typeof deferred>;
|
|
|
|
};
|
|
|
|
global.globalPromise = deferred();
|
|
|
|
const originalEval = globalThis.eval;
|
|
|
|
let wasCalled = false;
|
|
|
|
globalThis.eval = (argument) => {
|
|
|
|
wasCalled = true;
|
|
|
|
return originalEval(argument);
|
|
|
|
};
|
|
|
|
setTimeout(
|
|
|
|
"globalThis.globalPromise.resolve();" as unknown as () => void,
|
|
|
|
0,
|
|
|
|
);
|
|
|
|
await global.globalPromise;
|
|
|
|
assert(!wasCalled);
|
|
|
|
Reflect.deleteProperty(global, "globalPromise");
|
|
|
|
globalThis.eval = originalEval;
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutArgs() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-10-05 07:29:55 -04:00
|
|
|
const arg = 1;
|
2021-08-25 16:04:14 -04:00
|
|
|
let capturedArgs: unknown[] = [];
|
2018-10-08 02:58:44 -04:00
|
|
|
setTimeout(
|
2021-08-25 16:04:14 -04:00
|
|
|
function () {
|
|
|
|
capturedArgs = [...arguments];
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2018-10-08 02:58:44 -04:00
|
|
|
},
|
|
|
|
10,
|
|
|
|
arg,
|
|
|
|
arg.toString(),
|
2020-07-14 15:24:17 -04:00
|
|
|
[arg],
|
2018-10-08 02:58:44 -04:00
|
|
|
);
|
|
|
|
await promise;
|
2021-08-25 16:04:14 -04:00
|
|
|
assertEquals(capturedArgs, [
|
|
|
|
arg,
|
|
|
|
arg.toString(),
|
|
|
|
[arg],
|
|
|
|
]);
|
2018-09-05 01:35:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutCancelSuccess() {
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
const id = setTimeout(() => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2019-04-21 14:06:57 -04:00
|
|
|
}, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
// Cancelled, count should not increment
|
|
|
|
clearTimeout(id);
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(600);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 0);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutCancelMultiple() {
|
2019-03-09 12:30:38 -05:00
|
|
|
function uncalled(): never {
|
|
|
|
throw new Error("This function should not be called.");
|
|
|
|
}
|
|
|
|
|
2018-10-08 13:52:16 -04:00
|
|
|
// Set timers and cancel them in the same order.
|
|
|
|
const t1 = setTimeout(uncalled, 10);
|
|
|
|
const t2 = setTimeout(uncalled, 10);
|
|
|
|
const t3 = setTimeout(uncalled, 10);
|
|
|
|
clearTimeout(t1);
|
|
|
|
clearTimeout(t2);
|
|
|
|
clearTimeout(t3);
|
|
|
|
|
|
|
|
// Set timers and cancel them in reverse order.
|
|
|
|
const t4 = setTimeout(uncalled, 20);
|
|
|
|
const t5 = setTimeout(uncalled, 20);
|
|
|
|
const t6 = setTimeout(uncalled, 20);
|
|
|
|
clearTimeout(t6);
|
|
|
|
clearTimeout(t5);
|
|
|
|
clearTimeout(t4);
|
|
|
|
|
|
|
|
// Sleep until we're certain that the cancelled timers aren't gonna fire.
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(50);
|
2018-10-08 13:52:16 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutCancelInvalidSilentFail() {
|
2018-09-16 16:35:16 -04:00
|
|
|
// Expect no panic
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
const id = setTimeout(() => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
|
|
|
// Should have no effect
|
|
|
|
clearTimeout(id);
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2018-09-16 16:35:16 -04:00
|
|
|
}, 500);
|
|
|
|
await promise;
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
|
|
|
|
// Should silently fail (no panic)
|
|
|
|
clearTimeout(2147483647);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function intervalSuccess() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
const id = setInterval(() => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2019-04-21 14:06:57 -04:00
|
|
|
clearInterval(id);
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2019-04-21 14:06:57 -04:00
|
|
|
}, 100);
|
2018-09-16 16:35:16 -04:00
|
|
|
await promise;
|
|
|
|
// Clear interval
|
|
|
|
clearInterval(id);
|
|
|
|
// count should increment twice
|
2019-04-21 14:06:57 -04:00
|
|
|
assertEquals(count, 1);
|
2020-03-19 10:45:28 -04:00
|
|
|
// Similar false async leaking alarm.
|
|
|
|
// Force next round of polling.
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(0);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function intervalCancelSuccess() {
|
2018-09-16 16:35:16 -04:00
|
|
|
let count = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
const id = setInterval(() => {
|
2018-09-16 16:35:16 -04:00
|
|
|
count++;
|
2019-04-17 13:02:32 -04:00
|
|
|
}, 1);
|
2018-09-16 16:35:16 -04:00
|
|
|
clearInterval(id);
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(500);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 0);
|
2018-09-16 16:35:16 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function intervalOrdering() {
|
2020-02-19 15:36:18 -05:00
|
|
|
const timers: number[] = [];
|
2018-09-05 01:35:29 -04:00
|
|
|
let timeouts = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
function onTimeout() {
|
2018-09-05 01:35:29 -04:00
|
|
|
++timeouts;
|
|
|
|
for (let i = 1; i < timers.length; i++) {
|
|
|
|
clearTimeout(timers[i]);
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 12:30:38 -05:00
|
|
|
for (let i = 0; i < 10; i++) {
|
2019-04-17 13:02:32 -04:00
|
|
|
timers[i] = setTimeout(onTimeout, 1);
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(500);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(timeouts, 1);
|
2018-09-05 01:35:29 -04:00
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function intervalCancelInvalidSilentFail() {
|
2018-09-16 16:35:16 -04:00
|
|
|
// Should silently fail (no panic)
|
|
|
|
clearInterval(2147483647);
|
|
|
|
});
|
2019-01-26 16:10:38 -05:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
Deno.test(async function callbackTakesLongerThanInterval() {
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
let timeEndOfFirstCallback: number | undefined;
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
if (timeEndOfFirstCallback === undefined) {
|
|
|
|
// First callback
|
2022-06-13 15:28:00 -04:00
|
|
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 300);
|
2021-12-07 07:39:58 -05:00
|
|
|
timeEndOfFirstCallback = Date.now();
|
|
|
|
} else {
|
|
|
|
// Second callback
|
|
|
|
assert(Date.now() - 100 >= timeEndOfFirstCallback);
|
|
|
|
clearInterval(interval);
|
|
|
|
promise.resolve();
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11398
|
|
|
|
Deno.test(async function clearTimeoutAfterNextTimerIsDue1() {
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
promise.resolve();
|
|
|
|
}, 300);
|
|
|
|
|
|
|
|
const interval = setInterval(() => {
|
2022-06-13 15:28:00 -04:00
|
|
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 400);
|
2021-12-07 07:39:58 -05:00
|
|
|
// Both the interval and the timeout's due times are now in the past.
|
|
|
|
clearInterval(interval);
|
|
|
|
}, 100);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
|
|
|
// https://github.com/denoland/deno/issues/11398
|
|
|
|
Deno.test(async function clearTimeoutAfterNextTimerIsDue2() {
|
|
|
|
const promise = deferred();
|
|
|
|
|
|
|
|
const timeout1 = setTimeout(unreachable, 100);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
promise.resolve();
|
|
|
|
}, 200);
|
|
|
|
|
2022-06-13 15:28:00 -04:00
|
|
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 300);
|
2021-12-07 07:39:58 -05:00
|
|
|
// Both of the timeouts' due times are now in the past.
|
|
|
|
clearTimeout(timeout1);
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function fireCallbackImmediatelyWhenDelayOverMaxValue() {
|
2019-01-26 16:10:38 -05:00
|
|
|
let count = 0;
|
2021-08-05 07:08:58 -04:00
|
|
|
setTimeout(() => {
|
2019-01-26 16:10:38 -05:00
|
|
|
count++;
|
|
|
|
}, 2 ** 31);
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(1);
|
2019-03-06 20:48:46 -05:00
|
|
|
assertEquals(count, 1);
|
2019-01-26 16:10:38 -05:00
|
|
|
});
|
2019-06-11 03:50:36 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutCallbackThis() {
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2021-08-25 16:04:14 -04:00
|
|
|
let capturedThis: unknown;
|
2019-06-11 03:50:36 -04:00
|
|
|
const obj = {
|
2021-08-05 07:08:58 -04:00
|
|
|
foo() {
|
2021-08-25 16:04:14 -04:00
|
|
|
capturedThis = this;
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-06-11 03:50:36 -04:00
|
|
|
};
|
|
|
|
setTimeout(obj.foo, 1);
|
|
|
|
await promise;
|
2021-08-25 16:04:14 -04:00
|
|
|
assertEquals(capturedThis, window);
|
2019-06-11 03:50:36 -04:00
|
|
|
});
|
2019-06-13 11:08:27 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timeoutBindThis() {
|
2019-06-13 11:08:27 -04:00
|
|
|
const thisCheckPassed = [null, undefined, window, globalThis];
|
|
|
|
|
|
|
|
const thisCheckFailed = [
|
|
|
|
0,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
[],
|
|
|
|
"foo",
|
2021-08-05 07:08:58 -04:00
|
|
|
() => {},
|
2020-03-28 13:03:49 -04:00
|
|
|
Object.prototype,
|
2019-06-13 11:08:27 -04:00
|
|
|
];
|
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
for (const thisArg of thisCheckPassed) {
|
2020-11-26 11:22:36 -05:00
|
|
|
const resolvable = deferred();
|
2020-03-03 12:22:53 -05:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
setTimeout.call(thisArg, () => resolvable.resolve(), 1);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2019-06-13 11:08:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 12:22:53 -05:00
|
|
|
await resolvable;
|
|
|
|
assertEquals(hasThrown, 1);
|
|
|
|
}
|
2019-06-13 11:08:27 -04:00
|
|
|
|
2020-03-03 12:22:53 -05:00
|
|
|
for (const thisArg of thisCheckFailed) {
|
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
setTimeout.call(thisArg, () => {}, 1);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
2019-06-13 11:08:27 -04:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 12:22:53 -05:00
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
}
|
2019-06-13 11:08:27 -04:00
|
|
|
});
|
2019-06-17 13:42:20 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function clearTimeoutShouldConvertToNumber() {
|
2019-06-17 13:42:20 -04:00
|
|
|
let called = false;
|
|
|
|
const obj = {
|
|
|
|
valueOf(): number {
|
|
|
|
called = true;
|
|
|
|
return 1;
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2019-06-17 13:42:20 -04:00
|
|
|
};
|
|
|
|
clearTimeout((obj as unknown) as number);
|
|
|
|
assert(called);
|
|
|
|
});
|
2019-06-18 09:24:20 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function setTimeoutShouldThrowWithBigint() {
|
2019-08-29 10:57:09 -04:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
2021-08-05 07:08:58 -04:00
|
|
|
setTimeout(() => {}, (1n as unknown) as number);
|
2019-08-29 10:57:09 -04:00
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function clearTimeoutShouldThrowWithBigint() {
|
2019-08-30 11:51:53 -04:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
clearTimeout((1n as unknown) as number);
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof TypeError) {
|
|
|
|
hasThrown = 2;
|
|
|
|
} else {
|
|
|
|
hasThrown = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 2);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function testFunctionName() {
|
2019-06-18 09:24:20 -04:00
|
|
|
assertEquals(clearTimeout.name, "clearTimeout");
|
|
|
|
assertEquals(clearInterval.name, "clearInterval");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function testFunctionParamsLength() {
|
2019-07-18 06:09:32 -04:00
|
|
|
assertEquals(setTimeout.length, 1);
|
|
|
|
assertEquals(setInterval.length, 1);
|
|
|
|
assertEquals(clearTimeout.length, 0);
|
|
|
|
assertEquals(clearInterval.length, 0);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function clearTimeoutAndClearIntervalNotBeEquals() {
|
2019-06-18 09:24:20 -04:00
|
|
|
assertNotEquals(clearTimeout, clearInterval);
|
|
|
|
});
|
2019-10-19 17:09:24 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timerMaxCpuBug() {
|
2019-10-19 17:09:24 -04:00
|
|
|
// There was a bug where clearing a timeout would cause Deno to use 100% CPU.
|
|
|
|
clearTimeout(setTimeout(() => {}, 1000));
|
|
|
|
// We can check this by counting how many ops have triggered in the interim.
|
|
|
|
// Certainly less than 10 ops should have been dispatched in next 100 ms.
|
2022-02-16 13:53:17 -05:00
|
|
|
const { ops: pre } = Deno.metrics();
|
2021-06-24 21:44:14 -04:00
|
|
|
await delay(100);
|
2022-02-16 13:53:17 -05:00
|
|
|
const { ops: post } = Deno.metrics();
|
|
|
|
const before = pre.op_sleep.opsDispatched;
|
|
|
|
const after = post.op_sleep.opsDispatched;
|
|
|
|
assert(after - before < 10);
|
2019-10-19 17:09:24 -04:00
|
|
|
});
|
2019-12-03 22:19:03 -05:00
|
|
|
|
2021-12-07 07:39:58 -05:00
|
|
|
Deno.test(async function timerOrdering() {
|
|
|
|
const array: number[] = [];
|
|
|
|
const donePromise = deferred();
|
|
|
|
|
|
|
|
function push(n: number) {
|
|
|
|
array.push(n);
|
|
|
|
if (array.length === 6) {
|
|
|
|
donePromise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
push(1);
|
|
|
|
setTimeout(() => push(4));
|
|
|
|
}, 0);
|
|
|
|
setTimeout(() => {
|
|
|
|
push(2);
|
|
|
|
setTimeout(() => push(5));
|
|
|
|
}, 0);
|
|
|
|
setTimeout(() => {
|
|
|
|
push(3);
|
|
|
|
setTimeout(() => push(6));
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
await donePromise;
|
|
|
|
|
|
|
|
assertEquals(array, [1, 2, 3, 4, 5, 6]);
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timerBasicMicrotaskOrdering() {
|
2019-12-03 22:19:03 -05:00
|
|
|
let s = "";
|
|
|
|
let count = 0;
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2019-12-03 22:19:03 -05:00
|
|
|
setTimeout(() => {
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
count++;
|
|
|
|
s += "de";
|
|
|
|
if (count === 2) {
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2019-12-03 22:19:03 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
setTimeout(() => {
|
|
|
|
count++;
|
|
|
|
s += "no";
|
|
|
|
if (count === 2) {
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2019-12-03 22:19:03 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
await promise;
|
|
|
|
assertEquals(s, "deno");
|
|
|
|
});
|
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timerNestedMicrotaskOrdering() {
|
2019-12-03 22:19:03 -05:00
|
|
|
let s = "";
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2019-12-03 22:19:03 -05:00
|
|
|
s += "0";
|
|
|
|
setTimeout(() => {
|
|
|
|
s += "4";
|
2020-03-19 10:45:28 -04:00
|
|
|
setTimeout(() => (s += "A"));
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
s += "B";
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.resolve();
|
2020-03-19 10:45:28 -04:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
s += "5";
|
2019-12-03 22:19:03 -05:00
|
|
|
});
|
|
|
|
});
|
2020-03-19 10:45:28 -04:00
|
|
|
setTimeout(() => (s += "6"));
|
2019-12-03 22:19:03 -05:00
|
|
|
Promise.resolve().then(() => (s += "2"));
|
|
|
|
Promise.resolve().then(() =>
|
|
|
|
setTimeout(() => {
|
2020-03-19 10:45:28 -04:00
|
|
|
s += "7";
|
|
|
|
Promise.resolve()
|
|
|
|
.then(() => (s += "8"))
|
|
|
|
.then(() => {
|
|
|
|
s += "9";
|
|
|
|
});
|
2019-12-03 22:19:03 -05:00
|
|
|
})
|
|
|
|
);
|
|
|
|
Promise.resolve().then(() => Promise.resolve().then(() => (s += "3")));
|
|
|
|
s += "1";
|
|
|
|
await promise;
|
2020-03-19 10:45:28 -04:00
|
|
|
assertEquals(s, "0123456789AB");
|
2019-12-03 22:19:03 -05:00
|
|
|
});
|
2020-03-24 13:39:41 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(function testQueueMicrotask() {
|
2020-03-24 13:39:41 -04:00
|
|
|
assertEquals(typeof queueMicrotask, "function");
|
|
|
|
});
|
2020-07-04 15:16:27 -04:00
|
|
|
|
2021-11-23 11:45:18 -05:00
|
|
|
Deno.test(async function timerIgnoresDateOverride() {
|
2020-07-04 15:16:27 -04:00
|
|
|
const OriginalDate = Date;
|
2020-11-26 11:22:36 -05:00
|
|
|
const promise = deferred();
|
2020-07-04 15:16:27 -04:00
|
|
|
let hasThrown = 0;
|
|
|
|
try {
|
|
|
|
const overrideCalled: () => number = () => {
|
2020-09-25 12:34:20 -04:00
|
|
|
promise.reject("global Date override used over original Date object");
|
2020-07-04 15:16:27 -04:00
|
|
|
return 0;
|
|
|
|
};
|
2021-08-05 07:08:58 -04:00
|
|
|
const DateOverride = () => {
|
2020-07-04 15:16:27 -04:00
|
|
|
overrideCalled();
|
2020-08-31 14:11:17 -04:00
|
|
|
};
|
2020-07-04 15:16:27 -04:00
|
|
|
globalThis.Date = DateOverride as DateConstructor;
|
|
|
|
globalThis.Date.now = overrideCalled;
|
|
|
|
globalThis.Date.UTC = overrideCalled;
|
|
|
|
globalThis.Date.parse = overrideCalled;
|
2020-09-25 12:34:20 -04:00
|
|
|
queueMicrotask(promise.resolve);
|
2020-07-04 15:16:27 -04:00
|
|
|
await promise;
|
|
|
|
hasThrown = 1;
|
|
|
|
} catch (err) {
|
|
|
|
if (typeof err === "string") {
|
|
|
|
assertEquals(err, "global Date override used over original Date object");
|
|
|
|
hasThrown = 2;
|
|
|
|
} else if (err instanceof TypeError) {
|
|
|
|
hasThrown = 3;
|
|
|
|
} else {
|
|
|
|
hasThrown = 4;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
globalThis.Date = OriginalDate;
|
|
|
|
}
|
|
|
|
assertEquals(hasThrown, 1);
|
|
|
|
});
|
2020-10-15 21:06:31 -04:00
|
|
|
|
2021-12-09 03:00:55 -05:00
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2021-12-09 03:00:55 -05:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
2023-05-22 08:09:31 -04:00
|
|
|
const timer = setTimeout(() => console.log("1"), 1);
|
2021-12-09 03:00:55 -05:00
|
|
|
Deno.unrefTimer(timer);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer - mix ref and unref 1",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2021-12-09 03:00:55 -05:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const timer1 = setTimeout(() => console.log("1"), 200);
|
|
|
|
const timer2 = setTimeout(() => console.log("2"), 400);
|
|
|
|
const timer3 = setTimeout(() => console.log("3"), 600);
|
|
|
|
Deno.unrefTimer(timer3);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "1\n2\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer - mix ref and unref 2",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2021-12-09 03:00:55 -05:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const timer1 = setTimeout(() => console.log("1"), 200);
|
|
|
|
const timer2 = setTimeout(() => console.log("2"), 400);
|
|
|
|
const timer3 = setTimeout(() => console.log("3"), 600);
|
|
|
|
Deno.unrefTimer(timer1);
|
|
|
|
Deno.unrefTimer(timer2);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "1\n2\n3\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer - unref interval",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2021-12-09 03:00:55 -05:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
let i = 0;
|
|
|
|
const timer1 = setInterval(() => {
|
|
|
|
console.log("1");
|
|
|
|
i++;
|
|
|
|
if (i === 5) {
|
|
|
|
Deno.unrefTimer(timer1);
|
|
|
|
}
|
|
|
|
}, 10);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "1\n1\n1\n1\n1\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer - unref then ref 1",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2021-12-09 03:00:55 -05:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const timer1 = setTimeout(() => console.log("1"), 10);
|
|
|
|
Deno.unrefTimer(timer1);
|
|
|
|
Deno.refTimer(timer1);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "1\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer - unref then ref",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2021-12-09 03:00:55 -05:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const timer1 = setTimeout(() => {
|
|
|
|
console.log("1");
|
|
|
|
Deno.refTimer(timer2);
|
|
|
|
}, 10);
|
|
|
|
const timer2 = setTimeout(() => console.log("2"), 20);
|
|
|
|
Deno.unrefTimer(timer2);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "1\n2\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "unrefTimer - invalid calls do nothing",
|
|
|
|
fn: () => {
|
|
|
|
Deno.unrefTimer(NaN);
|
|
|
|
Deno.refTimer(NaN);
|
|
|
|
},
|
|
|
|
});
|
2022-03-14 15:19:22 -04:00
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "AbortSignal.timeout() with no listeners",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2022-03-14 15:19:22 -04:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const signal = AbortSignal.timeout(2000);
|
|
|
|
|
|
|
|
// This unref timer expires before the signal, and if it does expire, then
|
|
|
|
// it means the signal has kept the event loop alive.
|
|
|
|
const timer = setTimeout(() => console.log("Unexpected!"), 1500);
|
|
|
|
Deno.unrefTimer(timer);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "AbortSignal.timeout() with listeners",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2022-03-14 15:19:22 -04:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const signal = AbortSignal.timeout(1000);
|
|
|
|
signal.addEventListener("abort", () => console.log("Event fired!"));
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "Event fired!\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "AbortSignal.timeout() with removed listeners",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2022-03-14 15:19:22 -04:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const signal = AbortSignal.timeout(2000);
|
|
|
|
|
|
|
|
const callback = () => console.log("Unexpected: Event fired");
|
|
|
|
signal.addEventListener("abort", callback);
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
console.log("Removing the listener.");
|
|
|
|
signal.removeEventListener("abort", callback);
|
|
|
|
}, 500);
|
|
|
|
|
|
|
|
Deno.unrefTimer(
|
|
|
|
setTimeout(() => console.log("Unexpected: Unref timer"), 1500)
|
|
|
|
);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "Removing the listener.\n");
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.test({
|
|
|
|
name: "AbortSignal.timeout() with listener for a non-abort event",
|
2022-03-20 17:46:39 -04:00
|
|
|
permissions: { run: true, read: true },
|
2022-03-14 15:19:22 -04:00
|
|
|
fn: async () => {
|
|
|
|
const [statusCode, output] = await execCode(`
|
|
|
|
const signal = AbortSignal.timeout(2000);
|
|
|
|
|
|
|
|
signal.addEventListener("someOtherEvent", () => {
|
|
|
|
console.log("Unexpected: someOtherEvent called");
|
|
|
|
});
|
|
|
|
|
|
|
|
Deno.unrefTimer(
|
|
|
|
setTimeout(() => console.log("Unexpected: Unref timer"), 1500)
|
|
|
|
);
|
|
|
|
`);
|
|
|
|
assertEquals(statusCode, 0);
|
|
|
|
assertEquals(output, "");
|
|
|
|
},
|
|
|
|
});
|
2023-08-10 00:01:35 -04:00
|
|
|
|
|
|
|
// Regression test for https://github.com/denoland/deno/issues/19866
|
|
|
|
Deno.test({
|
|
|
|
name: "regression for #19866",
|
|
|
|
fn: async () => {
|
|
|
|
const timeoutsFired = [];
|
|
|
|
|
|
|
|
// deno-lint-ignore require-await
|
|
|
|
async function start(n: number) {
|
|
|
|
let i = 0;
|
|
|
|
const intervalId = setInterval(() => {
|
|
|
|
i++;
|
|
|
|
if (i > 2) {
|
|
|
|
clearInterval(intervalId!);
|
|
|
|
}
|
|
|
|
timeoutsFired.push(n);
|
|
|
|
}, 20);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let n = 0; n < 100; n++) {
|
|
|
|
start(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3s should be plenty of time for all the intervals to fire
|
|
|
|
// but it might still be flaky on CI.
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 3000));
|
|
|
|
assertEquals(timeoutsFired.length, 300);
|
|
|
|
},
|
|
|
|
});
|
2023-09-04 17:05:06 -04:00
|
|
|
|
|
|
|
// Regression test for https://github.com/denoland/deno/issues/20367
|
|
|
|
Deno.test({
|
|
|
|
name: "regression for #20367",
|
|
|
|
fn: async () => {
|
|
|
|
const promise = deferred<number>();
|
|
|
|
const start = performance.now();
|
|
|
|
setTimeout(() => {
|
|
|
|
const end = performance.now();
|
|
|
|
promise.resolve(end - start);
|
|
|
|
}, 1000);
|
|
|
|
clearTimeout(setTimeout(() => {}, 1000));
|
|
|
|
|
|
|
|
const result = await promise;
|
|
|
|
assert(result >= 1000);
|
|
|
|
},
|
|
|
|
});
|