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

test: implement safe abstraction using the Wake trait #6898

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nurmohammed840
Copy link
Contributor

No description provided.

@Darksonn Darksonn added the A-tokio-test Area: The tokio-test crate label Oct 11, 2024
@Darksonn
Copy link
Contributor

This seems to contain two independent changes: using the Wake trait and a new block_on method. Could you explain what you're trying to do?

@nurmohammed840
Copy link
Contributor Author

Wake trait provides a safe abstraction for creating Waker from an Arc pointer data:

struct MockTask {
waker: Arc<ThreadWaker>,
}

This reduces both boilerplate and, more importantly unsafe code.


I'm really surprised that there isn't a block_on function:
Although original implementation do support this:

ThreadWaker {
state: Mutex::new(IDLE),
condvar: Condvar::new(),
}

// The other half is sleeping, so we wake it up.
assert_eq!(prev, SLEEP);
self.condvar.notify_one();

Original implementation has Condvar, But it isn't used anywhere.
Also the name ThreadWaker implies that it's used to wake a sleeping thread, but no thread is actually sleeping (because there is no block_on), I believe the author may have forgotten to include block_on function.

This is my main reason to add block_on

It could also be useful as a basic future executor, similar to futures::executor::block_on

@Darksonn
Copy link
Contributor

We use tokio_test::task::spawn quite a lot in our test suite, and we don't need it to include a block_on there. Usually, it's used to manually call poll in a convenient manner. What are you trying to use it for?

@nurmohammed840
Copy link
Contributor Author

nurmohammed840 commented Oct 11, 2024

This could be useful outside of Tokio test suites for general-purpose future execution.
Most test cases don't require an I/O event loop or Timer

It's also well-suited for benchmarking asynchronous code due to its lightweight nature.

In some cases, you may want to avoid a runtime. For example, when running tools like Miri,
Currently #[tokio::test] cannot be used to test async code using miri.


Most importantly original implementation has already support for it.

Maybe we should consider replacing entire wake() function with just single AtomicBool::store operation, if we decide to drop block_on support.

fn wake(&self) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-test Area: The tokio-test crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants