Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(testing/time): fix FakeTime.restoreFor accuracy for sync callbacks #3531

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 10 additions & 5 deletions testing/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,26 @@ export class FakeTime {
/**
* Restores real time temporarily until callback returns and resolves.
*/
static async restoreFor<T>(
static restoreFor<T>(
// deno-lint-ignore no-explicit-any
callback: (...args: any[]) => Promise<T> | T,
// deno-lint-ignore no-explicit-any
...args: any[]
): Promise<T> {
if (!time) throw new TimeError("no fake time");
let result: T;
restoreGlobals();
try {
result = await callback.apply(null, args);
} finally {
const result = callback.apply(null, args);
if (result instanceof Promise) {
return result.finally(() => overrideGlobals());
} else {
overrideGlobals();
return Promise.resolve(result);
}
} catch (e) {
overrideGlobals();
return Promise.reject(e);
}
return result;
}

/**
Expand Down
66 changes: 66 additions & 0 deletions testing/time_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,72 @@ Deno.test("FakeTime restoreFor restores real time temporarily", async () => {
}
});

Deno.test("FakeTime restoreFor restores real time and re-overridden atomically", async () => {
const time: FakeTime = new FakeTime();
const fakeSetTimeout = setTimeout;
const actualSetTimeouts: (typeof setTimeout)[] = [];

try {
const asyncFn = async () => {
actualSetTimeouts.push(setTimeout);
await Promise.resolve();
actualSetTimeouts.push(setTimeout);
await Promise.resolve();
actualSetTimeouts.push(setTimeout);
};
const promise = asyncFn();
await new Promise((resolve) => {
FakeTime.restoreFor(() => setTimeout(resolve, 0));
});
await promise;
assertEquals(actualSetTimeouts, [
fakeSetTimeout,
fakeSetTimeout,
fakeSetTimeout,
]);
} finally {
time.restore();
}
});

Deno.test("FakeTime restoreFor returns promise that resolved to result of callback", async () => {
const time: FakeTime = new FakeTime();

try {
const resultSync = await FakeTime.restoreFor(() => "a");
assertEquals(resultSync, "a");
const resultAsync = await FakeTime.restoreFor(() => Promise.resolve("b"));
assertEquals(resultAsync, "b");
} finally {
time.restore();
}
});

Deno.test("FakeTime restoreFor returns promise that rejected to error in callback", async () => {
const time: FakeTime = new FakeTime();

try {
await assertRejects(
() =>
FakeTime.restoreFor(() => {
throw new Error("Error in sync callback");
}),
Error,
"Error in sync callback",
);
await assertRejects(
() =>
FakeTime.restoreFor(() => {
return Promise.reject(new Error("Error in async callback"));
}),
Error,
"Error in async callback",
);
} finally {
time.restore();
}
});

Deno.test("delay uses real time", async () => {
const time: FakeTime = new FakeTime();
const start: number = Date.now();
Expand Down