-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add Stream
mock to tokio-test
#4106
Comments
The mock type in tokio-test is written for the |
Ah, good point. Perhaps this is not the right place for such a feature request, then? |
I don't mind adding some sort of |
Sounds perfect. |
Though I guess it would need to be written for the |
tokio_test::Builder
polymorphic in the stream typeStream
mock to tokio-test
You could do both. I'm also ok with something that starts with just |
I'm interested in taking this on. So that I understand: the desired behavior for the mock As for mocking a It also seems like we might want sequence-building functionality like the current A rough example test snippet could be helpful here. |
Right, we need it to do more than just A minimal builder would probably look like this, mirroring the existing builder.
Of course, if you have better ideas, that's fine too. |
The best example I can think of where this could be used is for e.g. testing code that communicates over a web socket. Such code would probably split it into two halves with |
Digging into this some more, it looks like both
As you mentioned in the previous comment, there's some awkwardness around the fact that |
I think it would probably best to tie the Just to be clear, none of the things you mentioned can cause undefined behavior. That has a very specific meaning in Rust-land, which e.g. the
The word for this kind of thing is unspecified behavior. |
Ah, that makes sense. Thanks for the clarification. I have the |
I'm puzzling out some additional behavior for waking, which is a bit more complex with a two-sided communication channel. In particular, the documentation for
There is no such guidance for the poll methods on the |
In general, multiple calls to any poll method should only wake the most recent waker. This includes sinks. |
Ok, one final question: is Is the function useful enough that it should just be included anyway? And finally, if it is necessary, I'm not seeing any tests for similar functionality in |
I believe Regarding tests that use time, you should not do that using |
Is this still open ? and is this a good begginer issue ? |
It is indeed still open. You are welcome to give it a try! |
Awesome! from where could I start ?
tokio_stream::{self, StreamExt}; tokio stream impl Builder<T, U, E> {
fn next(&mut self, result: T);
fn send_ok(&mut self, item: U);
fn send_err(&mut self, error: E);
fn wait(&mut self, duration: Duration);
} mentioned builder
|
I believe that the implementation would be very similar to the mock in |
Okay, I think I'm getting somewere. How can i create a stream from a Mock type since its not iterable? use futures_util::stream; // why not the use tokio_stream::{self as stream, StreamExt}; ?
/* .... */
#[tokio::test]
async fn stream_read(){
let mut mock: Stream = stream:: something_here ::from(Builder::new().read(b"hello ").read(b"world!").build());
let res: Poll<Option<...>::item> = mock.poll_next(); // here i'm trying to figure out how to get the read values
assert_eq!(res, b"hello "); |
The |
@Grubba27, are you still working on this? If not, I would like to take it over. |
Inroduce a new mock type to tests streams and eventually sinks. Only includes next() and wait() for now. Fixes tokio-rs#4106
Is your feature request related to a problem? Please describe.
I want to test streams wrapped in tokio_serde, but that's not currently possible.
Describe the solution you'd like
Make
Action
polymorphic in the transport type. That way, I can construct aBuilder<Framed<...>>
or aBuilder<&[u8]>
depending on my needs.The text was updated successfully, but these errors were encountered: