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

Add use_generic_streaming_requests option to make testing server impls easier #2115

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

Conversation

yotamofek
Copy link
Contributor

@yotamofek yotamofek commented Jan 2, 2025

Motivation

Currently, testing the server trait implementations of client-streaming methods (i.e. requests that receive a stream) is quite difficult, and requires jumping through hoops in order to construct a Streaming object (see #462 for one way of doing it).

Solution

Adds a codegen option that makes the server trait go from this:

trait Test {
    async fn test_request(
        &self,
        req: Request<Streaming<Message>>,
    ) -> Result<Response<Message>, Status>;
}

to this:

trait Test {
    async fn test_request(
        &self,
        req: impl IntoStreamingRequest<Message = Result<Message, Status>, Stream: Unpin> + Send,
    ) -> Result<Response<Message>, Status>;
}

Since Request<Streaming<Message>> impls IntoStreamingRequest<Message = Result<Message, Status>>, the grpc server can continue invoking these handlers in the same way, but it also allows any object implementing Stream<Item = Result<Message, Status>> to be used.
This makes testing much easier.

@yotamofek
Copy link
Contributor Author

Just realized this uses associated type bounds which were only stabilized in 1.79.0.
How often is the MSRV raised?
The Unpin bound in IntoStreamingRequest<Stream: Unpin> isn't strictly necessary (and might actually make the trait less flexible), and can be either removed or emulated with a named generic param (i.e. test_request<S: Unpin>(req: impl IntoStreamIterator<Stream = S>) or so)

Adds an option for server trait generation that causes client-streaming
methods to receive a generic `impl IntoStreamingRequest` param
instead of a concrete `Request<Streaming<...>>` param.
This allows for testing trait implementions with any type implementing `Stream`,
and not having to go through the trouble of encoding a `Streaming`
object.

Fixes hyperium#462
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.

1 participant