Skip to content

make codec::decode::Streaming a trait for easier testability #462

@stefanhoelzl

Description

@stefanhoelzl

Feature Request

make codec::decode::Streaming a trait.
This will allow easier mocking needed for testing of grpc calls using client streaming.

Crates

tonic
tonic-build

Motivation

currently when I have a grpc call with client streaming like ByteStream::Write I get a rust function with this signature:

async fn write(&self, request: Request<Streaming<WriteRequest>>) -> Result<Response<WriteResponse>, Status>

If I want to test this function I would need to create a Streaming object which is quite complicated.
It would be much easier if the generated function write would be generic accepting a argument implementing the trait Stream and Streaming would implement this.

Proposal

Adding a new trait

trait Stream {
    type Item;
    async fn message<'_>(&'_ mut self) -> Result<Option<Self::Item>, Status>;
    async fn trailers<'_>(&'_ mut self) -> Result<Option<MetadataMap>, Status>;
}

and generate function definitions for ByteStream::write with such a signature

async fn write<S: Stream<Item = WriteRequest >>(&self, request: Request<S>);

This would allow to create simple Mocks which implement Stream for testing.
And otherwise Streaming is still used, which must then implement Stream.

Alternatives

Please let me know if you know alternative how testing can be simplified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions