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