#include <string>
#include <thread>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
#include "absl/memory/memory.h"
#include "absl/strings/str_cat.h"
#include "src/core/ext/filters/client_channel/global_subchannel_pool.h"
#include "src/core/ext/filters/client_channel/resolver/fake/fake_resolver.h"
#include "src/core/ext/filters/client_channel/server_address.h"
+#include "src/core/lib/address_utils/parse_address.h"
#include "src/core/lib/backoff/backoff.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/gprpp/debug_location.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
-#include "src/core/lib/iomgr/parse_address.h"
#include "src/core/lib/iomgr/tcp_client.h"
#include "src/core/lib/security/credentials/fake/fake_credentials.h"
#include "src/cpp/client/secure_credentials.h"
#include "src/cpp/server/secure_server_credentials.h"
-
#include "src/proto/grpc/testing/echo.grpc.pb.h"
#include "test/core/util/port.h"
+#include "test/core/util/resolve_localhost_ip46.h"
#include "test/core/util/test_config.h"
#include "test/cpp/end2end/test_service_impl.h"
-#include <gmock/gmock.h>
-#include <gtest/gtest.h>
-
using grpc::testing::EchoRequest;
using grpc::testing::EchoResponse;
-using std::chrono::system_clock;
namespace grpc {
namespace testing {
grpc_init();
response_generator_ =
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 {
grpc_core::Resolver::Result BuildFakeResults(const std::vector<int>& ports) {
grpc_core::Resolver::Result result;
for (const int& port : ports) {
- std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", port);
- grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
- GPR_ASSERT(lb_uri != nullptr);
+ std::string lb_uri_str =
+ absl::StrCat(ipv6_only_ ? "ipv6:[::1]:" : "ipv4:127.0.0.1:", port);
+ absl::StatusOr<grpc_core::URI> lb_uri = grpc_core::URI::Parse(lb_uri_str);
+ GPR_ASSERT(lb_uri.ok());
grpc_resolved_address address;
- GPR_ASSERT(grpc_parse_uri(lb_uri, &address));
+ GPR_ASSERT(grpc_parse_uri(*lb_uri, &address));
result.addresses.emplace_back(address.addr, address.len,
nullptr /* args */);
- grpc_uri_destroy(lb_uri);
}
return result;
}
}
struct ServerData {
- int port_;
+ const int port_;
std::unique_ptr<Server> server_;
MyTestServiceImpl service_;
std::unique_ptr<std::thread> thread_;
- bool server_ready_ = false;
- bool started_ = false;
- explicit ServerData(int port = 0) {
- port_ = port > 0 ? port : grpc_pick_unused_port_or_die();
- }
+ grpc::internal::Mutex mu_;
+ grpc::internal::CondVar cond_;
+ bool server_ready_ ABSL_GUARDED_BY(mu_) = false;
+ bool started_ ABSL_GUARDED_BY(mu_) = false;
+
+ explicit ServerData(int port = 0)
+ : port_(port > 0 ? port : grpc_pick_unused_port_or_die()) {}
void Start(const std::string& server_host) {
gpr_log(GPR_INFO, "starting server on port %d", port_);
+ grpc::internal::MutexLock lock(&mu_);
started_ = true;
- grpc::internal::Mutex mu;
- grpc::internal::MutexLock lock(&mu);
- grpc::internal::CondVar cond;
thread_ = absl::make_unique<std::thread>(
- std::bind(&ServerData::Serve, this, server_host, &mu, &cond));
- cond.WaitUntil(&mu, [this] { return server_ready_; });
+ std::bind(&ServerData::Serve, this, server_host));
+ while (!server_ready_) {
+ cond_.Wait(&mu_);
+ }
server_ready_ = false;
gpr_log(GPR_INFO, "server startup complete");
}
- void Serve(const std::string& server_host, grpc::internal::Mutex* mu,
- grpc::internal::CondVar* cond) {
+ void Serve(const std::string& server_host) {
std::ostringstream server_address;
server_address << server_host << ":" << port_;
ServerBuilder builder;
builder.AddListeningPort(server_address.str(), std::move(creds));
builder.RegisterService(&service_);
server_ = builder.BuildAndStart();
- grpc::internal::MutexLock lock(mu);
+ grpc::internal::MutexLock lock(&mu_);
server_ready_ = true;
- cond->Signal();
+ cond_.Signal();
}
void Shutdown() {
+ grpc::internal::MutexLock lock(&mu_);
if (!started_) return;
server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));
thread_->join();
return "{\"version\": \"invalid_default\"";
}
+ bool ipv6_only_ = false;
const std::string server_host_;
std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;
std::vector<std::unique_ptr<ServerData>> servers_;