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