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

Allow VirtualTimeScheduler to run on any thread #2610

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

danielt1263
Copy link
Collaborator

…as long as critical methods are all called on the same thread.

This is designed to fix issue #2609

@danielt1263 danielt1263 self-assigned this Jul 10, 2024
@danielt1263 danielt1263 linked an issue Jul 10, 2024 that may be closed by this pull request
17 tasks
@danielt1263
Copy link
Collaborator Author

As far as I can tell, this is 100% backwards compatible and doesn't require any API change from users of the library. It may not solve every future use-case that Swift Testing might throw at it, but it solves the single most obvious one.

I see no reason that this can't be accepted into the next minor release/update.

Comment on lines +111 to +114
guard thread == nil || Thread.current == thread else {
rxFatalError("Executing on the wrong thread. Please ensure all work on the same thread.")
}
thread = Thread.current

Choose a reason for hiding this comment

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

Is it safe to read and write thread without any locking?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Isn't that what we always do on the main thread? Read/write without locking? Maybe I don't understand the question...

Choose a reason for hiding this comment

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

Indeed, it should be completely fine as long scheduler's methods are actually called on one thread, but what if there would be two close calls from different threads, it's not impossible that both will see thread == nil and proceed, for example.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right! Good point. I will update to account for that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@nikolaykasyanov What do you think about just assigning the thread in the init? Then we won't need a lock...

Choose a reason for hiding this comment

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

@danielt1263 sounds good!

@bennnjamin
Copy link

bennnjamin commented Aug 23, 2024

What's the hold up in getting this merged? Would love to start being able to migrate our RxSwift tests to Swift Testing @nikolaykasyanov @danielt1263

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Why is VirtualTimeScheduler checking to ensure it's on the main thread?
3 participants