| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <climits> |
| 20 |
#include <iostream> |
| 21 |
|
| 22 |
#include <gmock/gmock.h> |
| 23 |
#include <gtest/gtest.h> |
| 24 |
|
| 25 |
#include "absl/types/optional.h" |
| 26 |
|
| 27 |
#include <grpc/grpc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
#include <grpc/support/time.h> |
| 30 |
#include <grpcpp/channel.h> |
| 31 |
#include <grpcpp/client_context.h> |
| 32 |
#include <grpcpp/create_channel.h> |
| 33 |
#include <grpcpp/server.h> |
| 34 |
#include <grpcpp/server_builder.h> |
| 35 |
#include <grpcpp/server_context.h> |
| 36 |
#include <grpcpp/test/default_reactor_test_peer.h> |
| 37 |
#include <grpcpp/test/mock_stream.h> |
| 38 |
|
| 39 |
#include "src/proto/grpc/testing/duplicate/echo_duplicate.grpc.pb.h" |
| 40 |
#include "src/proto/grpc/testing/echo.grpc.pb.h" |
| 41 |
#include "src/proto/grpc/testing/echo_mock.grpc.pb.h" |
| 42 |
#include "test/core/util/port.h" |
| 43 |
#include "test/core/util/test_config.h" |
| 44 |
|
| 45 |
using std::vector; |
| 46 |
using ::testing::_; |
| 47 |
using ::testing::AtLeast; |
| 48 |
using ::testing::DoAll; |
| 49 |
using ::testing::Return; |
| 50 |
using ::testing::SaveArg; |
| 51 |
using ::testing::SetArgPointee; |
| 52 |
using ::testing::WithArg; |
| 53 |
|
| 54 |
namespace grpc { |
| 55 |
namespace testing { |
| 56 |
|
| 57 |
namespace { |
| 58 |
class FakeClient { |
| 59 |
public: |
| 60 |
explicit FakeClient(EchoTestService::StubInterface* stub) : stub_(stub) {} |
| 61 |
|
| 62 |
void DoEcho() { |
| 63 |
ClientContext context; |
| 64 |
EchoRequest request; |
| 65 |
EchoResponse response; |
| 66 |
request.set_message("hello world"); |
| 67 |
Status s = stub_->Echo(&context, request, &response); |
| 68 |
EXPECT_EQ(request.message(), response.message()); |
| 69 |
EXPECT_TRUE(s.ok()); |
| 70 |
} |
| 71 |
|
| 72 |
void DoRequestStream() { |
| 73 |
EchoRequest request; |
| 74 |
EchoResponse response; |
| 75 |
|
| 76 |
ClientContext context; |
| 77 |
std::string msg("hello"); |
| 78 |
std::string exp(msg); |
| 79 |
|
| 80 |
std::unique_ptr<ClientWriterInterface<EchoRequest>> cstream = |
| 81 |
stub_->RequestStream(&context, &response); |
| 82 |
|
| 83 |
request.set_message(msg); |
| 84 |
EXPECT_TRUE(cstream->Write(request)); |
| 85 |
|
| 86 |
msg = ", world"; |
| 87 |
request.set_message(msg); |
| 88 |
exp.append(msg); |
| 89 |
EXPECT_TRUE(cstream->Write(request)); |
| 90 |
|
| 91 |
cstream->WritesDone(); |
| 92 |
Status s = cstream->Finish(); |
| 93 |
|
| 94 |
EXPECT_EQ(exp, response.message()); |
| 95 |
EXPECT_TRUE(s.ok()); |
| 96 |
} |
| 97 |
|
| 98 |
void DoResponseStream() { |
| 99 |
EchoRequest request; |
| 100 |
EchoResponse response; |
| 101 |
request.set_message("hello world"); |
| 102 |
|
| 103 |
ClientContext context; |
| 104 |
std::unique_ptr<ClientReaderInterface<EchoResponse>> cstream = |
| 105 |
stub_->ResponseStream(&context, request); |
| 106 |
|
| 107 |
std::string exp = ""; |
| 108 |
EXPECT_TRUE(cstream->Read(&response)); |
| 109 |
exp.append(response.message() + " "); |
| 110 |
|
| 111 |
EXPECT_TRUE(cstream->Read(&response)); |
| 112 |
exp.append(response.message()); |
| 113 |
|
| 114 |
EXPECT_FALSE(cstream->Read(&response)); |
| 115 |
EXPECT_EQ(request.message(), exp); |
| 116 |
|
| 117 |
Status s = cstream->Finish(); |
| 118 |
EXPECT_TRUE(s.ok()); |
| 119 |
} |
| 120 |
|
| 121 |
void DoBidiStream() { |
| 122 |
EchoRequest request; |
| 123 |
EchoResponse response; |
| 124 |
ClientContext context; |
| 125 |
std::string msg("hello"); |
| 126 |
|
| 127 |
std::unique_ptr<ClientReaderWriterInterface<EchoRequest, EchoResponse>> |
| 128 |
stream = stub_->BidiStream(&context); |
| 129 |
|
| 130 |
request.set_message(msg + "0"); |
| 131 |
EXPECT_TRUE(stream->Write(request)); |
| 132 |
EXPECT_TRUE(stream->Read(&response)); |
| 133 |
EXPECT_EQ(response.message(), request.message()); |
| 134 |
|
| 135 |
request.set_message(msg + "1"); |
| 136 |
EXPECT_TRUE(stream->Write(request)); |
| 137 |
EXPECT_TRUE(stream->Read(&response)); |
| 138 |
EXPECT_EQ(response.message(), request.message()); |
| 139 |
|
| 140 |
request.set_message(msg + "2"); |
| 141 |
EXPECT_TRUE(stream->Write(request)); |
| 142 |
EXPECT_TRUE(stream->Read(&response)); |
| 143 |
EXPECT_EQ(response.message(), request.message()); |
| 144 |
|
| 145 |
stream->WritesDone(); |
| 146 |
EXPECT_FALSE(stream->Read(&response)); |
| 147 |
|
| 148 |
Status s = stream->Finish(); |
| 149 |
EXPECT_TRUE(s.ok()); |
| 150 |
} |
| 151 |
|
| 152 |
void ResetStub(EchoTestService::StubInterface* stub) { stub_ = stub; } |
| 153 |
|
| 154 |
private: |
| 155 |
EchoTestService::StubInterface* stub_; |
| 156 |
}; |
| 157 |
|
| 158 |
class CallbackTestServiceImpl : public EchoTestService::CallbackService { |
| 159 |
public: |
| 160 |
ServerUnaryReactor* Echo(CallbackServerContext* context, |
| 161 |
const EchoRequest* request, |
| 162 |
EchoResponse* response) override { |
| 163 |
// Make the mock service explicitly treat empty input messages as invalid |
| 164 |
// arguments so that we can test various results of status. In general, a |
| 165 |
// mocked service should just use the original service methods, but we are |
| 166 |
// adding this variance in Status return value just to improve coverage in |
| 167 |
// this test. |
| 168 |
auto* reactor = context->DefaultReactor(); |
| 169 |
if (request->message().length() > 0) { |
| 170 |
response->set_message(request->message()); |
| 171 |
reactor->Finish(Status::OK); |
| 172 |
} else { |
| 173 |
reactor->Finish(Status(StatusCode::INVALID_ARGUMENT, "Invalid request")); |
| 174 |
} |
| 175 |
return reactor; |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
class MockCallbackTest : public ::testing::Test { |
| 180 |
protected: |
| 181 |
CallbackTestServiceImpl service_; |
| 182 |
ServerContext context_; |
| 183 |
}; |
| 184 |
|
| 185 |
TEST_F(MockCallbackTest, MockedCallSucceedsWithWait) { |
| 186 |
CallbackServerContext ctx; |
| 187 |
EchoRequest req; |
| 188 |
EchoResponse resp; |
| 189 |
struct { |
| 190 |
grpc::internal::Mutex mu; |
| 191 |
grpc::internal::CondVar cv; |
| 192 |
absl::optional<grpc::Status> ABSL_GUARDED_BY(mu) status; |
| 193 |
} status; |
| 194 |
DefaultReactorTestPeer peer(&ctx, [&](grpc::Status s) { |
| 195 |
grpc::internal::MutexLock l(&status.mu); |
| 196 |
status.status = std::move(s); |
| 197 |
status.cv.Signal(); |
| 198 |
}); |
| 199 |
|
| 200 |
req.set_message("mock 1"); |
| 201 |
auto* reactor = service_.Echo(&ctx, &req, &resp); |
| 202 |
|
| 203 |
grpc::internal::MutexLock l(&status.mu); |
| 204 |
while (!status.status.has_value()) { |
| 205 |
status.cv.Wait(&status.mu); |
| 206 |
} |
| 207 |
|
| 208 |
EXPECT_EQ(reactor, peer.reactor()); |
| 209 |
EXPECT_TRUE(peer.test_status_set()); |
| 210 |
EXPECT_TRUE(peer.test_status().ok()); |
| 211 |
EXPECT_TRUE(status.status.has_value()); |
| 212 |
EXPECT_TRUE(status.status.value().ok()); |
| 213 |
EXPECT_EQ(req.message(), resp.message()); |
| 214 |
} |
| 215 |
|
| 216 |
TEST_F(MockCallbackTest, MockedCallSucceeds) { |
| 217 |
CallbackServerContext ctx; |
| 218 |
EchoRequest req; |
| 219 |
EchoResponse resp; |
| 220 |
DefaultReactorTestPeer peer(&ctx); |
| 221 |
|
| 222 |
req.set_message("ha ha, consider yourself mocked."); |
| 223 |
auto* reactor = service_.Echo(&ctx, &req, &resp); |
| 224 |
EXPECT_EQ(reactor, peer.reactor()); |
| 225 |
EXPECT_TRUE(peer.test_status_set()); |
| 226 |
EXPECT_TRUE(peer.test_status().ok()); |
| 227 |
} |
| 228 |
|
| 229 |
TEST_F(MockCallbackTest, MockedCallFails) { |
| 230 |
CallbackServerContext ctx; |
| 231 |
EchoRequest req; |
| 232 |
EchoResponse resp; |
| 233 |
DefaultReactorTestPeer peer(&ctx); |
| 234 |
|
| 235 |
auto* reactor = service_.Echo(&ctx, &req, &resp); |
| 236 |
EXPECT_EQ(reactor, peer.reactor()); |
| 237 |
EXPECT_TRUE(peer.test_status_set()); |
| 238 |
EXPECT_EQ(peer.test_status().error_code(), StatusCode::INVALID_ARGUMENT); |
| 239 |
} |
| 240 |
|
| 241 |
class TestServiceImpl : public EchoTestService::Service { |
| 242 |
public: |
| 243 |
Status Echo(ServerContext* /*context*/, const EchoRequest* request, |
| 244 |
EchoResponse* response) override { |
| 245 |
response->set_message(request->message()); |
| 246 |
return Status::OK; |
| 247 |
} |
| 248 |
|
| 249 |
Status RequestStream(ServerContext* /*context*/, |
| 250 |
ServerReader<EchoRequest>* reader, |
| 251 |
EchoResponse* response) override { |
| 252 |
EchoRequest request; |
| 253 |
std::string resp(""); |
| 254 |
while (reader->Read(&request)) { |
| 255 |
gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 256 |
resp.append(request.message()); |
| 257 |
} |
| 258 |
response->set_message(resp); |
| 259 |
return Status::OK; |
| 260 |
} |
| 261 |
|
| 262 |
Status ResponseStream(ServerContext* /*context*/, const EchoRequest* request, |
| 263 |
ServerWriter<EchoResponse>* writer) override { |
| 264 |
EchoResponse response; |
| 265 |
vector<std::string> tokens = split(request->message()); |
| 266 |
for (const std::string& token : tokens) { |
| 267 |
response.set_message(token); |
| 268 |
writer->Write(response); |
| 269 |
} |
| 270 |
return Status::OK; |
| 271 |
} |
| 272 |
|
| 273 |
Status BidiStream( |
| 274 |
ServerContext* /*context*/, |
| 275 |
ServerReaderWriter<EchoResponse, EchoRequest>* stream) override { |
| 276 |
EchoRequest request; |
| 277 |
EchoResponse response; |
| 278 |
while (stream->Read(&request)) { |
| 279 |
gpr_log(GPR_INFO, "recv msg %s", request.message().c_str()); |
| 280 |
response.set_message(request.message()); |
| 281 |
stream->Write(response); |
| 282 |
} |
| 283 |
return Status::OK; |
| 284 |
} |
| 285 |
|
| 286 |
private: |
| 287 |
vector<std::string> split(const std::string& input) { |
| 288 |
std::string buff(""); |
| 289 |
vector<std::string> result; |
| 290 |
|
| 291 |
for (auto n : input) { |
| 292 |
if (n != ' ') { |
| 293 |
buff += n; |
| 294 |
continue; |
| 295 |
} |
| 296 |
if (buff.empty()) continue; |
| 297 |
result.push_back(buff); |
| 298 |
buff = ""; |
| 299 |
} |
| 300 |
if (!buff.empty()) result.push_back(buff); |
| 301 |
|
| 302 |
return result; |
| 303 |
} |
| 304 |
}; |
| 305 |
|
| 306 |
class MockTest : public ::testing::Test { |
| 307 |
protected: |
| 308 |
MockTest() {} |
| 309 |
|
| 310 |
void SetUp() override { |
| 311 |
int port = grpc_pick_unused_port_or_die(); |
| 312 |
server_address_ << "localhost:" << port; |
| 313 |
// Setup server |
| 314 |
ServerBuilder builder; |
| 315 |
builder.AddListeningPort(server_address_.str(), |
| 316 |
InsecureServerCredentials()); |
| 317 |
builder.RegisterService(&service_); |
| 318 |
server_ = builder.BuildAndStart(); |
| 319 |
} |
| 320 |
|
| 321 |
void TearDown() override { server_->Shutdown(); } |
| 322 |
|
| 323 |
void ResetStub() { |
| 324 |
std::shared_ptr<Channel> channel = grpc::CreateChannel( |
| 325 |
server_address_.str(), InsecureChannelCredentials()); |
| 326 |
stub_ = grpc::testing::EchoTestService::NewStub(channel); |
| 327 |
} |
| 328 |
|
| 329 |
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; |
| 330 |
std::unique_ptr<Server> server_; |
| 331 |
std::ostringstream server_address_; |
| 332 |
TestServiceImpl service_; |
| 333 |
}; |
| 334 |
|
| 335 |
// Do one real rpc and one mocked one |
| 336 |
TEST_F(MockTest, SimpleRpc) { |
| 337 |
ResetStub(); |
| 338 |
FakeClient client(stub_.get()); |
| 339 |
client.DoEcho(); |
| 340 |
MockEchoTestServiceStub stub; |
| 341 |
EchoResponse resp; |
| 342 |
resp.set_message("hello world"); |
| 343 |
EXPECT_CALL(stub, Echo(_, _, _)) |
| 344 |
.Times(AtLeast(1)) |
| 345 |
.WillOnce(DoAll(SetArgPointee<2>(resp), Return(Status::OK))); |
| 346 |
client.ResetStub(&stub); |
| 347 |
client.DoEcho(); |
| 348 |
} |
| 349 |
|
| 350 |
TEST_F(MockTest, ClientStream) { |
| 351 |
ResetStub(); |
| 352 |
FakeClient client(stub_.get()); |
| 353 |
client.DoRequestStream(); |
| 354 |
|
| 355 |
MockEchoTestServiceStub stub; |
| 356 |
auto w = new MockClientWriter<EchoRequest>(); |
| 357 |
EchoResponse resp; |
| 358 |
resp.set_message("hello, world"); |
| 359 |
|
| 360 |
EXPECT_CALL(*w, Write(_, _)).Times(2).WillRepeatedly(Return(true)); |
| 361 |
EXPECT_CALL(*w, WritesDone()); |
| 362 |
EXPECT_CALL(*w, Finish()).WillOnce(Return(Status::OK)); |
| 363 |
|
| 364 |
EXPECT_CALL(stub, RequestStreamRaw(_, _)) |
| 365 |
.WillOnce(DoAll(SetArgPointee<1>(resp), Return(w))); |
| 366 |
client.ResetStub(&stub); |
| 367 |
client.DoRequestStream(); |
| 368 |
} |
| 369 |
|
| 370 |
TEST_F(MockTest, ServerStream) { |
| 371 |
ResetStub(); |
| 372 |
FakeClient client(stub_.get()); |
| 373 |
client.DoResponseStream(); |
| 374 |
|
| 375 |
MockEchoTestServiceStub stub; |
| 376 |
auto r = new MockClientReader<EchoResponse>(); |
| 377 |
EchoResponse resp1; |
| 378 |
resp1.set_message("hello"); |
| 379 |
EchoResponse resp2; |
| 380 |
resp2.set_message("world"); |
| 381 |
|
| 382 |
EXPECT_CALL(*r, Read(_)) |
| 383 |
.WillOnce(DoAll(SetArgPointee<0>(resp1), Return(true))) |
| 384 |
.WillOnce(DoAll(SetArgPointee<0>(resp2), Return(true))) |
| 385 |
.WillOnce(Return(false)); |
| 386 |
EXPECT_CALL(*r, Finish()).WillOnce(Return(Status::OK)); |
| 387 |
|
| 388 |
EXPECT_CALL(stub, ResponseStreamRaw(_, _)).WillOnce(Return(r)); |
| 389 |
|
| 390 |
client.ResetStub(&stub); |
| 391 |
client.DoResponseStream(); |
| 392 |
} |
| 393 |
|
| 394 |
ACTION_P(copy, msg) { arg0->set_message(msg->message()); } |
| 395 |
|
| 396 |
TEST_F(MockTest, BidiStream) { |
| 397 |
ResetStub(); |
| 398 |
FakeClient client(stub_.get()); |
| 399 |
client.DoBidiStream(); |
| 400 |
MockEchoTestServiceStub stub; |
| 401 |
auto rw = new MockClientReaderWriter<EchoRequest, EchoResponse>(); |
| 402 |
EchoRequest msg; |
| 403 |
|
| 404 |
EXPECT_CALL(*rw, Write(_, _)) |
| 405 |
.Times(3) |
| 406 |
.WillRepeatedly(DoAll(SaveArg<0>(&msg), Return(true))); |
| 407 |
EXPECT_CALL(*rw, Read(_)) |
| 408 |
.WillOnce(DoAll(WithArg<0>(copy(&msg)), Return(true))) |
| 409 |
.WillOnce(DoAll(WithArg<0>(copy(&msg)), Return(true))) |
| 410 |
.WillOnce(DoAll(WithArg<0>(copy(&msg)), Return(true))) |
| 411 |
.WillOnce(Return(false)); |
| 412 |
EXPECT_CALL(*rw, WritesDone()); |
| 413 |
EXPECT_CALL(*rw, Finish()).WillOnce(Return(Status::OK)); |
| 414 |
|
| 415 |
EXPECT_CALL(stub, BidiStreamRaw(_)).WillOnce(Return(rw)); |
| 416 |
client.ResetStub(&stub); |
| 417 |
client.DoBidiStream(); |
| 418 |
} |
| 419 |
|
| 420 |
} // namespace |
| 421 |
} // namespace testing |
| 422 |
} // namespace grpc |
| 423 |
|
| 424 |
int main(int argc, char** argv) { |
| 425 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 426 |
::testing::InitGoogleTest(&argc, argv); |
| 427 |
return RUN_ALL_TESTS(); |
| 428 |
} |