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

time: A way to advance time manually #2777

Closed
amikhalev opened this issue Aug 19, 2020 · 3 comments
Closed

time: A way to advance time manually #2777

amikhalev opened this issue Aug 19, 2020 · 3 comments
Labels
A-tokio Area: The main tokio crate C-question User questions that are neither feature requests nor bug reports M-time Module: tokio/time

Comments

@amikhalev
Copy link

Is your feature request related to a problem? Please describe.
I am trying to write unit tests for code which involves running things throughout time in a task. I saw the tokio::time::{pause, advance} functions and thought those would work. I thought I could do something like this

tokio::time::pause();
do_thing_in_10s().await;
tokio::time::advance(Duration::from_secs(1)).await;
assert!(thing_did_not_happen_yet());

send_message_which_modifies_thing().await;
// need this so task will get the message 
tokio::task::yield_now().await;

tokio::time::advance(Duration::from_secs(1)).await;
assert!(thing_did_not_happen_yet()); // this assert fails

But since #2059, the yield_now will cause the executor to park the thread and the time driver advances time, so the thing happens early.

Describe the solution you'd like
A way to actually pause time and advance manually.

@amikhalev amikhalev added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels Aug 19, 2020
@Darksonn Darksonn added the M-time Module: tokio/time label Aug 19, 2020
@carllerche
Copy link
Member

Why is advancing time using sleep(Duration::from_secs(1)).await not sufficient here?

@carllerche carllerche added the S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. label Nov 9, 2020
@amikhalev
Copy link
Author

Since creating this issue I realized that sleeping while paused does exactly what I needed. This issue could either be closed or made into an issue for improving the docs. Ideally they would have an example of using sleep while paused to write tests.

@carllerche
Copy link
Member

@amikhalev I am going ot close this, but I would love if you could follow up w/ a doc PR. The usage pattern can be described in the API docs for time::pause().

@taiki-e taiki-e added C-question User questions that are neither feature requests nor bug reports and removed S-waiting-on-author Status: awaiting some action (such as code changes) from the PR or issue author. C-feature-request Category: A feature request. labels Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-question User questions that are neither feature requests nor bug reports M-time Module: tokio/time
Projects
None yet
Development

No branches or pull requests

4 participants