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

Conversation

Milly
Copy link
Contributor

@Milly Milly commented Aug 9, 2023

Fixed FakeTime.restoreFor to atomically restore and re-override globalThis.setTimeout etc.
This also fixes FakeTime.prototype.delay and FakeTime.prototype.runMicrotasks and others.

Of course, we don't get atomicity when an async function is specified to restoreFor.
However, FakeTime.prototype.delay that uses an non-async function will be fixed.

Example of runMicrotasks (that calls delay that calls restoreFor):

import { FakeTime } from "./testing/time.ts";

const testTarget = async() => {
   console.log(1, setTimeout);
   await Promise.resolve();
   console.log(2, setTimeout);
   await Promise.resolve();
   console.log(3, setTimeout);
};

const time = new FakeTime();
const promise = testTarget(); // no await, microtask is queued
time.runMicrotasks();
await promise; // already resolved
time.restore();

// Outputs before PR:
// 1 [Function: fakeSetTimeout]
// 2 [Function: setTimeout]
// 3 [Function: fakeSetTimeout]

// Outputs this PR:
// 1 [Function: fakeSetTimeout]
// 2 [Function: fakeSetTimeout]
// 3 [Function: fakeSetTimeout]

@Milly Milly requested a review from kt3k as a code owner August 9, 2023 02:30
@CLAassistant
Copy link

CLAassistant commented Aug 9, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Wording of PR title might sound confusing, but this PR only changes the timing of overrideGlobals being called inside FakeTime.restoreFor when the given callback is sync. By this change, FakeTime.restoreFor works more accurately when the callback is sync, and that seems fixing the edge cases described in the above.

@Milly Milly changed the title fix(testing/time): fix FakeTime.restoreFor to be atomic fix(testing/time): fix FakeTime.restoreFor accuracy for sync callbacks Aug 14, 2023
@Milly
Copy link
Contributor Author

Milly commented Aug 14, 2023

Additional fix pushed.
Reject instead of throw on TimeError.
Match the original behavior.

@Milly Milly requested a review from kt3k August 14, 2023 08:48
@kt3k
Copy link
Member

kt3k commented Aug 14, 2023

Thanks. That makes more sense. Also thanks for updating the title

@kt3k kt3k merged commit bca446e into denoland:main Aug 14, 2023
@Milly Milly deleted the faketime-restorefor-atomic branch August 21, 2023 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants