| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203 | /* * * Copyright 2017 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 <memory>#include <mutex>#include <numeric>#include <set>#include <sstream>#include <thread>#include <grpc/grpc.h>#include <grpc/support/alloc.h>#include <grpc/support/log.h>#include <grpc/support/string_util.h>#include <grpc/support/time.h>#include <grpcpp/channel.h>#include <grpcpp/client_context.h>#include <grpcpp/create_channel.h>#include <grpcpp/server.h>#include <grpcpp/server_builder.h>#include "src/core/ext/filters/client_channel/backup_poller.h"#include "src/core/ext/filters/client_channel/parse_address.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/ext/filters/client_channel/xds/xds_api.h"#include "src/core/lib/gpr/env.h"#include "src/core/lib/gpr/tmpfile.h"#include "src/core/lib/gprpp/map.h"#include "src/core/lib/gprpp/ref_counted_ptr.h"#include "src/core/lib/gprpp/sync.h"#include "src/core/lib/iomgr/sockaddr.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 "test/core/util/port.h"#include "test/core/util/test_config.h"#include "test/cpp/end2end/test_service_impl.h"#include "src/proto/grpc/testing/echo.grpc.pb.h"#include "src/proto/grpc/testing/xds/ads_for_test.grpc.pb.h"#include "src/proto/grpc/testing/xds/cds_for_test.grpc.pb.h"#include "src/proto/grpc/testing/xds/eds_for_test.grpc.pb.h"#include "src/proto/grpc/testing/xds/lds_rds_for_test.grpc.pb.h"#include "src/proto/grpc/testing/xds/lrs_for_test.grpc.pb.h"#include <gmock/gmock.h>#include <gtest/gtest.h>// TODO(dgq): Other scenarios in need of testing:// - Send a serverlist with faulty ip:port addresses (port > 2^16, etc).// - Test reception of invalid serverlist// - Test against a non-LB server.// - Random LB server closing the stream unexpectedly.//// Findings from end to end testing to be covered here:// - Handling of LB servers restart, including reconnection after backing-off//   retries.// - Destruction of load balanced channel (and therefore of xds instance)//   while://   1) the internal LB call is still active. This should work by virtue//   of the weak reference the LB call holds. The call should be terminated as//   part of the xds shutdown process.//   2) the retry timer is active. Again, the weak reference it holds should//   prevent a premature call to \a glb_destroy.namespace grpc {namespace testing {namespace {using std::chrono::system_clock;using ::envoy::api::v2::Cluster;using ::envoy::api::v2::ClusterLoadAssignment;using ::envoy::api::v2::DiscoveryRequest;using ::envoy::api::v2::DiscoveryResponse;using ::envoy::api::v2::FractionalPercent;using ::envoy::api::v2::HttpConnectionManager;using ::envoy::api::v2::Listener;using ::envoy::api::v2::RouteConfiguration;using ::envoy::service::discovery::v2::AggregatedDiscoveryService;using ::envoy::service::load_stats::v2::ClusterStats;using ::envoy::service::load_stats::v2::LoadReportingService;using ::envoy::service::load_stats::v2::LoadStatsRequest;using ::envoy::service::load_stats::v2::LoadStatsResponse;using ::envoy::service::load_stats::v2::UpstreamLocalityStats;constexpr char kLdsTypeUrl[] = "type.googleapis.com/envoy.api.v2.Listener";constexpr char kRdsTypeUrl[] =    "type.googleapis.com/envoy.api.v2.RouteConfiguration";constexpr char kCdsTypeUrl[] = "type.googleapis.com/envoy.api.v2.Cluster";constexpr char kEdsTypeUrl[] =    "type.googleapis.com/envoy.api.v2.ClusterLoadAssignment";constexpr char kDefaultLocalityRegion[] = "xds_default_locality_region";constexpr char kDefaultLocalityZone[] = "xds_default_locality_zone";constexpr char kLbDropType[] = "lb";constexpr char kThrottleDropType[] = "throttle";constexpr int kDefaultLocalityWeight = 3;constexpr int kDefaultLocalityPriority = 0;constexpr char kBootstrapFile[] =    "{\n"    "  \"xds_servers\": [\n"    "    {\n"    "      \"server_uri\": \"fake:///lb\",\n"    "      \"channel_creds\": [\n"    "        {\n"    "          \"type\": \"fake\"\n"    "        }\n"    "      ]\n"    "    }\n"    "  ],\n"    "  \"node\": {\n"    "    \"id\": \"xds_end2end_test\",\n"    "    \"cluster\": \"test\",\n"    "    \"metadata\": {\n"    "      \"foo\": \"bar\"\n"    "    },\n"    "    \"locality\": {\n"    "      \"region\": \"corp\",\n"    "      \"zone\": \"svl\",\n"    "      \"subzone\": \"mp3\"\n"    "    }\n"    "  }\n"    "}\n";constexpr char kBootstrapFileBad[] =    "{\n"    "  \"xds_servers\": [\n"    "    {\n"    "      \"server_uri\": \"fake:///wrong_lb\",\n"    "      \"channel_creds\": [\n"    "        {\n"    "          \"type\": \"fake\"\n"    "        }\n"    "      ]\n"    "    }\n"    "  ],\n"    "  \"node\": {\n"    "  }\n"    "}\n";char* g_bootstrap_file;char* g_bootstrap_file_bad;void WriteBootstrapFiles() {  char* bootstrap_file;  FILE* out = gpr_tmpfile("xds_bootstrap", &bootstrap_file);  fputs(kBootstrapFile, out);  fclose(out);  g_bootstrap_file = bootstrap_file;  out = gpr_tmpfile("xds_bootstrap_bad", &bootstrap_file);  fputs(kBootstrapFileBad, out);  fclose(out);  g_bootstrap_file_bad = bootstrap_file;}// Helper class to minimize the number of unique ports we use for this test.class PortSaver { public:  int GetPort() {    if (idx_ >= ports_.size()) {      ports_.push_back(grpc_pick_unused_port_or_die());    }    return ports_[idx_++];  }  void Reset() { idx_ = 0; } private:  std::vector<int> ports_;  size_t idx_ = 0;};PortSaver* g_port_saver = nullptr;template <typename ServiceType>class CountedService : public ServiceType { public:  size_t request_count() {    grpc_core::MutexLock lock(&mu_);    return request_count_;  }  size_t response_count() {    grpc_core::MutexLock lock(&mu_);    return response_count_;  }  void IncreaseResponseCount() {    grpc_core::MutexLock lock(&mu_);    ++response_count_;  }  void IncreaseRequestCount() {    grpc_core::MutexLock lock(&mu_);    ++request_count_;  }  void ResetCounters() {    grpc_core::MutexLock lock(&mu_);    request_count_ = 0;    response_count_ = 0;  } protected:  grpc_core::Mutex mu_; private:  size_t request_count_ = 0;  size_t response_count_ = 0;};using BackendService = CountedService<TestServiceImpl>;using AdsService = CountedService<AggregatedDiscoveryService::Service>;using LrsService = CountedService<LoadReportingService::Service>;const char g_kCallCredsMdKey[] = "Balancer should not ...";const char g_kCallCredsMdValue[] = "... receive me";class BackendServiceImpl : public BackendService { public:  BackendServiceImpl() {}  Status Echo(ServerContext* context, const EchoRequest* request,              EchoResponse* response) override {    // Backend should receive the call credentials metadata.    auto call_credentials_entry =        context->client_metadata().find(g_kCallCredsMdKey);    EXPECT_NE(call_credentials_entry, context->client_metadata().end());    if (call_credentials_entry != context->client_metadata().end()) {      EXPECT_EQ(call_credentials_entry->second, g_kCallCredsMdValue);    }    IncreaseRequestCount();    const auto status = TestServiceImpl::Echo(context, request, response);    IncreaseResponseCount();    AddClient(context->peer());    return status;  }  void Start() {}  void Shutdown() {}  std::set<grpc::string> clients() {    grpc_core::MutexLock lock(&clients_mu_);    return clients_;  } private:  void AddClient(const grpc::string& client) {    grpc_core::MutexLock lock(&clients_mu_);    clients_.insert(client);  }  grpc_core::Mutex mu_;  grpc_core::Mutex clients_mu_;  std::set<grpc::string> clients_;};class ClientStats { public:  struct LocalityStats {    // Converts from proto message class.    LocalityStats(const UpstreamLocalityStats& upstream_locality_stats)        : total_successful_requests(              upstream_locality_stats.total_successful_requests()),          total_requests_in_progress(              upstream_locality_stats.total_requests_in_progress()),          total_error_requests(upstream_locality_stats.total_error_requests()),          total_issued_requests(              upstream_locality_stats.total_issued_requests()) {}    uint64_t total_successful_requests;    uint64_t total_requests_in_progress;    uint64_t total_error_requests;    uint64_t total_issued_requests;  };  // Converts from proto message class.  ClientStats(const ClusterStats& cluster_stats)      : total_dropped_requests_(cluster_stats.total_dropped_requests()) {    for (const auto& input_locality_stats :         cluster_stats.upstream_locality_stats()) {      locality_stats_.emplace(input_locality_stats.locality().sub_zone(),                              LocalityStats(input_locality_stats));    }    for (const auto& input_dropped_requests :         cluster_stats.dropped_requests()) {      dropped_requests_.emplace(input_dropped_requests.category(),                                input_dropped_requests.dropped_count());    }  }  uint64_t total_successful_requests() const {    uint64_t sum = 0;    for (auto& p : locality_stats_) {      sum += p.second.total_successful_requests;    }    return sum;  }  uint64_t total_requests_in_progress() const {    uint64_t sum = 0;    for (auto& p : locality_stats_) {      sum += p.second.total_requests_in_progress;    }    return sum;  }  uint64_t total_error_requests() const {    uint64_t sum = 0;    for (auto& p : locality_stats_) {      sum += p.second.total_error_requests;    }    return sum;  }  uint64_t total_issued_requests() const {    uint64_t sum = 0;    for (auto& p : locality_stats_) {      sum += p.second.total_issued_requests;    }    return sum;  }  uint64_t total_dropped_requests() const { return total_dropped_requests_; }  uint64_t dropped_requests(const grpc::string& category) const {    auto iter = dropped_requests_.find(category);    GPR_ASSERT(iter != dropped_requests_.end());    return iter->second;  } private:  std::map<grpc::string, LocalityStats> locality_stats_;  uint64_t total_dropped_requests_;  std::map<grpc::string, uint64_t> dropped_requests_;};// TODO(roth): Change this service to a real fake.class AdsServiceImpl : public AdsService { public:  enum ResponseState {    NOT_SENT,    SENT,    ACKED,    NACKED,  };  struct ResponseArgs {    struct Locality {      Locality(const grpc::string& sub_zone, std::vector<int> ports,               int lb_weight = kDefaultLocalityWeight,               int priority = kDefaultLocalityPriority,               std::vector<envoy::api::v2::HealthStatus> health_statuses = {})          : sub_zone(std::move(sub_zone)),            ports(std::move(ports)),            lb_weight(lb_weight),            priority(priority),            health_statuses(std::move(health_statuses)) {}      const grpc::string sub_zone;      std::vector<int> ports;      int lb_weight;      int priority;      std::vector<envoy::api::v2::HealthStatus> health_statuses;    };    ResponseArgs() = default;    explicit ResponseArgs(std::vector<Locality> locality_list)        : locality_list(std::move(locality_list)) {}    std::vector<Locality> locality_list;    std::map<grpc::string, uint32_t> drop_categories;    FractionalPercent::DenominatorType drop_denominator =        FractionalPercent::MILLION;  };  using Stream = ServerReaderWriter<DiscoveryResponse, DiscoveryRequest>;  using ResponseDelayPair = std::pair<DiscoveryResponse, int>;  AdsServiceImpl(bool enable_load_reporting) {    // Construct RDS response data.    default_route_config_.set_name("application_target_name");    auto* virtual_host = default_route_config_.add_virtual_hosts();    virtual_host->add_domains("*");    auto* route = virtual_host->add_routes();    route->mutable_match()->set_prefix("");    route->mutable_route()->set_cluster("application_target_name");    rds_response_data_ = {        {"application_target_name", default_route_config_},    };    // Construct LDS response data (with inlined RDS result).    default_listener_ = BuildListener(default_route_config_);    lds_response_data_ = {        {"application_target_name", default_listener_},    };    // Construct CDS response data.    default_cluster_.set_name("application_target_name");    default_cluster_.set_type(envoy::api::v2::Cluster::EDS);    default_cluster_.mutable_eds_cluster_config()        ->mutable_eds_config()        ->mutable_ads();    default_cluster_.set_lb_policy(envoy::api::v2::Cluster::ROUND_ROBIN);    if (enable_load_reporting) {      default_cluster_.mutable_lrs_server()->mutable_self();    }    cds_response_data_ = {        {"application_target_name", default_cluster_},    };  }  void HandleLdsRequest(DiscoveryRequest* request, Stream* stream) {    gpr_log(GPR_INFO, "ADS[%p]: received LDS request '%s'", this,            request->DebugString().c_str());    const std::string version_str = "version_1";    const std::string nonce_str = "nonce_1";    grpc_core::MutexLock lock(&ads_mu_);    if (lds_ignore_) return;    if (lds_response_state_ == NOT_SENT) {      DiscoveryResponse response;      response.set_type_url(kLdsTypeUrl);      response.set_version_info(version_str);      response.set_nonce(nonce_str);      for (const auto& server_name : request->resource_names()) {        auto iter = lds_response_data_.find(server_name);        if (iter == lds_response_data_.end()) continue;        response.add_resources()->PackFrom(iter->second);      }      stream->Write(response);      lds_response_state_ = SENT;    } else if (lds_response_state_ == SENT) {      GPR_ASSERT(!request->response_nonce().empty());      lds_response_state_ =          request->version_info() == version_str ? ACKED : NACKED;    }  }  void HandleRdsRequest(DiscoveryRequest* request, Stream* stream) {    gpr_log(GPR_INFO, "ADS[%p]: received RDS request '%s'", this,            request->DebugString().c_str());    const std::string version_str = "version_1";    const std::string nonce_str = "nonce_1";    grpc_core::MutexLock lock(&ads_mu_);    if (rds_ignore_) return;    if (rds_response_state_ == NOT_SENT) {      DiscoveryResponse response;      response.set_type_url(kRdsTypeUrl);      response.set_version_info(version_str);      response.set_nonce(nonce_str);      for (const auto& route_config_name : request->resource_names()) {        auto iter = rds_response_data_.find(route_config_name);        if (iter == rds_response_data_.end()) continue;        response.add_resources()->PackFrom(iter->second);      }      stream->Write(response);      rds_response_state_ = SENT;    } else if (rds_response_state_ == SENT) {      GPR_ASSERT(!request->response_nonce().empty());      rds_response_state_ =          request->version_info() == version_str ? ACKED : NACKED;    }  }  void HandleCdsRequest(DiscoveryRequest* request, Stream* stream) {    gpr_log(GPR_INFO, "ADS[%p]: received CDS request '%s'", this,            request->DebugString().c_str());    const std::string version_str = "version_1";    const std::string nonce_str = "nonce_1";    grpc_core::MutexLock lock(&ads_mu_);    if (cds_ignore_) return;    if (cds_response_state_ == NOT_SENT) {      DiscoveryResponse response;      response.set_type_url(kCdsTypeUrl);      response.set_version_info(version_str);      response.set_nonce(nonce_str);      for (const auto& cluster_name : request->resource_names()) {        auto iter = cds_response_data_.find(cluster_name);        if (iter == cds_response_data_.end()) continue;        response.add_resources()->PackFrom(iter->second);      }      stream->Write(response);      cds_response_state_ = SENT;    } else if (cds_response_state_ == SENT) {      GPR_ASSERT(!request->response_nonce().empty());      cds_response_state_ =          request->version_info() == version_str ? ACKED : NACKED;    }  }  void HandleEdsRequest(DiscoveryRequest* request, Stream* stream) {    gpr_log(GPR_INFO, "ADS[%p]: received EDS request '%s'", this,            request->DebugString().c_str());    IncreaseRequestCount();    std::vector<ResponseDelayPair> responses_and_delays;    {      grpc_core::MutexLock lock(&ads_mu_);      if (eds_ignore_) return;      responses_and_delays = eds_responses_and_delays_;    }    // Send response.    for (const auto& p : responses_and_delays) {      const DiscoveryResponse& response = p.first;      const int delay_ms = p.second;      gpr_log(GPR_INFO, "ADS[%p]: sleeping for %d ms...", this, delay_ms);      if (delay_ms > 0) {        gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(delay_ms));      }      gpr_log(GPR_INFO, "ADS[%p]: Woke up! Sending response '%s'", this,              response.DebugString().c_str());      IncreaseResponseCount();      stream->Write(response);    }  }  Status StreamAggregatedResources(ServerContext* context,                                   Stream* stream) override {    gpr_log(GPR_INFO, "ADS[%p]: StreamAggregatedResources starts", this);    [&]() {      {        grpc_core::MutexLock lock(&ads_mu_);        if (ads_done_) return;      }      // Balancer shouldn't receive the call credentials metadata.      EXPECT_EQ(context->client_metadata().find(g_kCallCredsMdKey),                context->client_metadata().end());      // Keep servicing requests until the EDS response has been sent back.      DiscoveryRequest request;      // TODO(roth): For each supported type, we currently only handle one      // request without replying to any new requests (for ACK/NACK or new      // resource names). It's not causing a big problem now but should be      // fixed.      bool eds_sent = false;      bool seen_first_request = false;      while (!eds_sent || cds_response_state_ == SENT) {        if (!stream->Read(&request)) return;        if (!seen_first_request) {          EXPECT_TRUE(request.has_node());          seen_first_request = true;        }        if (request.type_url() == kLdsTypeUrl) {          HandleLdsRequest(&request, stream);        } else if (request.type_url() == kRdsTypeUrl) {          HandleRdsRequest(&request, stream);        } else if (request.type_url() == kCdsTypeUrl) {          HandleCdsRequest(&request, stream);        } else if (request.type_url() == kEdsTypeUrl) {          HandleEdsRequest(&request, stream);          eds_sent = true;        }      }      // Wait until notified done.      grpc_core::MutexLock lock(&ads_mu_);      ads_cond_.WaitUntil(&ads_mu_, [this] { return ads_done_; });    }();    gpr_log(GPR_INFO, "ADS[%p]: StreamAggregatedResources done", this);    return Status::OK;  }  Listener default_listener() const { return default_listener_; }  RouteConfiguration default_route_config() const {    return default_route_config_;  }  Cluster default_cluster() const { return default_cluster_; }  ResponseState lds_response_state() {    grpc_core::MutexLock lock(&ads_mu_);    return lds_response_state_;  }  ResponseState rds_response_state() {    grpc_core::MutexLock lock(&ads_mu_);    return rds_response_state_;  }  ResponseState cds_response_state() {    grpc_core::MutexLock lock(&ads_mu_);    return cds_response_state_;  }  void SetLdsResponse(      std::map<std::string /*server_name*/, Listener> lds_response_data) {    lds_response_data_ = std::move(lds_response_data);  }  void set_lds_ignore() { lds_ignore_ = true; }  void SetRdsResponse(      std::map<std::string /*route_config_name*/, RouteConfiguration>          rds_response_data) {    rds_response_data_ = std::move(rds_response_data);  }  void set_rds_ignore() { rds_ignore_ = true; }  void SetCdsResponse(      std::map<std::string /*cluster_name*/, Cluster> cds_response_data) {    cds_response_data_ = std::move(cds_response_data);  }  void set_cds_ignore() { cds_ignore_ = true; }  void AddEdsResponse(const DiscoveryResponse& response, int send_after_ms) {    grpc_core::MutexLock lock(&ads_mu_);    eds_responses_and_delays_.push_back(        std::make_pair(response, send_after_ms));  }  void set_eds_ignore() { eds_ignore_ = true; }  void SetLdsToUseDynamicRds() {    auto listener = default_listener_;    HttpConnectionManager http_connection_manager;    http_connection_manager.mutable_rds()->set_route_config_name(        "application_target_name");    listener.mutable_api_listener()->mutable_api_listener()->PackFrom(        http_connection_manager);    SetLdsResponse({{"application_target_name", std::move(listener)}});  }  static Listener BuildListener(const RouteConfiguration& route_config) {    HttpConnectionManager http_connection_manager;    *(http_connection_manager.mutable_route_config()) = route_config;    Listener listener;    listener.set_name("application_target_name");    listener.mutable_api_listener()->mutable_api_listener()->PackFrom(        http_connection_manager);    return listener;  }  void Start() {    grpc_core::MutexLock lock(&ads_mu_);    ads_done_ = false;    eds_responses_and_delays_.clear();  }  void Shutdown() {    {      grpc_core::MutexLock lock(&ads_mu_);      NotifyDoneWithAdsCallLocked();      eds_responses_and_delays_.clear();    }    gpr_log(GPR_INFO, "ADS[%p]: shut down", this);  }  static DiscoveryResponse BuildResponse(const ResponseArgs& args) {    ClusterLoadAssignment assignment;    assignment.set_cluster_name("application_target_name");    for (const auto& locality : args.locality_list) {      auto* endpoints = assignment.add_endpoints();      endpoints->mutable_load_balancing_weight()->set_value(locality.lb_weight);      endpoints->set_priority(locality.priority);      endpoints->mutable_locality()->set_region(kDefaultLocalityRegion);      endpoints->mutable_locality()->set_zone(kDefaultLocalityZone);      endpoints->mutable_locality()->set_sub_zone(locality.sub_zone);      for (size_t i = 0; i < locality.ports.size(); ++i) {        const int& port = locality.ports[i];        auto* lb_endpoints = endpoints->add_lb_endpoints();        if (locality.health_statuses.size() > i &&            locality.health_statuses[i] !=                envoy::api::v2::HealthStatus::UNKNOWN) {          lb_endpoints->set_health_status(locality.health_statuses[i]);        }        auto* endpoint = lb_endpoints->mutable_endpoint();        auto* address = endpoint->mutable_address();        auto* socket_address = address->mutable_socket_address();        socket_address->set_address("127.0.0.1");        socket_address->set_port_value(port);      }    }    if (!args.drop_categories.empty()) {      auto* policy = assignment.mutable_policy();      for (const auto& p : args.drop_categories) {        const grpc::string& name = p.first;        const uint32_t parts_per_million = p.second;        auto* drop_overload = policy->add_drop_overloads();        drop_overload->set_category(name);        auto* drop_percentage = drop_overload->mutable_drop_percentage();        drop_percentage->set_numerator(parts_per_million);        drop_percentage->set_denominator(args.drop_denominator);      }    }    DiscoveryResponse response;    response.set_type_url(kEdsTypeUrl);    response.add_resources()->PackFrom(assignment);    return response;  }  void NotifyDoneWithAdsCall() {    grpc_core::MutexLock lock(&ads_mu_);    NotifyDoneWithAdsCallLocked();  }  void NotifyDoneWithAdsCallLocked() {    if (!ads_done_) {      ads_done_ = true;      ads_cond_.Broadcast();    }  } private:  grpc_core::CondVar ads_cond_;  // Protect the members below.  grpc_core::Mutex ads_mu_;  bool ads_done_ = false;  // LDS response data.  Listener default_listener_;  std::map<std::string /*server_name*/, Listener> lds_response_data_;  ResponseState lds_response_state_ = NOT_SENT;  bool lds_ignore_ = false;  // RDS response data.  RouteConfiguration default_route_config_;  std::map<std::string /*route_config_name*/, RouteConfiguration>      rds_response_data_;  ResponseState rds_response_state_ = NOT_SENT;  bool rds_ignore_ = false;  // CDS response data.  Cluster default_cluster_;  std::map<std::string /*cluster_name*/, Cluster> cds_response_data_;  ResponseState cds_response_state_ = NOT_SENT;  bool cds_ignore_ = false;  // EDS response data.  std::vector<ResponseDelayPair> eds_responses_and_delays_;  bool eds_ignore_ = false;};class LrsServiceImpl : public LrsService { public:  using Stream = ServerReaderWriter<LoadStatsResponse, LoadStatsRequest>;  explicit LrsServiceImpl(int client_load_reporting_interval_seconds)      : client_load_reporting_interval_seconds_(            client_load_reporting_interval_seconds) {}  Status StreamLoadStats(ServerContext* /*context*/, Stream* stream) override {    gpr_log(GPR_INFO, "LRS[%p]: StreamLoadStats starts", this);    // Read request.    LoadStatsRequest request;    if (stream->Read(&request)) {      if (client_load_reporting_interval_seconds_ > 0) {        IncreaseRequestCount();        // Send response.        LoadStatsResponse response;        std::string server_name;        auto it = request.node().metadata().fields().find(            "PROXYLESS_CLIENT_HOSTNAME");        if (it != request.node().metadata().fields().end()) {          server_name = it->second.string_value();        }        GPR_ASSERT(server_name != "");        response.add_clusters(server_name);        response.mutable_load_reporting_interval()->set_seconds(            client_load_reporting_interval_seconds_);        stream->Write(response);        IncreaseResponseCount();        // Wait for report.        request.Clear();        if (stream->Read(&request)) {          gpr_log(GPR_INFO, "LRS[%p]: received client load report message '%s'",                  this, request.DebugString().c_str());          GPR_ASSERT(request.cluster_stats().size() == 1);          const ClusterStats& cluster_stats = request.cluster_stats()[0];          // We need to acquire the lock here in order to prevent the notify_one          // below from firing before its corresponding wait is executed.          grpc_core::MutexLock lock(&load_report_mu_);          GPR_ASSERT(client_stats_ == nullptr);          client_stats_.reset(new ClientStats(cluster_stats));          load_report_ready_ = true;          load_report_cond_.Signal();        }      }      // Wait until notified done.      grpc_core::MutexLock lock(&lrs_mu_);      lrs_cv_.WaitUntil(&lrs_mu_, [this] { return lrs_done; });    }    gpr_log(GPR_INFO, "LRS[%p]: StreamLoadStats done", this);    return Status::OK;  }  void Start() {    lrs_done = false;    load_report_ready_ = false;    client_stats_.reset();  }  void Shutdown() {    {      grpc_core::MutexLock lock(&lrs_mu_);      NotifyDoneWithLrsCallLocked();    }    gpr_log(GPR_INFO, "LRS[%p]: shut down", this);  }  ClientStats* WaitForLoadReport() {    grpc_core::MutexLock lock(&load_report_mu_);    load_report_cond_.WaitUntil(&load_report_mu_,                                [this] { return load_report_ready_; });    load_report_ready_ = false;    return client_stats_.get();  }  void NotifyDoneWithLrsCall() {    grpc_core::MutexLock lock(&lrs_mu_);    NotifyDoneWithLrsCallLocked();  }  void NotifyDoneWithLrsCallLocked() {    if (!lrs_done) {      lrs_done = true;      lrs_cv_.Broadcast();    }  } private:  const int client_load_reporting_interval_seconds_;  grpc_core::CondVar lrs_cv_;  // Protect lrs_done.  grpc_core::Mutex lrs_mu_;  bool lrs_done = false;  grpc_core::CondVar load_report_cond_;  // Protect the members below.  grpc_core::Mutex load_report_mu_;  std::unique_ptr<ClientStats> client_stats_;  bool load_report_ready_ = false;};class TestType { public:  TestType(bool use_xds_resolver, bool enable_load_reporting)      : use_xds_resolver_(use_xds_resolver),        enable_load_reporting_(enable_load_reporting) {}  bool use_xds_resolver() const { return use_xds_resolver_; }  bool enable_load_reporting() const { return enable_load_reporting_; }  grpc::string AsString() const {    grpc::string retval = (use_xds_resolver_ ? "XdsResolver" : "FakeResolver");    if (enable_load_reporting_) retval += "WithLoadReporting";    return retval;  } private:  const bool use_xds_resolver_;  const bool enable_load_reporting_;};class XdsEnd2endTest : public ::testing::TestWithParam<TestType> { protected:  XdsEnd2endTest(size_t num_backends, size_t num_balancers,                 int client_load_reporting_interval_seconds = 100)      : server_host_("localhost"),        num_backends_(num_backends),        num_balancers_(num_balancers),        client_load_reporting_interval_seconds_(            client_load_reporting_interval_seconds) {}  static void SetUpTestCase() {    // Make the backup poller poll very frequently in order to pick up    // updates from all the subchannels's FDs.    GPR_GLOBAL_CONFIG_SET(grpc_client_channel_backup_poll_interval_ms, 1);#if TARGET_OS_IPHONE    // Workaround Apple CFStream bug    gpr_setenv("grpc_cfstream", "0");#endif    grpc_init();  }  static void TearDownTestCase() { grpc_shutdown(); }  void SetUp() override {    gpr_setenv("GRPC_XDS_BOOTSTRAP", g_bootstrap_file);    g_port_saver->Reset();    response_generator_ =        grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();    lb_channel_response_generator_ =        grpc_core::MakeRefCounted<grpc_core::FakeResolverResponseGenerator>();    // Start the backends.    for (size_t i = 0; i < num_backends_; ++i) {      backends_.emplace_back(new BackendServerThread);      backends_.back()->Start(server_host_);    }    // Start the load balancers.    for (size_t i = 0; i < num_balancers_; ++i) {      balancers_.emplace_back(          new BalancerServerThread(GetParam().enable_load_reporting()                                       ? client_load_reporting_interval_seconds_                                       : 0));      balancers_.back()->Start(server_host_);    }    ResetStub();  }  void TearDown() override {    ShutdownAllBackends();    for (auto& balancer : balancers_) balancer->Shutdown();  }  void StartAllBackends() {    for (auto& backend : backends_) backend->Start(server_host_);  }  void StartBackend(size_t index) { backends_[index]->Start(server_host_); }  void ShutdownAllBackends() {    for (auto& backend : backends_) backend->Shutdown();  }  void ShutdownBackend(size_t index) { backends_[index]->Shutdown(); }  void ResetStub(int fallback_timeout = 0, int failover_timeout = 0,                 const grpc::string& expected_targets = "",                 int xds_resource_does_not_exist_timeout = 0) {    ChannelArguments args;    // TODO(juanlishen): Add setter to ChannelArguments.    if (fallback_timeout > 0) {      args.SetInt(GRPC_ARG_XDS_FALLBACK_TIMEOUT_MS, fallback_timeout);    }    if (failover_timeout > 0) {      args.SetInt(GRPC_ARG_XDS_FAILOVER_TIMEOUT_MS, failover_timeout);    }    if (xds_resource_does_not_exist_timeout > 0) {      args.SetInt(GRPC_ARG_XDS_RESOURCE_DOES_NOT_EXIST_TIMEOUT_MS,                  xds_resource_does_not_exist_timeout);    }    // If the parent channel is using the fake resolver, we inject the    // response generator for the parent here, and then SetNextResolution()    // will inject the xds channel's response generator via the parent's    // response generator.    //    // In contrast, if we are using the xds resolver, then the parent    // channel never uses a response generator, and we inject the xds    // channel's response generator here.    args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,                    GetParam().use_xds_resolver()                        ? lb_channel_response_generator_.get()                        : response_generator_.get());    if (!expected_targets.empty()) {      args.SetString(GRPC_ARG_FAKE_SECURITY_EXPECTED_TARGETS, expected_targets);    }    grpc::string scheme =        GetParam().use_xds_resolver() ? "xds-experimental" : "fake";    std::ostringstream uri;    uri << scheme << ":///" << kApplicationTargetName_;    // TODO(dgq): templatize tests to run everything using both secure and    // insecure channel credentials.    grpc_channel_credentials* channel_creds =        grpc_fake_transport_security_credentials_create();    grpc_call_credentials* call_creds = grpc_md_only_test_credentials_create(        g_kCallCredsMdKey, g_kCallCredsMdValue, false);    std::shared_ptr<ChannelCredentials> creds(        new SecureChannelCredentials(grpc_composite_channel_credentials_create(            channel_creds, call_creds, nullptr)));    call_creds->Unref();    channel_creds->Unref();    channel_ = ::grpc::CreateCustomChannel(uri.str(), creds, args);    stub_ = grpc::testing::EchoTestService::NewStub(channel_);  }  void ResetBackendCounters() {    for (auto& backend : backends_) backend->backend_service()->ResetCounters();  }  bool SeenAllBackends(size_t start_index = 0, size_t stop_index = 0) {    if (stop_index == 0) stop_index = backends_.size();    for (size_t i = start_index; i < stop_index; ++i) {      if (backends_[i]->backend_service()->request_count() == 0) return false;    }    return true;  }  void SendRpcAndCount(int* num_total, int* num_ok, int* num_failure,                       int* num_drops) {    const Status status = SendRpc();    if (status.ok()) {      ++*num_ok;    } else {      if (status.error_message() == "Call dropped by load balancing policy") {        ++*num_drops;      } else {        ++*num_failure;      }    }    ++*num_total;  }  std::tuple<int, int, int> WaitForAllBackends(size_t start_index = 0,                                               size_t stop_index = 0,                                               bool reset_counters = true) {    int num_ok = 0;    int num_failure = 0;    int num_drops = 0;    int num_total = 0;    while (!SeenAllBackends(start_index, stop_index)) {      SendRpcAndCount(&num_total, &num_ok, &num_failure, &num_drops);    }    if (reset_counters) ResetBackendCounters();    gpr_log(GPR_INFO,            "Performed %d warm up requests against the backends. "            "%d succeeded, %d failed, %d dropped.",            num_total, num_ok, num_failure, num_drops);    return std::make_tuple(num_ok, num_failure, num_drops);  }  void WaitForBackend(size_t backend_idx, bool reset_counters = true) {    gpr_log(GPR_INFO, "========= WAITING FOR BACKEND %lu ==========",            static_cast<unsigned long>(backend_idx));    do {      (void)SendRpc();    } while (backends_[backend_idx]->backend_service()->request_count() == 0);    if (reset_counters) ResetBackendCounters();    gpr_log(GPR_INFO, "========= BACKEND %lu READY ==========",            static_cast<unsigned long>(backend_idx));  }  grpc_core::ServerAddressList CreateAddressListFromPortList(      const std::vector<int>& ports) {    grpc_core::ServerAddressList addresses;    for (int port : ports) {      char* lb_uri_str;      gpr_asprintf(&lb_uri_str, "ipv4:127.0.0.1:%d", port);      grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str, true);      GPR_ASSERT(lb_uri != nullptr);      grpc_resolved_address address;      GPR_ASSERT(grpc_parse_uri(lb_uri, &address));      addresses.emplace_back(address.addr, address.len, nullptr);      grpc_uri_destroy(lb_uri);      gpr_free(lb_uri_str);    }    return addresses;  }  void SetNextResolution(const std::vector<int>& ports,                         grpc_core::FakeResolverResponseGenerator*                             lb_channel_response_generator = nullptr) {    if (GetParam().use_xds_resolver()) return;  // Not used with xds resolver.    grpc_core::ExecCtx exec_ctx;    grpc_core::Resolver::Result result;    result.addresses = CreateAddressListFromPortList(ports);    grpc_error* error = GRPC_ERROR_NONE;    const char* service_config_json =        GetParam().enable_load_reporting()            ? kDefaultServiceConfig_            : kDefaultServiceConfigWithoutLoadReporting_;    result.service_config =        grpc_core::ServiceConfig::Create(service_config_json, &error);    GRPC_ERROR_UNREF(error);    grpc_arg arg = grpc_core::FakeResolverResponseGenerator::MakeChannelArg(        lb_channel_response_generator == nullptr            ? lb_channel_response_generator_.get()            : lb_channel_response_generator);    result.args = grpc_channel_args_copy_and_add(nullptr, &arg, 1);    response_generator_->SetResponse(std::move(result));  }  void SetNextResolutionForLbChannelAllBalancers(      const char* service_config_json = nullptr,      grpc_core::FakeResolverResponseGenerator* lb_channel_response_generator =          nullptr) {    std::vector<int> ports;    for (size_t i = 0; i < balancers_.size(); ++i) {      ports.emplace_back(balancers_[i]->port());    }    SetNextResolutionForLbChannel(ports, service_config_json,                                  lb_channel_response_generator);  }  void SetNextResolutionForLbChannel(      const std::vector<int>& ports, const char* service_config_json = nullptr,      grpc_core::FakeResolverResponseGenerator* lb_channel_response_generator =          nullptr) {    grpc_core::ExecCtx exec_ctx;    grpc_core::Resolver::Result result;    result.addresses = CreateAddressListFromPortList(ports);    if (service_config_json != nullptr) {      grpc_error* error = GRPC_ERROR_NONE;      result.service_config =          grpc_core::ServiceConfig::Create(service_config_json, &error);      GRPC_ERROR_UNREF(error);    }    if (lb_channel_response_generator == nullptr) {      lb_channel_response_generator = lb_channel_response_generator_.get();    }    lb_channel_response_generator->SetResponse(std::move(result));  }  void SetNextReresolutionResponse(const std::vector<int>& ports) {    grpc_core::ExecCtx exec_ctx;    grpc_core::Resolver::Result result;    result.addresses = CreateAddressListFromPortList(ports);    response_generator_->SetReresolutionResponse(std::move(result));  }  const std::vector<int> GetBackendPorts(size_t start_index = 0,                                         size_t stop_index = 0) const {    if (stop_index == 0) stop_index = backends_.size();    std::vector<int> backend_ports;    for (size_t i = start_index; i < stop_index; ++i) {      backend_ports.push_back(backends_[i]->port());    }    return backend_ports;  }  void ScheduleResponseForBalancer(size_t i, const DiscoveryResponse& response,                                   int delay_ms) {    balancers_[i]->ads_service()->AddEdsResponse(response, delay_ms);  }  Status SendRpc(EchoResponse* response = nullptr, int timeout_ms = 1000,                 bool wait_for_ready = false) {    const bool local_response = (response == nullptr);    if (local_response) response = new EchoResponse;    EchoRequest request;    request.set_message(kRequestMessage_);    ClientContext context;    context.set_deadline(grpc_timeout_milliseconds_to_deadline(timeout_ms));    if (wait_for_ready) context.set_wait_for_ready(true);    Status status = stub_->Echo(&context, request, response);    if (local_response) delete response;    return status;  }  void CheckRpcSendOk(const size_t times = 1, const int timeout_ms = 1000,                      bool wait_for_ready = false) {    for (size_t i = 0; i < times; ++i) {      EchoResponse response;      const Status status = SendRpc(&response, timeout_ms, wait_for_ready);      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  void CheckRpcSendFailure() {    const Status status = SendRpc();    EXPECT_FALSE(status.ok());  }  class ServerThread {   public:    ServerThread() : port_(g_port_saver->GetPort()) {}    virtual ~ServerThread(){};    void Start(const grpc::string& server_host) {      gpr_log(GPR_INFO, "starting %s server on port %d", Type(), port_);      GPR_ASSERT(!running_);      running_ = true;      StartAllServices();      grpc_core::Mutex mu;      // We need to acquire the lock here in order to prevent the notify_one      // by ServerThread::Serve from firing before the wait below is hit.      grpc_core::MutexLock lock(&mu);      grpc_core::CondVar cond;      thread_.reset(new std::thread(          std::bind(&ServerThread::Serve, this, server_host, &mu, &cond)));      cond.Wait(&mu);      gpr_log(GPR_INFO, "%s server startup complete", Type());    }    void Serve(const grpc::string& server_host, grpc_core::Mutex* mu,               grpc_core::CondVar* cond) {      // We need to acquire the lock here in order to prevent the notify_one      // below from firing before its corresponding wait is executed.      grpc_core::MutexLock lock(mu);      std::ostringstream server_address;      server_address << server_host << ":" << port_;      ServerBuilder builder;      std::shared_ptr<ServerCredentials> creds(new SecureServerCredentials(          grpc_fake_transport_security_server_credentials_create()));      builder.AddListeningPort(server_address.str(), creds);      RegisterAllServices(&builder);      server_ = builder.BuildAndStart();      cond->Signal();    }    void Shutdown() {      if (!running_) return;      gpr_log(GPR_INFO, "%s about to shutdown", Type());      ShutdownAllServices();      server_->Shutdown(grpc_timeout_milliseconds_to_deadline(0));      thread_->join();      gpr_log(GPR_INFO, "%s shutdown completed", Type());      running_ = false;    }    int port() const { return port_; }   private:    virtual void RegisterAllServices(ServerBuilder* builder) = 0;    virtual void StartAllServices() = 0;    virtual void ShutdownAllServices() = 0;    virtual const char* Type() = 0;    const int port_;    std::unique_ptr<Server> server_;    std::unique_ptr<std::thread> thread_;    bool running_ = false;  };  class BackendServerThread : public ServerThread {   public:    BackendServiceImpl* backend_service() { return &backend_service_; }   private:    void RegisterAllServices(ServerBuilder* builder) override {      builder->RegisterService(&backend_service_);    }    void StartAllServices() override { backend_service_.Start(); }    void ShutdownAllServices() override { backend_service_.Shutdown(); }    const char* Type() override { return "Backend"; }    BackendServiceImpl backend_service_;  };  class BalancerServerThread : public ServerThread {   public:    explicit BalancerServerThread(int client_load_reporting_interval = 0)        : ads_service_(client_load_reporting_interval > 0),          lrs_service_(client_load_reporting_interval) {}    AdsServiceImpl* ads_service() { return &ads_service_; }    LrsServiceImpl* lrs_service() { return &lrs_service_; }   private:    void RegisterAllServices(ServerBuilder* builder) override {      builder->RegisterService(&ads_service_);      builder->RegisterService(&lrs_service_);    }    void StartAllServices() override {      ads_service_.Start();      lrs_service_.Start();    }    void ShutdownAllServices() override {      ads_service_.Shutdown();      lrs_service_.Shutdown();    }    const char* Type() override { return "Balancer"; }    AdsServiceImpl ads_service_;    LrsServiceImpl lrs_service_;  };  const grpc::string server_host_;  const size_t num_backends_;  const size_t num_balancers_;  const int client_load_reporting_interval_seconds_;  std::shared_ptr<Channel> channel_;  std::unique_ptr<grpc::testing::EchoTestService::Stub> stub_;  std::vector<std::unique_ptr<BackendServerThread>> backends_;  std::vector<std::unique_ptr<BalancerServerThread>> balancers_;  grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>      response_generator_;  grpc_core::RefCountedPtr<grpc_core::FakeResolverResponseGenerator>      lb_channel_response_generator_;  const grpc::string kRequestMessage_ = "Live long and prosper.";  const grpc::string kApplicationTargetName_ = "application_target_name";  const char* kDefaultServiceConfig_ =      "{\n"      "  \"loadBalancingConfig\":[\n"      "    { \"does_not_exist\":{} },\n"      "    { \"xds_experimental\":{\n"      "      \"lrsLoadReportingServerName\": \"\"\n"      "    } }\n"      "  ]\n"      "}";  const char* kDefaultServiceConfigWithoutLoadReporting_ =      "{\n"      "  \"loadBalancingConfig\":[\n"      "    { \"does_not_exist\":{} },\n"      "    { \"xds_experimental\":{\n"      "    } }\n"      "  ]\n"      "}";};class BasicTest : public XdsEnd2endTest { public:  BasicTest() : XdsEnd2endTest(4, 1) {}};// Tests that the balancer sends the correct response to the client, and the// client sends RPCs to the backends using the default child policy.TEST_P(BasicTest, Vanilla) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcsPerAddress = 100;  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // We need to wait for all backends to come online.  WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress,              backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  // Check LB policy name for the channel.  EXPECT_EQ(      (GetParam().use_xds_resolver() ? "cds_experimental" : "xds_experimental"),      channel_->GetLoadBalancingPolicyName());}TEST_P(BasicTest, IgnoresUnhealthyEndpoints) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcsPerAddress = 100;  AdsServiceImpl::ResponseArgs args({      {"locality0",       GetBackendPorts(),       kDefaultLocalityWeight,       kDefaultLocalityPriority,       {envoy::api::v2::HealthStatus::DRAINING}},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // We need to wait for all backends to come online.  WaitForAllBackends(/*start_index=*/1);  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * (num_backends_ - 1));  // Each backend should have gotten 100 requests.  for (size_t i = 1; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress,              backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that subchannel sharing works when the same backend is listed multiple// times.TEST_P(BasicTest, SameBackendListedMultipleTimes) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  // Same backend listed twice.  std::vector<int> ports(2, backends_[0]->port());  AdsServiceImpl::ResponseArgs args({      {"locality0", ports},  });  const size_t kNumRpcsPerAddress = 10;  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // We need to wait for the backend to come online.  WaitForBackend(0);  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * ports.size());  // Backend should have gotten 20 requests.  EXPECT_EQ(kNumRpcsPerAddress * ports.size(),            backends_[0]->backend_service()->request_count());  // And they should have come from a single client port, because of  // subchannel sharing.  EXPECT_EQ(1UL, backends_[0]->backend_service()->clients().size());}// Tests that RPCs will be blocked until a non-empty serverlist is received.TEST_P(BasicTest, InitiallyEmptyServerlist) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor();  const int kCallDeadlineMs = kServerlistDelayMs * 2;  // First response is an empty serverlist, sent right away.  AdsServiceImpl::ResponseArgs::Locality empty_locality("locality0", {});  AdsServiceImpl::ResponseArgs args({      empty_locality,  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Send non-empty serverlist only after kServerlistDelayMs.  args = AdsServiceImpl::ResponseArgs({      {"locality0", GetBackendPorts()},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args),                              kServerlistDelayMs);  const auto t0 = system_clock::now();  // Client will block: LB will initially send empty serverlist.  CheckRpcSendOk(1, kCallDeadlineMs, true /* wait_for_ready */);  const auto ellapsed_ms =      std::chrono::duration_cast<std::chrono::milliseconds>(          system_clock::now() - t0);  // but eventually, the LB sends a serverlist update that allows the call to  // proceed. The call delay must be larger than the delay in sending the  // populated serverlist but under the call's deadline (which is enforced by  // the call's deadline).  EXPECT_GT(ellapsed_ms.count(), kServerlistDelayMs);  // The ADS service got a single request.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  // and sent two responses.  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}// Tests that RPCs will fail with UNAVAILABLE instead of DEADLINE_EXCEEDED if// all the servers are unreachable.TEST_P(BasicTest, AllServersUnreachableFailFast) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumUnreachableServers = 5;  std::vector<int> ports;  for (size_t i = 0; i < kNumUnreachableServers; ++i) {    ports.push_back(g_port_saver->GetPort());  }  AdsServiceImpl::ResponseArgs args({      {"locality0", ports},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  const Status status = SendRpc();  // The error shouldn't be DEADLINE_EXCEEDED.  EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code());  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that RPCs fail when the backends are down, and will succeed again after// the backends are restarted.TEST_P(BasicTest, BackendsRestart) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForAllBackends();  // Stop backends.  RPCs should fail.  ShutdownAllBackends();  CheckRpcSendFailure();  // Restart all backends.  RPCs should start succeeding again.  StartAllBackends();  CheckRpcSendOk(1 /* times */, 2000 /* timeout_ms */,                 true /* wait_for_ready */);}using SecureNamingTest = BasicTest;// Tests that secure naming check passes if target name is expected.TEST_P(SecureNamingTest, TargetNameIsExpected) {  // TODO(juanlishen): Use separate fake creds for the balancer channel.  ResetStub(0, 0, kApplicationTargetName_ + ";lb");  SetNextResolution({});  SetNextResolutionForLbChannel({balancers_[0]->port()});  const size_t kNumRpcsPerAddress = 100;  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Make sure that trying to connect works without a call.  channel_->GetState(true /* try_to_connect */);  // We need to wait for all backends to come online.  WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress,              backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that secure naming check fails if target name is unexpected.TEST_P(SecureNamingTest, TargetNameIsUnexpected) {  gpr_setenv("GRPC_XDS_BOOTSTRAP", g_bootstrap_file_bad);  ::testing::FLAGS_gtest_death_test_style = "threadsafe";  // Make sure that we blow up (via abort() from the security connector) when  // the name from the balancer doesn't match expectations.  ASSERT_DEATH_IF_SUPPORTED(      {        ResetStub(0, 0, kApplicationTargetName_ + ";lb");        SetNextResolution({});        SetNextResolutionForLbChannel({balancers_[0]->port()});        channel_->WaitForConnected(grpc_timeout_seconds_to_deadline(1));      },      "");}using LdsTest = BasicTest;// Tests that LDS client should send an ACK upon correct LDS response (with// inlined RDS result).TEST_P(LdsTest, Vanilla) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::ACKED);}// Tests that LDS client should send a NACK if there is no API listener in the// Listener in the LDS response.TEST_P(LdsTest, NoApiListener) {  auto listener = balancers_[0]->ads_service()->default_listener();  listener.clear_api_listener();  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name", listener}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::NACKED);}// Tests that LDS client should send a NACK if the route_specifier in the// http_connection_manager is neither inlined route_config nor RDS.TEST_P(LdsTest, WrongRouteSpecifier) {  auto listener = balancers_[0]->ads_service()->default_listener();  HttpConnectionManager http_connection_manager;  http_connection_manager.mutable_scoped_routes();  listener.mutable_api_listener()->mutable_api_listener()->PackFrom(      http_connection_manager);  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name", std::move(listener)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::NACKED);}// Tests that LDS client should send a NACK if matching domain can't be found in// the LDS response.TEST_P(LdsTest, NoMatchedDomain) {  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)->clear_domains();  route_config.mutable_virtual_hosts(0)->add_domains("unmatched_domain");  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name",        AdsServiceImpl::BuildListener(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::NACKED);}// Tests that LDS client should choose the virtual host with matching domain if// multiple virtual hosts exist in the LDS response.TEST_P(LdsTest, ChooseMatchedDomain) {  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  *(route_config.add_virtual_hosts()) = route_config.virtual_hosts(0);  route_config.mutable_virtual_hosts(0)->clear_domains();  route_config.mutable_virtual_hosts(0)->add_domains("unmatched_domain");  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_route()      ->mutable_cluster_header();  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name",        AdsServiceImpl::BuildListener(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::ACKED);}// Tests that LDS client should choose the last route in the virtual host if// multiple routes exist in the LDS response.TEST_P(LdsTest, ChooseLastRoute) {  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  *(route_config.mutable_virtual_hosts(0)->add_routes()) =      route_config.virtual_hosts(0).routes(0);  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_route()      ->mutable_cluster_header();  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name",        AdsServiceImpl::BuildListener(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::ACKED);}// Tests that LDS client should send a NACK if route match has non-empty prefix// in the LDS response.TEST_P(LdsTest, RouteMatchHasNonemptyPrefix) {  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_match()      ->set_prefix("nonempty_prefix");  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name",        AdsServiceImpl::BuildListener(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::NACKED);}// Tests that LDS client should send a NACK if route has an action other than// RouteAction in the LDS response.TEST_P(LdsTest, RouteHasNoRouteAction) {  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)->mutable_routes(0)->mutable_redirect();  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name",        AdsServiceImpl::BuildListener(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::NACKED);}// Tests that LDS client should send a NACK if RouteAction has a// cluster_specifier other than cluster in the LDS response.TEST_P(LdsTest, RouteActionHasNoCluster) {  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_route()      ->mutable_cluster_header();  balancers_[0]->ads_service()->SetLdsResponse(      {{"application_target_name",        AdsServiceImpl::BuildListener(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state(),            AdsServiceImpl::NACKED);}// Tests that LDS client times out when no response received.TEST_P(LdsTest, Timeout) {  ResetStub(0, 0, "", 500);  balancers_[0]->ads_service()->set_lds_ignore();  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();}using RdsTest = BasicTest;// Tests that RDS client should send an ACK upon correct RDS response.TEST_P(RdsTest, Vanilla) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::ACKED);}// Tests that RDS client should send a NACK if matching domain can't be found in// the RDS response.TEST_P(RdsTest, NoMatchedDomain) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)->clear_domains();  route_config.mutable_virtual_hosts(0)->add_domains("unmatched_domain");  balancers_[0]->ads_service()->SetRdsResponse(      {{"application_target_name", std::move(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::NACKED);}// Tests that RDS client should choose the virtual host with matching domain if// multiple virtual hosts exist in the RDS response.TEST_P(RdsTest, ChooseMatchedDomain) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  *(route_config.add_virtual_hosts()) = route_config.virtual_hosts(0);  route_config.mutable_virtual_hosts(0)->clear_domains();  route_config.mutable_virtual_hosts(0)->add_domains("unmatched_domain");  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_route()      ->mutable_cluster_header();  balancers_[0]->ads_service()->SetRdsResponse(      {{"application_target_name", std::move(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::ACKED);}// Tests that RDS client should choose the last route in the virtual host if// multiple routes exist in the RDS response.TEST_P(RdsTest, ChooseLastRoute) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  *(route_config.mutable_virtual_hosts(0)->add_routes()) =      route_config.virtual_hosts(0).routes(0);  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_route()      ->mutable_cluster_header();  balancers_[0]->ads_service()->SetRdsResponse(      {{"application_target_name", std::move(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::ACKED);}// Tests that RDS client should send a NACK if route match has non-empty prefix// in the RDS response.TEST_P(RdsTest, RouteMatchHasNonemptyPrefix) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_match()      ->set_prefix("nonempty_prefix");  balancers_[0]->ads_service()->SetRdsResponse(      {{"application_target_name", std::move(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::NACKED);}// Tests that RDS client should send a NACK if route has an action other than// RouteAction in the RDS response.TEST_P(RdsTest, RouteHasNoRouteAction) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)->mutable_routes(0)->mutable_redirect();  balancers_[0]->ads_service()->SetRdsResponse(      {{"application_target_name", std::move(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::NACKED);}// Tests that RDS client should send a NACK if RouteAction has a// cluster_specifier other than cluster in the RDS response.TEST_P(RdsTest, RouteActionHasNoCluster) {  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  RouteConfiguration route_config =      balancers_[0]->ads_service()->default_route_config();  route_config.mutable_virtual_hosts(0)      ->mutable_routes(0)      ->mutable_route()      ->mutable_cluster_header();  balancers_[0]->ads_service()->SetRdsResponse(      {{"application_target_name", std::move(route_config)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->rds_response_state(),            AdsServiceImpl::NACKED);}// Tests that RDS client times out when no response received.TEST_P(RdsTest, Timeout) {  ResetStub(0, 0, "", 500);  balancers_[0]->ads_service()->SetLdsToUseDynamicRds();  balancers_[0]->ads_service()->set_rds_ignore();  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();}using CdsTest = BasicTest;// Tests that CDS client should send an ACK upon correct CDS response.TEST_P(CdsTest, Vanilla) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  (void)SendRpc();  EXPECT_EQ(balancers_[0]->ads_service()->cds_response_state(),            AdsServiceImpl::ACKED);}// Tests that CDS client should send a NACK if the cluster type in CDS response// is other than EDS.TEST_P(CdsTest, WrongClusterType) {  auto cluster = balancers_[0]->ads_service()->default_cluster();  cluster.set_type(envoy::api::v2::Cluster::STATIC);  balancers_[0]->ads_service()->SetCdsResponse(      {{"application_target_name", std::move(cluster)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->cds_response_state(),            AdsServiceImpl::NACKED);}// Tests that CDS client should send a NACK if the eds_config in CDS response is// other than ADS.TEST_P(CdsTest, WrongEdsConfig) {  auto cluster = balancers_[0]->ads_service()->default_cluster();  cluster.mutable_eds_cluster_config()->mutable_eds_config()->mutable_self();  balancers_[0]->ads_service()->SetCdsResponse(      {{"application_target_name", std::move(cluster)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->cds_response_state(),            AdsServiceImpl::NACKED);}// Tests that CDS client should send a NACK if the lb_policy in CDS response is// other than ROUND_ROBIN.TEST_P(CdsTest, WrongLbPolicy) {  auto cluster = balancers_[0]->ads_service()->default_cluster();  cluster.set_lb_policy(envoy::api::v2::Cluster::LEAST_REQUEST);  balancers_[0]->ads_service()->SetCdsResponse(      {{"application_target_name", std::move(cluster)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->cds_response_state(),            AdsServiceImpl::NACKED);}// Tests that CDS client should send a NACK if the lrs_server in CDS response is// other than SELF.TEST_P(CdsTest, WrongLrsServer) {  auto cluster = balancers_[0]->ads_service()->default_cluster();  cluster.mutable_lrs_server()->mutable_ads();  balancers_[0]->ads_service()->SetCdsResponse(      {{"application_target_name", std::move(cluster)}});  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();  EXPECT_EQ(balancers_[0]->ads_service()->cds_response_state(),            AdsServiceImpl::NACKED);}// Tests that CDS client times out when no response received.TEST_P(CdsTest, Timeout) {  ResetStub(0, 0, "", 500);  balancers_[0]->ads_service()->set_cds_ignore();  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();}using EdsTest = BasicTest;// TODO(roth): Add tests showing that RPCs fail when EDS data is invalid.TEST_P(EdsTest, Timeout) {  ResetStub(0, 0, "", 500);  balancers_[0]->ads_service()->set_eds_ignore();  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  CheckRpcSendFailure();}using LocalityMapTest = BasicTest;// Tests that the localities in a locality map are picked according to their// weights.TEST_P(LocalityMapTest, WeightedRoundRobin) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 5000;  const int kLocalityWeight0 = 2;  const int kLocalityWeight1 = 8;  const int kTotalLocalityWeight = kLocalityWeight0 + kLocalityWeight1;  const double kLocalityWeightRate0 =      static_cast<double>(kLocalityWeight0) / kTotalLocalityWeight;  const double kLocalityWeightRate1 =      static_cast<double>(kLocalityWeight1) / kTotalLocalityWeight;  // ADS response contains 2 localities, each of which contains 1 backend.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kLocalityWeight0},      {"locality1", GetBackendPorts(1, 2), kLocalityWeight1},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Wait for both backends to be ready.  WaitForAllBackends(0, 2);  // Send kNumRpcs RPCs.  CheckRpcSendOk(kNumRpcs);  // The locality picking rates should be roughly equal to the expectation.  const double locality_picked_rate_0 =      static_cast<double>(backends_[0]->backend_service()->request_count()) /      kNumRpcs;  const double locality_picked_rate_1 =      static_cast<double>(backends_[1]->backend_service()->request_count()) /      kNumRpcs;  const double kErrorTolerance = 0.2;  EXPECT_THAT(locality_picked_rate_0,              ::testing::AllOf(                  ::testing::Ge(kLocalityWeightRate0 * (1 - kErrorTolerance)),                  ::testing::Le(kLocalityWeightRate0 * (1 + kErrorTolerance))));  EXPECT_THAT(locality_picked_rate_1,              ::testing::AllOf(                  ::testing::Ge(kLocalityWeightRate1 * (1 - kErrorTolerance)),                  ::testing::Le(kLocalityWeightRate1 * (1 + kErrorTolerance))));  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that the locality map can work properly even when it contains a large// number of localities.TEST_P(LocalityMapTest, StressTest) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumLocalities = 100;  // The first ADS response contains kNumLocalities localities, each of which  // contains backend 0.  AdsServiceImpl::ResponseArgs args;  for (size_t i = 0; i < kNumLocalities; ++i) {    grpc::string name = "locality" + std::to_string(i);    AdsServiceImpl::ResponseArgs::Locality locality(name,                                                    {backends_[0]->port()});    args.locality_list.emplace_back(std::move(locality));  }  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // The second ADS response contains 1 locality, which contains backend 1.  args = AdsServiceImpl::ResponseArgs({      {"locality0", GetBackendPorts(1, 2)},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args),                              60 * 1000);  // Wait until backend 0 is ready, before which kNumLocalities localities are  // received and handled by the xds policy.  WaitForBackend(0, /*reset_counters=*/false);  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  // Wait until backend 1 is ready, before which kNumLocalities localities are  // removed by the xds policy.  WaitForBackend(1);  // The ADS service got a single request.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  // and sent two responses.  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}// Tests that the localities in a locality map are picked correctly after update// (addition, modification, deletion).TEST_P(LocalityMapTest, UpdateMap) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 1000;  // The locality weight for the first 3 localities.  const std::vector<int> kLocalityWeights0 = {2, 3, 4};  const double kTotalLocalityWeight0 =      std::accumulate(kLocalityWeights0.begin(), kLocalityWeights0.end(), 0);  std::vector<double> locality_weight_rate_0;  for (int weight : kLocalityWeights0) {    locality_weight_rate_0.push_back(weight / kTotalLocalityWeight0);  }  // Delete the first locality, keep the second locality, change the third  // locality's weight from 4 to 2, and add a new locality with weight 6.  const std::vector<int> kLocalityWeights1 = {3, 2, 6};  const double kTotalLocalityWeight1 =      std::accumulate(kLocalityWeights1.begin(), kLocalityWeights1.end(), 0);  std::vector<double> locality_weight_rate_1 = {      0 /* placeholder for locality 0 */};  for (int weight : kLocalityWeights1) {    locality_weight_rate_1.push_back(weight / kTotalLocalityWeight1);  }  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), 2},      {"locality1", GetBackendPorts(1, 2), 3},      {"locality2", GetBackendPorts(2, 3), 4},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  args = AdsServiceImpl::ResponseArgs({      {"locality1", GetBackendPorts(1, 2), 3},      {"locality2", GetBackendPorts(2, 3), 2},      {"locality3", GetBackendPorts(3, 4), 6},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 5000);  // Wait for the first 3 backends to be ready.  WaitForAllBackends(0, 3);  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  // Send kNumRpcs RPCs.  CheckRpcSendOk(kNumRpcs);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // The picking rates of the first 3 backends should be roughly equal to the  // expectation.  std::vector<double> locality_picked_rates;  for (size_t i = 0; i < 3; ++i) {    locality_picked_rates.push_back(        static_cast<double>(backends_[i]->backend_service()->request_count()) /        kNumRpcs);  }  const double kErrorTolerance = 0.2;  for (size_t i = 0; i < 3; ++i) {    EXPECT_THAT(        locality_picked_rates[i],        ::testing::AllOf(            ::testing::Ge(locality_weight_rate_0[i] * (1 - kErrorTolerance)),            ::testing::Le(locality_weight_rate_0[i] * (1 + kErrorTolerance))));  }  // Backend 3 hasn't received any request.  EXPECT_EQ(0U, backends_[3]->backend_service()->request_count());  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  // Wait until the locality update has been processed, as signaled by backend 3  // receiving a request.  WaitForBackend(3);  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  // Send kNumRpcs RPCs.  CheckRpcSendOk(kNumRpcs);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // Backend 0 no longer receives any request.  EXPECT_EQ(0U, backends_[0]->backend_service()->request_count());  // The picking rates of the last 3 backends should be roughly equal to the  // expectation.  locality_picked_rates = {0 /* placeholder for backend 0 */};  for (size_t i = 1; i < 4; ++i) {    locality_picked_rates.push_back(        static_cast<double>(backends_[i]->backend_service()->request_count()) /        kNumRpcs);  }  for (size_t i = 1; i < 4; ++i) {    EXPECT_THAT(        locality_picked_rates[i],        ::testing::AllOf(            ::testing::Ge(locality_weight_rate_1[i] * (1 - kErrorTolerance)),            ::testing::Le(locality_weight_rate_1[i] * (1 + kErrorTolerance))));  }  // The ADS service got a single request.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  // and sent two responses.  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}class FailoverTest : public BasicTest { public:  FailoverTest() { ResetStub(0, 100, ""); }};// Localities with the highest priority are used when multiple priority exist.TEST_P(FailoverTest, ChooseHighestPriority) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 1},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 2},      {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 3},      {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 0},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForBackend(3, false);  for (size_t i = 0; i < 3; ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// If the higher priority localities are not reachable, failover to the highest// priority among the rest.TEST_P(FailoverTest, Failover) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 1},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 2},      {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 3},      {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 0},  });  ShutdownBackend(3);  ShutdownBackend(0);  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForBackend(1, false);  for (size_t i = 0; i < 4; ++i) {    if (i == 1) continue;    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// If a locality with higher priority than the current one becomes ready,// switch to it.TEST_P(FailoverTest, SwitchBackToHigherPriority) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 100;  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 1},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 2},      {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 3},      {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 0},  });  ShutdownBackend(3);  ShutdownBackend(0);  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForBackend(1, false);  for (size_t i = 0; i < 4; ++i) {    if (i == 1) continue;    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  StartBackend(0);  WaitForBackend(0);  CheckRpcSendOk(kNumRpcs);  EXPECT_EQ(kNumRpcs, backends_[0]->backend_service()->request_count());  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// The first update only contains unavailable priorities. The second update// contains available priorities.TEST_P(FailoverTest, UpdateInitialUnavailable) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 1},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  args = AdsServiceImpl::ResponseArgs({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 1},      {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 2},      {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 3},  });  ShutdownBackend(0);  ShutdownBackend(1);  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 1000);  gpr_timespec deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),                                       gpr_time_from_millis(500, GPR_TIMESPAN));  // Send 0.5 second worth of RPCs.  do {    CheckRpcSendFailure();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  WaitForBackend(2, false);  for (size_t i = 0; i < 4; ++i) {    if (i == 2) continue;    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}// Tests that after the localities' priorities are updated, we still choose the// highest READY priority with the updated localities.TEST_P(FailoverTest, UpdatePriority) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 100;  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 1},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 2},      {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 3},      {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 0},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  args = AdsServiceImpl::ResponseArgs({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 2},      {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 0},      {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 1},      {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 3},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 1000);  WaitForBackend(3, false);  for (size_t i = 0; i < 3; ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  WaitForBackend(1);  CheckRpcSendOk(kNumRpcs);  EXPECT_EQ(kNumRpcs, backends_[1]->backend_service()->request_count());  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}// Moves all localities in the current priority to a higher priority.TEST_P(FailoverTest, MoveAllLocalitiesInCurrentPriorityToHigherPriority) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  // First update:  // - Priority 0 is locality 0, containing backend 0, which is down.  // - Priority 1 is locality 1, containing backends 1 and 2, which are up.  ShutdownBackend(0);  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},      {"locality1", GetBackendPorts(1, 3), kDefaultLocalityWeight, 1},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Second update:  // - Priority 0 contains both localities 0 and 1.  // - Priority 1 is not present.  // - We add backend 3 to locality 1, just so we have a way to know  //   when the update has been seen by the client.  args = AdsServiceImpl::ResponseArgs({      {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},      {"locality1", GetBackendPorts(1, 4), kDefaultLocalityWeight, 0},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 1000);  // When we get the first update, all backends in priority 0 are down,  // so we will create priority 1.  Backends 1 and 2 should have traffic,  // but backend 3 should not.  WaitForAllBackends(1, 3, false);  EXPECT_EQ(0UL, backends_[3]->backend_service()->request_count());  // When backend 3 gets traffic, we know the second update has been seen.  WaitForBackend(3);  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}using DropTest = BasicTest;// Tests that RPCs are dropped according to the drop config.TEST_P(DropTest, Vanilla) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 5000;  const uint32_t kDropPerMillionForLb = 100000;  const uint32_t kDropPerMillionForThrottle = 200000;  const double kDropRateForLb = kDropPerMillionForLb / 1000000.0;  const double kDropRateForThrottle = kDropPerMillionForThrottle / 1000000.0;  const double KDropRateForLbAndThrottle =      kDropRateForLb + (1 - kDropRateForLb) * kDropRateForThrottle;  // The ADS response contains two drop categories.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, kDropPerMillionForLb},                          {kThrottleDropType, kDropPerMillionForThrottle}};  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForAllBackends();  // Send kNumRpcs RPCs and count the drops.  size_t num_drops = 0;  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  // The drop rate should be roughly equal to the expectation.  const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs;  const double kErrorTolerance = 0.2;  EXPECT_THAT(      seen_drop_rate,      ::testing::AllOf(          ::testing::Ge(KDropRateForLbAndThrottle * (1 - kErrorTolerance)),          ::testing::Le(KDropRateForLbAndThrottle * (1 + kErrorTolerance))));  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that drop config is converted correctly from per hundred.TEST_P(DropTest, DropPerHundred) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 5000;  const uint32_t kDropPerHundredForLb = 10;  const double kDropRateForLb = kDropPerHundredForLb / 100.0;  // The ADS response contains one drop category.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, kDropPerHundredForLb}};  args.drop_denominator = FractionalPercent::HUNDRED;  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForAllBackends();  // Send kNumRpcs RPCs and count the drops.  size_t num_drops = 0;  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  // The drop rate should be roughly equal to the expectation.  const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs;  const double kErrorTolerance = 0.2;  EXPECT_THAT(      seen_drop_rate,      ::testing::AllOf(::testing::Ge(kDropRateForLb * (1 - kErrorTolerance)),                       ::testing::Le(kDropRateForLb * (1 + kErrorTolerance))));  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that drop config is converted correctly from per ten thousand.TEST_P(DropTest, DropPerTenThousand) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 5000;  const uint32_t kDropPerTenThousandForLb = 1000;  const double kDropRateForLb = kDropPerTenThousandForLb / 10000.0;  // The ADS response contains one drop category.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, kDropPerTenThousandForLb}};  args.drop_denominator = FractionalPercent::TEN_THOUSAND;  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  WaitForAllBackends();  // Send kNumRpcs RPCs and count the drops.  size_t num_drops = 0;  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  // The drop rate should be roughly equal to the expectation.  const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs;  const double kErrorTolerance = 0.2;  EXPECT_THAT(      seen_drop_rate,      ::testing::AllOf(::testing::Ge(kDropRateForLb * (1 - kErrorTolerance)),                       ::testing::Le(kDropRateForLb * (1 + kErrorTolerance))));  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that drop is working correctly after update.TEST_P(DropTest, Update) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 1000;  const uint32_t kDropPerMillionForLb = 100000;  const uint32_t kDropPerMillionForThrottle = 200000;  const double kDropRateForLb = kDropPerMillionForLb / 1000000.0;  const double kDropRateForThrottle = kDropPerMillionForThrottle / 1000000.0;  const double KDropRateForLbAndThrottle =      kDropRateForLb + (1 - kDropRateForLb) * kDropRateForThrottle;  // The first ADS response contains one drop category.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, kDropPerMillionForLb}};  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // The second ADS response contains two drop categories.  // TODO(juanlishen): Change the ADS response sending to deterministic style  // (e.g., by using condition variable) so that we can shorten the test  // duration.  args.drop_categories = {{kLbDropType, kDropPerMillionForLb},                          {kThrottleDropType, kDropPerMillionForThrottle}};  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 10000);  WaitForAllBackends();  // Send kNumRpcs RPCs and count the drops.  size_t num_drops = 0;  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // The drop rate should be roughly equal to the expectation.  double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs;  const double kErrorTolerance = 0.3;  EXPECT_THAT(      seen_drop_rate,      ::testing::AllOf(::testing::Ge(kDropRateForLb * (1 - kErrorTolerance)),                       ::testing::Le(kDropRateForLb * (1 + kErrorTolerance))));  // Wait until the drop rate increases to the middle of the two configs, which  // implies that the update has been in effect.  const double kDropRateThreshold =      (kDropRateForLb + KDropRateForLbAndThrottle) / 2;  size_t num_rpcs = kNumRpcs;  while (seen_drop_rate < kDropRateThreshold) {    EchoResponse response;    const Status status = SendRpc(&response);    ++num_rpcs;    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }    seen_drop_rate = static_cast<double>(num_drops) / num_rpcs;  }  // Send kNumRpcs RPCs and count the drops.  num_drops = 0;  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // The new drop rate should be roughly equal to the expectation.  seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs;  EXPECT_THAT(      seen_drop_rate,      ::testing::AllOf(          ::testing::Ge(KDropRateForLbAndThrottle * (1 - kErrorTolerance)),          ::testing::Le(KDropRateForLbAndThrottle * (1 + kErrorTolerance))));  // The ADS service got a single request,  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  // and sent two responses  EXPECT_EQ(2U, balancers_[0]->ads_service()->response_count());}// Tests that all the RPCs are dropped if any drop category drops 100%.TEST_P(DropTest, DropAll) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 1000;  const uint32_t kDropPerMillionForLb = 100000;  const uint32_t kDropPerMillionForThrottle = 1000000;  // The ADS response contains two drop categories.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, kDropPerMillionForLb},                          {kThrottleDropType, kDropPerMillionForThrottle}};  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Send kNumRpcs RPCs and all of them are dropped.  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    EXPECT_TRUE(!status.ok() && status.error_message() ==                                    "Call dropped by load balancing policy");  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}using FallbackTest = BasicTest;// Tests that RPCs are handled by the fallback backends before the serverlist is// received, but will be handled by the serverlist after it's received.TEST_P(FallbackTest, Vanilla) {  const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor();  const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor();  const size_t kNumBackendsInResolution = backends_.size() / 2;  ResetStub(kFallbackTimeoutMs);  SetNextResolution(GetBackendPorts(0, kNumBackendsInResolution));  SetNextResolutionForLbChannelAllBalancers();  // Send non-empty serverlist only after kServerlistDelayMs.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(kNumBackendsInResolution)},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args),                              kServerlistDelayMs);  // Wait until all the fallback backends are reachable.  WaitForAllBackends(0 /* start_index */,                     kNumBackendsInResolution /* stop_index */);  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(kNumBackendsInResolution);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // Fallback is used: each backend returned by the resolver should have  // gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(1U, backends_[i]->backend_service()->request_count());  }  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  // Wait until the serverlist reception has been processed and all backends  // in the serverlist are reachable.  WaitForAllBackends(kNumBackendsInResolution /* start_index */);  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(backends_.size() - kNumBackendsInResolution);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // Serverlist is used: each backend returned by the balancer should  // have gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    EXPECT_EQ(1U, backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that RPCs are handled by the updated fallback backends before// serverlist is received,TEST_P(FallbackTest, Update) {  const int kFallbackTimeoutMs = 200 * grpc_test_slowdown_factor();  const int kServerlistDelayMs = 500 * grpc_test_slowdown_factor();  const size_t kNumBackendsInResolution = backends_.size() / 3;  const size_t kNumBackendsInResolutionUpdate = backends_.size() / 3;  ResetStub(kFallbackTimeoutMs);  SetNextResolution(GetBackendPorts(0, kNumBackendsInResolution));  SetNextResolutionForLbChannelAllBalancers();  // Send non-empty serverlist only after kServerlistDelayMs.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(kNumBackendsInResolution +                                    kNumBackendsInResolutionUpdate)},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args),                              kServerlistDelayMs);  // Wait until all the fallback backends are reachable.  WaitForAllBackends(0 /* start_index */,                     kNumBackendsInResolution /* stop_index */);  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(kNumBackendsInResolution);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // Fallback is used: each backend returned by the resolver should have  // gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(1U, backends_[i]->backend_service()->request_count());  }  for (size_t i = kNumBackendsInResolution; i < backends_.size(); ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  SetNextResolution(GetBackendPorts(      kNumBackendsInResolution,      kNumBackendsInResolution + kNumBackendsInResolutionUpdate));  // Wait until the resolution update has been processed and all the new  // fallback backends are reachable.  WaitForAllBackends(kNumBackendsInResolution /* start_index */,                     kNumBackendsInResolution +                         kNumBackendsInResolutionUpdate /* stop_index */);  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(kNumBackendsInResolutionUpdate);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // The resolution update is used: each backend in the resolution update should  // have gotten one request.  for (size_t i = 0; i < kNumBackendsInResolution; ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  for (size_t i = kNumBackendsInResolution;       i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) {    EXPECT_EQ(1U, backends_[i]->backend_service()->request_count());  }  for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate;       i < backends_.size(); ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  // Wait until the serverlist reception has been processed and all backends  // in the serverlist are reachable.  WaitForAllBackends(kNumBackendsInResolution +                     kNumBackendsInResolutionUpdate /* start_index */);  gpr_log(GPR_INFO, "========= BEFORE THIRD BATCH ==========");  CheckRpcSendOk(backends_.size() - kNumBackendsInResolution -                 kNumBackendsInResolutionUpdate);  gpr_log(GPR_INFO, "========= DONE WITH THIRD BATCH ==========");  // Serverlist is used: each backend returned by the balancer should  // have gotten one request.  for (size_t i = 0;       i < kNumBackendsInResolution + kNumBackendsInResolutionUpdate; ++i) {    EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());  }  for (size_t i = kNumBackendsInResolution + kNumBackendsInResolutionUpdate;       i < backends_.size(); ++i) {    EXPECT_EQ(1U, backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}// Tests that fallback will kick in immediately if the balancer channel fails.TEST_P(FallbackTest, FallbackEarlyWhenBalancerChannelFails) {  const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  // Return an unreachable balancer and one fallback backend.  SetNextResolution({backends_[0]->port()});  SetNextResolutionForLbChannel({g_port_saver->GetPort()});  // Send RPC with deadline less than the fallback timeout and make sure it  // succeeds.  CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000,                 /* wait_for_ready */ false);}// Tests that fallback will kick in immediately if the balancer call fails.TEST_P(FallbackTest, FallbackEarlyWhenBalancerCallFails) {  const int kFallbackTimeoutMs = 10000 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  // Return one balancer and one fallback backend.  SetNextResolution({backends_[0]->port()});  SetNextResolutionForLbChannelAllBalancers();  // Balancer drops call without sending a serverlist.  balancers_[0]->ads_service()->NotifyDoneWithAdsCall();  // Send RPC with deadline less than the fallback timeout and make sure it  // succeeds.  CheckRpcSendOk(/* times */ 1, /* timeout_ms */ 1000,                 /* wait_for_ready */ false);}// Tests that fallback mode is entered if balancer response is received but the// backends can't be reached.TEST_P(FallbackTest, FallbackIfResponseReceivedButChildNotReady) {  const int kFallbackTimeoutMs = 500 * grpc_test_slowdown_factor();  ResetStub(kFallbackTimeoutMs);  SetNextResolution({backends_[0]->port()});  SetNextResolutionForLbChannelAllBalancers();  // Send a serverlist that only contains an unreachable backend before fallback  // timeout.  AdsServiceImpl::ResponseArgs args({      {"locality0", {g_port_saver->GetPort()}},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Because no child policy is ready before fallback timeout, we enter fallback  // mode.  WaitForBackend(0);}// Tests that fallback mode is exited if the balancer tells the client to drop// all the calls.TEST_P(FallbackTest, FallbackModeIsExitedWhenBalancerSaysToDropAllCalls) {  // Return an unreachable balancer and one fallback backend.  SetNextResolution({backends_[0]->port()});  SetNextResolutionForLbChannel({g_port_saver->GetPort()});  // Enter fallback mode because the LB channel fails to connect.  WaitForBackend(0);  // Return a new balancer that sends a response to drop all calls.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, 1000000}};  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  SetNextResolutionForLbChannelAllBalancers();  // Send RPCs until failure.  gpr_timespec deadline = gpr_time_add(      gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(5000, GPR_TIMESPAN));  do {    auto status = SendRpc();    if (!status.ok()) break;  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  CheckRpcSendFailure();}// Tests that fallback mode is exited if the child policy becomes ready.TEST_P(FallbackTest, FallbackModeIsExitedAfterChildRready) {  // Return an unreachable balancer and one fallback backend.  SetNextResolution({backends_[0]->port()});  SetNextResolutionForLbChannel({g_port_saver->GetPort()});  // Enter fallback mode because the LB channel fails to connect.  WaitForBackend(0);  // Return a new balancer that sends a dead backend.  ShutdownBackend(1);  AdsServiceImpl::ResponseArgs args({      {"locality0", {backends_[1]->port()}},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  SetNextResolutionForLbChannelAllBalancers();  // The state (TRANSIENT_FAILURE) update from the child policy will be ignored  // because we are still in fallback mode.  gpr_timespec deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),                                       gpr_time_from_millis(500, GPR_TIMESPAN));  // Send 0.5 second worth of RPCs.  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // After the backend is restarted, the child policy will eventually be READY,  // and we will exit fallback mode.  StartBackend(1);  WaitForBackend(1);  // We have exited fallback mode, so calls will go to the child policy  // exclusively.  CheckRpcSendOk(100);  EXPECT_EQ(0U, backends_[0]->backend_service()->request_count());  EXPECT_EQ(100U, backends_[1]->backend_service()->request_count());}class BalancerUpdateTest : public XdsEnd2endTest { public:  BalancerUpdateTest() : XdsEnd2endTest(4, 3) {}};// Tests that the old LB call is still used after the balancer address update as// long as that call is still alive.TEST_P(BalancerUpdateTest, UpdateBalancersButKeepUsingOriginalBalancer) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  AdsServiceImpl::ResponseArgs args({      {"locality0", {backends_[0]->port()}},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  args = AdsServiceImpl::ResponseArgs({      {"locality0", {backends_[1]->port()}},  });  ScheduleResponseForBalancer(1, AdsServiceImpl::BuildResponse(args), 0);  // Wait until the first backend is ready.  WaitForBackend(0);  // Send 10 requests.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->backend_service()->request_count());  // The ADS service of balancer 0 got a single request, and sent a single  // response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->response_count());  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 ==========");  SetNextResolutionForLbChannel({balancers_[1]->port()});  gpr_log(GPR_INFO, "========= UPDATE 1 DONE ==========");  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  gpr_timespec deadline = gpr_time_add(      gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(10000, GPR_TIMESPAN));  // Send 10 seconds worth of RPCs  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // The current LB call is still working, so xds continued using it to the  // first balancer, which doesn't assign the second backend.  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->response_count());}// Tests that the old LB call is still used after multiple balancer address// updates as long as that call is still alive. Send an update with the same set// of LBs as the one in SetUp() in order to verify that the LB channel inside// xds keeps the initial connection (which by definition is also present in the// update).TEST_P(BalancerUpdateTest, Repeated) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  AdsServiceImpl::ResponseArgs args({      {"locality0", {backends_[0]->port()}},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  args = AdsServiceImpl::ResponseArgs({      {"locality0", {backends_[1]->port()}},  });  ScheduleResponseForBalancer(1, AdsServiceImpl::BuildResponse(args), 0);  // Wait until the first backend is ready.  WaitForBackend(0);  // Send 10 requests.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->backend_service()->request_count());  // The ADS service of balancer 0 got a single request, and sent a single  // response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->response_count());  std::vector<int> ports;  ports.emplace_back(balancers_[0]->port());  ports.emplace_back(balancers_[1]->port());  ports.emplace_back(balancers_[2]->port());  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 ==========");  SetNextResolutionForLbChannel(ports);  gpr_log(GPR_INFO, "========= UPDATE 1 DONE ==========");  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  gpr_timespec deadline = gpr_time_add(      gpr_now(GPR_CLOCK_REALTIME), gpr_time_from_millis(10000, GPR_TIMESPAN));  // Send 10 seconds worth of RPCs  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // xds continued using the original LB call to the first balancer, which  // doesn't assign the second backend.  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  ports.clear();  ports.emplace_back(balancers_[0]->port());  ports.emplace_back(balancers_[1]->port());  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 2 ==========");  SetNextResolutionForLbChannel(ports);  gpr_log(GPR_INFO, "========= UPDATE 2 DONE ==========");  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  deadline = gpr_time_add(gpr_now(GPR_CLOCK_REALTIME),                          gpr_time_from_millis(10000, GPR_TIMESPAN));  // Send 10 seconds worth of RPCs  do {    CheckRpcSendOk();  } while (gpr_time_cmp(gpr_now(GPR_CLOCK_REALTIME), deadline) < 0);  // xds continued using the original LB call to the first balancer, which  // doesn't assign the second backend.  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());}// Tests that if the balancer is down, the RPCs will still be sent to the// backends according to the last balancer response, until a new balancer is// reachable.TEST_P(BalancerUpdateTest, DeadUpdate) {  SetNextResolution({});  SetNextResolutionForLbChannel({balancers_[0]->port()});  AdsServiceImpl::ResponseArgs args({      {"locality0", {backends_[0]->port()}},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  args = AdsServiceImpl::ResponseArgs({      {"locality0", {backends_[1]->port()}},  });  ScheduleResponseForBalancer(1, AdsServiceImpl::BuildResponse(args), 0);  // Start servers and send 10 RPCs per server.  gpr_log(GPR_INFO, "========= BEFORE FIRST BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH FIRST BATCH ==========");  // All 10 requests should have gone to the first backend.  EXPECT_EQ(10U, backends_[0]->backend_service()->request_count());  // Kill balancer 0  gpr_log(GPR_INFO, "********** ABOUT TO KILL BALANCER 0 *************");  balancers_[0]->Shutdown();  gpr_log(GPR_INFO, "********** KILLED BALANCER 0 *************");  // This is serviced by the existing child policy.  gpr_log(GPR_INFO, "========= BEFORE SECOND BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH SECOND BATCH ==========");  // All 10 requests should again have gone to the first backend.  EXPECT_EQ(20U, backends_[0]->backend_service()->request_count());  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  // The ADS service of balancer 0 got a single request, and sent a single  // response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[1]->ads_service()->response_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->response_count());  gpr_log(GPR_INFO, "========= ABOUT TO UPDATE 1 ==========");  SetNextResolutionForLbChannel({balancers_[1]->port()});  gpr_log(GPR_INFO, "========= UPDATE 1 DONE ==========");  // Wait until update has been processed, as signaled by the second backend  // receiving a request. In the meantime, the client continues to be serviced  // (by the first backend) without interruption.  EXPECT_EQ(0U, backends_[1]->backend_service()->request_count());  WaitForBackend(1);  // This is serviced by the updated RR policy  backends_[1]->backend_service()->ResetCounters();  gpr_log(GPR_INFO, "========= BEFORE THIRD BATCH ==========");  CheckRpcSendOk(10);  gpr_log(GPR_INFO, "========= DONE WITH THIRD BATCH ==========");  // All 10 requests should have gone to the second backend.  EXPECT_EQ(10U, backends_[1]->backend_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  // The second balancer, published as part of the first update, may end up  // getting two requests (that is, 1 <= #req <= 2) if the LB call retry timer  // firing races with the arrival of the update containing the second  // balancer.  EXPECT_GE(balancers_[1]->ads_service()->request_count(), 1U);  EXPECT_GE(balancers_[1]->ads_service()->response_count(), 1U);  EXPECT_LE(balancers_[1]->ads_service()->request_count(), 2U);  EXPECT_LE(balancers_[1]->ads_service()->response_count(), 2U);  EXPECT_EQ(0U, balancers_[2]->ads_service()->request_count());  EXPECT_EQ(0U, balancers_[2]->ads_service()->response_count());}// The re-resolution tests are deferred because they rely on the fallback mode,// which hasn't been supported.// TODO(juanlishen): Add TEST_P(BalancerUpdateTest, ReresolveDeadBackend).// TODO(juanlishen): Add TEST_P(UpdatesWithClientLoadReportingTest,// ReresolveDeadBalancer)class ClientLoadReportingTest : public XdsEnd2endTest { public:  ClientLoadReportingTest() : XdsEnd2endTest(4, 1, 3) {}};// Tests that the load report received at the balancer is correct.TEST_P(ClientLoadReportingTest, Vanilla) {  SetNextResolution({});  SetNextResolutionForLbChannel({balancers_[0]->port()});  const size_t kNumRpcsPerAddress = 100;  // TODO(juanlishen): Partition the backends after multiple localities is  // tested.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Wait until all backends are ready.  int num_ok = 0;  int num_failure = 0;  int num_drops = 0;  std::tie(num_ok, num_failure, num_drops) = WaitForAllBackends();  // Send kNumRpcsPerAddress RPCs per server.  CheckRpcSendOk(kNumRpcsPerAddress * num_backends_);  // Each backend should have gotten 100 requests.  for (size_t i = 0; i < backends_.size(); ++i) {    EXPECT_EQ(kNumRpcsPerAddress,              backends_[i]->backend_service()->request_count());  }  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());  // The LRS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->lrs_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->lrs_service()->response_count());  // The load report received at the balancer should be correct.  ClientStats* client_stats = balancers_[0]->lrs_service()->WaitForLoadReport();  EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok,            client_stats->total_successful_requests());  EXPECT_EQ(0U, client_stats->total_requests_in_progress());  EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok,            client_stats->total_issued_requests());  EXPECT_EQ(0U, client_stats->total_error_requests());  EXPECT_EQ(0U, client_stats->total_dropped_requests());}// Tests that if the balancer restarts, the client load report contains the// stats before and after the restart correctly.TEST_P(ClientLoadReportingTest, BalancerRestart) {  SetNextResolution({});  SetNextResolutionForLbChannel({balancers_[0]->port()});  const size_t kNumBackendsFirstPass = backends_.size() / 2;  const size_t kNumBackendsSecondPass =      backends_.size() - kNumBackendsFirstPass;  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts(0, kNumBackendsFirstPass)},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Wait until all backends returned by the balancer are ready.  int num_ok = 0;  int num_failure = 0;  int num_drops = 0;  std::tie(num_ok, num_failure, num_drops) =      WaitForAllBackends(/* start_index */ 0,                         /* stop_index */ kNumBackendsFirstPass);  ClientStats* client_stats = balancers_[0]->lrs_service()->WaitForLoadReport();  EXPECT_EQ(static_cast<size_t>(num_ok),            client_stats->total_successful_requests());  EXPECT_EQ(0U, client_stats->total_requests_in_progress());  EXPECT_EQ(0U, client_stats->total_error_requests());  EXPECT_EQ(0U, client_stats->total_dropped_requests());  // Shut down the balancer.  balancers_[0]->Shutdown();  // We should continue using the last EDS response we received from the  // balancer before it was shut down.  // Note: We need to use WaitForAllBackends() here instead of just  // CheckRpcSendOk(kNumBackendsFirstPass), because when the balancer  // shuts down, the XdsClient will generate an error to the  // ServiceConfigWatcher, which will cause the xds resolver to send a  // no-op update to the LB policy.  When this update gets down to the  // round_robin child policy for the locality, it will generate a new  // subchannel list, which resets the start index randomly.  So we need  // to be a little more permissive here to avoid spurious failures.  ResetBackendCounters();  int num_started = std::get<0>(WaitForAllBackends(      /* start_index */ 0, /* stop_index */ kNumBackendsFirstPass));  // Now restart the balancer, this time pointing to the new backends.  balancers_[0]->Start(server_host_);  args = AdsServiceImpl::ResponseArgs({      {"locality0", GetBackendPorts(kNumBackendsFirstPass)},  });  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  // Wait for queries to start going to one of the new backends.  // This tells us that we're now using the new serverlist.  std::tie(num_ok, num_failure, num_drops) =      WaitForAllBackends(/* start_index */ kNumBackendsFirstPass);  num_started += num_ok + num_failure + num_drops;  // Send one RPC per backend.  CheckRpcSendOk(kNumBackendsSecondPass);  num_started += kNumBackendsSecondPass;  // Check client stats.  client_stats = balancers_[0]->lrs_service()->WaitForLoadReport();  EXPECT_EQ(num_started, client_stats->total_successful_requests());  EXPECT_EQ(0U, client_stats->total_requests_in_progress());  EXPECT_EQ(0U, client_stats->total_error_requests());  EXPECT_EQ(0U, client_stats->total_dropped_requests());}class ClientLoadReportingWithDropTest : public XdsEnd2endTest { public:  ClientLoadReportingWithDropTest() : XdsEnd2endTest(4, 1, 20) {}};// Tests that the drop stats are correctly reported by client load reporting.TEST_P(ClientLoadReportingWithDropTest, Vanilla) {  SetNextResolution({});  SetNextResolutionForLbChannelAllBalancers();  const size_t kNumRpcs = 3000;  const uint32_t kDropPerMillionForLb = 100000;  const uint32_t kDropPerMillionForThrottle = 200000;  const double kDropRateForLb = kDropPerMillionForLb / 1000000.0;  const double kDropRateForThrottle = kDropPerMillionForThrottle / 1000000.0;  const double KDropRateForLbAndThrottle =      kDropRateForLb + (1 - kDropRateForLb) * kDropRateForThrottle;  // The ADS response contains two drop categories.  AdsServiceImpl::ResponseArgs args({      {"locality0", GetBackendPorts()},  });  args.drop_categories = {{kLbDropType, kDropPerMillionForLb},                          {kThrottleDropType, kDropPerMillionForThrottle}};  ScheduleResponseForBalancer(0, AdsServiceImpl::BuildResponse(args), 0);  int num_ok = 0;  int num_failure = 0;  int num_drops = 0;  std::tie(num_ok, num_failure, num_drops) = WaitForAllBackends();  const size_t num_warmup = num_ok + num_failure + num_drops;  // Send kNumRpcs RPCs and count the drops.  for (size_t i = 0; i < kNumRpcs; ++i) {    EchoResponse response;    const Status status = SendRpc(&response);    if (!status.ok() &&        status.error_message() == "Call dropped by load balancing policy") {      ++num_drops;    } else {      EXPECT_TRUE(status.ok()) << "code=" << status.error_code()                               << " message=" << status.error_message();      EXPECT_EQ(response.message(), kRequestMessage_);    }  }  // The drop rate should be roughly equal to the expectation.  const double seen_drop_rate = static_cast<double>(num_drops) / kNumRpcs;  const double kErrorTolerance = 0.2;  EXPECT_THAT(      seen_drop_rate,      ::testing::AllOf(          ::testing::Ge(KDropRateForLbAndThrottle * (1 - kErrorTolerance)),          ::testing::Le(KDropRateForLbAndThrottle * (1 + kErrorTolerance))));  // Check client stats.  ClientStats* client_stats = balancers_[0]->lrs_service()->WaitForLoadReport();  EXPECT_EQ(num_drops, client_stats->total_dropped_requests());  const size_t total_rpc = num_warmup + kNumRpcs;  EXPECT_THAT(      client_stats->dropped_requests(kLbDropType),      ::testing::AllOf(          ::testing::Ge(total_rpc * kDropRateForLb * (1 - kErrorTolerance)),          ::testing::Le(total_rpc * kDropRateForLb * (1 + kErrorTolerance))));  EXPECT_THAT(client_stats->dropped_requests(kThrottleDropType),              ::testing::AllOf(                  ::testing::Ge(total_rpc * (1 - kDropRateForLb) *                                kDropRateForThrottle * (1 - kErrorTolerance)),                  ::testing::Le(total_rpc * (1 - kDropRateForLb) *                                kDropRateForThrottle * (1 + kErrorTolerance))));  // The ADS service got a single request, and sent a single response.  EXPECT_EQ(1U, balancers_[0]->ads_service()->request_count());  EXPECT_EQ(1U, balancers_[0]->ads_service()->response_count());}grpc::string TestTypeName(const ::testing::TestParamInfo<TestType>& info) {  return info.param.AsString();}INSTANTIATE_TEST_SUITE_P(XdsTest, BasicTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false),                                           TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);INSTANTIATE_TEST_SUITE_P(XdsTest, SecureNamingTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false),                                           TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);// LDS depends on XdsResolver.INSTANTIATE_TEST_SUITE_P(XdsTest, LdsTest,                         ::testing::Values(TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);// RDS depends on XdsResolver.INSTANTIATE_TEST_SUITE_P(XdsTest, RdsTest,                         ::testing::Values(TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);// CDS depends on XdsResolver.INSTANTIATE_TEST_SUITE_P(XdsTest, CdsTest,                         ::testing::Values(TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);// EDS could be tested with or without XdsResolver, but the tests would// be the same either way, so we test it only with XdsResolver.INSTANTIATE_TEST_SUITE_P(XdsTest, EdsTest,                         ::testing::Values(TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);INSTANTIATE_TEST_SUITE_P(XdsTest, LocalityMapTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false),                                           TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);INSTANTIATE_TEST_SUITE_P(XdsTest, FailoverTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false),                                           TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);INSTANTIATE_TEST_SUITE_P(XdsTest, DropTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false),                                           TestType(true, false),                                           TestType(true, true)),                         &TestTypeName);// Fallback does not work with xds resolver.INSTANTIATE_TEST_SUITE_P(XdsTest, FallbackTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false)),                         &TestTypeName);INSTANTIATE_TEST_SUITE_P(XdsTest, BalancerUpdateTest,                         ::testing::Values(TestType(false, true),                                           TestType(false, false),                                           TestType(true, true)),                         &TestTypeName);// Load reporting tests are not run with load reporting disabled.INSTANTIATE_TEST_SUITE_P(XdsTest, ClientLoadReportingTest,                         ::testing::Values(TestType(false, true),                                           TestType(true, true)),                         &TestTypeName);// Load reporting tests are not run with load reporting disabled.INSTANTIATE_TEST_SUITE_P(XdsTest, ClientLoadReportingWithDropTest,                         ::testing::Values(TestType(false, true),                                           TestType(true, true)),                         &TestTypeName);}  // namespace}  // namespace testing}  // namespace grpcint main(int argc, char** argv) {  grpc::testing::TestEnvironment env(argc, argv);  ::testing::InitGoogleTest(&argc, argv);  grpc::testing::WriteBootstrapFiles();  grpc::testing::g_port_saver = new grpc::testing::PortSaver();  const auto result = RUN_ALL_TESTS();  return result;}
 |