| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
- <meta http-equiv="X-UA-Compatible" content="IE=9"/>
- <meta name="generator" content="Doxygen 1.8.6"/>
- <title>GRPC Core: How to write unit tests for gRPC C client.</title>
- <link href="tabs.css" rel="stylesheet" type="text/css"/>
- <script type="text/javascript" src="jquery.js"></script>
- <script type="text/javascript" src="dynsections.js"></script>
- <link href="search/search.css" rel="stylesheet" type="text/css"/>
- <script type="text/javascript" src="search/search.js"></script>
- <script type="text/javascript">
- $(document).ready(function() { searchBox.OnSelectItem(0); });
- </script>
- <link href="doxygen.css" rel="stylesheet" type="text/css" />
- </head>
- <body>
- <div id="top"><!-- do not remove this div, it is closed by doxygen! -->
- <div id="titlearea">
- <table cellspacing="0" cellpadding="0">
- <tbody>
- <tr style="height: 56px;">
- <td style="padding-left: 0.5em;">
- <div id="projectname">GRPC Core
-  <span id="projectnumber">4.0.0</span>
- </div>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- <!-- end header part -->
- <!-- Generated by Doxygen 1.8.6 -->
- <script type="text/javascript">
- var searchBox = new SearchBox("searchBox", "search",false,'Search');
- </script>
- <div id="navrow1" class="tabs">
- <ul class="tablist">
- <li><a href="index.html"><span>Main Page</span></a></li>
- <li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
- <li><a href="modules.html"><span>Modules</span></a></li>
- <li><a href="annotated.html"><span>Data Structures</span></a></li>
- <li><a href="files.html"><span>Files</span></a></li>
- <li>
- <div id="MSearchBox" class="MSearchBoxInactive">
- <span class="left">
- <img id="MSearchSelect" src="search/mag_sel.png"
- onmouseover="return searchBox.OnSearchSelectShow()"
- onmouseout="return searchBox.OnSearchSelectHide()"
- alt=""/>
- <input type="text" id="MSearchField" value="Search" accesskey="S"
- onfocus="searchBox.OnSearchFieldFocus(true)"
- onblur="searchBox.OnSearchFieldFocus(false)"
- onkeyup="searchBox.OnSearchFieldChange(event)"/>
- </span><span class="right">
- <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
- </span>
- </div>
- </li>
- </ul>
- </div>
- <!-- window showing the filter options -->
- <div id="MSearchSelectWindow"
- onmouseover="return searchBox.OnSearchSelectShow()"
- onmouseout="return searchBox.OnSearchSelectHide()"
- onkeydown="return searchBox.OnSearchSelectKey(event)">
- <a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark"> </span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark"> </span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark"> </span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark"> </span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark"> </span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark"> </span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark"> </span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark"> </span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark"> </span>Macros</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark"> </span>Groups</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark"> </span>Pages</a></div>
- <!-- iframe showing the search results (closed by default) -->
- <div id="MSearchResultsWindow">
- <iframe src="javascript:void(0)" frameborder="0"
- name="MSearchResults" id="MSearchResults">
- </iframe>
- </div>
- </div><!-- top -->
- <div class="header">
- <div class="headertitle">
- <div class="title">How to write unit tests for gRPC C client. </div> </div>
- </div><!--header-->
- <div class="contents">
- <div class="textblock"><p>tl;dr: <a href="https://github.com/grpc/grpc/blob/master/test/cpp/end2end/mock_test.cc">Example code</a>.</p>
- <p>To unit-test client-side logic via the synchronous API, gRPC provides a mocked Stub based on googletest(googlemock) that can be programmed upon and easily incorporated in the test code.</p>
- <p>For instance, consider an EchoService like this:</p>
- <p>```proto service EchoTestService { rpc Echo(EchoRequest) returns (EchoResponse); rpc BidiStream(stream EchoRequest) returns (stream EchoResponse); } ```</p>
- <p>The code generated would look something like this:</p>
- <p>```c class EchoTestService final { public: class StubInterface { virtual ::grpc::Status Echo(::grpc::ClientContext* context, const ::grpc::testing::EchoRequest& request, ::grpc::testing::EchoResponse* response) = 0; … std::unique_ptr< ::grpc::ClientReaderWriterInterface< ::grpc::testing::EchoRequest, ::grpc::testing::EchoResponse>> BidiStream(::grpc::ClientContext* context) { return std::unique_ptr< ::grpc::ClientReaderWriterInterface< ::grpc::testing::EchoRequest, ::grpc::testing::EchoResponse>>(BidiStreamRaw(context)); } … private: virtual ::grpc::ClientReaderWriterInterface< ::grpc::testing::EchoRequest, ::grpc::testing::EchoResponse>* BidiStreamRaw(::grpc::ClientContext* context) = 0; … } // End StubInterface … } // End EchoTestService ```</p>
- <p>If we mock the StubInterface and set expectations on the pure-virtual methods we can test client-side logic without having to make any rpcs.</p>
- <p>A mock for this StubInterface will look like this:</p>
- <p>```c class MockEchoTestServiceStub : public EchoTestService::StubInterface { public: MOCK_METHOD3(Echo, ::grpc::Status(::grpc::ClientContext* context, const ::grpc::testing::EchoRequest& request, ::grpc::testing::EchoResponse* response)); MOCK_METHOD1(BidiStreamRaw, ::grpc::ClientReaderWriterInterface< ::grpc::testing::EchoRequest, ::grpc::testing::EchoResponse>*(::grpc::ClientContext* context)); }; ```</p>
- <p><b>Generating mock code:</b></p>
- <p>Such a mock can be auto-generated by:</p>
- <ol type="1">
- <li>Setting flag(generate_mock_code=true) on grpc plugin for protoc, or</li>
- </ol>
- <ol type="1">
- <li>Setting an attribute(generate_mock) in your bazel rule.</li>
- </ol>
- <p>Protoc plugin flag:</p>
- <p>``<code>sh protoc -I . --grpc_out=generate_mock_code=true:. --plugin=protoc-gen-grpc=</code>which grpc_cpp_plugin<code>echo.proto </code>``</p>
- <p>Bazel rule:</p>
- <p>```py grpc_proto_library( name = "echo_proto", srcs = ["echo.proto"], generate_mock = True, ) ```</p>
- <p>By adding such a flag now a header file <code>echo_mock.grpc.pb.h</code> containing the mocked stub will also be generated.</p>
- <p>This header file can then be included in test files along with a gmock dependency.</p>
- <p><b>Writing tests with mocked Stub.</b></p>
- <p>Consider the following client a user might have:</p>
- <p>```c class FakeClient { public: explicit FakeClient(EchoTestService::StubInterface* stub) : stub_(stub) {}</p>
- <p>void DoEcho() { ClientContext context; EchoRequest request; EchoResponse response; request.set_message("hello world"); Status s = stub_->Echo(&context, request, &response); EXPECT_EQ(request.message(), response.message()); EXPECT_TRUE(s.ok()); }</p>
- <p>void DoBidiStream() { EchoRequest request; EchoResponse response; ClientContext context; grpc::string msg("hello");</p>
- <p>std::unique_ptr<ClientReaderWriterInterface<EchoRequest, EchoResponse>> stream = stub_->BidiStream(&context);</p>
- <p>request.set_message(msg "0"); EXPECT_TRUE(stream->Write(request)); EXPECT_TRUE(stream->Read(&response)); EXPECT_EQ(response.message(), request.message());</p>
- <p>request.set_message(msg "1"); EXPECT_TRUE(stream->Write(request)); EXPECT_TRUE(stream->Read(&response)); EXPECT_EQ(response.message(), request.message());</p>
- <p>request.set_message(msg "2"); EXPECT_TRUE(stream->Write(request)); EXPECT_TRUE(stream->Read(&response)); EXPECT_EQ(response.message(), request.message());</p>
- <p>stream->WritesDone(); EXPECT_FALSE(stream->Read(&response));</p>
- <p>Status s = stream->Finish(); EXPECT_TRUE(s.ok()); }</p>
- <p>void ResetStub(EchoTestService::StubInterface* stub) { stub_ = stub; }</p>
- <p>private: EchoTestService::StubInterface* stub_; }; ```</p>
- <p>A test could initialize this FakeClient with a mocked stub having set expectations on it:</p>
- <p>Unary RPC:</p>
- <p>```c MockEchoTestServiceStub stub; EchoResponse resp; resp.set_message("hello world"); Expect_CALL(stub, Echo(_,_,_)).Times(Atleast(1)).WillOnce(DoAll(SetArgPointee<2>(resp), Return(Status::OK))); FakeClient client(stub); client.DoEcho(); ```</p>
- <p>Streaming RPC:</p>
- <p>```c ACTION_P(copy, msg) { arg0->set_message(msg->message()); }</p>
- <p>auto rw = new MockClientReaderWriter<EchoRequest, EchoResponse>(); EchoRequest msg; EXPECT_CALL(*rw, Write(_, _)).Times(3).WillRepeatedly(DoAll(SaveArg<0>(&msg), Return(true))); EXPECT_CALL(*rw, Read(_)). WillOnce(DoAll(WithArg<0>(copy(&msg)), Return(true))). WillOnce(DoAll(WithArg<0>(copy(&msg)), Return(true))). WillOnce(DoAll(WithArg<0>(copy(&msg)), Return(true))). WillOnce(Return(false));</p>
- <p>MockEchoTestServiceStub stub; EXPECT_CALL(stub, BidiStreamRaw(_)).Times(AtLeast(1)).WillOnce(Return(rw));</p>
- <p>FakeClient client(stub); client.DoBidiStream(); ``` </p>
- </div></div><!-- contents -->
- <!-- start footer part -->
- <hr class="footer"/><address class="footer"><small>
- Generated on Wed Jun 21 2017 14:13:07 for GRPC Core by  <a href="http://www.doxygen.org/index.html">
- <img class="footer" src="doxygen.png" alt="doxygen"/>
- </a> 1.8.6
- </small></address>
- </body>
- </html>
|