service_config_end2end_test.cc 22 KB

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