Crates.io | tonic-mock |
lib.rs | tonic-mock |
version | 0.3.0 |
source | src |
created_at | 2021-03-13 04:02:58.2393 |
updated_at | 2024-05-07 03:16:58.931066 |
description | Test utilities for easy mocking tonic streaming interface |
homepage | https://github.com/tyrchen/tonic-mock |
repository | https://github.com/tyrchen/tonic-mock |
max_upload_size | |
id | 368130 |
size | 28,902 |
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:
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:
#[tokio::test]
async fn service_push_works() -> anyhow::Result<()> {
let mut events: Vec<RequestPush> = 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:
Note these functions are for testing purpose only. DO NOT use them in other cases.
prost-helper
is distributed under the terms of MIT.
See LICENSE for details.
Copyright 2021 Tyr Chen