如何在 Python 中使用 grpcio-testing 测试 ServiceInterceptor

发布于 2025-01-12 20:30:09 字数 1130 浏览 3 评论 0原文

我有一个像这样的原型文件

syntax = "proto3";

package hello;

message HelloRequest {
}

message HelloResponse {
}

service HelloService {
    rpc UnaryUnaryHello (HelloRequest) returns (HelloResponse) {}
    rpc UnaryStreamHello (HelloRequest) returns (stream HelloResponse) {}
    rpc StreamUnaryHello (stream HelloRequest) returns (HelloResponse) {}
    rpc StreamStreamHello (stream HelloRequest) returns (stream HelloResponse) {}
}

,然后我为其生成了基本服务程序和存根。

我还为其创建了一个服务器拦截器,MyServerInterceptor

现在我尝试使用 grpcio-testing 测试拦截器,但是不能'搞不懂。实际上,我找不到有关如何使用该库的任何文档,这看起来像已知问题< /a>.

从那个问题中,我找到了 这个示例,但是我不知道如何将拦截器添加到测试服务器。

此外,测试使用较低级别描述符的方式似乎相当笨拙。有没有办法使用生成的存根来发出请求?

最后,该示例显示将字典 {} 作为 invocable_metadata 传递,但我认为 involution_metadata 应该是元组列表 List [元组[str,str]]

I have a proto file like

syntax = "proto3";

package hello;

message HelloRequest {
}

message HelloResponse {
}

service HelloService {
    rpc UnaryUnaryHello (HelloRequest) returns (HelloResponse) {}
    rpc UnaryStreamHello (HelloRequest) returns (stream HelloResponse) {}
    rpc StreamUnaryHello (stream HelloRequest) returns (HelloResponse) {}
    rpc StreamStreamHello (stream HelloRequest) returns (stream HelloResponse) {}
}

And then I've generated the base servicer and stubs for it.

I also created a server interceptor for it, MyServerInterceptor.

Now I am trying to test the interceptor using grpcio-testing, but can't figure it out. I am actually having trouble finding any documentation on how to use the library, which looks like a known issue.

From that issue, I found this example, however I can't figure out how to add interceptors to the test server.

Also, the way the tests use the lower level DESCRIPTORS seems rather clunky. Is there a way to use the generated stubs to make the requests?

Finally, the example shows passing a dict {} as invocation_metadata, but I thought invocation_metadata was supposed to be a list of tuples List[Tuple[str,str]]?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2025-01-19 20:30:09

您发布的示例尚未合并。我建议使用实际的 gRPC 客户端/服务器而不是模拟库来测试 Python 中的 gRPC 服务。通过这种方式,您可以像客户端一样测试服务器逻辑。

您对元数据类型的看法是正确的,它应该是 List[Tuple[str, str]

The example you posted is not merged yet. I would recommend to test the gRPC service in Python with actual gRPC client/server instead of the mocking library. In this way, you are testing the server logic in the way that a client would.

And you are right about the metadata type, it should be List[Tuple[str, str].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文