Google mock on gRPC

Viewed 363

I want to test client and thus mocking server by gmock. My proto is defined as

// The greeting service definition.
service Greeter {
  // Streaming request and reply
  rpc StreamRequestAndReply (stream Request) returns (stream Reply) {}
}


message Request {
  int32 idx = 1;
  bool is_pause = 2; // control message
}

message Reply {
  string msg = 1;
  bool is_eof = 2;
}

And the correspinding function on server-side looks like

Status StreamRequestAndReply(ServerContext *context, ServerReaderWriter<Reply, Request> *stream) override {
  // do something
}

My question is: how to write my predefined Reply message via the mocked stub by gmock, for example I want to reply three valid message and then set is_eof in the fourth one?

My current thinking is using testing::Invoke, but haven't figured out how to capture the stream and call the Write function. Can someone give me some suggestions and hints?

1 Answers

Synchronous gRPC provides virtual functions for all interfaces, which could be mocked by StubInterface.

Asynchronous gRPC (i.e. CompletionQueue) doesn't provide such functionality, since it's just a thin wrapper around C implementation. In this case, better to set-up a local fake server, and unit test it in an integration-like way.

Related