blob: 1d29096b86def9da8ed21dee9f16668febed39d3 [file] [log] [blame]
Yang Gao196ade32015-05-05 13:31:12 -07001/*
2 *
murgatroid99ace28d32016-01-14 10:12:10 -08003 * Copyright 2015-2016, Google Inc.
Yang Gao196ade32015-05-05 13:31:12 -07004 * 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
yang-g8c2be9f2015-08-19 16:28:09 -070036#include <grpc++/channel.h>
Yang Gao196ade32015-05-05 13:31:12 -070037#include <grpc++/client_context.h>
38#include <grpc++/create_channel.h>
Yang Gao196ade32015-05-05 13:31:12 -070039#include <grpc++/server.h>
40#include <grpc++/server_builder.h>
41#include <grpc++/server_context.h>
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080042#include <grpc/grpc.h>
43#include <grpc/support/thd.h>
44#include <grpc/support/time.h>
Yang Gao196ade32015-05-05 13:31:12 -070045#include <gtest/gtest.h>
46
Craig Tiller1b4e3302015-12-17 16:35:00 -080047#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h"
48#include "src/proto/grpc/testing/echo.grpc.pb.h"
Sree Kuchibhotlab0d0c8e2016-01-13 22:52:17 -080049#include "test/core/util/port.h"
50#include "test/core/util/test_config.h"
Yang Gao196ade32015-05-05 13:31:12 -070051
Craig Tiller1b4e3302015-12-17 16:35:00 -080052using grpc::testing::EchoRequest;
53using grpc::testing::EchoResponse;
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -080054using grpc::testing::EchoTestService;
Yang Gao196ade32015-05-05 13:31:12 -070055using std::chrono::system_clock;
56
57namespace grpc {
58namespace testing {
59
60namespace {
61template <class W, class R>
62class MockClientReaderWriter GRPC_FINAL
63 : public ClientReaderWriterInterface<W, R> {
64 public:
vjpai6bf1de92015-11-02 14:48:57 -080065 void WaitForInitialMetadata() GRPC_OVERRIDE {}
Yang Gao196ade32015-05-05 13:31:12 -070066 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};
71template <>
72class MockClientReaderWriter<EchoRequest, EchoResponse> GRPC_FINAL
73 : public ClientReaderWriterInterface<EchoRequest, EchoResponse> {
74 public:
75 MockClientReaderWriter() : writes_done_(false) {}
vjpai6bf1de92015-11-02 14:48:57 -080076 void WaitForInitialMetadata() GRPC_OVERRIDE {}
Yang Gao196ade32015-05-05 13:31:12 -070077 bool Read(EchoResponse* msg) GRPC_OVERRIDE {
78 if (writes_done_) return false;
79 msg->set_message(last_message_);
80 return true;
81 }
David Garcia Quintas6a3cf972015-07-13 13:38:18 -070082
83 bool Write(const EchoRequest& msg,
84 const WriteOptions& options) GRPC_OVERRIDE {
Yang Gao196ade32015-05-05 13:31:12 -070085 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.
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800101class MockStub : public EchoTestService::StubInterface {
Yang Gao196ade32015-05-05 13:31:12 -0700102 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(
Craig Tiller5f871ac2015-05-08 13:05:51 -0700117 ClientContext* context, const EchoRequest& request,
118 CompletionQueue* cq) GRPC_OVERRIDE {
Yang Gao196ade32015-05-05 13:31:12 -0700119 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(
Craig Tiller5f871ac2015-05-08 13:05:51 -0700149 ClientContext* context, const EchoRequest& request,
150 CompletionQueue* cq) GRPC_OVERRIDE {
Yang Gao196ade32015-05-05 13:31:12 -0700151 return nullptr;
152 }
153};
154
155class FakeClient {
156 public:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800157 explicit FakeClient(EchoTestService::StubInterface* stub) : stub_(stub) {}
Yang Gao196ade32015-05-05 13:31:12 -0700158
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());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700166 EXPECT_TRUE(s.ok());
Yang Gao196ade32015-05-05 13:31:12 -0700167 }
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();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700197 EXPECT_TRUE(s.ok());
Yang Gao196ade32015-05-05 13:31:12 -0700198 }
199
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800200 void ResetStub(EchoTestService::StubInterface* stub) { stub_ = stub; }
Yang Gao196ade32015-05-05 13:31:12 -0700201
202 private:
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800203 EchoTestService::StubInterface* stub_;
Yang Gao196ade32015-05-05 13:31:12 -0700204};
205
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800206class TestServiceImpl : public EchoTestService::Service {
Yang Gao196ade32015-05-05 13:31:12 -0700207 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
228class MockTest : public ::testing::Test {
229 protected:
Vijay Paie8a7e302015-08-24 10:52:33 -0700230 MockTest() {}
Yang Gao196ade32015-05-05 13:31:12 -0700231
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_);
Yang Gao196ade32015-05-05 13:31:12 -0700240 server_ = builder.BuildAndStart();
241 }
242
243 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
244
245 void ResetStub() {
yang-g730055d2015-08-27 12:29:45 -0700246 std::shared_ptr<Channel> channel =
Julien Boeufe5adc0e2015-10-12 14:08:10 -0700247 CreateChannel(server_address_.str(), InsecureChannelCredentials());
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800248 stub_ = grpc::testing::EchoTestService::NewStub(channel);
Yang Gao196ade32015-05-05 13:31:12 -0700249 }
250
Sree Kuchibhotla5a05f512016-01-13 22:43:20 -0800251 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
Yang Gao196ade32015-05-05 13:31:12 -0700252 std::unique_ptr<Server> server_;
253 std::ostringstream server_address_;
254 TestServiceImpl service_;
Yang Gao196ade32015-05-05 13:31:12 -0700255};
256
257// Do one real rpc and one mocked one
258TEST_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
267TEST_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
280int main(int argc, char** argv) {
281 grpc_test_init(argc, argv);
282 ::testing::InitGoogleTest(&argc, argv);
283 return RUN_ALL_TESTS();
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800284}