27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
30 #include "absl/memory/memory.h"
31 #include "absl/strings/str_cat.h"
62 #include "src/proto/grpc/testing/echo.grpc.pb.h"
68 using grpc::testing::EchoRequest;
69 using grpc::testing::EchoResponse;
96 void ResetCounters() {
101 std::set<std::string>
clients() {
120 ServiceConfigEnd2endTest()
123 creds_(
new SecureChannelCredentials(
126 static void SetUpTestCase() {
132 void SetUp()
override {
135 grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();
136 bool localhost_resolves_to_ipv4 =
false;
137 bool localhost_resolves_to_ipv6 =
false;
139 &localhost_resolves_to_ipv6);
140 ipv6_only_ = !localhost_resolves_to_ipv4 && localhost_resolves_to_ipv6;
143 void TearDown()
override {
144 for (
size_t i = 0;
i <
servers_.size(); ++
i) {
156 void CreateServers(
size_t num_servers,
157 std::vector<int> ports = std::vector<int>()) {
159 for (
size_t i = 0;
i < num_servers; ++
i) {
161 if (ports.size() == num_servers)
port = ports[
i];
168 void StartServers(
size_t num_servers,
169 std::vector<int> ports = std::vector<int>()) {
170 CreateServers(num_servers,
std::move(ports));
171 for (
size_t i = 0;
i < num_servers; ++
i) {
179 for (
const int&
port : ports) {
192 void SetNextResolutionNoServiceConfig(
const std::vector<int>& ports) {
198 void SetNextResolutionValidServiceConfig(
const std::vector<int>& ports) {
208 void SetNextResolutionInvalidServiceConfig(
const std::vector<int>& ports) {
216 void SetNextResolutionWithServiceConfig(
const std::vector<int>& ports,
217 const char* svc_cfg) {
230 std::vector<int> GetServersPorts(
size_t start_index = 0) {
231 std::vector<int> ports;
232 for (
size_t i = start_index;
i <
servers_.size(); ++
i) {
238 std::unique_ptr<grpc::testing::EchoTestService::Stub> BuildStub(
239 const std::shared_ptr<Channel>&
channel) {
240 return grpc::testing::EchoTestService::NewStub(
channel);
243 std::shared_ptr<Channel> BuildChannel() {
244 ChannelArguments
args;
250 std::shared_ptr<Channel> BuildChannelWithDefaultServiceConfig() {
251 ChannelArguments
args;
253 ValidDefaultServiceConfig()),
255 args.SetServiceConfigJSON(ValidDefaultServiceConfig());
261 std::shared_ptr<Channel> BuildChannelWithInvalidDefaultServiceConfig() {
262 ChannelArguments
args;
264 InvalidDefaultServiceConfig()),
266 args.SetServiceConfigJSON(InvalidDefaultServiceConfig());
273 const std::unique_ptr<grpc::testing::EchoTestService::Stub>&
stub,
276 const bool local_response = (
response ==
nullptr);
277 if (local_response)
response =
new EchoResponse;
285 if (local_response)
delete response;
290 const std::unique_ptr<grpc::testing::EchoTestService::Stub>&
stub,
298 <<
"Error: " <<
status.error_message() <<
" "
299 <<
status.error_details();
301 <<
"From " << location.
file() <<
":" << location.
line();
302 if (!success) abort();
305 void CheckRpcSendFailure(
306 const std::unique_ptr<grpc::testing::EchoTestService::Stub>&
stub) {
322 explicit ServerData(
int port = 0)
329 thread_ = absl::make_unique<std::thread>(
331 while (!server_ready_) {
334 server_ready_ =
false;
342 std::shared_ptr<ServerCredentials> creds(
new SecureServerCredentials(
348 server_ready_ =
true;
361 server_->GetHealthCheckService()->SetServingStatus(
service, serving);
365 void ResetCounters() {
370 const std::unique_ptr<grpc::testing::EchoTestService::Stub>&
stub,
372 bool ignore_failure =
false) {
374 if (ignore_failure) {
377 CheckRpcSendOk(
stub, location,
true);
389 if (!
channel->WaitForStateChange(
state, deadline))
return false;
400 if (!
channel->WaitForStateChange(
state, deadline))
return false;
405 bool SeenAllServers() {
407 if (
server->service_.request_count() == 0)
return false;
414 void UpdateConnectionOrder(
415 const std::vector<std::unique_ptr<ServerData>>&
servers,
416 std::vector<int>* connection_order) {
417 for (
size_t i = 0;
i <
servers.size(); ++
i) {
421 std::find(connection_order->begin(), connection_order->end(), i);
422 if (
it == connection_order->end()) {
423 connection_order->push_back(i);
430 const char* ValidServiceConfigV1() {
return "{\"version\": \"1\"}"; }
432 const char* ValidServiceConfigV2() {
return "{\"version\": \"2\"}"; }
434 const char* ValidDefaultServiceConfig() {
435 return "{\"version\": \"valid_default\"}";
438 const char* InvalidDefaultServiceConfig() {
439 return "{\"version\": \"invalid_default\"";
444 std::unique_ptr<grpc::testing::EchoTestService::Stub>
stub_;
449 std::shared_ptr<ChannelCredentials>
creds_;
452 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigTest) {
456 SetNextResolutionNoServiceConfig(GetServersPorts());
461 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigWithDefaultConfigTest) {
463 auto channel = BuildChannelWithDefaultServiceConfig();
465 SetNextResolutionNoServiceConfig(GetServersPorts());
468 channel->GetServiceConfigJSON().c_str());
471 TEST_F(ServiceConfigEnd2endTest, InvalidServiceConfigTest) {
475 SetNextResolutionInvalidServiceConfig(GetServersPorts());
476 CheckRpcSendFailure(
stub);
479 TEST_F(ServiceConfigEnd2endTest, ValidServiceConfigUpdatesTest) {
483 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
486 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV2());
491 TEST_F(ServiceConfigEnd2endTest,
492 NoServiceConfigUpdateAfterValidServiceConfigTest) {
496 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
499 SetNextResolutionNoServiceConfig(GetServersPorts());
504 TEST_F(ServiceConfigEnd2endTest,
505 NoServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest) {
507 auto channel = BuildChannelWithDefaultServiceConfig();
509 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
512 SetNextResolutionNoServiceConfig(GetServersPorts());
515 channel->GetServiceConfigJSON().c_str());
518 TEST_F(ServiceConfigEnd2endTest,
519 InvalidServiceConfigUpdateAfterValidServiceConfigTest) {
523 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
526 SetNextResolutionInvalidServiceConfig(GetServersPorts());
531 TEST_F(ServiceConfigEnd2endTest,
532 InvalidServiceConfigUpdateAfterValidServiceConfigWithDefaultConfigTest) {
534 auto channel = BuildChannelWithDefaultServiceConfig();
536 SetNextResolutionWithServiceConfig(GetServersPorts(), ValidServiceConfigV1());
539 SetNextResolutionInvalidServiceConfig(GetServersPorts());
544 TEST_F(ServiceConfigEnd2endTest,
545 ValidServiceConfigAfterInvalidServiceConfigTest) {
549 SetNextResolutionInvalidServiceConfig(GetServersPorts());
550 CheckRpcSendFailure(
stub);
551 SetNextResolutionValidServiceConfig(GetServersPorts());
555 TEST_F(ServiceConfigEnd2endTest, NoServiceConfigAfterInvalidServiceConfigTest) {
559 SetNextResolutionInvalidServiceConfig(GetServersPorts());
560 CheckRpcSendFailure(
stub);
561 SetNextResolutionNoServiceConfig(GetServersPorts());
566 TEST_F(ServiceConfigEnd2endTest,
567 AnotherInvalidServiceConfigAfterInvalidServiceConfigTest) {
571 SetNextResolutionInvalidServiceConfig(GetServersPorts());
572 CheckRpcSendFailure(
stub);
573 SetNextResolutionInvalidServiceConfig(GetServersPorts());
574 CheckRpcSendFailure(
stub);
577 TEST_F(ServiceConfigEnd2endTest, InvalidDefaultServiceConfigTest) {
579 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
583 CheckRpcSendFailure(
stub);
586 TEST_F(ServiceConfigEnd2endTest,
587 InvalidDefaultServiceConfigTestWithValidServiceConfig) {
589 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
591 CheckRpcSendFailure(
stub);
594 SetNextResolutionValidServiceConfig(GetServersPorts());
595 CheckRpcSendFailure(
stub);
598 TEST_F(ServiceConfigEnd2endTest,
599 InvalidDefaultServiceConfigTestWithInvalidServiceConfig) {
601 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
603 CheckRpcSendFailure(
stub);
606 SetNextResolutionInvalidServiceConfig(GetServersPorts());
607 CheckRpcSendFailure(
stub);
610 TEST_F(ServiceConfigEnd2endTest,
611 InvalidDefaultServiceConfigTestWithNoServiceConfig) {
613 auto channel = BuildChannelWithInvalidDefaultServiceConfig();
615 CheckRpcSendFailure(
stub);
618 SetNextResolutionNoServiceConfig(GetServersPorts());
619 CheckRpcSendFailure(
stub);
626 int main(
int argc,
char** argv) {