Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: update the stub code generation of Protobuf IDL file in trpc_protocol_service.md #126

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 24 additions & 23 deletions docs/en/trpc_protocol_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ The above defines a standard Protobuf IDL file. Next, we will build the project
## Build the project code based on the Protobuf IDL file

If the user has already built the project, this section can be skipped, and you can proceed to the next section.
To facilitate project building, the framework provides a script for quickly building projects based on Protobuf IDL files: [create_trpc_server.sh](../../create_trpc_server.sh). The usage of this script can be found in the README. To build the project using the script, execute the following command:

To facilitate project building, the framework provides the trpc-cmdline tool for quickly building projects based on Protobuf IDL files. In this case, execute the following command:
```sh
./create_trpc_server.sh ./ ./helloworld.proto
#Here, 'trpc' is the trpc-cmdline tool installed during your environment setup. '-l' specifies the language as C++, 'protofile' is the proto file used for generating stub code.
trpc create -l cpp --protofile=helloworld.proto
```

After executing the command, a `helloworld` project will be generated in the current directory with the following directory structure.
Expand All @@ -65,35 +65,36 @@ After executing the command, a `helloworld` project will be generated in the cur
.
├── build.sh
├── clean.sh
├── client
│   ├── BUILD
│   ├── conf
│   │   ├── trpc_cpp_fiber.yaml
│   │   └── trpc_cpp_future.yaml
│   ├── fiber_client.cc
│   └── future_client.cc
├── proto
│   ├── BUILD
│   ├── helloworld.proto
│   └── WORKSPACE
├── README.md
├── run_client.sh
├── run_server.sh
├── test
│ ├── helloworld
│ │ ├── BUILD
│ │ ├── conf
│ │ │ ├── trpc_cpp_fiber.yaml
│ │ │ └── trpc_cpp.yaml
│ │ ├── greeter_service.cc
│ │ ├── greeter_service.h
│ │ ├── helloworld.proto
│ │ ├── helloworld_server.cc
│ │ └── helloworld_server.h
│ └── test
│ ├── BUILD
│ ├── conf
│ │ ├── trpc_cpp_fiber.yaml
│ │ └── trpc_cpp_future.yaml
│ ├── fiber_client.cc
│ ├── future_client.cc
│ └── README.md
├── server
│   ├── BUILD
│   ├── conf
│   │   ├── trpc_cpp_fiber.yaml
│   │   └── trpc_cpp.yaml
│   ├── server.cc
│   ├── server.h
│   ├── service.cc
│   └── service.h
└── WORKSPACE
```

Introducing the project directory:

- build.sh and clean.sh are used for building and cleaning the project, respectively. run_client.sh and run_server.sh are used to start the client and server tests.
- The first `test` directory represents the `app` name, and its subdirectory `helloworld` represents the `server` name. The second test directory stores the test client code, which is only used for testing purposes.
- The code in the server directory is related to the implementation of this service, while the code in the client directory is used for testing the service locally.
- `WORKSPACE` is used for configuring the workspace required for `bazel` compilation. Its contents are as follows:

```bzl
Expand Down
4 changes: 2 additions & 2 deletions docs/en/unit_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ See [GreeterServiceTest_SayHelloOK](/examples/helloworld/greeter_service_test.cc

In the scenario of asynchronous response, the user needs to actively call `context->SendUnaryResponse` in the program to send the response. Since this scenario involves network calls, mocking the network is necessary in unit tests.

To facilitate writing unit tests for this scenario, we provides the [MockServerTransport](/trpc/server/testing/server_testing.h#L44) class, which is designed specifically for testing purposes.
To facilitate writing unit tests for this scenario, we provides the [MockServerTransport](/trpc/server/testing/mock_server_transport.h#L25) class, which is designed specifically for testing purposes.

To write test with MockServerTransport, first you can should set the transport of service to `MockServerTransport` by calling the `Service::SetServerTransport` interface. Then, you can mock the network calls using the `EXPECT_CALL` syntax from gmock. Here is the pseudocode:

```cpp
#include "trpc/server/testing/server_testing.h"
#include "trpc/server/testing/mock_server_transport.h"

TEST(GreeterServiceTest, SayHelloOK) {
auto transport= std::make_unique<::trpc::testing::MockServerTransport>();
Expand Down
47 changes: 24 additions & 23 deletions docs/zh/trpc_protocol_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ message HelloReply {
## 根据 Protobuf IDL 文件构建项目代码

若用户已经构建了项目,则本节可以直接跳过,直接查看下节。
为了方便用户构建项目,框架提供基于 Protobuf 的 IDL 文件快速构建项目的脚本: [create_trpc_server.sh](../../create_trpc_server.sh),其使用方式参考[README](../../trpc/tools/trpc_create_server_plugin/README.md),采用脚本构建项目命令

为了方便用户构建项目,框架提供基于 Protobuf 的 IDL 文件快速构建项目的trpc-cmdline工具,这时执行下面命令:
```sh
./create_trpc_server.sh ./ ./helloworld.proto
# 其中trpc为你在环境搭建中安装的trpc-cmdline工具,-l指定language为cpp语言,protofile为用于生成桩代码的proto文件
trpc create -l cpp --protofile=helloworld.proto
```

命令执行后,会在当前目录下生成`helloworld`项目,其目录结构如下:
Expand All @@ -65,35 +65,36 @@ message HelloReply {
.
├── build.sh
├── clean.sh
├── client
│   ├── BUILD
│   ├── conf
│   │   ├── trpc_cpp_fiber.yaml
│   │   └── trpc_cpp_future.yaml
│   ├── fiber_client.cc
│   └── future_client.cc
├── proto
│   ├── BUILD
│   ├── helloworld.proto
│   └── WORKSPACE
├── README.md
├── run_client.sh
├── run_server.sh
├── test
│ ├── helloworld
│ │ ├── BUILD
│ │ ├── conf
│ │ │ ├── trpc_cpp_fiber.yaml
│ │ │ └── trpc_cpp.yaml
│ │ ├── greeter_service.cc
│ │ ├── greeter_service.h
│ │ ├── helloworld.proto
│ │ ├── helloworld_server.cc
│ │ └── helloworld_server.h
│ └── test
│ ├── BUILD
│ ├── conf
│ │ ├── trpc_cpp_fiber.yaml
│ │ └── trpc_cpp_future.yaml
│ ├── fiber_client.cc
│ ├── future_client.cc
│ └── README.md
├── server
│   ├── BUILD
│   ├── conf
│   │   ├── trpc_cpp_fiber.yaml
│   │   └── trpc_cpp.yaml
│   ├── server.cc
│   ├── server.h
│   ├── service.cc
│   └── service.h
└── WORKSPACE
```

介绍一下该项目目录:

- build.sh 和 clean.sh表示构建和清理项目;run_client.sh 和 run_server.sh表示启动客户端和服务端测试
- 第一个`test`目录表示`app`名称,其子目录`helloworld`表示`server`名称;第二个test目录存储测试客户端代码,仅用于测试
- 其中server目录下的代码是此服务相关实现的代码,client目录下的代码是用于本地测试服务的客户端代码
- `WORKSPACE` 用于`bazel`编译所需工作区配置内容如下

```bzl
Expand Down
4 changes: 2 additions & 2 deletions docs/zh/unit_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ TEST(GreeterServiceTest, SayHelloOK) {

异步回包场景相比同步回包场景,需要用户在程序中主动调用`context->SendUnaryResponse`进行回包。此场景涉及网络调用,故单测中需要对网络进行 mock。

为了方便写单测,框架提供了针对这种测试场景的[MockServerTransport](/trpc/server/testing/server_testing.h#L44)类。
为了方便写单测,框架提供了针对这种测试场景的[MockServerTransport](/trpc/server/testing/mock_server_transport.h#L25)类。

使用时先调用 `Service::SetServerTransport` 接口将 transport 设置为 MockServerTransport,然后通过 gmock 的 EXPECT_CALL 语法来对网络调用进行 mock,伪代码如下所示:

```cpp
#include "trpc/server/testing/server_testing.h"
#include "trpc/server/testing/mock_server_transport.h"

TEST(GreeterServiceTest, SayHelloOK) {
auto transport= std::make_unique<::trpc::testing::MockServerTransport>();
Expand Down
Loading