0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 09:31:22 -05:00

chore(tests): improve unit tests using deferred (#11842)

This commit is contained in:
David Sherret 2021-08-25 16:04:14 -04:00 committed by GitHub
parent 66476efec5
commit 5d7d9d6443
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 22 deletions

View file

@ -803,8 +803,6 @@ unitTest(
{ perms: { net: true } },
async function httpServerIncompleteMessage() {
const listener = Deno.listen({ port: 4501 });
const def1 = deferred();
const def2 = deferred();
const client = await Deno.connect({ port: 4501 });
await client.write(new TextEncoder().encode(
@ -830,23 +828,21 @@ unitTest(
const errors: Error[] = [];
writeResponse()
const writePromise = writeResponse()
.catch((error: Error) => {
errors.push(error);
})
.then(() => def1.resolve());
});
const res = new Response(readable);
respondWith(res)
.catch((error: Error) => errors.push(error))
.then(() => def2.resolve());
const respondPromise = respondWith(res)
.catch((error: Error) => errors.push(error));
client.close();
await Promise.all([
def1,
def2,
writePromise,
respondPromise,
]);
listener.close();

View file

@ -394,8 +394,6 @@ unitTest(
unitTest(
{ perms: { net: true } },
async function netTcpListenIteratorBreakClosesResource() {
const promise = deferred();
async function iterate(listener: Deno.Listener) {
let i = 0;
@ -407,13 +405,11 @@ unitTest(
break;
}
}
promise.resolve();
}
const addr = { hostname: "127.0.0.1", port: 8888 };
const listener = Deno.listen(addr);
iterate(listener);
const iteratePromise = iterate(listener);
await delay(100);
const conn1 = await Deno.connect(addr);
@ -421,7 +417,7 @@ unitTest(
const conn2 = await Deno.connect(addr);
conn2.close();
await promise;
await iteratePromise;
},
);

View file

@ -11,12 +11,14 @@ import {
unitTest({ perms: { hrtime: false } }, async function performanceNow() {
const resolvable = deferred();
const start = performance.now();
let totalTime = 0;
setTimeout(() => {
const end = performance.now();
assert(end - start >= 10);
totalTime = end - start;
resolvable.resolve();
}, 10);
await resolvable;
assert(totalTime >= 10);
});
unitTest(function performanceMark() {

View file

@ -86,11 +86,10 @@ unitTest(async function timeoutEvalNoScopeLeak() {
unitTest(async function timeoutArgs() {
const promise = deferred();
const arg = 1;
let capturedArgs: unknown[] = [];
setTimeout(
(a, b, c) => {
assertEquals(a, arg);
assertEquals(b, arg.toString());
assertEquals(c, [arg]);
function () {
capturedArgs = [...arguments];
promise.resolve();
},
10,
@ -99,6 +98,11 @@ unitTest(async function timeoutArgs() {
[arg],
);
await promise;
assertEquals(capturedArgs, [
arg,
arg.toString(),
[arg],
]);
});
unitTest(async function timeoutCancelSuccess() {
@ -214,14 +218,16 @@ unitTest(async function fireCallbackImmediatelyWhenDelayOverMaxValue() {
unitTest(async function timeoutCallbackThis() {
const promise = deferred();
let capturedThis: unknown;
const obj = {
foo() {
assertEquals(this, window);
capturedThis = this;
promise.resolve();
},
};
setTimeout(obj.foo, 1);
await promise;
assertEquals(capturedThis, window);
});
unitTest(async function timeoutBindThis() {