#grpc #tonic #testing #mocking #streaming #interface #rpc

tonic-mock

用于简化 tonic 流式接口测试的测试工具

2 个不稳定版本

0.3.0 2024 年 5 月 7 日
0.1.0 2021 年 3 月 13 日

#1307开发工具

Download history 132/week @ 2024-05-01 37/week @ 2024-05-08 2/week @ 2024-05-15 5/week @ 2024-05-22 26/week @ 2024-05-29 71/week @ 2024-06-05 73/week @ 2024-06-12 65/week @ 2024-06-19 97/week @ 2024-06-26 95/week @ 2024-07-03 100/week @ 2024-07-10 140/week @ 2024-07-17 124/week @ 2024-07-24 113/week @ 2024-07-31 46/week @ 2024-08-07 61/week @ 2024-08-14

361 每月下载量

MIT 许可证

11KB
127

tonic-mock

tonic 是构建 GRPC 应用程序的优秀crate。然而,使用 tonic 构建的 RPC 测试并不简单,尤其是对于流式接口。如果你有一个这样的 RPC

rpc Push(stream RequestPush) returns (stream ResponsePush);

测试通常需要大量工作来正确模拟数据。这个小 crate 有助于更容易地模拟传入数据并操作响应,以便您可以专注于测试逻辑本身。例如

#[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(())
}

提供三个主要功能

  • streaming_request: 基于消息向量构建流式请求。
  • process_streaming_response: 迭代流式响应并调用用户提供的闭包。
  • stream_to_vec: 迭代流式响应并生成用于进一步处理的向量。

请注意,这些函数仅用于测试目的。请勿在其他情况下使用它们。

许可证

prost-helper 在 MIT 许可证下分发。

有关详细信息,请参阅 LICENSE

版权所有 2021 Tyr Chen

依赖项

~4.5–6.5MB
~112K SLoC