3 * Copyright 2016 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/atm.h>
29 #include <grpc/support/log.h>
30 #include <grpc/support/string_util.h>
31 #include <grpc/support/time.h>
32 #include <grpcpp/channel.h>
33 #include <grpcpp/client_context.h>
34 #include <grpcpp/create_channel.h>
35 #include <grpcpp/health_check_service_interface.h>
36 #include <grpcpp/impl/codegen/sync.h>
37 #include <grpcpp/server.h>
38 #include <grpcpp/server_builder.h>
39 #include <grpcpp/support/validate_service_config.h>
41 #include "src/core/ext/filters/client_channel/backup_poller.h"
42 #include "src/core/ext/filters/client_channel/global_subchannel_pool.h"
43 #include "src/core/ext/filters/client_channel/parse_address.h"
44 #include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
45 #include "src/core/ext/filters/client_channel/server_address.h"
46 #include "src/core/lib/backoff/backoff.h"
47 #include "src/core/lib/channel/channel_args.h"
48 #include "src/core/lib/gprpp/debug_location.h"
49 #include "src/core/lib/gprpp/ref_counted_ptr.h"
50 #include "src/core/lib/iomgr/tcp_client.h"
51 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
52 #include "src/cpp/client/secure_credentials.h"
53 #include "src/cpp/server/secure_server_credentials.h"
55 #include "src/proto/grpc/testing/echo.grpc.pb.h"
56 #include "test/core/util/port.h"
57 #include "test/core/util/test_config.h"
58 #include "test/cpp/end2end/test_service_impl.h"
60 #include <gmock/gmock.h>
61 #include <gtest/gtest.h>
63 using grpc::testing::EchoRequest;
64 using grpc::testing::EchoResponse;
65 using std::chrono::system_clock;
71 // Subclass of TestServiceImpl that increments a request counter for
72 // every call to the Echo RPC.
73 class MyTestServiceImpl : public TestServiceImpl {
75 MyTestServiceImpl() : request_count_(0) {}
77 Status Echo(ServerContext* context, const EchoRequest* request,
78 EchoResponse* response) override {
80 grpc::internal::MutexLock lock(&mu_);
83 AddClient(context->peer());
84 return TestServiceImpl::Echo(context, request, response);
88 grpc::internal::MutexLock lock(&mu_);
89 return request_count_;
92 void ResetCounters() {
93 grpc::internal::MutexLock lock(&mu_);
97 std::set<grpc::string> clients() {
98 grpc::internal::MutexLock lock(&clients_mu_);
103 void AddClient(const grpc::string& client) {
104 grpc::internal::MutexLock lock(&clients_mu_);
105 clients_.insert(client);
108 grpc::internal::Mutex mu_;
110 grpc::internal::Mutex clients_mu_;
111 std::set<grpc::string> clients_;
114 class ServiceConfigEnd2endTest : public ::testing::Test {
116 ServiceConfigEnd2endTest()
117 : server_host_("localhost"),
118 kRequestMessage_("Live long and prosper."),
119 creds_(new SecureChannelCredentials(
120 grpc_fake_transport_security_credentials_create())) {}
122 void SetUp() override {
124 response_generator_ =
125 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
128 void TearDown() override {
129 for (size_t i = 0; i < servers_.size(); ++i) {
130 servers_[i]->Shutdown();
132 // Explicitly destroy all the members so that we can make sure grpc_shutdown
133 // has finished by the end of this function, and thus all the registered
134 // LB policy factories are removed.
138 grpc_shutdown_blocking();
141 void CreateServers(size_t num_servers,
142 std::vector<int> ports = std::vector<int>()) {
144 for (size_t i = 0; i < num_servers; ++i) {
146 if (ports.size() == num_servers) port = ports[i];
147 servers_.emplace_back(new ServerData(port));
151 void StartServer(size_t index) { servers_[index]->Start(server_host_); }
153 void StartServers(size_t num_servers,
154 std::vector<int> ports = std::vector<int>()) {
155 CreateServers(num_servers, std::move(ports));
156 for (size_t i = 0; i < num_servers; ++i) {
161 grpc_core::Resolver::Result BuildFakeResults(const std::vector<int>& ports) {
162 grpc_core::Resolver::Result result;
163 for (const int& port : ports) {
165 gpr_asprintf(&lb_uri_str, "ipv4:127.0.0.1:%d", port);
166 grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str, true);
167 GPR_ASSERT(lb_uri != nullptr);
168 grpc_resolved_address address;
169 GPR_ASSERT(grpc_parse_uri(lb_uri, &address));
170 result.addresses.emplace_back(address.addr, address.len,
172 grpc_uri_destroy(lb_uri);
173 gpr_free(lb_uri_str);
178 void SetNextResolutionNoServiceConfig(const std::vector<int>& ports) {
179 grpc_core::ExecCtx exec_ctx;
180 grpc_core::Resolver::Result result = BuildFakeResults(ports);
181 response_generator_->SetResponse(result);
184 void SetNextResolutionValidServiceConfig(const std::vector<int>& ports) {
185 grpc_core::ExecCtx exec_ctx;
186 grpc_core::Resolver::Result result = BuildFakeResults(ports);
187 result.service_config =
188 grpc_core::ServiceConfig::Create("{}", &result.service_config_error);
189 response_generator_->SetResponse(result);
192 void SetNextResolutionInvalidServiceConfig(const std::vector<int>& ports) {
193 grpc_core::ExecCtx exec_ctx;
194 grpc_core::Resolver::Result result = BuildFakeResults(ports);
195 result.service_config =
196 grpc_core::ServiceConfig::Create("{", &result.service_config_error);
197 response_generator_->SetResponse(result);
200 void SetNextResolutionWithServiceConfig(const std::vector<int>& ports,
201 const char* svc_cfg) {
202 grpc_core::ExecCtx exec_ctx;
203 grpc_core::Resolver::Result result = BuildFakeResults(ports);
204 result.service_config =
205 grpc_core::ServiceConfig::Create(svc_cfg, &result.service_config_error);
206 response_generator_->SetResponse(result);
209 std::vector<int> GetServersPorts(size_t start_index = 0) {
210 std::vector<int> ports;
211 for (size_t i = start_index; i < servers_.size(); ++i) {
212 ports.push_back(servers_[i]->port_);
217 std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub(
218 const std::shared_ptr<Channel>& channel) {
219 return grpc::testing::EchoTestService::NewStub(channel);
222 std::shared_ptr<Channel> BuildChannel() {
223 ChannelArguments args;
224 args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
225 response_generator_.get());
226 return ::grpc::CreateCustomChannel("fake:///", creds_, args);
229 std::shared_ptr<Channel> BuildChannelWithDefaultServiceConfig() {
230 ChannelArguments args;
231 EXPECT_THAT(grpc::experimental::ValidateServiceConfigJSON(
232 ValidDefaultServiceConfig()),
233 ::testing::StrEq(""));
234 args.SetServiceConfigJSON(ValidDefaultServiceConfig());
235 args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
236 response_generator_.get());
237 return ::grpc::CreateCustomChannel("fake:///", creds_, args);
240 std::shared_ptr<Channel> BuildChannelWithInvalidDefaultServiceConfig() {
241 ChannelArguments args;
243 grpc::experimental::ValidateServiceConfigJSON(
244 InvalidDefaultServiceConfig()),
245 ::testing::HasSubstr("failed to parse JSON for service config"));
246 args.SetServiceConfigJSON(InvalidDefaultServiceConfig());
247 args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
248 response_generator_.get());
249 return ::grpc::CreateCustomChannel("fake:///", creds_, args);
253 const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
254 EchoResponse* response = nullptr, int timeout_ms = 1000,
255 Status* result = nullptr, bool wait_for_ready = false) {
256 const bool local_response = (response == nullptr);
257 if (local_response) response = new EchoResponse;
259 request.set_message(kRequestMessage_);
260 ClientContext context;
261 context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms));
262 if (wait_for_ready) context.set_wait_for_ready(true);
263 Status status = stub->Echo(&context, request, response);
264 if (result != nullptr) *result = status;
265 if (local_response) delete response;
270 const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
271 const grpc_core::DebugLocation& location, bool wait_for_ready = false) {
272 EchoResponse response;
275 SendRpc(stub, &response, 2000, &status, wait_for_ready);
276 ASSERT_TRUE(success) << "From " << location.file() << ":" << location.line()
278 << "Error: " << status.error_message() << " "
279 << status.error_details();
280 ASSERT_EQ(response.message(), kRequestMessage_)
281 << "From " << location.file() << ":" << location.line();
282 if (!success) abort();
285 void CheckRpcSendFailure(
286 const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub) {
287 const bool success = SendRpc(stub);
288 EXPECT_FALSE(success);
293 std::unique_ptr<Server> server_;
294 MyTestServiceImpl service_;
295 std::unique_ptr<std::thread> thread_;
296 bool server_ready_ = false;
297 bool started_ = false;
299 explicit ServerData(int port = 0) {
300 port_ = port > 0 ? port : grpc_pick_unused_port_or_die();
303 void Start(const grpc::string& server_host) {
304 gpr_log(GPR_INFO, "starting server on port %d", port_);
306 grpc::internal::Mutex mu;
307 grpc::internal::MutexLock lock(&mu);
308 grpc::internal::CondVar cond;
309 thread_.reset(new std::thread(
310 std::bind(&ServerData::Serve, this, server_host, &mu, &cond)));
311 cond.WaitUntil(&mu, [this] { return server_ready_; });
312 server_ready_ = false;
313 gpr_log(GPR_INFO, "server startup complete");
316 void Serve(const grpc::string& server_host, grpc::internal::Mutex* mu,
317 grpc::internal::CondVar* cond) {
318 std::ostringstream server_address;
319 server_address << server_host << ":" << port_;
320 ServerBuilder builder;
321 std::shared_ptr<ServerCredentials> creds(new SecureServerCredentials(
322 grpc_fake_transport_security_server_credentials_create()));
323 builder.AddListeningPort(server_address.str(), std::move(creds));
324 builder.RegisterService(&service_);
325 server_ = builder.BuildAndStart();
326 grpc::internal::MutexLock lock(mu);
327 server_ready_ = true;
332 if (!started_) return;
333 server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));
338 void SetServingStatus(const grpc::string& service, bool serving) {
339 server_->GetHealthCheckService()->SetServingStatus(service, serving);
343 void ResetCounters() {
344 for (const auto& server : servers_) server->service_.ResetCounters();
348 const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
349 size_t server_idx, const grpc_core::DebugLocation& location,
350 bool ignore_failure = false) {
352 if (ignore_failure) {
355 CheckRpcSendOk(stub, location, true);
357 } while (servers_[server_idx]->service_.request_count() == 0);
361 bool WaitForChannelNotReady(Channel* channel, int timeout_seconds = 5) {
362 const gpr_timespec deadline =
363 grpc_timeout_seconds_to_deadline(timeout_seconds);
364 grpc_connectivity_state state;
365 while ((state = channel->GetState(false /* try_to_connect */)) ==
366 GRPC_CHANNEL_READY) {
367 if (!channel->WaitForStateChange(state, deadline)) return false;
372 bool WaitForChannelReady(Channel* channel, int timeout_seconds = 5) {
373 const gpr_timespec deadline =
374 grpc_timeout_seconds_to_deadline(timeout_seconds);
375 grpc_connectivity_state state;
376 while ((state = channel->GetState(true /* try_to_connect */)) !=
377 GRPC_CHANNEL_READY) {
378 if (!channel->WaitForStateChange(state, deadline)) return false;
383 bool SeenAllServers() {
384 for (const auto& server : servers_) {
385 if (server->service_.request_count() == 0) return false;
390 // Updates \a connection_order by appending to it the index of the newly
391 // connected server. Must be called after every single RPC.
392 void UpdateConnectionOrder(
393 const std::vector<std::unique_ptr<ServerData>>& servers,
394 std::vector<int>* connection_order) {
395 for (size_t i = 0; i < servers.size(); ++i) {
396 if (servers[i]->service_.request_count() == 1) {
397 // Was the server index known? If not, update connection_order.
399 std::find(connection_order->begin(), connection_order->end(), i);
400 if (it == connection_order->end()) {
401 connection_order->push_back(i);
408 const char* ValidServiceConfigV1() { return "{\"version\": \"1\"}"; }
410 const char* ValidServiceConfigV2() { return "{\"version\": \"2\"}"; }
412 const char* ValidDefaultServiceConfig() {
413 return "{\"version\": \"valid_default\"}";
416 const char* InvalidDefaultServiceConfig() {
417 return "{\"version\": \"invalid_default\"";
420 const grpc::string server_host_;
421 std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
422 std::vector<std::unique_ptr<ServerData>> servers_;
423 grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
425 const grpc::string kRequestMessage_;
426 std::shared_ptr<ChannelCredentials> creds_;
429 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigTest) {
431 auto channel = BuildChannel();
432 auto stub = BuildStub(channel);
433 SetNextResolutionNoServiceConfig(GetServersPorts());
434 CheckRpcSendOk(stub, DEBUG_LOCATION);
435 EXPECT_STREQ("", channel->GetServiceConfigJSON().c_str());
438 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigWithDefaultConfigTest) {
440 auto channel = BuildChannelWithDefaultServiceConfig();
441 auto stub = BuildStub(channel);
442 SetNextResolutionNoServiceConfig(GetServersPorts());
443 CheckRpcSendOk(stub, DEBUG_LOCATION);
444 EXPECT_STREQ(ValidDefaultServiceConfig(),
445 channel->GetServiceConfigJSON().c_str());
448 TEST_F(ServiceConfigEnd2endTest, InvalidServiceConfigTest) {
450 auto channel = BuildChannel();
451 auto stub = BuildStub(channel);
452 SetNextResolutionInvalidServiceConfig(GetServersPorts());
453 CheckRpcSendFailure(stub);
456 TEST_F(ServiceConfigEnd2endTest, InvalidServiceConfigWithDefaultConfigTest) {
458 auto channel = BuildChannelWithDefaultServiceConfig();
459 auto stub = BuildStub(channel);
460 SetNextResolutionInvalidServiceConfig(GetServersPorts());
461 CheckRpcSendOk(stub, DEBUG_LOCATION);
462 EXPECT_STREQ(ValidDefaultServiceConfig(),
463 channel->GetServiceConfigJSON().c_str());
466 TEST_F(ServiceConfigEnd2endTest, ValidServiceConfigUpdatesTest) {
468 auto channel = BuildChannel();
469 auto stub = BuildStub(channel);
470 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
471 CheckRpcSendOk(stub, DEBUG_LOCATION);
472 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
473 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV2());
474 CheckRpcSendOk(stub, DEBUG_LOCATION);
475 EXPECT_STREQ(ValidServiceConfigV2(), channel->GetServiceConfigJSON().c_str());
478 TEST_F(ServiceConfigEnd2endTest,
479 NoServiceConfigUpdateAfterValidServiceConfigTest) {
481 auto channel = BuildChannel();
482 auto stub = BuildStub(channel);
483 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
484 CheckRpcSendOk(stub, DEBUG_LOCATION);
485 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
486 SetNextResolutionNoServiceConfig(GetServersPorts());
487 CheckRpcSendOk(stub, DEBUG_LOCATION);
488 EXPECT_STREQ("", channel->GetServiceConfigJSON().c_str());
491 TEST_F(ServiceConfigEnd2endTest,
492 NoServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest) {
494 auto channel = BuildChannelWithDefaultServiceConfig();
495 auto stub = BuildStub(channel);
496 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
497 CheckRpcSendOk(stub, DEBUG_LOCATION);
498 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
499 SetNextResolutionNoServiceConfig(GetServersPorts());
500 CheckRpcSendOk(stub, DEBUG_LOCATION);
501 EXPECT_STREQ(ValidDefaultServiceConfig(),
502 channel->GetServiceConfigJSON().c_str());
505 TEST_F(ServiceConfigEnd2endTest,
506 InvalidServiceConfigUpdateAfterValidServiceConfigTest) {
508 auto channel = BuildChannel();
509 auto stub = BuildStub(channel);
510 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
511 CheckRpcSendOk(stub, DEBUG_LOCATION);
512 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
513 SetNextResolutionInvalidServiceConfig(GetServersPorts());
514 CheckRpcSendOk(stub, DEBUG_LOCATION);
515 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
518 TEST_F(ServiceConfigEnd2endTest,
519 InvalidServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest) {
521 auto channel = BuildChannelWithDefaultServiceConfig();
522 auto stub = BuildStub(channel);
523 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
524 CheckRpcSendOk(stub, DEBUG_LOCATION);
525 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
526 SetNextResolutionInvalidServiceConfig(GetServersPorts());
527 CheckRpcSendOk(stub, DEBUG_LOCATION);
528 EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
531 TEST_F(ServiceConfigEnd2endTest,
532 ValidServiceConfigAfterInvalidServiceConfigTest) {
534 auto channel = BuildChannel();
535 auto stub = BuildStub(channel);
536 SetNextResolutionInvalidServiceConfig(GetServersPorts());
537 CheckRpcSendFailure(stub);
538 SetNextResolutionValidServiceConfig(GetServersPorts());
539 CheckRpcSendOk(stub, DEBUG_LOCATION);
542 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigAfterInvalidServiceConfigTest) {
544 auto channel = BuildChannel();
545 auto stub = BuildStub(channel);
546 SetNextResolutionInvalidServiceConfig(GetServersPorts());
547 CheckRpcSendFailure(stub);
548 SetNextResolutionNoServiceConfig(GetServersPorts());
549 CheckRpcSendOk(stub, DEBUG_LOCATION);
550 EXPECT_STREQ("", channel->GetServiceConfigJSON().c_str());
553 TEST_F(ServiceConfigEnd2endTest,
554 AnotherInvalidServiceConfigAfterInvalidServiceConfigTest) {
556 auto channel = BuildChannel();
557 auto stub = BuildStub(channel);
558 SetNextResolutionInvalidServiceConfig(GetServersPorts());
559 CheckRpcSendFailure(stub);
560 SetNextResolutionInvalidServiceConfig(GetServersPorts());
561 CheckRpcSendFailure(stub);
564 TEST_F(ServiceConfigEnd2endTest, InvalidDefaultServiceConfigTest) {
566 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
567 auto stub = BuildStub(channel);
568 // An invalid default service config results in a lame channel which fails all
570 CheckRpcSendFailure(stub);
573 TEST_F(ServiceConfigEnd2endTest,
574 InvalidDefaultServiceConfigTestWithValidServiceConfig) {
576 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
577 auto stub = BuildStub(channel);
578 CheckRpcSendFailure(stub);
579 // An invalid default service config results in a lame channel which fails all
581 SetNextResolutionValidServiceConfig(GetServersPorts());
582 CheckRpcSendFailure(stub);
585 TEST_F(ServiceConfigEnd2endTest,
586 InvalidDefaultServiceConfigTestWithInvalidServiceConfig) {
588 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
589 auto stub = BuildStub(channel);
590 CheckRpcSendFailure(stub);
591 // An invalid default service config results in a lame channel which fails all
593 SetNextResolutionInvalidServiceConfig(GetServersPorts());
594 CheckRpcSendFailure(stub);
597 TEST_F(ServiceConfigEnd2endTest,
598 InvalidDefaultServiceConfigTestWithNoServiceConfig) {
600 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
601 auto stub = BuildStub(channel);
602 CheckRpcSendFailure(stub);
603 // An invalid default service config results in a lame channel which fails all
605 SetNextResolutionNoServiceConfig(GetServersPorts());
606 CheckRpcSendFailure(stub);
610 } // namespace testing
613 int main(int argc, char** argv) {
614 // Make the backup poller poll very frequently in order to pick up
615 // updates from all the subchannels's FDs.
616 GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1);
617 ::testing::InitGoogleTest(&argc, argv);
618 grpc::testing::TestEnvironment env(argc, argv);
619 const auto result = RUN_ALL_TESTS();