xref: /aosp_15_r20/external/grpc-grpc/test/cpp/end2end/service_config_end2end_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2016 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 <algorithm>
20 #include <memory>
21 #include <mutex>
22 #include <random>
23 #include <set>
24 #include <string>
25 #include <thread>
26 
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 
30 #include "absl/memory/memory.h"
31 #include "absl/strings/str_cat.h"
32 
33 #include <grpc/grpc.h>
34 #include <grpc/support/alloc.h>
35 #include <grpc/support/atm.h>
36 #include <grpc/support/log.h>
37 #include <grpc/support/time.h>
38 #include <grpcpp/channel.h>
39 #include <grpcpp/client_context.h>
40 #include <grpcpp/create_channel.h>
41 #include <grpcpp/health_check_service_interface.h>
42 #include <grpcpp/impl/sync.h>
43 #include <grpcpp/server.h>
44 #include <grpcpp/server_builder.h>
45 #include <grpcpp/support/validate_service_config.h>
46 
47 #include "src/core/client_channel/backup_poller.h"
48 #include "src/core/client_channel/global_subchannel_pool.h"
49 #include "src/core/lib/address_utils/parse_address.h"
50 #include "src/core/lib/backoff/backoff.h"
51 #include "src/core/lib/channel/channel_args.h"
52 #include "src/core/lib/config/config_vars.h"
53 #include "src/core/lib/gprpp/crash.h"
54 #include "src/core/lib/gprpp/debug_location.h"
55 #include "src/core/lib/gprpp/ref_counted_ptr.h"
56 #include "src/core/lib/iomgr/tcp_client.h"
57 #include "src/core/lib/security/credentials/fake/fake_credentials.h"
58 #include "src/core/lib/transport/error_utils.h"
59 #include "src/core/resolver/endpoint_addresses.h"
60 #include "src/core/resolver/fake/fake_resolver.h"
61 #include "src/core/service_config/service_config_impl.h"
62 #include "src/cpp/server/secure_server_credentials.h"
63 #include "src/proto/grpc/testing/echo.grpc.pb.h"
64 #include "test/core/util/port.h"
65 #include "test/core/util/resolve_localhost_ip46.h"
66 #include "test/core/util/test_config.h"
67 #include "test/cpp/end2end/test_service_impl.h"
68 #include "test/cpp/util/credentials.h"
69 
70 namespace grpc {
71 namespace testing {
72 namespace {
73 
74 // Subclass of TestServiceImpl that increments a request counter for
75 // every call to the Echo RPC.
76 class MyTestServiceImpl : public TestServiceImpl {
77  public:
MyTestServiceImpl()78   MyTestServiceImpl() : request_count_(0) {}
79 
Echo(ServerContext * context,const EchoRequest * request,EchoResponse * response)80   Status Echo(ServerContext* context, const EchoRequest* request,
81               EchoResponse* response) override {
82     {
83       grpc::internal::MutexLock lock(&mu_);
84       ++request_count_;
85     }
86     AddClient(context->peer());
87     return TestServiceImpl::Echo(context, request, response);
88   }
89 
request_count()90   int request_count() {
91     grpc::internal::MutexLock lock(&mu_);
92     return request_count_;
93   }
94 
ResetCounters()95   void ResetCounters() {
96     grpc::internal::MutexLock lock(&mu_);
97     request_count_ = 0;
98   }
99 
clients()100   std::set<std::string> clients() {
101     grpc::internal::MutexLock lock(&clients_mu_);
102     return clients_;
103   }
104 
105  private:
AddClient(const std::string & client)106   void AddClient(const std::string& client) {
107     grpc::internal::MutexLock lock(&clients_mu_);
108     clients_.insert(client);
109   }
110 
111   grpc::internal::Mutex mu_;
112   int request_count_;
113   grpc::internal::Mutex clients_mu_;
114   std::set<std::string> clients_;
115 };
116 
117 class ServiceConfigEnd2endTest : public ::testing::Test {
118  protected:
ServiceConfigEnd2endTest()119   ServiceConfigEnd2endTest()
120       : server_host_("localhost"),
121         kRequestMessage_("Live long and prosper."),
122         creds_(std::make_shared<FakeTransportSecurityChannelCredentials>()) {}
123 
SetUpTestSuite()124   static void SetUpTestSuite() {
125     // Make the backup poller poll very frequently in order to pick up
126     // updates from all the subchannels's FDs.
127     grpc_core::ConfigVars::Overrides overrides;
128     overrides.client_channel_backup_poll_interval_ms = 1;
129     grpc_core::ConfigVars::SetOverrides(overrides);
130   }
131 
SetUp()132   void SetUp() override {
133     grpc_init();
134     response_generator_ =
135         grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
136   }
137 
TearDown()138   void TearDown() override {
139     for (size_t i = 0; i < servers_.size(); ++i) {
140       servers_[i]->Shutdown();
141     }
142     // Explicitly destroy all the members so that we can make sure grpc_shutdown
143     // has finished by the end of this function, and thus all the registered
144     // LB policy factories are removed.
145     stub_.reset();
146     servers_.clear();
147     creds_.reset();
148     grpc_shutdown();
149   }
150 
CreateServers(size_t num_servers,std::vector<int> ports=std::vector<int> ())151   void CreateServers(size_t num_servers,
152                      std::vector<int> ports = std::vector<int>()) {
153     servers_.clear();
154     for (size_t i = 0; i < num_servers; ++i) {
155       int port = 0;
156       if (ports.size() == num_servers) port = ports[i];
157       servers_.emplace_back(new ServerData(port));
158     }
159   }
160 
StartServer(size_t index)161   void StartServer(size_t index) { servers_[index]->Start(server_host_); }
162 
StartServers(size_t num_servers,std::vector<int> ports=std::vector<int> ())163   void StartServers(size_t num_servers,
164                     std::vector<int> ports = std::vector<int>()) {
165     CreateServers(num_servers, std::move(ports));
166     for (size_t i = 0; i < num_servers; ++i) {
167       StartServer(i);
168     }
169   }
170 
BuildFakeResults(const std::vector<int> & ports)171   grpc_core::Resolver::Result BuildFakeResults(const std::vector<int>& ports) {
172     grpc_core::Resolver::Result result;
173     result.addresses = grpc_core::EndpointAddressesList();
174     for (const int& port : ports) {
175       absl::StatusOr<grpc_core::URI> lb_uri =
176           grpc_core::URI::Parse(grpc_core::LocalIpUri(port));
177       GPR_ASSERT(lb_uri.ok());
178       grpc_resolved_address address;
179       GPR_ASSERT(grpc_parse_uri(*lb_uri, &address));
180       result.addresses->emplace_back(address, grpc_core::ChannelArgs());
181     }
182     return result;
183   }
184 
SetNextResolutionNoServiceConfig(const std::vector<int> & ports)185   void SetNextResolutionNoServiceConfig(const std::vector<int>& ports) {
186     grpc_core::ExecCtx exec_ctx;
187     grpc_core::Resolver::Result result = BuildFakeResults(ports);
188     response_generator_->SetResponseSynchronously(result);
189   }
190 
SetNextResolutionValidServiceConfig(const std::vector<int> & ports)191   void SetNextResolutionValidServiceConfig(const std::vector<int>& ports) {
192     grpc_core::ExecCtx exec_ctx;
193     grpc_core::Resolver::Result result = BuildFakeResults(ports);
194     result.service_config =
195         grpc_core::ServiceConfigImpl::Create(grpc_core::ChannelArgs(), "{}");
196     ASSERT_TRUE(result.service_config.ok()) << result.service_config.status();
197     response_generator_->SetResponseSynchronously(result);
198   }
199 
SetNextResolutionInvalidServiceConfig(const std::vector<int> & ports)200   void SetNextResolutionInvalidServiceConfig(const std::vector<int>& ports) {
201     grpc_core::ExecCtx exec_ctx;
202     grpc_core::Resolver::Result result = BuildFakeResults(ports);
203     result.service_config =
204         absl::InvalidArgumentError("error parsing service config");
205     response_generator_->SetResponseSynchronously(result);
206   }
207 
SetNextResolutionWithServiceConfig(const std::vector<int> & ports,const char * svc_cfg)208   void SetNextResolutionWithServiceConfig(const std::vector<int>& ports,
209                                           const char* svc_cfg) {
210     grpc_core::ExecCtx exec_ctx;
211     grpc_core::Resolver::Result result = BuildFakeResults(ports);
212     result.service_config =
213         grpc_core::ServiceConfigImpl::Create(grpc_core::ChannelArgs(), svc_cfg);
214     response_generator_->SetResponseSynchronously(result);
215   }
216 
GetServersPorts(size_t start_index=0)217   std::vector<int> GetServersPorts(size_t start_index = 0) {
218     std::vector<int> ports;
219     for (size_t i = start_index; i < servers_.size(); ++i) {
220       ports.push_back(servers_[i]->port_);
221     }
222     return ports;
223   }
224 
BuildStub(const std::shared_ptr<Channel> & channel)225   std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub(
226       const std::shared_ptr<Channel>& channel) {
227     return grpc::testing::EchoTestService::NewStub(channel);
228   }
229 
BuildChannel()230   std::shared_ptr<Channel> BuildChannel() {
231     ChannelArguments args;
232     args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
233                     response_generator_.get());
234     return grpc::CreateCustomChannel("fake:///", creds_, args);
235   }
236 
BuildChannelWithDefaultServiceConfig()237   std::shared_ptr<Channel> BuildChannelWithDefaultServiceConfig() {
238     ChannelArguments args;
239     EXPECT_THAT(grpc::experimental::ValidateServiceConfigJSON(
240                     ValidDefaultServiceConfig()),
241                 ::testing::StrEq(""));
242     args.SetServiceConfigJSON(ValidDefaultServiceConfig());
243     args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
244                     response_generator_.get());
245     return grpc::CreateCustomChannel("fake:///", creds_, args);
246   }
247 
BuildChannelWithInvalidDefaultServiceConfig()248   std::shared_ptr<Channel> BuildChannelWithInvalidDefaultServiceConfig() {
249     ChannelArguments args;
250     EXPECT_THAT(grpc::experimental::ValidateServiceConfigJSON(
251                     InvalidDefaultServiceConfig()),
252                 ::testing::HasSubstr("JSON parse error"));
253     args.SetServiceConfigJSON(InvalidDefaultServiceConfig());
254     args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
255                     response_generator_.get());
256     return grpc::CreateCustomChannel("fake:///", creds_, args);
257   }
258 
SendRpc(const std::unique_ptr<grpc::testing::EchoTestService::Stub> & stub,EchoResponse * response=nullptr,int timeout_ms=1000,Status * result=nullptr,bool wait_for_ready=false)259   bool SendRpc(
260       const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
261       EchoResponse* response = nullptr, int timeout_ms = 1000,
262       Status* result = nullptr, bool wait_for_ready = false) {
263     const bool local_response = (response == nullptr);
264     if (local_response) response = new EchoResponse;
265     EchoRequest request;
266     request.set_message(kRequestMessage_);
267     ClientContext context;
268     context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms));
269     if (wait_for_ready) context.set_wait_for_ready(true);
270     Status status = stub->Echo(&context, request, response);
271     if (result != nullptr) *result = status;
272     if (local_response) delete response;
273     return status.ok();
274   }
275 
CheckRpcSendOk(const std::unique_ptr<grpc::testing::EchoTestService::Stub> & stub,const grpc_core::DebugLocation & location,bool wait_for_ready=false)276   void CheckRpcSendOk(
277       const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
278       const grpc_core::DebugLocation& location, bool wait_for_ready = false) {
279     EchoResponse response;
280     Status status;
281     const bool success =
282         SendRpc(stub, &response, 2000, &status, wait_for_ready);
283     ASSERT_TRUE(success) << "From " << location.file() << ":" << location.line()
284                          << "\n"
285                          << "Error: " << status.error_message() << " "
286                          << status.error_details();
287     ASSERT_EQ(response.message(), kRequestMessage_)
288         << "From " << location.file() << ":" << location.line();
289     if (!success) abort();
290   }
291 
CheckRpcSendFailure(const std::unique_ptr<grpc::testing::EchoTestService::Stub> & stub)292   void CheckRpcSendFailure(
293       const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub) {
294     const bool success = SendRpc(stub);
295     EXPECT_FALSE(success);
296   }
297 
298   struct ServerData {
299     const int port_;
300     std::unique_ptr<Server> server_;
301     MyTestServiceImpl service_;
302     std::unique_ptr<std::thread> thread_;
303 
304     grpc::internal::Mutex mu_;
305     grpc::internal::CondVar cond_;
306     bool server_ready_ ABSL_GUARDED_BY(mu_) = false;
307     bool started_ ABSL_GUARDED_BY(mu_) = false;
308 
ServerDatagrpc::testing::__anon50cf22e30111::ServiceConfigEnd2endTest::ServerData309     explicit ServerData(int port = 0)
310         : port_(port > 0 ? port : grpc_pick_unused_port_or_die()) {}
311 
Startgrpc::testing::__anon50cf22e30111::ServiceConfigEnd2endTest::ServerData312     void Start(const std::string& server_host) {
313       gpr_log(GPR_INFO, "starting server on port %d", port_);
314       grpc::internal::MutexLock lock(&mu_);
315       started_ = true;
316       thread_ = std::make_unique<std::thread>(
317           std::bind(&ServerData::Serve, this, server_host));
318       while (!server_ready_) {
319         cond_.Wait(&mu_);
320       }
321       server_ready_ = false;
322       gpr_log(GPR_INFO, "server startup complete");
323     }
324 
Servegrpc::testing::__anon50cf22e30111::ServiceConfigEnd2endTest::ServerData325     void Serve(const std::string& server_host) {
326       std::ostringstream server_address;
327       server_address << server_host << ":" << port_;
328       ServerBuilder builder;
329       std::shared_ptr<ServerCredentials> creds(new SecureServerCredentials(
330           grpc_fake_transport_security_server_credentials_create()));
331       builder.AddListeningPort(server_address.str(), std::move(creds));
332       builder.RegisterService(&service_);
333       server_ = builder.BuildAndStart();
334       grpc::internal::MutexLock lock(&mu_);
335       server_ready_ = true;
336       cond_.Signal();
337     }
338 
Shutdowngrpc::testing::__anon50cf22e30111::ServiceConfigEnd2endTest::ServerData339     void Shutdown() {
340       grpc::internal::MutexLock lock(&mu_);
341       if (!started_) return;
342       server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));
343       thread_->join();
344       started_ = false;
345     }
346 
SetServingStatusgrpc::testing::__anon50cf22e30111::ServiceConfigEnd2endTest::ServerData347     void SetServingStatus(const std::string& service, bool serving) {
348       server_->GetHealthCheckService()->SetServingStatus(service, serving);
349     }
350   };
351 
ResetCounters()352   void ResetCounters() {
353     for (const auto& server : servers_) server->service_.ResetCounters();
354   }
355 
WaitForServer(const std::unique_ptr<grpc::testing::EchoTestService::Stub> & stub,size_t server_idx,const grpc_core::DebugLocation & location,bool ignore_failure=false)356   void WaitForServer(
357       const std::unique_ptr<grpc::testing::EchoTestService::Stub>& stub,
358       size_t server_idx, const grpc_core::DebugLocation& location,
359       bool ignore_failure = false) {
360     do {
361       if (ignore_failure) {
362         SendRpc(stub);
363       } else {
364         CheckRpcSendOk(stub, location, true);
365       }
366     } while (servers_[server_idx]->service_.request_count() == 0);
367     ResetCounters();
368   }
369 
WaitForChannelNotReady(Channel * channel,int timeout_seconds=5)370   bool WaitForChannelNotReady(Channel* channel, int timeout_seconds = 5) {
371     const gpr_timespec deadline =
372         grpc_timeout_seconds_to_deadline(timeout_seconds);
373     grpc_connectivity_state state;
374     while ((state = channel->GetState(false /* try_to_connect */)) ==
375            GRPC_CHANNEL_READY) {
376       if (!channel->WaitForStateChange(state, deadline)) return false;
377     }
378     return true;
379   }
380 
WaitForChannelReady(Channel * channel,int timeout_seconds=5)381   bool WaitForChannelReady(Channel* channel, int timeout_seconds = 5) {
382     const gpr_timespec deadline =
383         grpc_timeout_seconds_to_deadline(timeout_seconds);
384     grpc_connectivity_state state;
385     while ((state = channel->GetState(true /* try_to_connect */)) !=
386            GRPC_CHANNEL_READY) {
387       if (!channel->WaitForStateChange(state, deadline)) return false;
388     }
389     return true;
390   }
391 
SeenAllServers()392   bool SeenAllServers() {
393     for (const auto& server : servers_) {
394       if (server->service_.request_count() == 0) return false;
395     }
396     return true;
397   }
398 
399   // Updates \a connection_order by appending to it the index of the newly
400   // connected server. Must be called after every single RPC.
UpdateConnectionOrder(const std::vector<std::unique_ptr<ServerData>> & servers,std::vector<int> * connection_order)401   void UpdateConnectionOrder(
402       const std::vector<std::unique_ptr<ServerData>>& servers,
403       std::vector<int>* connection_order) {
404     for (size_t i = 0; i < servers.size(); ++i) {
405       if (servers[i]->service_.request_count() == 1) {
406         // Was the server index known? If not, update connection_order.
407         const auto it =
408             std::find(connection_order->begin(), connection_order->end(), i);
409         if (it == connection_order->end()) {
410           connection_order->push_back(i);
411           return;
412         }
413       }
414     }
415   }
416 
ValidServiceConfigV1()417   const char* ValidServiceConfigV1() { return "{\"version\": \"1\"}"; }
418 
ValidServiceConfigV2()419   const char* ValidServiceConfigV2() { return "{\"version\": \"2\"}"; }
420 
ValidDefaultServiceConfig()421   const char* ValidDefaultServiceConfig() {
422     return "{\"version\": \"valid_default\"}";
423   }
424 
InvalidDefaultServiceConfig()425   const char* InvalidDefaultServiceConfig() {
426     return "{\"version\": \"invalid_default\"";
427   }
428 
429   const std::string server_host_;
430   std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
431   std::vector<std::unique_ptr<ServerData>> servers_;
432   grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
433       response_generator_;
434   const std::string kRequestMessage_;
435   std::shared_ptr<ChannelCredentials> creds_;
436 };
437 
TEST_F(ServiceConfigEnd2endTest,NoServiceConfigTest)438 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigTest) {
439   StartServers(1);
440   auto channel = BuildChannel();
441   auto stub = BuildStub(channel);
442   SetNextResolutionNoServiceConfig(GetServersPorts());
443   CheckRpcSendOk(stub, DEBUG_LOCATION);
444   EXPECT_STREQ("{}", channel->GetServiceConfigJSON().c_str());
445 }
446 
TEST_F(ServiceConfigEnd2endTest,NoServiceConfigWithDefaultConfigTest)447 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigWithDefaultConfigTest) {
448   StartServers(1);
449   auto channel = BuildChannelWithDefaultServiceConfig();
450   auto stub = BuildStub(channel);
451   SetNextResolutionNoServiceConfig(GetServersPorts());
452   CheckRpcSendOk(stub, DEBUG_LOCATION);
453   EXPECT_STREQ(ValidDefaultServiceConfig(),
454                channel->GetServiceConfigJSON().c_str());
455 }
456 
TEST_F(ServiceConfigEnd2endTest,InvalidServiceConfigTest)457 TEST_F(ServiceConfigEnd2endTest, InvalidServiceConfigTest) {
458   StartServers(1);
459   auto channel = BuildChannel();
460   auto stub = BuildStub(channel);
461   SetNextResolutionInvalidServiceConfig(GetServersPorts());
462   CheckRpcSendFailure(stub);
463 }
464 
TEST_F(ServiceConfigEnd2endTest,ValidServiceConfigUpdatesTest)465 TEST_F(ServiceConfigEnd2endTest, ValidServiceConfigUpdatesTest) {
466   StartServers(1);
467   auto channel = BuildChannel();
468   auto stub = BuildStub(channel);
469   SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
470   CheckRpcSendOk(stub, DEBUG_LOCATION);
471   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
472   SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV2());
473   CheckRpcSendOk(stub, DEBUG_LOCATION);
474   EXPECT_STREQ(ValidServiceConfigV2(), channel->GetServiceConfigJSON().c_str());
475 }
476 
TEST_F(ServiceConfigEnd2endTest,NoServiceConfigUpdateAfterValidServiceConfigTest)477 TEST_F(ServiceConfigEnd2endTest,
478        NoServiceConfigUpdateAfterValidServiceConfigTest) {
479   StartServers(1);
480   auto channel = BuildChannel();
481   auto stub = BuildStub(channel);
482   SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
483   CheckRpcSendOk(stub, DEBUG_LOCATION);
484   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
485   SetNextResolutionNoServiceConfig(GetServersPorts());
486   CheckRpcSendOk(stub, DEBUG_LOCATION);
487   EXPECT_STREQ("{}", channel->GetServiceConfigJSON().c_str());
488 }
489 
TEST_F(ServiceConfigEnd2endTest,NoServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest)490 TEST_F(ServiceConfigEnd2endTest,
491        NoServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest) {
492   StartServers(1);
493   auto channel = BuildChannelWithDefaultServiceConfig();
494   auto stub = BuildStub(channel);
495   SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
496   CheckRpcSendOk(stub, DEBUG_LOCATION);
497   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
498   SetNextResolutionNoServiceConfig(GetServersPorts());
499   CheckRpcSendOk(stub, DEBUG_LOCATION);
500   EXPECT_STREQ(ValidDefaultServiceConfig(),
501                channel->GetServiceConfigJSON().c_str());
502 }
503 
TEST_F(ServiceConfigEnd2endTest,InvalidServiceConfigUpdateAfterValidServiceConfigTest)504 TEST_F(ServiceConfigEnd2endTest,
505        InvalidServiceConfigUpdateAfterValidServiceConfigTest) {
506   StartServers(1);
507   auto channel = BuildChannel();
508   auto stub = BuildStub(channel);
509   SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
510   CheckRpcSendOk(stub, DEBUG_LOCATION);
511   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
512   SetNextResolutionInvalidServiceConfig(GetServersPorts());
513   CheckRpcSendOk(stub, DEBUG_LOCATION);
514   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
515 }
516 
TEST_F(ServiceConfigEnd2endTest,InvalidServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest)517 TEST_F(ServiceConfigEnd2endTest,
518        InvalidServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest) {
519   StartServers(1);
520   auto channel = BuildChannelWithDefaultServiceConfig();
521   auto stub = BuildStub(channel);
522   SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
523   CheckRpcSendOk(stub, DEBUG_LOCATION);
524   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
525   SetNextResolutionInvalidServiceConfig(GetServersPorts());
526   CheckRpcSendOk(stub, DEBUG_LOCATION);
527   EXPECT_STREQ(ValidServiceConfigV1(), channel->GetServiceConfigJSON().c_str());
528 }
529 
TEST_F(ServiceConfigEnd2endTest,ValidServiceConfigAfterInvalidServiceConfigTest)530 TEST_F(ServiceConfigEnd2endTest,
531        ValidServiceConfigAfterInvalidServiceConfigTest) {
532   StartServers(1);
533   auto channel = BuildChannel();
534   auto stub = BuildStub(channel);
535   SetNextResolutionInvalidServiceConfig(GetServersPorts());
536   CheckRpcSendFailure(stub);
537   SetNextResolutionValidServiceConfig(GetServersPorts());
538   CheckRpcSendOk(stub, DEBUG_LOCATION);
539 }
540 
TEST_F(ServiceConfigEnd2endTest,NoServiceConfigAfterInvalidServiceConfigTest)541 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigAfterInvalidServiceConfigTest) {
542   StartServers(1);
543   auto channel = BuildChannel();
544   auto stub = BuildStub(channel);
545   SetNextResolutionInvalidServiceConfig(GetServersPorts());
546   CheckRpcSendFailure(stub);
547   SetNextResolutionNoServiceConfig(GetServersPorts());
548   CheckRpcSendOk(stub, DEBUG_LOCATION);
549   EXPECT_STREQ("{}", channel->GetServiceConfigJSON().c_str());
550 }
551 
TEST_F(ServiceConfigEnd2endTest,AnotherInvalidServiceConfigAfterInvalidServiceConfigTest)552 TEST_F(ServiceConfigEnd2endTest,
553        AnotherInvalidServiceConfigAfterInvalidServiceConfigTest) {
554   StartServers(1);
555   auto channel = BuildChannel();
556   auto stub = BuildStub(channel);
557   SetNextResolutionInvalidServiceConfig(GetServersPorts());
558   CheckRpcSendFailure(stub);
559   SetNextResolutionInvalidServiceConfig(GetServersPorts());
560   CheckRpcSendFailure(stub);
561 }
562 
TEST_F(ServiceConfigEnd2endTest,InvalidDefaultServiceConfigTest)563 TEST_F(ServiceConfigEnd2endTest, InvalidDefaultServiceConfigTest) {
564   StartServers(1);
565   auto channel = BuildChannelWithInvalidDefaultServiceConfig();
566   auto stub = BuildStub(channel);
567   // An invalid default service config results in a lame channel which fails all
568   // RPCs
569   CheckRpcSendFailure(stub);
570 }
571 
TEST_F(ServiceConfigEnd2endTest,InvalidDefaultServiceConfigTestWithValidServiceConfig)572 TEST_F(ServiceConfigEnd2endTest,
573        InvalidDefaultServiceConfigTestWithValidServiceConfig) {
574   StartServers(1);
575   auto channel = BuildChannelWithInvalidDefaultServiceConfig();
576   auto stub = BuildStub(channel);
577   CheckRpcSendFailure(stub);
578   // An invalid default service config results in a lame channel which fails all
579   // RPCs
580   SetNextResolutionValidServiceConfig(GetServersPorts());
581   CheckRpcSendFailure(stub);
582 }
583 
TEST_F(ServiceConfigEnd2endTest,InvalidDefaultServiceConfigTestWithInvalidServiceConfig)584 TEST_F(ServiceConfigEnd2endTest,
585        InvalidDefaultServiceConfigTestWithInvalidServiceConfig) {
586   StartServers(1);
587   auto channel = BuildChannelWithInvalidDefaultServiceConfig();
588   auto stub = BuildStub(channel);
589   CheckRpcSendFailure(stub);
590   // An invalid default service config results in a lame channel which fails all
591   // RPCs
592   SetNextResolutionInvalidServiceConfig(GetServersPorts());
593   CheckRpcSendFailure(stub);
594 }
595 
TEST_F(ServiceConfigEnd2endTest,InvalidDefaultServiceConfigTestWithNoServiceConfig)596 TEST_F(ServiceConfigEnd2endTest,
597        InvalidDefaultServiceConfigTestWithNoServiceConfig) {
598   StartServers(1);
599   auto channel = BuildChannelWithInvalidDefaultServiceConfig();
600   auto stub = BuildStub(channel);
601   CheckRpcSendFailure(stub);
602   // An invalid default service config results in a lame channel which fails all
603   // RPCs
604   SetNextResolutionNoServiceConfig(GetServersPorts());
605   CheckRpcSendFailure(stub);
606 }
607 
608 }  // namespace
609 }  // namespace testing
610 }  // namespace grpc
611 
main(int argc,char ** argv)612 int main(int argc, char** argv) {
613   ::testing::InitGoogleTest(&argc, argv);
614   grpc::testing::TestEnvironment env(&argc, argv);
615   const auto result = RUN_ALL_TESTS();
616   return result;
617 }
618