Merge pull request #24521 from yang-g/ip46

Check localhost is resolved to ipv4 or ipv6 and update test accordingly.
pull/24555/head
Yang Gao 4 years ago committed by GitHub
commit 01596014af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      CMakeLists.txt
  2. 6
      build_autogenerated.yaml
  3. 2
      gRPC-Core.podspec
  4. 2
      grpc.gyp
  5. 3
      test/core/util/BUILD
  6. 58
      test/core/util/resolve_localhost_ip46.cc
  7. 29
      test/core/util/resolve_localhost_ip46.h
  8. 46
      test/cpp/end2end/client_lb_end2end_test.cc
  9. 4
      test/cpp/end2end/end2end_test.cc
  10. 178
      test/cpp/end2end/grpclb_end2end_test.cc
  11. 2
      test/cpp/end2end/server_early_return_test.cc
  12. 10
      test/cpp/end2end/service_config_end2end_test.cc
  13. 488
      test/cpp/end2end/xds_end2end_test.cc
  14. 2
      test/cpp/ext/filters/census/stats_plugin_end2end_test.cc

@ -2087,6 +2087,7 @@ add_library(grpc_test_util
test/core/util/port_isolated_runtime_environment.cc test/core/util/port_isolated_runtime_environment.cc
test/core/util/port_server_client.cc test/core/util/port_server_client.cc
test/core/util/reconnect_server.cc test/core/util/reconnect_server.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc test/core/util/slice_splitter.cc
test/core/util/subprocess_posix.cc test/core/util/subprocess_posix.cc
test/core/util/subprocess_windows.cc test/core/util/subprocess_windows.cc
@ -2158,6 +2159,7 @@ add_library(grpc_test_util_unsecure
test/core/util/port_isolated_runtime_environment.cc test/core/util/port_isolated_runtime_environment.cc
test/core/util/port_server_client.cc test/core/util/port_server_client.cc
test/core/util/reconnect_server.cc test/core/util/reconnect_server.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc test/core/util/slice_splitter.cc
test/core/util/subprocess_posix.cc test/core/util/subprocess_posix.cc
test/core/util/subprocess_windows.cc test/core/util/subprocess_windows.cc
@ -15072,6 +15074,7 @@ if(_gRPC_PLATFORM_LINUX OR _gRPC_PLATFORM_MAC OR _gRPC_PLATFORM_POSIX)
test/core/util/port_isolated_runtime_environment.cc test/core/util/port_isolated_runtime_environment.cc
test/core/util/port_server_client.cc test/core/util/port_server_client.cc
test/core/util/reconnect_server.cc test/core/util/reconnect_server.cc
test/core/util/resolve_localhost_ip46.cc
test/core/util/slice_splitter.cc test/core/util/slice_splitter.cc
test/core/util/subprocess_posix.cc test/core/util/subprocess_posix.cc
test/core/util/subprocess_windows.cc test/core/util/subprocess_windows.cc

@ -1416,6 +1416,7 @@ libs:
- test/core/util/port.h - test/core/util/port.h
- test/core/util/port_server_client.h - test/core/util/port_server_client.h
- test/core/util/reconnect_server.h - test/core/util/reconnect_server.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h - test/core/util/slice_splitter.h
- test/core/util/subprocess.h - test/core/util/subprocess.h
- test/core/util/test_config.h - test/core/util/test_config.h
@ -1438,6 +1439,7 @@ libs:
- test/core/util/port_isolated_runtime_environment.cc - test/core/util/port_isolated_runtime_environment.cc
- test/core/util/port_server_client.cc - test/core/util/port_server_client.cc
- test/core/util/reconnect_server.cc - test/core/util/reconnect_server.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc - test/core/util/slice_splitter.cc
- test/core/util/subprocess_posix.cc - test/core/util/subprocess_posix.cc
- test/core/util/subprocess_windows.cc - test/core/util/subprocess_windows.cc
@ -1472,6 +1474,7 @@ libs:
- test/core/util/port.h - test/core/util/port.h
- test/core/util/port_server_client.h - test/core/util/port_server_client.h
- test/core/util/reconnect_server.h - test/core/util/reconnect_server.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h - test/core/util/slice_splitter.h
- test/core/util/subprocess.h - test/core/util/subprocess.h
- test/core/util/test_config.h - test/core/util/test_config.h
@ -1494,6 +1497,7 @@ libs:
- test/core/util/port_isolated_runtime_environment.cc - test/core/util/port_isolated_runtime_environment.cc
- test/core/util/port_server_client.cc - test/core/util/port_server_client.cc
- test/core/util/reconnect_server.cc - test/core/util/reconnect_server.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc - test/core/util/slice_splitter.cc
- test/core/util/subprocess_posix.cc - test/core/util/subprocess_posix.cc
- test/core/util/subprocess_windows.cc - test/core/util/subprocess_windows.cc
@ -7667,6 +7671,7 @@ targets:
- test/core/util/port.h - test/core/util/port.h
- test/core/util/port_server_client.h - test/core/util/port_server_client.h
- test/core/util/reconnect_server.h - test/core/util/reconnect_server.h
- test/core/util/resolve_localhost_ip46.h
- test/core/util/slice_splitter.h - test/core/util/slice_splitter.h
- test/core/util/subprocess.h - test/core/util/subprocess.h
- test/core/util/test_config.h - test/core/util/test_config.h
@ -7692,6 +7697,7 @@ targets:
- test/core/util/port_isolated_runtime_environment.cc - test/core/util/port_isolated_runtime_environment.cc
- test/core/util/port_server_client.cc - test/core/util/port_server_client.cc
- test/core/util/reconnect_server.cc - test/core/util/reconnect_server.cc
- test/core/util/resolve_localhost_ip46.cc
- test/core/util/slice_splitter.cc - test/core/util/slice_splitter.cc
- test/core/util/subprocess_posix.cc - test/core/util/subprocess_posix.cc
- test/core/util/subprocess_windows.cc - test/core/util/subprocess_windows.cc

@ -2021,6 +2021,8 @@ Pod::Spec.new do |s|
'test/core/util/port_server_client.h', 'test/core/util/port_server_client.h',
'test/core/util/reconnect_server.cc', 'test/core/util/reconnect_server.cc',
'test/core/util/reconnect_server.h', 'test/core/util/reconnect_server.h',
'test/core/util/resolve_localhost_ip46.cc',
'test/core/util/resolve_localhost_ip46.h',
'test/core/util/slice_splitter.cc', 'test/core/util/slice_splitter.cc',
'test/core/util/slice_splitter.h', 'test/core/util/slice_splitter.h',
'test/core/util/subprocess.h', 'test/core/util/subprocess.h',

@ -1007,6 +1007,7 @@
'test/core/util/port_isolated_runtime_environment.cc', 'test/core/util/port_isolated_runtime_environment.cc',
'test/core/util/port_server_client.cc', 'test/core/util/port_server_client.cc',
'test/core/util/reconnect_server.cc', 'test/core/util/reconnect_server.cc',
'test/core/util/resolve_localhost_ip46.cc',
'test/core/util/slice_splitter.cc', 'test/core/util/slice_splitter.cc',
'test/core/util/subprocess_posix.cc', 'test/core/util/subprocess_posix.cc',
'test/core/util/subprocess_windows.cc', 'test/core/util/subprocess_windows.cc',
@ -1044,6 +1045,7 @@
'test/core/util/port_isolated_runtime_environment.cc', 'test/core/util/port_isolated_runtime_environment.cc',
'test/core/util/port_server_client.cc', 'test/core/util/port_server_client.cc',
'test/core/util/reconnect_server.cc', 'test/core/util/reconnect_server.cc',
'test/core/util/resolve_localhost_ip46.cc',
'test/core/util/slice_splitter.cc', 'test/core/util/slice_splitter.cc',
'test/core/util/subprocess_posix.cc', 'test/core/util/subprocess_posix.cc',
'test/core/util/subprocess_windows.cc', 'test/core/util/subprocess_windows.cc',

@ -51,6 +51,7 @@ grpc_cc_library(
"port_isolated_runtime_environment.cc", "port_isolated_runtime_environment.cc",
"port_server_client.cc", "port_server_client.cc",
"reconnect_server.cc", "reconnect_server.cc",
"resolve_localhost_ip46.cc",
"slice_splitter.cc", "slice_splitter.cc",
"subprocess_posix.cc", "subprocess_posix.cc",
"subprocess_windows.cc", "subprocess_windows.cc",
@ -73,6 +74,7 @@ grpc_cc_library(
"port.h", "port.h",
"port_server_client.h", "port_server_client.h",
"reconnect_server.h", "reconnect_server.h",
"resolve_localhost_ip46.h",
"slice_splitter.h", "slice_splitter.h",
"subprocess.h", "subprocess.h",
"test_config.h", "test_config.h",
@ -94,6 +96,7 @@ grpc_cc_library(
deps = [ deps = [
":grpc_debugger_macros", ":grpc_debugger_macros",
"//:gpr", "//:gpr",
"//:grpc_base_c",
"//:grpc_common", "//:grpc_common",
], ],
) )

@ -0,0 +1,58 @@
//
//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#include "test/core/util/resolve_localhost_ip46.h"
#include <grpc/support/log.h>
#include "src/core/lib/iomgr/port.h"
#include "src/core/lib/iomgr/resolve_address.h"
#include "src/core/lib/iomgr/sockaddr.h"
namespace grpc_core {
namespace {
bool localhost_to_ipv4 = false;
bool localhost_to_ipv6 = false;
gpr_once g_resolve_localhost_ipv46 = GPR_ONCE_INIT;
void InitResolveLocalhost() {
grpc_resolved_addresses* addresses;
grpc_error* err =
grpc_blocking_resolve_address("localhost", "https", &addresses);
GPR_ASSERT(err == GRPC_ERROR_NONE);
for (size_t i = 0; i < addresses->naddrs; i++) {
grpc_sockaddr* addr =
reinterpret_cast<grpc_sockaddr*>(addresses->addrs[i].addr);
if (addr->sa_family == GRPC_AF_INET) {
localhost_to_ipv4 = true;
} else if (addr->sa_family == GRPC_AF_INET6) {
localhost_to_ipv6 = true;
}
}
grpc_resolved_addresses_destroy(addresses);
}
} // namespace
void LocalhostResolves(bool* ipv4, bool* ipv6) {
gpr_once_init(&g_resolve_localhost_ipv46, InitResolveLocalhost);
*ipv4 = localhost_to_ipv4;
*ipv6 = localhost_to_ipv6;
}
} // namespace grpc_core

@ -0,0 +1,29 @@
//
//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
#ifndef GRPC_TEST_CORE_UTIL_RESOLVE_LOCALHOST_IP46_H_
#define GRPC_TEST_CORE_UTIL_RESOLVE_LOCALHOST_IP46_H_
namespace grpc_core {
// Test whether localhost resolves to ipv4 and/or ipv6
void LocalhostResolves(bool* ipv4, bool* ipv6);
} // namespace grpc_core
#endif // GRPC_TEST_CORE_UTIL_RESOLVE_LOCALHOST_IP46_H_

@ -59,6 +59,7 @@
#include "src/proto/grpc/testing/echo.grpc.pb.h" #include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "src/proto/grpc/testing/xds/orca_load_report_for_test.pb.h" #include "src/proto/grpc/testing/xds/orca_load_report_for_test.pb.h"
#include "test/core/util/port.h" #include "test/core/util/port.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
#include "test/core/util/test_lb_policies.h" #include "test/core/util/test_lb_policies.h"
#include "test/cpp/end2end/test_service_impl.h" #include "test/cpp/end2end/test_service_impl.h"
@ -152,12 +153,14 @@ class MyTestServiceImpl : public TestServiceImpl {
class FakeResolverResponseGeneratorWrapper { class FakeResolverResponseGeneratorWrapper {
public: public:
FakeResolverResponseGeneratorWrapper() explicit FakeResolverResponseGeneratorWrapper(bool ipv6_only)
: response_generator_(grpc_core::MakeRefCounted< : ipv6_only_(ipv6_only),
response_generator_(grpc_core::MakeRefCounted<
grpc_core::FakeResolverResponseGenerator>()) {} grpc_core::FakeResolverResponseGenerator>()) {}
FakeResolverResponseGeneratorWrapper( FakeResolverResponseGeneratorWrapper(
FakeResolverResponseGeneratorWrapper&& other) noexcept { FakeResolverResponseGeneratorWrapper&& other) noexcept {
ipv6_only_ = other.ipv6_only_;
response_generator_ = std::move(other.response_generator_); response_generator_ = std::move(other.response_generator_);
} }
@ -167,13 +170,22 @@ class FakeResolverResponseGeneratorWrapper {
std::unique_ptr<grpc_core::ServerAddress::AttributeInterface> attribute = std::unique_ptr<grpc_core::ServerAddress::AttributeInterface> attribute =
nullptr) { nullptr) {
grpc_core::ExecCtx exec_ctx; grpc_core::ExecCtx exec_ctx;
response_generator_->SetResponse(BuildFakeResults( // This is a dummy log line as a workaround for
ports, service_config_json, attribute_key, std::move(attribute))); // https://github.com/grpc/grpc/issues/24550. It should be removed once the
// testing infrastructure updates compiler for MacOS.
if (service_config_json != nullptr) {
gpr_log(GPR_INFO, "SetNextResolution with service_config_json: %s",
service_config_json);
}
response_generator_->SetResponse(
BuildFakeResults(ipv6_only_, ports, service_config_json, attribute_key,
std::move(attribute)));
} }
void SetNextResolutionUponError(const std::vector<int>& ports) { void SetNextResolutionUponError(const std::vector<int>& ports) {
grpc_core::ExecCtx exec_ctx; grpc_core::ExecCtx exec_ctx;
response_generator_->SetReresolutionResponse(BuildFakeResults(ports)); response_generator_->SetReresolutionResponse(
BuildFakeResults(ipv6_only_, ports));
} }
void SetFailureOnReresolution() { void SetFailureOnReresolution() {
@ -187,13 +199,15 @@ class FakeResolverResponseGeneratorWrapper {
private: private:
static grpc_core::Resolver::Result BuildFakeResults( static grpc_core::Resolver::Result BuildFakeResults(
const std::vector<int>& ports, const char* service_config_json = nullptr, bool ipv6_only, const std::vector<int>& ports,
const char* service_config_json = nullptr,
const char* attribute_key = nullptr, const char* attribute_key = nullptr,
std::unique_ptr<grpc_core::ServerAddress::AttributeInterface> attribute = std::unique_ptr<grpc_core::ServerAddress::AttributeInterface> attribute =
nullptr) { nullptr) {
grpc_core::Resolver::Result result; grpc_core::Resolver::Result result;
for (const int& port : ports) { for (const int& port : ports) {
std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", port); std::string lb_uri_str =
absl::StrCat(ipv6_only ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", port);
grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true); grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
GPR_ASSERT(lb_uri != nullptr); GPR_ASSERT(lb_uri != nullptr);
grpc_resolved_address address; grpc_resolved_address address;
@ -216,6 +230,7 @@ class FakeResolverResponseGeneratorWrapper {
return result; return result;
} }
bool ipv6_only_ = false;
grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator> grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>
response_generator_; response_generator_;
}; };
@ -238,7 +253,14 @@ class ClientLbEnd2endTest : public ::testing::Test {
#endif #endif
} }
void SetUp() override { grpc_init(); } void SetUp() override {
grpc_init();
bool localhost_resolves_to_ipv4 = false;
bool localhost_resolves_to_ipv6 = false;
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4,
&localhost_resolves_to_ipv6);
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
}
void TearDown() override { void TearDown() override {
for (size_t i = 0; i < servers_.size(); ++i) { for (size_t i = 0; i < servers_.size(); ++i) {
@ -278,7 +300,7 @@ class ClientLbEnd2endTest : public ::testing::Test {
} }
FakeResolverResponseGeneratorWrapper BuildResolverResponseGenerator() { FakeResolverResponseGeneratorWrapper BuildResolverResponseGenerator() {
return FakeResolverResponseGeneratorWrapper(); return FakeResolverResponseGeneratorWrapper(ipv6_only_);
} }
std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub( std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub(
@ -468,6 +490,7 @@ class ClientLbEnd2endTest : public ::testing::Test {
std::vector<std::unique_ptr<ServerData>> servers_; std::vector<std::unique_ptr<ServerData>> servers_;
const std::string kRequestMessage_; const std::string kRequestMessage_;
std::shared_ptr<ChannelCredentials> creds_; std::shared_ptr<ChannelCredentials> creds_;
bool ipv6_only_ = false;
}; };
TEST_F(ClientLbEnd2endTest, ChannelStateConnectingWhenResolving) { TEST_F(ClientLbEnd2endTest, ChannelStateConnectingWhenResolving) {
@ -1969,8 +1992,9 @@ TEST_F(ClientLbAddressTest, Basic) {
// Make sure that the attributes wind up on the subchannels. // Make sure that the attributes wind up on the subchannels.
std::vector<std::string> expected; std::vector<std::string> expected;
for (const int port : GetServersPorts()) { for (const int port : GetServersPorts()) {
expected.emplace_back(absl::StrCat( expected.emplace_back(
"127.0.0.1:", port, " args={} attributes={", kAttributeKey, "=foo}")); absl::StrCat(ipv6_only_ ? "[::1]:" : "127.0.0.1:", port,
" args={} attributes={", kAttributeKey, "=foo}"));
} }
EXPECT_EQ(addresses_seen(), expected); EXPECT_EQ(addresses_seen(), expected);
} }

@ -350,7 +350,7 @@ class End2endTest : public ::testing::TestWithParam<TestScenario> {
void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) { void StartServer(const std::shared_ptr<AuthMetadataProcessor>& processor) {
int port = grpc_pick_unused_port_or_die(); int port = grpc_pick_unused_port_or_die();
first_picked_port_ = port; first_picked_port_ = port;
server_address_ << "127.0.0.1:" << port; server_address_ << "localhost:" << port;
// Setup server // Setup server
BuildAndStartServer(processor); BuildAndStartServer(processor);
} }
@ -1414,7 +1414,7 @@ TEST_P(End2endTest, ChannelStateTimeout) {
} }
int port = grpc_pick_unused_port_or_die(); int port = grpc_pick_unused_port_or_die();
std::ostringstream server_address; std::ostringstream server_address;
server_address << "127.0.0.1:" << port; server_address << "localhost:" << port;
// Channel to non-existing server // Channel to non-existing server
auto channel = auto channel =
grpc::CreateChannel(server_address.str(), InsecureChannelCredentials()); grpc::CreateChannel(server_address.str(), InsecureChannelCredentials());

@ -53,6 +53,7 @@
#include "src/cpp/server/secure_server_credentials.h" #include "src/cpp/server/secure_server_credentials.h"
#include "test/core/util/port.h" #include "test/core/util/port.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h" #include "test/cpp/end2end/test_service_impl.h"
@ -184,6 +185,12 @@ std::string Ip4ToPackedString(const char* ip_str) {
return std::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4)); return std::string(reinterpret_cast<const char*>(&ip4), sizeof(ip4));
} }
std::string Ip6ToPackedString(const char* ip_str) {
struct in6_addr ip6;
GPR_ASSERT(inet_pton(AF_INET6, ip_str, &ip6) == 1);
return std::string(reinterpret_cast<const char*>(&ip6), sizeof(ip6));
}
struct ClientStats { struct ClientStats {
size_t num_calls_started = 0; size_t num_calls_started = 0;
size_t num_calls_finished = 0; size_t num_calls_finished = 0;
@ -321,28 +328,6 @@ class BalancerServiceImpl : public BalancerService {
gpr_log(GPR_INFO, "LB[%p]: shut down", this); gpr_log(GPR_INFO, "LB[%p]: shut down", this);
} }
static LoadBalanceResponse BuildResponseForBackends(
const std::vector<int>& backend_ports,
const std::map<std::string, size_t>& drop_token_counts) {
LoadBalanceResponse response;
for (const auto& drop_token_count : drop_token_counts) {
for (size_t i = 0; i < drop_token_count.second; ++i) {
auto* server = response.mutable_server_list()->add_servers();
server->set_drop(true);
server->set_load_balance_token(drop_token_count.first);
}
}
for (const int& backend_port : backend_ports) {
auto* server = response.mutable_server_list()->add_servers();
server->set_ip_address(Ip4ToPackedString("127.0.0.1"));
server->set_port(backend_port);
static int token_count = 0;
server->set_load_balance_token(
absl::StrFormat("token%03d", ++token_count));
}
return response;
}
ClientStats WaitForLoadReport() { ClientStats WaitForLoadReport() {
grpc::internal::MutexLock lock(&mu_); grpc::internal::MutexLock lock(&mu_);
grpc::internal::CondVar cv; grpc::internal::CondVar cv;
@ -418,6 +403,11 @@ class GrpclbEnd2endTest : public ::testing::Test {
static void TearDownTestCase() { grpc_shutdown(); } static void TearDownTestCase() { grpc_shutdown(); }
void SetUp() override { void SetUp() override {
bool localhost_resolves_to_ipv4 = false;
bool localhost_resolves_to_ipv6 = false;
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4,
&localhost_resolves_to_ipv6);
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
response_generator_ = response_generator_ =
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>(); grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
// Start the backends. // Start the backends.
@ -546,11 +536,12 @@ class GrpclbEnd2endTest : public ::testing::Test {
std::string balancer_name; std::string balancer_name;
}; };
static grpc_core::ServerAddressList CreateLbAddressesFromAddressDataList( grpc_core::ServerAddressList CreateLbAddressesFromAddressDataList(
const std::vector<AddressData>& address_data) { const std::vector<AddressData>& address_data) {
grpc_core::ServerAddressList addresses; grpc_core::ServerAddressList addresses;
for (const auto& addr : address_data) { for (const auto& addr : address_data) {
std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", addr.port); std::string lb_uri_str = absl::StrCat(
ipv6_only_ ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", addr.port);
grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true); grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
GPR_ASSERT(lb_uri != nullptr); GPR_ASSERT(lb_uri != nullptr);
grpc_resolved_address address; grpc_resolved_address address;
@ -565,7 +556,7 @@ class GrpclbEnd2endTest : public ::testing::Test {
return addresses; return addresses;
} }
static grpc_core::Resolver::Result MakeResolverResult( grpc_core::Resolver::Result MakeResolverResult(
const std::vector<AddressData>& balancer_address_data, const std::vector<AddressData>& balancer_address_data,
const std::vector<AddressData>& backend_address_data = {}, const std::vector<AddressData>& backend_address_data = {},
const char* service_config_json = kDefaultServiceConfig) { const char* service_config_json = kDefaultServiceConfig) {
@ -628,6 +619,29 @@ class GrpclbEnd2endTest : public ::testing::Test {
balancers_[i]->service_.add_response(response, delay_ms); balancers_[i]->service_.add_response(response, delay_ms);
} }
LoadBalanceResponse BuildResponseForBackends(
const std::vector<int>& backend_ports,
const std::map<std::string, size_t>& drop_token_counts) {
LoadBalanceResponse response;
for (const auto& drop_token_count : drop_token_counts) {
for (size_t i = 0; i < drop_token_count.second; ++i) {
auto* server = response.mutable_server_list()->add_servers();
server->set_drop(true);
server->set_load_balance_token(drop_token_count.first);
}
}
for (const int& backend_port : backend_ports) {
auto* server = response.mutable_server_list()->add_servers();
server->set_ip_address(ipv6_only_ ? Ip6ToPackedString("::1")
: Ip4ToPackedString("127.0.0.1"));
server->set_port(backend_port);
static int token_count = 0;
server->set_load_balance_token(
absl::StrFormat("token%03d", ++token_count));
}
return response;
}
Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000, Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000,
bool wait_for_ready = false, bool wait_for_ready = false,
const Status& expected_status = Status::OK) { const Status& expected_status = Status::OK) {
@ -726,6 +740,7 @@ class GrpclbEnd2endTest : public ::testing::Test {
const size_t num_backends_; const size_t num_backends_;
const size_t num_balancers_; const size_t num_balancers_;
const int client_load_reporting_interval_seconds_; const int client_load_reporting_interval_seconds_;
bool ipv6_only_ = false;
std::shared_ptr<Channel> channel_; std::shared_ptr<Channel> channel_;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
std::vector<std::unique_ptr<ServerThread<BackendServiceImpl>>> backends_; std::vector<std::unique_ptr<ServerThread<BackendServiceImpl>>> backends_;
@ -745,8 +760,7 @@ TEST_F(SingleBalancerTest, Vanilla) {
SetNextResolutionAllBalancers(); SetNextResolutionAllBalancers();
const size_t kNumRpcsPerAddress = 100; const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
// Make sure that trying to connect works without a call. // Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */); channel_->GetState(true /* try_to_connect */);
// We need to wait for all backends to come online. // We need to wait for all backends to come online.
@ -771,8 +785,7 @@ TEST_F(SingleBalancerTest, Vanilla) {
TEST_F(SingleBalancerTest, ReturnServerStatus) { TEST_F(SingleBalancerTest, ReturnServerStatus) {
SetNextResolutionAllBalancers(); SetNextResolutionAllBalancers();
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
// We need to wait for all backends to come online. // We need to wait for all backends to come online.
WaitForAllBackends(); WaitForAllBackends();
// Send a request that the backend will fail, and make sure we get // Send a request that the backend will fail, and make sure we get
@ -793,8 +806,7 @@ TEST_F(SingleBalancerTest, SelectGrpclbWithMigrationServiceConfig) {
" ]\n" " ]\n"
"}"); "}");
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
CheckRpcSendOk(1, 1000 /* timeout_ms */, true /* wait_for_ready */); CheckRpcSendOk(1, 1000 /* timeout_ms */, true /* wait_for_ready */);
balancers_[0]->service_.NotifyDoneWithServerlists(); balancers_[0]->service_.NotifyDoneWithServerlists();
// The balancer got a single request. // The balancer got a single request.
@ -841,8 +853,7 @@ TEST_F(SingleBalancerTest, UsePickFirstChildPolicy) {
" ]\n" " ]\n"
"}"); "}");
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
const size_t kNumRpcs = num_backends_ * 2; const size_t kNumRpcs = num_backends_ * 2;
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */); CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);
balancers_[0]->service_.NotifyDoneWithServerlists(); balancers_[0]->service_.NotifyDoneWithServerlists();
@ -872,8 +883,7 @@ TEST_F(SingleBalancerTest, SwapChildPolicy) {
" ]\n" " ]\n"
"}"); "}");
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
const size_t kNumRpcs = num_backends_ * 2; const size_t kNumRpcs = num_backends_ * 2;
CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */); CheckRpcSendOk(kNumRpcs, 1000 /* timeout_ms */, true /* wait_for_ready */);
// Check that all requests went to the first backend. This verifies // Check that all requests went to the first backend. This verifies
@ -908,8 +918,7 @@ TEST_F(SingleBalancerTest, SameBackendListedMultipleTimes) {
ports.push_back(backends_[0]->port_); ports.push_back(backends_[0]->port_);
ports.push_back(backends_[0]->port_); ports.push_back(backends_[0]->port_);
const size_t kNumRpcsPerAddress = 10; const size_t kNumRpcsPerAddress = 10;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(0, BuildResponseForBackends(ports, {}), 0);
0, BalancerServiceImpl::BuildResponseForBackends(ports, {}), 0);
// We need to wait for the backend to come online. // We need to wait for the backend to come online.
WaitForBackend(0); WaitForBackend(0);
// Send kNumRpcsPerAddress RPCs per server. // Send kNumRpcsPerAddress RPCs per server.
@ -927,8 +936,7 @@ TEST_F(SingleBalancerTest, SecureNaming) {
SetNextResolution({AddressData{balancers_[0]->port_, "lb"}}); SetNextResolution({AddressData{balancers_[0]->port_, "lb"}});
const size_t kNumRpcsPerAddress = 100; const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
// Make sure that trying to connect works without a call. // Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */); channel_->GetState(true /* try_to_connect */);
// We need to wait for all backends to come online. // We need to wait for all backends to come online.
@ -970,8 +978,7 @@ TEST_F(SingleBalancerTest, InitiallyEmptyServerlist) {
ScheduleResponseForBalancer(0, LoadBalanceResponse(), 0); ScheduleResponseForBalancer(0, LoadBalanceResponse(), 0);
// Send non-empty serverlist only after kServerlistDelayMs // Send non-empty serverlist only after kServerlistDelayMs
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), kServerlistDelayMs);
kServerlistDelayMs);
const auto t0 = system_clock::now(); const auto t0 = system_clock::now();
// Client will block: LB will initially send empty serverlist. // Client will block: LB will initially send empty serverlist.
CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */); CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */);
@ -997,8 +1004,7 @@ TEST_F(SingleBalancerTest, AllServersUnreachableFailFast) {
for (size_t i = 0; i < kNumUnreachableServers; ++i) { for (size_t i = 0; i < kNumUnreachableServers; ++i) {
ports.push_back(grpc_pick_unused_port_or_die()); ports.push_back(grpc_pick_unused_port_or_die());
} }
ScheduleResponseForBalancer( ScheduleResponseForBalancer(0, BuildResponseForBackends(ports, {}), 0);
0, BalancerServiceImpl::BuildResponseForBackends(ports, {}), 0);
const Status status = SendRpc(); const Status status = SendRpc();
// The error shouldn't be DEADLINE_EXCEEDED. // The error shouldn't be DEADLINE_EXCEEDED.
EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code()); EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code());
@ -1027,7 +1033,7 @@ TEST_F(SingleBalancerTest, Fallback) {
// Send non-empty serverlist only after kServerlistDelayMs. // Send non-empty serverlist only after kServerlistDelayMs.
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(
GetBackendPorts(kNumBackendsInResolution /* start_index */), {}), GetBackendPorts(kNumBackendsInResolution /* start_index */), {}),
kServerlistDelayMs); kServerlistDelayMs);
@ -1096,7 +1102,7 @@ TEST_F(SingleBalancerTest, FallbackUpdate) {
// Send non-empty serverlist only after kServerlistDelayMs. // Send non-empty serverlist only after kServerlistDelayMs.
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(
GetBackendPorts(kNumBackendsInResolution + GetBackendPorts(kNumBackendsInResolution +
kNumBackendsInResolutionUpdate /* start_index */), kNumBackendsInResolutionUpdate /* start_index */),
{}), {}),
@ -1201,9 +1207,8 @@ TEST_F(SingleBalancerTest,
balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""}); balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""});
} }
SetNextResolution(balancer_addresses, backend_addresses); SetNextResolution(balancer_addresses, backend_addresses);
ScheduleResponseForBalancer(0, ScheduleResponseForBalancer(
BalancerServiceImpl::BuildResponseForBackends( 0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
GetBackendPorts(kNumFallbackBackends), {}),
0); 0);
// Try to connect. // Try to connect.
channel_->GetState(true /* try_to_connect */); channel_->GetState(true /* try_to_connect */);
@ -1234,9 +1239,8 @@ TEST_F(SingleBalancerTest,
// Now start the balancer again. This should cause us to exit // Now start the balancer again. This should cause us to exit
// fallback mode. // fallback mode.
balancers_[0]->Start(server_host_); balancers_[0]->Start(server_host_);
ScheduleResponseForBalancer(0, ScheduleResponseForBalancer(
BalancerServiceImpl::BuildResponseForBackends( 0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
GetBackendPorts(kNumFallbackBackends), {}),
0); 0);
WaitForAllBackends(1 /* num_requests_multiple_of */, WaitForAllBackends(1 /* num_requests_multiple_of */,
kNumFallbackBackends /* start_index */); kNumFallbackBackends /* start_index */);
@ -1256,9 +1260,8 @@ TEST_F(SingleBalancerTest,
balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""}); balancer_addresses.emplace_back(AddressData{balancers_[i]->port_, ""});
} }
SetNextResolution(balancer_addresses, backend_addresses); SetNextResolution(balancer_addresses, backend_addresses);
ScheduleResponseForBalancer(0, ScheduleResponseForBalancer(
BalancerServiceImpl::BuildResponseForBackends( 0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
GetBackendPorts(kNumFallbackBackends), {}),
0); 0);
// Try to connect. // Try to connect.
channel_->GetState(true /* try_to_connect */); channel_->GetState(true /* try_to_connect */);
@ -1287,9 +1290,8 @@ TEST_F(SingleBalancerTest,
// Now start the balancer again. This should cause us to exit // Now start the balancer again. This should cause us to exit
// fallback mode. // fallback mode.
balancers_[0]->Start(server_host_); balancers_[0]->Start(server_host_);
ScheduleResponseForBalancer(0, ScheduleResponseForBalancer(
BalancerServiceImpl::BuildResponseForBackends( 0, BuildResponseForBackends(GetBackendPorts(kNumFallbackBackends), {}),
GetBackendPorts(kNumFallbackBackends), {}),
0); 0);
WaitForAllBackends(1 /* num_requests_multiple_of */, WaitForAllBackends(1 /* num_requests_multiple_of */,
kNumFallbackBackends /* start_index */); kNumFallbackBackends /* start_index */);
@ -1358,7 +1360,7 @@ TEST_F(SingleBalancerTest, FallbackControlledByBalancer_AfterFirstServerlist) {
// then sends the serverlist again. // then sends the serverlist again.
// The serverlist points to backend 1. // The serverlist points to backend 1.
LoadBalanceResponse serverlist_resp = LoadBalanceResponse serverlist_resp =
BalancerServiceImpl::BuildResponseForBackends({backends_[1]->port_}, {}); BuildResponseForBackends({backends_[1]->port_}, {});
LoadBalanceResponse fallback_resp; LoadBalanceResponse fallback_resp;
fallback_resp.mutable_fallback_response(); fallback_resp.mutable_fallback_response();
ScheduleResponseForBalancer(0, serverlist_resp, 0); ScheduleResponseForBalancer(0, serverlist_resp, 0);
@ -1375,8 +1377,7 @@ TEST_F(SingleBalancerTest, BackendsRestart) {
SetNextResolutionAllBalancers(); SetNextResolutionAllBalancers();
const size_t kNumRpcsPerAddress = 100; const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
// Make sure that trying to connect works without a call. // Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */); channel_->GetState(true /* try_to_connect */);
// Send kNumRpcsPerAddress RPCs per server. // Send kNumRpcsPerAddress RPCs per server.
@ -1406,8 +1407,7 @@ TEST_F(SingleBalancerTest, ServiceNameFromLbPolicyConfig) {
SetNextResolutionAllBalancers(kServiceConfigWithTarget); SetNextResolutionAllBalancers(kServiceConfigWithTarget);
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
// Make sure that trying to connect works without a call. // Make sure that trying to connect works without a call.
channel_->GetState(true /* try_to_connect */); channel_->GetState(true /* try_to_connect */);
// We need to wait for all backends to come online. // We need to wait for all backends to come online.
@ -1424,10 +1424,10 @@ TEST_F(UpdatesTest, UpdateBalancersButKeepUsingOriginalBalancer) {
SetNextResolutionAllBalancers(); SetNextResolutionAllBalancers();
const std::vector<int> first_backend{GetBackendPorts()[0]}; const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[1]}; const std::vector<int> second_backend{GetBackendPorts()[1]};
ScheduleResponseForBalancer( ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0); 0);
ScheduleResponseForBalancer( ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0); 0);
// Wait until the first backend is ready. // Wait until the first backend is ready.
WaitForBackend(0); WaitForBackend(0);
@ -1482,10 +1482,10 @@ TEST_F(UpdatesTest, UpdateBalancersRepeated) {
const std::vector<int> first_backend{GetBackendPorts()[0]}; const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[0]}; const std::vector<int> second_backend{GetBackendPorts()[0]};
ScheduleResponseForBalancer( ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0); 0);
ScheduleResponseForBalancer( ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0); 0);
// Wait until the first backend is ready. // Wait until the first backend is ready.
WaitForBackend(0); WaitForBackend(0);
@ -1555,10 +1555,10 @@ TEST_F(UpdatesTest, UpdateBalancersDeadUpdate) {
const std::vector<int> first_backend{GetBackendPorts()[0]}; const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[1]}; const std::vector<int> second_backend{GetBackendPorts()[1]};
ScheduleResponseForBalancer( ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0); 0);
ScheduleResponseForBalancer( ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0); 0);
// Start servers and send 10 RPCs per server. // Start servers and send 10 RPCs per server.
gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH =========="); gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");
@ -1688,10 +1688,10 @@ class UpdatesWithClientLoadReportingTest : public GrpclbEnd2endTest {
TEST_F(UpdatesWithClientLoadReportingTest, ReresolveDeadBalancer) { TEST_F(UpdatesWithClientLoadReportingTest, ReresolveDeadBalancer) {
const std::vector<int> first_backend{GetBackendPorts()[0]}; const std::vector<int> first_backend{GetBackendPorts()[0]};
const std::vector<int> second_backend{GetBackendPorts()[1]}; const std::vector<int> second_backend{GetBackendPorts()[1]};
ScheduleResponseForBalancer( ScheduleResponseForBalancer(0, BuildResponseForBackends(first_backend, {}),
0, BalancerServiceImpl::BuildResponseForBackends(first_backend, {}), 0); 0);
ScheduleResponseForBalancer( ScheduleResponseForBalancer(1, BuildResponseForBackends(second_backend, {}),
1, BalancerServiceImpl::BuildResponseForBackends(second_backend, {}), 0); 0);
// Ask channel to connect to trigger resolver creation. // Ask channel to connect to trigger resolver creation.
channel_->GetState(true); channel_->GetState(true);
@ -1767,7 +1767,7 @@ TEST_F(SingleBalancerTest, Drop) {
const int num_total_addresses = num_backends_ + num_of_drop_addresses; const int num_total_addresses = num_backends_ + num_of_drop_addresses;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(
GetBackendPorts(), GetBackendPorts(),
{{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), {"load_balancing", num_of_drop_by_load_balancing_addresses}}),
@ -1806,7 +1806,7 @@ TEST_F(SingleBalancerTest, DropAllFirst) {
const int num_of_drop_by_load_balancing_addresses = 1; const int num_of_drop_by_load_balancing_addresses = 1;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(
{}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, {}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), {"load_balancing", num_of_drop_by_load_balancing_addresses}}),
0); 0);
@ -1818,13 +1818,12 @@ TEST_F(SingleBalancerTest, DropAllFirst) {
TEST_F(SingleBalancerTest, DropAll) { TEST_F(SingleBalancerTest, DropAll) {
SetNextResolutionAllBalancers(); SetNextResolutionAllBalancers();
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
const int num_of_drop_by_rate_limiting_addresses = 1; const int num_of_drop_by_rate_limiting_addresses = 1;
const int num_of_drop_by_load_balancing_addresses = 1; const int num_of_drop_by_load_balancing_addresses = 1;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(
{}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, {}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), {"load_balancing", num_of_drop_by_load_balancing_addresses}}),
1000); 1000);
@ -1850,8 +1849,7 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, Vanilla) {
SetNextResolutionAllBalancers(); SetNextResolutionAllBalancers();
const size_t kNumRpcsPerAddress = 100; const size_t kNumRpcsPerAddress = 100;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, BalancerServiceImpl::BuildResponseForBackends(GetBackendPorts(), {}), 0, BuildResponseForBackends(GetBackendPorts(), {}), 0);
0);
// Wait until all backends are ready. // Wait until all backends are ready.
int num_ok = 0; int num_ok = 0;
int num_failure = 0; int num_failure = 0;
@ -1892,8 +1890,7 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, BalancerRestart) {
// Balancer returns backends starting at index 1. // Balancer returns backends starting at index 1.
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(GetBackendPorts(0, kNumBackendsFirstPass), {}),
GetBackendPorts(0, kNumBackendsFirstPass), {}),
0); 0);
// Wait until all backends returned by the balancer are ready. // Wait until all backends returned by the balancer are ready.
int num_ok = 0; int num_ok = 0;
@ -1922,9 +1919,8 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, BalancerRestart) {
} }
// Now restart the balancer, this time pointing to all backends. // Now restart the balancer, this time pointing to all backends.
balancers_[0]->Start(server_host_); balancers_[0]->Start(server_host_);
ScheduleResponseForBalancer(0, ScheduleResponseForBalancer(
BalancerServiceImpl::BuildResponseForBackends( 0, BuildResponseForBackends(GetBackendPorts(kNumBackendsFirstPass), {}),
GetBackendPorts(kNumBackendsFirstPass), {}),
0); 0);
// Wait for queries to start going to one of the new backends. // Wait for queries to start going to one of the new backends.
// This tells us that we're now using the new serverlist. // This tells us that we're now using the new serverlist.
@ -1955,7 +1951,7 @@ TEST_F(SingleBalancerWithClientLoadReportingTest, Drop) {
const int num_total_addresses = num_backends_ + num_of_drop_addresses; const int num_total_addresses = num_backends_ + num_of_drop_addresses;
ScheduleResponseForBalancer( ScheduleResponseForBalancer(
0, 0,
BalancerServiceImpl::BuildResponseForBackends( BuildResponseForBackends(
GetBackendPorts(), GetBackendPorts(),
{{"rate_limiting", num_of_drop_by_rate_limiting_addresses}, {{"rate_limiting", num_of_drop_by_rate_limiting_addresses},
{"load_balancing", num_of_drop_by_load_balancing_addresses}}), {"load_balancing", num_of_drop_by_load_balancing_addresses}}),

@ -115,7 +115,7 @@ class ServerEarlyReturnTest : public ::testing::Test {
void SetUp() override { void SetUp() override {
int port = grpc_pick_unused_port_or_die(); int port = grpc_pick_unused_port_or_die();
picked_port_ = port; picked_port_ = port;
server_address_ << "127.0.0.1:" << port; server_address_ << "localhost:" << port;
ServerBuilder builder; ServerBuilder builder;
builder.AddListeningPort(server_address_.str(), builder.AddListeningPort(server_address_.str(),
InsecureServerCredentials()); InsecureServerCredentials());

@ -57,6 +57,7 @@
#include "src/proto/grpc/testing/echo.grpc.pb.h" #include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "test/core/util/port.h" #include "test/core/util/port.h"
#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h" #include "test/core/util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h" #include "test/cpp/end2end/test_service_impl.h"
@ -131,6 +132,11 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
grpc_init(); grpc_init();
response_generator_ = response_generator_ =
grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>(); grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
bool localhost_resolves_to_ipv4 = false;
bool localhost_resolves_to_ipv6 = false;
grpc_core::LocalhostResolves(&localhost_resolves_to_ipv4,
&localhost_resolves_to_ipv6);
ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
} }
void TearDown() override { void TearDown() override {
@ -169,7 +175,8 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
grpc_core::Resolver::Result BuildFakeResults(const std::vector<int>& ports) { grpc_core::Resolver::Result BuildFakeResults(const std::vector<int>& ports) {
grpc_core::Resolver::Result result; grpc_core::Resolver::Result result;
for (const int& port : ports) { for (const int& port : ports) {
std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", port); std::string lb_uri_str =
absl::StrCat(ipv6_only_ ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", port);
grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true); grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
GPR_ASSERT(lb_uri != nullptr); GPR_ASSERT(lb_uri != nullptr);
grpc_resolved_address address; grpc_resolved_address address;
@ -422,6 +429,7 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
return "{\"version\": \"invalid_default\""; return "{\"version\": \"invalid_default\"";
} }
bool ipv6_only_ = false;
const std::string server_host_; const std::string server_host_;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_; std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
std::vector<std::unique_ptr<ServerData>> servers_; std::vector<std::unique_ptr<ServerData>> servers_;

File diff suppressed because it is too large Load Diff

@ -81,7 +81,7 @@ class StatsPluginEnd2EndTest : public ::testing::Test {
server_ = builder.BuildAndStart(); server_ = builder.BuildAndStart();
ASSERT_NE(nullptr, server_); ASSERT_NE(nullptr, server_);
ASSERT_NE(0, port); ASSERT_NE(0, port);
server_address_ = absl::StrCat("0.0.0.0:", port); server_address_ = absl::StrCat("localhost:", port);
server_thread_ = std::thread(&StatsPluginEnd2EndTest::RunServerLoop, this); server_thread_ = std::thread(&StatsPluginEnd2EndTest::RunServerLoop, this);
stub_ = EchoTestService::NewStub(::grpc::CreateChannel( stub_ = EchoTestService::NewStub(::grpc::CreateChannel(

Loading…
Cancel
Save