Make sure that you can compile and run core tests and read a basic example Writing your first HTTP server.
In this example, we will write a client side and a server side for a simple GreeterService
from greeter.proto
(see the schema below). Its single SayHello
method accepts a name
string and replies with a corresponding greeting
string.
We generate and link to a CMake library from our .proto
schema:
Register the necessary ugrpc
components:
Wrap the generated api::GreeterServiceClient
in a component that exposes a simplified interface:
A single request-response RPC handling is simple: fill in request
and context
, initiate the RPC, receive the response
.
Fill in the static config entries for the client side:
# Creates gRPC clients
grpc-client-factory:
# The TaskProcessor for blocking connection initiation
task-processor: grpc-blocking-task-processor
# Optional channel parameters for gRPC Core
# https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
channel-args: {}
# Our wrapper around the generated client for GreeterService
greeter-client:
# The service endpoint (URI). We talk to our own service,
# which is kind of pointless, but works for an example
endpoint: '[::1]:8091'
task_processors:
grpc-blocking-task-processor: # For blocking gRPC channel creation
worker_threads: 2
thread_name: grpc-worker
Implement the generated api::GreeterServiceBase
. As a convenience, a derived api::GreeterServiceBase::Component
class is provided for easy integration with the component system.
A single request-response RPC handling is simple: fill in the response
and send it.
Fill in the static config entries for the server side:
# Common configuration for gRPC server
grpc-server:
# The single listening port for incoming RPCs
port: $grpc_server_port
port#fallback: 8091
completion-queue-count: 3
# Our GreeterService implementation
greeter-service:
task-processor: main-task-processor
greeting-prefix: Hello
middlewares: []
Finally, we register our components and start the server.
To build the sample, execute the following build steps at the userver root directory:
The sample could be started by running make start-userver-samples-grpc_service
. The command would invoke testsuite start target that sets proper paths in the configuration files and starts the service.
To start the service manually run ./samples/grpc_service/userver-samples-grpc_service -c </path/to/static_config.yaml>
.
The service is available locally at port 8091 (as per our static_config.yaml
).
To implement Functional tests for the service some preparational steps should be done.
First of all, import the required modules and add the required pytest_userver.plugins.grpc pytest plugin:
To mock the gRPC server provide a hook for the static config to change the endpoint:
Write the mocking fixtures using grpc_mockserver:
After that everything is ready to check the service client requests:
To do the gRPC requests write a client fixture using grpc_channel:
Use it to do gRPC requests to the service:
See the full example at: