gRPC : tips, tricks and testing
GSoC Week 11–12 @SCoRe Lab
gRPC is default for server to server communication these days.
What is RPC?
RPC stands for Remote Procedural Calls. Instead of calling methods on the client, we call methods on the server side. With this, it is easy to change the code on the server rather than changing code for each client. One more advantage on that front is, we can have our server and client in different languages according to our requirements.
How RPC works?
- serialize the arguments.
Serialization is the process of converting a data object into a series of bytes that saves the state of the object in an easily transmittable form.
2. Send the serialized code to server
3. De-serialize the arguments at server
4. Call the function at server
5. serialize the results and send it to the server
6. deserialize the result at the client.
Protocol Buffers is the default serializer for gRPC. Protobufs are 4 times faster and generates 4 times smaller code than regular json serializtion.
gRPC uses HTTP2.0 which always secure http over TLS.
Timestamp
We are going to import “google/protobuf/timestamp.proto” which is the definition by Google of what is at timestamp. And in our start request we can add a field called time which is of type google.protobuf.timestamp. Generate the code.
Note that the generated type is not Python’s datetime.
Importing Timestamp:
from google.protobuf.timestamp_pb2 import Timestamp
It’s a different type, and you need to do conversion back and forth every time. We set the request time field with fromDatetime(python-datetime) as follows:
If you have a Timestamp you can convert it to datetime using the toDateTime() method:
And there is also a method to get the current time. So you can do Timestamp().GetCurrentTime().
Serializing Protobuf structure to JSON
Json is a very popular serialization format. Sometimes we like to take the types that were generated from protocol buffers and instead of serializing them to protocol buffers serialize them to json. When I want to convert it to json, I’m not going to use the json from the standard library but this json serialization library:
from google.protobuf.json_format import MessageToJson
It knows more about protocol buffers types and will do the right things. We get nice json document.
json = MessageToJson(proto_message)
gRPC Testing
Testing your code is important. gRPC has some utilities for testing. But I find out that only using pytest/unittest, you can test gRPC without the need for these test utilities. Here, it is for server testing:
- Create a request with all the data I need.
- For the context use mock from the mock library
- Create server.
- Call the method with the request and the context.
context = MagicMock()service = ClientConnectionService()request = pb2.ClientConnectRequest(host_name = “User Test 2”)request.request_epoch_time.FromDatetime(datetime.now())response = service.ClientConnect(request,context)self.assertEqual(response.connection_status,pb2.SUCCESS)self.assertIsNotNone(response.uid)