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