blob: b2c6dc39a8124ea281d50288e799fa0b15b68921 [file] [log] [blame]
Yang Gao196ade32015-05-05 13:31:12 -07001/*
2 *
3 * Copyright 2015, 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
yang-g9e2f90c2015-08-21 15:35:03 -070036#include <grpc/grpc.h>
37#include <grpc/support/thd.h>
38#include <grpc/support/time.h>
yang-g8c2be9f2015-08-19 16:28:09 -070039#include <grpc++/channel.h>
Yang Gao196ade32015-05-05 13:31:12 -070040#include <grpc++/client_context.h>
41#include <grpc++/create_channel.h>
42#include <grpc++/credentials.h>
43#include <grpc++/server.h>
44#include <grpc++/server_builder.h>
45#include <grpc++/server_context.h>
46#include <grpc++/server_credentials.h>
Yang Gao196ade32015-05-05 13:31:12 -070047#include <gtest/gtest.h>
48
yang-g9e2f90c2015-08-21 15:35:03 -070049#include "test/core/util/port.h"
50#include "test/core/util/test_config.h"
51#include "test/cpp/util/echo_duplicate.grpc.pb.h"
52#include "test/cpp/util/echo.grpc.pb.h"
Yang Gao196ade32015-05-05 13:31:12 -070053
54using grpc::cpp::test::util::EchoRequest;
55using grpc::cpp::test::util::EchoResponse;
56using grpc::cpp::test::util::TestService;
57using std::chrono::system_clock;
58
59namespace grpc {
60namespace testing {
61
62namespace {
63template <class W, class R>
64class MockClientReaderWriter GRPC_FINAL
65 : public ClientReaderWriterInterface<W, R> {
66 public:
67 void WaitForInitialMetadata() {}
68 bool Read(R* msg) GRPC_OVERRIDE { return true; }
69 bool Write(const W& msg) GRPC_OVERRIDE { return true; }
70 bool WritesDone() GRPC_OVERRIDE { return true; }
71 Status Finish() GRPC_OVERRIDE { return Status::OK; }
72};
73template <>
74class MockClientReaderWriter<EchoRequest, EchoResponse> GRPC_FINAL
75 : public ClientReaderWriterInterface<EchoRequest, EchoResponse> {
76 public:
77 MockClientReaderWriter() : writes_done_(false) {}
78 void WaitForInitialMetadata() {}
79 bool Read(EchoResponse* msg) GRPC_OVERRIDE {
80 if (writes_done_) return false;
81 msg->set_message(last_message_);
82 return true;
83 }
David Garcia Quintas6a3cf972015-07-13 13:38:18 -070084
85 bool Write(const EchoRequest& msg,
86 const WriteOptions& options) GRPC_OVERRIDE {
Yang Gao196ade32015-05-05 13:31:12 -070087 gpr_log(GPR_INFO, "mock recv msg %s", msg.message().c_str());
88 last_message_ = msg.message();
89 return true;
90 }
91 bool WritesDone() GRPC_OVERRIDE {
92 writes_done_ = true;
93 return true;
94 }
95 Status Finish() GRPC_OVERRIDE { return Status::OK; }
96
97 private:
98 bool writes_done_;
99 grpc::string last_message_;
100};
101
102// Mocked stub.
103class MockStub : public TestService::StubInterface {
104 public:
105 MockStub() {}
106 ~MockStub() {}
107 Status Echo(ClientContext* context, const EchoRequest& request,
108 EchoResponse* response) GRPC_OVERRIDE {
109 response->set_message(request.message());
110 return Status::OK;
111 }
112 Status Unimplemented(ClientContext* context, const EchoRequest& request,
113 EchoResponse* response) GRPC_OVERRIDE {
114 return Status::OK;
115 }
116
117 private:
118 ClientAsyncResponseReaderInterface<EchoResponse>* AsyncEchoRaw(
Craig Tiller5f871ac2015-05-08 13:05:51 -0700119 ClientContext* context, const EchoRequest& request,
120 CompletionQueue* cq) GRPC_OVERRIDE {
Yang Gao196ade32015-05-05 13:31:12 -0700121 return nullptr;
122 }
123 ClientWriterInterface<EchoRequest>* RequestStreamRaw(
124 ClientContext* context, EchoResponse* response) GRPC_OVERRIDE {
125 return nullptr;
126 }
127 ClientAsyncWriterInterface<EchoRequest>* AsyncRequestStreamRaw(
128 ClientContext* context, EchoResponse* response, CompletionQueue* cq,
129 void* tag) GRPC_OVERRIDE {
130 return nullptr;
131 }
132 ClientReaderInterface<EchoResponse>* ResponseStreamRaw(
133 ClientContext* context, const EchoRequest& request) GRPC_OVERRIDE {
134 return nullptr;
135 }
136 ClientAsyncReaderInterface<EchoResponse>* AsyncResponseStreamRaw(
137 ClientContext* context, const EchoRequest& request, CompletionQueue* cq,
138 void* tag) GRPC_OVERRIDE {
139 return nullptr;
140 }
141 ClientReaderWriterInterface<EchoRequest, EchoResponse>* BidiStreamRaw(
142 ClientContext* context) GRPC_OVERRIDE {
143 return new MockClientReaderWriter<EchoRequest, EchoResponse>();
144 }
145 ClientAsyncReaderWriterInterface<EchoRequest, EchoResponse>*
146 AsyncBidiStreamRaw(ClientContext* context, CompletionQueue* cq,
147 void* tag) GRPC_OVERRIDE {
148 return nullptr;
149 }
150 ClientAsyncResponseReaderInterface<EchoResponse>* AsyncUnimplementedRaw(
Craig Tiller5f871ac2015-05-08 13:05:51 -0700151 ClientContext* context, const EchoRequest& request,
152 CompletionQueue* cq) GRPC_OVERRIDE {
Yang Gao196ade32015-05-05 13:31:12 -0700153 return nullptr;
154 }
155};
156
157class FakeClient {
158 public:
159 explicit FakeClient(TestService::StubInterface* stub) : stub_(stub) {}
160
161 void DoEcho() {
162 ClientContext context;
163 EchoRequest request;
164 EchoResponse response;
165 request.set_message("hello world");
166 Status s = stub_->Echo(&context, request, &response);
167 EXPECT_EQ(request.message(), response.message());
Yang Gaoc1a2c312015-06-16 10:59:46 -0700168 EXPECT_TRUE(s.ok());
Yang Gao196ade32015-05-05 13:31:12 -0700169 }
170
171 void DoBidiStream() {
172 EchoRequest request;
173 EchoResponse response;
174 ClientContext context;
175 grpc::string msg("hello");
176
177 std::unique_ptr<ClientReaderWriterInterface<EchoRequest, EchoResponse>>
178 stream = stub_->BidiStream(&context);
179
180 request.set_message(msg + "0");
181 EXPECT_TRUE(stream->Write(request));
182 EXPECT_TRUE(stream->Read(&response));
183 EXPECT_EQ(response.message(), request.message());
184
185 request.set_message(msg + "1");
186 EXPECT_TRUE(stream->Write(request));
187 EXPECT_TRUE(stream->Read(&response));
188 EXPECT_EQ(response.message(), request.message());
189
190 request.set_message(msg + "2");
191 EXPECT_TRUE(stream->Write(request));
192 EXPECT_TRUE(stream->Read(&response));
193 EXPECT_EQ(response.message(), request.message());
194
195 stream->WritesDone();
196 EXPECT_FALSE(stream->Read(&response));
197
198 Status s = stream->Finish();
Yang Gaoc1a2c312015-06-16 10:59:46 -0700199 EXPECT_TRUE(s.ok());
Yang Gao196ade32015-05-05 13:31:12 -0700200 }
201
202 void ResetStub(TestService::StubInterface* stub) { stub_ = stub; }
203
204 private:
205 TestService::StubInterface* stub_;
206};
207
208class TestServiceImpl : public TestService::Service {
209 public:
210 Status Echo(ServerContext* context, const EchoRequest* request,
211 EchoResponse* response) GRPC_OVERRIDE {
212 response->set_message(request->message());
213 return Status::OK;
214 }
215
216 Status BidiStream(ServerContext* context,
217 ServerReaderWriter<EchoResponse, EchoRequest>* stream)
218 GRPC_OVERRIDE {
219 EchoRequest request;
220 EchoResponse response;
221 while (stream->Read(&request)) {
222 gpr_log(GPR_INFO, "recv msg %s", request.message().c_str());
223 response.set_message(request.message());
224 stream->Write(response);
225 }
226 return Status::OK;
227 }
228};
229
230class MockTest : public ::testing::Test {
231 protected:
Vijay Paie8a7e302015-08-24 10:52:33 -0700232 MockTest() {}
Yang Gao196ade32015-05-05 13:31:12 -0700233
234 void SetUp() GRPC_OVERRIDE {
235 int port = grpc_pick_unused_port_or_die();
236 server_address_ << "localhost:" << port;
237 // Setup server
238 ServerBuilder builder;
239 builder.AddListeningPort(server_address_.str(),
240 InsecureServerCredentials());
241 builder.RegisterService(&service_);
Yang Gao196ade32015-05-05 13:31:12 -0700242 server_ = builder.BuildAndStart();
243 }
244
245 void TearDown() GRPC_OVERRIDE { server_->Shutdown(); }
246
247 void ResetStub() {
yang-g8c2be9f2015-08-19 16:28:09 -0700248 std::shared_ptr<Channel> channel = CreateChannel(
Yang Gao196ade32015-05-05 13:31:12 -0700249 server_address_.str(), InsecureCredentials(), ChannelArguments());
250 stub_ = std::move(grpc::cpp::test::util::TestService::NewStub(channel));
251 }
252
253 std::unique_ptr<grpc::cpp::test::util::TestService::Stub> stub_;
254 std::unique_ptr<Server> server_;
255 std::ostringstream server_address_;
256 TestServiceImpl service_;
Yang Gao196ade32015-05-05 13:31:12 -0700257};
258
259// Do one real rpc and one mocked one
260TEST_F(MockTest, SimpleRpc) {
261 ResetStub();
262 FakeClient client(stub_.get());
263 client.DoEcho();
264 MockStub stub;
265 client.ResetStub(&stub);
266 client.DoEcho();
267}
268
269TEST_F(MockTest, BidiStream) {
270 ResetStub();
271 FakeClient client(stub_.get());
272 client.DoBidiStream();
273 MockStub stub;
274 client.ResetStub(&stub);
275 client.DoBidiStream();
276}
277
278} // namespace
279} // namespace testing
280} // namespace grpc
281
282int main(int argc, char** argv) {
283 grpc_test_init(argc, argv);
284 ::testing::InitGoogleTest(&argc, argv);
285 return RUN_ALL_TESTS();
286}