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

stream: add WatchStream::from_changes, add testing, etc. #5432

Merged
merged 22 commits into from
Feb 19, 2023

Conversation

brodycj
Copy link
Contributor

@brodycj brodycj commented Feb 6, 2023

other minor updates:

  • add a test case for existing WatchStream::from
  • update existing WatchStream test function name
  • apply minor documentation updates

resolves #5420

Motivation

see #5420

It looks like WatchStream::from was not covered by documentation or integration test cases, adding one here as well.

Quick disclaimer that I am still quite new with Rust in contrast to my years in quite a few other major systems programming languages.

Solution

add a test case for existing `WatchStream::from`

update existing WatchStream test function name

apply minor documentation updates
@@ -66,6 +67,13 @@ impl<T: 'static + Clone + Send + Sync> WatchStream<T> {
inner: ReusableBoxFuture::new(async move { (Ok(()), rx) }),
}
}

/// Create a new `WatchStream` that waits for the value to be changed.
pub fn new_on_changed(rx: Receiver<T>) -> Self {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I would be open to changing this function name. I tried to come up with something consistent with the Receiver::changed function that we await for, but doesn't feel as pretty as I would like.

I thought of from_changes and new_from_changes, but do not feel as consistent.

Choose a reason for hiding this comment

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

How about new_skip_unchanged? Or new_skip_until_changed?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think new_on_changed is fine.

@brodycj brodycj marked this pull request as draft February 9, 2023 20:22
tokio-stream/tests/watch.rs Outdated Show resolved Hide resolved
@brodycj brodycj marked this pull request as ready for review February 10, 2023 18:12
@brodycj brodycj marked this pull request as draft February 12, 2023 21:48
@brodycj brodycj marked this pull request as ready for review February 13, 2023 04:52
@brodycj
Copy link
Contributor Author

brodycj commented Feb 13, 2023

I have simplified the test with help from the tokio-test utility and have added an example to the documentation.

I have also aded a test to the documentation to assert that the watch stream is still pending until new data is available. If we keep this, it may be enough overlap to remove the test case I added.

@brodycj
Copy link
Contributor Author

brodycj commented Feb 14, 2023

I think this should be better now, please let me know if anything else is needed from my end to get this accepted and merged.

I think there is an overlap between the watch_stream_new_on_change test case and the documentation example that I have added with a poll test. I would prefer to keep the documentation with the poll test that I made but can remove the poll test from the documentation if needed.

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Other than this comment, it looks good to me.

tokio-stream/src/wrappers/watch.rs Outdated Show resolved Hide resolved
@brodycj brodycj marked this pull request as draft February 17, 2023 16:47
@brodycj brodycj changed the title stream: add WatchStream::new_on_changed, add testing, etc. stream: add WatchStream:: from_changes, add testing, etc. Feb 17, 2023
@brodycj brodycj changed the title stream: add WatchStream:: from_changes, add testing, etc. stream: add WatchStream::from_changes, add testing, etc. Feb 17, 2023
@brodycj brodycj marked this pull request as ready for review February 17, 2023 17:26
Comment on lines 40 to 41
futures-test = "0.3.26"
futures-util = "0.3.26"
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need these. We already import futures.

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that FutureExt is available from the futures crate.

Copy link
Contributor Author

@brodycj brodycj Feb 19, 2023

Choose a reason for hiding this comment

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

I removed futures-test. Unfortunately I still need futures-util to import FutureExt as I needed to get now_or_never() to work.

I noticed that tokio/src/sync/watch.rs is using recv.changed().now_or_never() from crate::sync::watch::channel(0i32) in some tests. I would like to investigate this a little further. Any pointers would still be highly appreciated as before.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

futures::future::FutureExt worked for me thanks!

@brodycj brodycj marked this pull request as draft February 19, 2023 13:48
@brodycj brodycj marked this pull request as ready for review February 19, 2023 14:27
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thanks.

@Darksonn Darksonn merged commit d07027f into tokio-rs:master Feb 19, 2023
@brodycj brodycj deleted the new-watch-stream-on-changed branch February 19, 2023 15:25
amab8901 pushed a commit to amab8901/tokio that referenced this pull request Feb 27, 2023
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 M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WatchStream: Add additional constructor which causes stream to only yield when value changes
3 participants