grpc / grpc/grpc-rust

Example of using a server as the `GrpcService` in a client

Open
#2,227 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

I wanted to write some end-to-end tests for code that uses a tonic client. I wanted to have a mock server _without_ needing to mock the gRPC client.

I started with the [`mock`](https://github.com/hyperium/tonic/blob/master/examples/src/mock/mock.rs) example which uses a duplex stream for in-process I/O, but that felt overly complicated. I then started looking at the `GrpcService` trait and creating an implementation that would use my mock server directly.

While writing that implementation, I discovered that tonic servers _already implement this trait_. You can simply pass a server to a client and it works.

```rust
let server = GreetServiceServer::new(Greeter::default());
let client = GreetServiceClient::new(server);
```

I couldn't find any examples of this pattern in the repo and it's very handy for testing.

Would you be willing to accept a PR to add an example demonstrating this? Would it be preferable to **replace** the `mock` example with this pattern?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.