Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(370)

Side by Side Diff: third_party/grpc/test/cpp/end2end/mock_test.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include <thread>
35
36 #include <grpc++/channel.h>
37 #include <grpc++/client_context.h>
38 #include <grpc++/create_channel.h>
39 #include <grpc++/server.h>
40 #include <grpc++/server_builder.h>
41 #include <grpc++/server_context.h>
42 #include <grpc/grpc.h>
43 #include <grpc/support/thd.h>
44 #include <grpc/support/time.h>
45 #include <gtest/gtest.h>
46
47 #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
48 #include "src/proto/grpc/testing/echo.grpc.pb.h"
49 #include "test/core/util/port.h"
50 #include "test/core/util/test_config.h"
51
52 using grpc::testing::EchoRequest;
53 using grpc::testing::EchoResponse;
54 using grpc::testing::EchoTestService;
55 using std::chrono::system_clock;
56
57 namespace grpc {
58 namespace testing {
59
60 namespace {
61 template <class W, class R>
62 class MockClientReaderWriter GRPC_FINAL
63 : public ClientReaderWriterInterface<W, R> {
64 public:
65 void WaitForInitialMetadata() GRPC_OVERRIDE {}
66 bool Read(R* msg) GRPC_OVERRIDE { return true; }
67 bool Write(const W& msg) GRPC_OVERRIDE { return true; }
68 bool WritesDone() GRPC_OVERRIDE { return true; }
69 Status Finish() GRPC_OVERRIDE { return Status::OK; }
70 };
71 template <>
72 class MockClientReaderWriter<EchoRequest, EchoResponse> GRPC_FINAL
73 : public ClientReaderWriterInterface<EchoRequest, EchoResponse> {
74 public:
75 MockClientReaderWriter() : writes_done_(false) {}
76 void WaitForInitialMetadata() GRPC_OVERRIDE {}
77 bool Read(EchoResponse* msg) GRPC_OVERRIDE {
78 if (writes_done_) return false;
79 msg->set_message(last_message_);
80 return true;
81 }
82
83 bool Write(const EchoRequest& msg,
84 const WriteOptions& options) GRPC_OVERRIDE {
85 gpr_log(GPR_INFO, "mock recv msg %s", msg.message().c_str());
86 last_message_ = msg.message();
87 return true;
88 }
89 bool WritesDone() GRPC_OVERRIDE {
90 writes_done_ = true;
91 return true;
92 }
93 Status Finish() GRPC_OVERRIDE { return Status::OK; }
94
95 private:
96 bool writes_done_;
97 grpc::string last_message_;
98 };
99
100 // Mocked stub.
101 class MockStub : public EchoTestService::StubInterface {
102 public:
103 MockStub() {}
104 ~MockStub() {}
105 Status Echo(ClientContext* context, const EchoRequest& request,
106 EchoResponse* response) GRPC_OVERRIDE {
107 response->set_message(request.message());
108 return Status::OK;
109 }
110 Status Unimplemented(ClientContext* context, const EchoRequest& request,
111 EchoResponse* response) GRPC_OVERRIDE {
112 return Status::OK;
113 }
114
115 private:
116 ClientAsyncResponseReaderInterface<EchoResponse>* AsyncEchoRaw(
117 ClientContext* context, const EchoRequest& request,
118 CompletionQueue* cq) GRPC_OVERRIDE {
119 return nullptr;
120 }
121 ClientWriterInterface<EchoRequest>* RequestStreamRaw(
122 ClientContext* context, EchoResponse* response) GRPC_OVERRIDE {
123 return nullptr;
124 }
125 ClientAsyncWriterInterface<EchoRequest>* AsyncRequestStreamRaw(
126 ClientContext* context, EchoResponse* response, CompletionQueue* cq,
127 void* tag) GRPC_OVERRIDE {
128 return nullptr;
129 }
130 ClientReaderInterface<EchoResponse>* ResponseStreamRaw(
131 ClientContext* context, const EchoRequest& request) GRPC_OVERRIDE {
132 return nullptr;
133 }
134 ClientAsyncReaderInterface<EchoResponse>* AsyncResponseStreamRaw(
135 ClientContext* context, const EchoRequest& request, CompletionQueue* cq,
136 void* tag) GRPC_OVERRIDE {
137 return nullptr;
138 }
139 ClientReaderWriterInterface<EchoRequest, EchoResponse>* BidiStreamRaw(
140 ClientContext* context) GRPC_OVERRIDE {
141 return new MockClientReaderWriter<EchoRequest, EchoResponse>();
142 }
143 ClientAsyncReaderWriterInterface<EchoRequest, EchoResponse>*
144 AsyncBidiStreamRaw(ClientContext* context, CompletionQueue* cq,
145 void* tag) GRPC_OVERRIDE {
146 return nullptr;
147 }
148 ClientAsyncResponseReaderInterface<EchoResponse>* AsyncUnimplementedRaw(
149 ClientContext* context, const EchoRequest& request,
150 CompletionQueue* cq) GRPC_OVERRIDE {
151 return nullptr;
152 }
153 };
154
155 class FakeClient {
156 public:
157 explicit FakeClient(EchoTestService::StubInterface* stub) : stub_(stub) {}
158
159 void DoEcho() {
160 ClientContext context;
161 EchoRequest request;
162 EchoResponse response;
163 request.set_message("hello world");
164 Status s = stub_->Echo(&context, request, &response);
165 EXPECT_EQ(request.message(), response.message());
166 EXPECT_TRUE(s.ok());
167 }
168
169 void DoBidiStream() {
170 EchoRequest request;
171 EchoResponse response;
172 ClientContext context;
173 grpc::string msg("hello");
174
175 std::unique_ptr<ClientReaderWriterInterface<EchoRequest, EchoResponse>>
176 stream = stub_->BidiStream(&context);
177
178 request.set_message(msg + "0");
179 EXPECT_TRUE(stream->Write(request));
180 EXPECT_TRUE(stream->Read(&response));
181 EXPECT_EQ(response.message(), request.message());
182
183 request.set_message(msg + "1");
184 EXPECT_TRUE(stream->Write(request));
185 EXPECT_TRUE(stream->Read(&response));
186 EXPECT_EQ(response.message(), request.message());
187
188 request.set_message(msg + "2");
189 EXPECT_TRUE(stream->Write(request));
190 EXPECT_TRUE(stream->Read(&response));
191 EXPECT_EQ(response.message(), request.message());
192
193 stream->WritesDone();
194 EXPECT_FALSE(stream->Read(&response));
195
196 Status s = stream->Finish();
197 EXPECT_TRUE(s.ok());
198 }
199
200 void ResetStub(EchoTestService::StubInterface* stub) { stub_ = stub; }
201
202 private:
203 EchoTestService::StubInterface* stub_;
204 };
205
206 class TestServiceImpl : public EchoTestService::Service {
207 public:
208 Status Echo(ServerContext* context, const EchoRequest* request,
209 EchoResponse* response) GRPC_OVERRIDE {
210 response->set_message(request->message());
211 return Status::OK;
212 }
213
214 Status BidiStream(ServerContext* context,
215 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
216 GRPC_OVERRIDE {
217 EchoRequest request;
218 EchoResponse response;
219 while (stream->Read(&request)) {
220 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
221 response.set_message(request.message());
222 stream->Write(response);
223 }
224 return Status::OK;
225 }
226 };
227
228 class MockTest : public ::testing::Test {
229 protected:
230 MockTest() {}
231
232 void SetUp() GRPC_OVERRIDE {
233 int port = grpc_pick_unused_port_or_die();
234 server_address_ << "localhost:" << port;
235 // Setup server
236 ServerBuilder builder;
237 builder.AddListeningPort(server_address_.str(),
238 InsecureServerCredentials());
239 builder.RegisterService(&service_);
240 server_ = builder.BuildAndStart();
241 }
242
243 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
244
245 void ResetStub() {
246 std::shared_ptr<Channel> channel =
247 CreateChannel(server_address_.str(), InsecureChannelCredentials());
248 stub_ = grpc::testing::EchoTestService::NewStub(channel);
249 }
250
251 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
252 std::unique_ptr<Server> server_;
253 std::ostringstream server_address_;
254 TestServiceImpl service_;
255 };
256
257 // Do one real rpc and one mocked one
258 TEST_F(MockTest, SimpleRpc) {
259 ResetStub();
260 FakeClient client(stub_.get());
261 client.DoEcho();
262 MockStub stub;
263 client.ResetStub(&stub);
264 client.DoEcho();
265 }
266
267 TEST_F(MockTest, BidiStream) {
268 ResetStub();
269 FakeClient client(stub_.get());
270 client.DoBidiStream();
271 MockStub stub;
272 client.ResetStub(&stub);
273 client.DoBidiStream();
274 }
275
276 } // namespace
277 } // namespace testing
278 } // namespace grpc
279
280 int main(int argc, char** argv) {
281 grpc_test_init(argc, argv);
282 ::testing::InitGoogleTest(&argc, argv);
283 return RUN_ALL_TESTS();
284 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/cpp/end2end/hybrid_end2end_test.cc ('k') | third_party/grpc/test/cpp/end2end/server_crash_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698