22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
25 #include "absl/types/optional.h"
39 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
40 #include "src/proto/grpc/testing/echo.grpc.pb.h"
41 #include "src/proto/grpc/testing/echo_mock.grpc.pb.h"
46 using grpc::testing::EchoRequest;
47 using grpc::testing::EchoResponse;
48 using grpc::testing::EchoTestService;
65 explicit FakeClient(EchoTestService::StubInterface*
stub) :
stub_(
stub) {}
71 request.set_message(
"hello world");
77 void DoRequestStream() {
85 std::unique_ptr<ClientWriterInterface<EchoRequest>> cstream =
96 cstream->WritesDone();
103 void DoResponseStream() {
106 request.set_message(
"hello world");
109 std::unique_ptr<ClientReaderInterface<EchoResponse>> cstream =
114 exp.append(
response.message() +
" ");
126 void DoBidiStream() {
132 std::unique_ptr<ClientReaderWriterInterface<EchoRequest, EchoResponse>>
157 void ResetStub(EchoTestService::StubInterface*
stub) {
stub_ =
stub; }
160 EchoTestService::StubInterface*
stub_;
163 class CallbackTestServiceImpl :
public EchoTestService::CallbackService {
165 ServerUnaryReactor*
Echo(CallbackServerContext*
context,
173 auto* reactor =
context->DefaultReactor();
174 if (
request->message().length() > 0) {
190 TEST_F(MockCallbackTest, MockedCallSucceedsWithWait) {
191 CallbackServerContext
ctx;
205 req.set_message(
"mock 1");
209 while (!
status.status.has_value()) {
221 TEST_F(MockCallbackTest, MockedCallSucceeds) {
222 CallbackServerContext
ctx;
227 req.set_message(
"ha ha, consider yourself mocked.");
234 TEST_F(MockCallbackTest, MockedCallFails) {
235 CallbackServerContext
ctx;
254 Status RequestStream(ServerContext* ,
255 ServerReader<EchoRequest>*
reader,
267 Status ResponseStream(ServerContext* ,
const EchoRequest*
request,
268 ServerWriter<EchoResponse>*
writer)
override {
280 ServerReaderWriter<EchoResponse, EchoRequest>*
stream)
override {
294 vector<std::string>
result;
296 for (
auto n :
input) {
301 if (buff.empty())
continue;
305 if (!buff.empty())
result.push_back(buff);
315 void SetUp()
override {
326 void TearDown()
override {
server_->Shutdown(); }
331 stub_ = grpc::testing::EchoTestService::NewStub(
channel);
334 std::unique_ptr<grpc::testing::EchoTestService::Stub>
stub_;
341 TEST_F(MockTest, SimpleRpc) {
345 MockEchoTestServiceStub
stub;
347 resp.set_message(
"hello world");
355 TEST_F(MockTest, ClientStream) {
360 MockEchoTestServiceStub
stub;
361 auto w =
new MockClientWriter<EchoRequest>();
363 resp.set_message(
"hello, world");
375 TEST_F(MockTest, ServerStream) {
378 client.DoResponseStream();
380 MockEchoTestServiceStub
stub;
381 auto r =
new MockClientReader<EchoResponse>();
383 resp1.set_message(
"hello");
385 resp2.set_message(
"world");
388 .WillOnce(
DoAll(SetArgPointee<0>(resp1),
Return(
true)))
389 .WillOnce(
DoAll(SetArgPointee<0>(resp2),
Return(
true)))
396 client.DoResponseStream();
401 TEST_F(MockTest, BidiStream) {
405 MockEchoTestServiceStub
stub;
429 int main(
int argc,
char** argv) {