Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
murgatroid99 | ace28d3 | 2016-01-14 10:12:10 -0800 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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-g | 8c2be9f | 2015-08-19 16:28:09 -0700 | [diff] [blame] | 36 | #include <grpc++/channel.h> |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 37 | #include <grpc++/client_context.h> |
| 38 | #include <grpc++/create_channel.h> |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 39 | #include <grpc++/server.h> |
| 40 | #include <grpc++/server_builder.h> |
| 41 | #include <grpc++/server_context.h> |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 42 | #include <grpc/grpc.h> |
| 43 | #include <grpc/support/thd.h> |
| 44 | #include <grpc/support/time.h> |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 45 | #include <gtest/gtest.h> |
| 46 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 47 | #include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 48 | #include "src/proto/grpc/testing/echo.grpc.pb.h" |
Sree Kuchibhotla | b0d0c8e | 2016-01-13 22:52:17 -0800 | [diff] [blame] | 49 | #include "test/core/util/port.h" |
| 50 | #include "test/core/util/test_config.h" |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 51 | |
Craig Tiller | 1b4e330 | 2015-12-17 16:35:00 -0800 | [diff] [blame] | 52 | using grpc::testing::EchoRequest; |
| 53 | using grpc::testing::EchoResponse; |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 54 | using grpc::testing::EchoTestService; |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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: |
vjpai | 6bf1de9 | 2015-11-02 14:48:57 -0800 | [diff] [blame] | 65 | void WaitForInitialMetadata() GRPC_OVERRIDE {} |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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) {} |
vjpai | 6bf1de9 | 2015-11-02 14:48:57 -0800 | [diff] [blame] | 76 | void WaitForInitialMetadata() GRPC_OVERRIDE {} |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 77 | bool Read(EchoResponse* msg) GRPC_OVERRIDE { |
| 78 | if (writes_done_) return false; |
| 79 | msg->set_message(last_message_); |
| 80 | return true; |
| 81 | } |
David Garcia Quintas | 6a3cf97 | 2015-07-13 13:38:18 -0700 | [diff] [blame] | 82 | |
| 83 | bool Write(const EchoRequest& msg, |
| 84 | const WriteOptions& options) GRPC_OVERRIDE { |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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. |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 101 | class MockStub : public EchoTestService::StubInterface { |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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( |
Craig Tiller | 5f871ac | 2015-05-08 13:05:51 -0700 | [diff] [blame] | 117 | ClientContext* context, const EchoRequest& request, |
| 118 | CompletionQueue* cq) GRPC_OVERRIDE { |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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( |
Craig Tiller | 5f871ac | 2015-05-08 13:05:51 -0700 | [diff] [blame] | 149 | ClientContext* context, const EchoRequest& request, |
| 150 | CompletionQueue* cq) GRPC_OVERRIDE { |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 151 | return nullptr; |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | class FakeClient { |
| 156 | public: |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 157 | explicit FakeClient(EchoTestService::StubInterface* stub) : stub_(stub) {} |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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()); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 166 | EXPECT_TRUE(s.ok()); |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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(); |
Yang Gao | c1a2c31 | 2015-06-16 10:59:46 -0700 | [diff] [blame] | 197 | EXPECT_TRUE(s.ok()); |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 200 | void ResetStub(EchoTestService::StubInterface* stub) { stub_ = stub; } |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 201 | |
| 202 | private: |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 203 | EchoTestService::StubInterface* stub_; |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 206 | class TestServiceImpl : public EchoTestService::Service { |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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: |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 230 | MockTest() {} |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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_); |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 240 | server_ = builder.BuildAndStart(); |
| 241 | } |
| 242 | |
| 243 | void TearDown() GRPC_OVERRIDE { server_->Shutdown(); } |
| 244 | |
| 245 | void ResetStub() { |
yang-g | 730055d | 2015-08-27 12:29:45 -0700 | [diff] [blame] | 246 | std::shared_ptr<Channel> channel = |
Julien Boeuf | e5adc0e | 2015-10-12 14:08:10 -0700 | [diff] [blame] | 247 | CreateChannel(server_address_.str(), InsecureChannelCredentials()); |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 248 | stub_ = grpc::testing::EchoTestService::NewStub(channel); |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Sree Kuchibhotla | 5a05f51 | 2016-01-13 22:43:20 -0800 | [diff] [blame] | 251 | std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 252 | std::unique_ptr<Server> server_; |
| 253 | std::ostringstream server_address_; |
| 254 | TestServiceImpl service_; |
Yang Gao | 196ade3 | 2015-05-05 13:31:12 -0700 | [diff] [blame] | 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(); |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 284 | } |