Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tyrchen/tonic-mock
Test utilities for easy mocking tonic streaming interface
https://github.com/tyrchen/tonic-mock
grpc testing-tools tonic
Last synced: 2 months ago
JSON representation
Test utilities for easy mocking tonic streaming interface
- Host: GitHub
- URL: https://github.com/tyrchen/tonic-mock
- Owner: tyrchen
- License: mit
- Created: 2021-03-13T03:45:09.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2024-05-07T03:16:54.000Z (9 months ago)
- Last Synced: 2024-11-15T00:38:42.702Z (3 months ago)
- Topics: grpc, testing-tools, tonic
- Language: Rust
- Homepage: https://crates.io/crates/tonic-mock
- Size: 14.6 KB
- Stars: 14
- Watchers: 3
- Forks: 11
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# tonic-mock
[tonic](https://docs.rs/tonic) is a great crate to build GRPC applications. However, testing RPC built with tonic is not straightforward, especially for the streaming interface. If you have an RPC like this:
```protobuf
rpc Push(stream RequestPush) returns (stream ResponsePush);
```Testing it usually involves lots of effort on properly mocking the data. This little crate helps to make it easier to mock the incoming data and to manipulate the response so that you could focus on testing the logic itself. For example:
```rust
#[tokio::test]
async fn service_push_works() -> anyhow::Result<()> {
let mut events: Vec = Vec::with_capacity(3);
for i in 0..3 {
events.push(RequestPush::new(id: Bytes::from(i.to_string), data: Bytes::from("a".repeat(10))));
}// preparing the streaming request
let req = tonic_mock::streaming_request(events);let server = start_server();
// call the service
let res = server.push(req).await?;// iterate the response and assert the result
tonic_mock::process_streaming_response(result, |msg, i| {
assert!(msg.is_ok());
assert_eq!(msg.as_ref().unwrap().code, i as i32);
})
.await;Ok(())
}
```Three main functions provided:
- streaming_request: build streaming requests based on a vector of messages.
- process_streaming_response: iterate the streaming response and call the closure user provided.
- stream_to_vec: iterate the streaming response and generate a vector for further processing.Note these functions are for testing purpose only. DO NOT use them in other cases.
## License
`prost-helper` is distributed under the terms of MIT.
See [LICENSE](LICENSE.md) for details.
Copyright 2021 Tyr Chen