| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643 | 
							- /*
 
-  *
 
-  * 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 <deque>
 
- #include <memory>
 
- #include <mutex>
 
- #include <numeric>
 
- #include <set>
 
- #include <sstream>
 
- #include <string>
 
- #include <thread>
 
- #include <vector>
 
- #include <gmock/gmock.h>
 
- #include <gtest/gtest.h>
 
- #include "absl/strings/str_cat.h"
 
- #include "absl/types/optional.h"
 
- #include <grpc/grpc.h>
 
- #include <grpc/support/alloc.h>
 
- #include <grpc/support/log.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/resolver/fake/fake_resolver.h"
 
- #include "src/core/ext/filters/client_channel/server_address.h"
 
- #include "src/core/ext/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/parse_address.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 "src/proto/grpc/testing/xds/v3/ads.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/cluster.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/discovery.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/endpoint.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/http_connection_manager.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/listener.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/lrs.grpc.pb.h"
 
- #include "src/proto/grpc/testing/xds/v3/route.grpc.pb.h"
 
- namespace grpc {
 
- namespace testing {
 
- namespace {
 
- using std::chrono::system_clock;
 
- using ::envoy::config::cluster::v3::Cluster;
 
- using ::envoy::config::endpoint::v3::ClusterLoadAssignment;
 
- using ::envoy::config::endpoint::v3::HealthStatus;
 
- using ::envoy::config::listener::v3::Listener;
 
- using ::envoy::config::route::v3::RouteConfiguration;
 
- using ::envoy::extensions::filters::network::http_connection_manager::v3::
 
-     HttpConnectionManager;
 
- using ::envoy::type::v3::FractionalPercent;
 
- constexpr char kLdsTypeUrl[] =
 
-     "type.googleapis.com/envoy.config.listener.v3.Listener";
 
- constexpr char kRdsTypeUrl[] =
 
-     "type.googleapis.com/envoy.config.route.v3.RouteConfiguration";
 
- constexpr char kCdsTypeUrl[] =
 
-     "type.googleapis.com/envoy.config.cluster.v3.Cluster";
 
- constexpr char kEdsTypeUrl[] =
 
-     "type.googleapis.com/envoy.config.endpoint.v3.ClusterLoadAssignment";
 
- constexpr char kLdsV2TypeUrl[] = "type.googleapis.com/envoy.api.v2.Listener";
 
- constexpr char kRdsV2TypeUrl[] =
 
-     "type.googleapis.com/envoy.api.v2.RouteConfiguration";
 
- constexpr char kCdsV2TypeUrl[] = "type.googleapis.com/envoy.api.v2.Cluster";
 
- constexpr char kEdsV2TypeUrl[] =
 
-     "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 char kServerName[] = "server.example.com";
 
- constexpr char kDefaultRouteConfigurationName[] = "route_config_name";
 
- constexpr char kDefaultClusterName[] = "cluster_name";
 
- constexpr char kDefaultEdsServiceName[] = "eds_service_name";
 
- constexpr int kDefaultLocalityWeight = 3;
 
- constexpr int kDefaultLocalityPriority = 0;
 
- constexpr char kRequestMessage[] = "Live long and prosper.";
 
- constexpr char kDefaultServiceConfig[] =
 
-     "{\n"
 
-     "  \"loadBalancingConfig\":[\n"
 
-     "    { \"does_not_exist\":{} },\n"
 
-     "    { \"eds_experimental\":{\n"
 
-     "      \"clusterName\": \"application_target_name\",\n"
 
-     "      \"lrsLoadReportingServerName\": \"\"\n"
 
-     "    } }\n"
 
-     "  ]\n"
 
-     "}";
 
- constexpr char kDefaultServiceConfigWithoutLoadReporting[] =
 
-     "{\n"
 
-     "  \"loadBalancingConfig\":[\n"
 
-     "    { \"does_not_exist\":{} },\n"
 
-     "    { \"eds_experimental\":{\n"
 
-     "      \"clusterName\": \"application_target_name\"\n"
 
-     "    } }\n"
 
-     "  ]\n"
 
-     "}";
 
- constexpr char kBootstrapFileV3[] =
 
-     "{\n"
 
-     "  \"xds_servers\": [\n"
 
-     "    {\n"
 
-     "      \"server_uri\": \"fake:///lb\",\n"
 
-     "      \"channel_creds\": [\n"
 
-     "        {\n"
 
-     "          \"type\": \"fake\"\n"
 
-     "        }\n"
 
-     "      ],\n"
 
-     "      \"server_features\": [\"xds_v3\"]\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 kBootstrapFileV2[] =
 
-     "{\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_v3;
 
- char* g_bootstrap_file_v2;
 
- char* g_bootstrap_file_bad;
 
- void WriteBootstrapFiles() {
 
-   char* bootstrap_file;
 
-   FILE* out = gpr_tmpfile("xds_bootstrap_v3", &bootstrap_file);
 
-   fputs(kBootstrapFileV3, out);
 
-   fclose(out);
 
-   g_bootstrap_file_v3 = bootstrap_file;
 
-   out = gpr_tmpfile("xds_bootstrap_v2", &bootstrap_file);
 
-   fputs(kBootstrapFileV2, out);
 
-   fclose(out);
 
-   g_bootstrap_file_v2 = 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;
 
-   }
 
-  private:
 
-   grpc_core::Mutex mu_;
 
-   size_t request_count_ = 0;
 
-   size_t response_count_ = 0;
 
- };
 
- const char g_kCallCredsMdKey[] = "Balancer should not ...";
 
- const char g_kCallCredsMdValue[] = "... receive me";
 
- template <typename RpcService>
 
- class BackendServiceImpl
 
-     : public CountedService<TestMultipleServiceImpl<RpcService>> {
 
-  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);
 
-     }
 
-     CountedService<TestMultipleServiceImpl<RpcService>>::IncreaseRequestCount();
 
-     const auto status =
 
-         TestMultipleServiceImpl<RpcService>::Echo(context, request, response);
 
-     CountedService<
 
-         TestMultipleServiceImpl<RpcService>>::IncreaseResponseCount();
 
-     AddClient(context->peer());
 
-     return status;
 
-   }
 
-   Status Echo1(ServerContext* context, const EchoRequest* request,
 
-                EchoResponse* response) override {
 
-     return Echo(context, request, response);
 
-   }
 
-   Status Echo2(ServerContext* context, const EchoRequest* request,
 
-                EchoResponse* response) override {
 
-     return Echo(context, request, response);
 
-   }
 
-   void Start() {}
 
-   void Shutdown() {}
 
-   std::set<std::string> clients() {
 
-     grpc_core::MutexLock lock(&clients_mu_);
 
-     return clients_;
 
-   }
 
-  private:
 
-   void AddClient(const std::string& client) {
 
-     grpc_core::MutexLock lock(&clients_mu_);
 
-     clients_.insert(client);
 
-   }
 
-   grpc_core::Mutex clients_mu_;
 
-   std::set<std::string> clients_;
 
- };
 
- class ClientStats {
 
-  public:
 
-   struct LocalityStats {
 
-     LocalityStats() {}
 
-     // Converts from proto message class.
 
-     template <class UpstreamLocalityStats>
 
-     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()) {}
 
-     LocalityStats& operator+=(const LocalityStats& other) {
 
-       total_successful_requests += other.total_successful_requests;
 
-       total_requests_in_progress += other.total_requests_in_progress;
 
-       total_error_requests += other.total_error_requests;
 
-       total_issued_requests += other.total_issued_requests;
 
-       return *this;
 
-     }
 
-     uint64_t total_successful_requests = 0;
 
-     uint64_t total_requests_in_progress = 0;
 
-     uint64_t total_error_requests = 0;
 
-     uint64_t total_issued_requests = 0;
 
-   };
 
-   ClientStats() {}
 
-   // Converts from proto message class.
 
-   template <class ClusterStats>
 
-   explicit ClientStats(const ClusterStats& cluster_stats)
 
-       : cluster_name_(cluster_stats.cluster_name()),
 
-         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());
 
-     }
 
-   }
 
-   const std::string& cluster_name() const { return cluster_name_; }
 
-   const std::map<std::string, LocalityStats>& locality_stats() const {
 
-     return locality_stats_;
 
-   }
 
-   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 std::string& category) const {
 
-     auto iter = dropped_requests_.find(category);
 
-     GPR_ASSERT(iter != dropped_requests_.end());
 
-     return iter->second;
 
-   }
 
-   ClientStats& operator+=(const ClientStats& other) {
 
-     for (const auto& p : other.locality_stats_) {
 
-       locality_stats_[p.first] += p.second;
 
-     }
 
-     total_dropped_requests_ += other.total_dropped_requests_;
 
-     for (const auto& p : other.dropped_requests_) {
 
-       dropped_requests_[p.first] += p.second;
 
-     }
 
-     return *this;
 
-   }
 
-  private:
 
-   std::string cluster_name_;
 
-   std::map<std::string, LocalityStats> locality_stats_;
 
-   uint64_t total_dropped_requests_ = 0;
 
-   std::map<std::string, uint64_t> dropped_requests_;
 
- };
 
- class AdsServiceImpl : public std::enable_shared_from_this<AdsServiceImpl> {
 
-  public:
 
-   struct ResponseState {
 
-     enum State { NOT_SENT, SENT, ACKED, NACKED };
 
-     State state = NOT_SENT;
 
-     std::string error_message;
 
-   };
 
-   struct EdsResourceArgs {
 
-     struct Locality {
 
-       Locality(const std::string& sub_zone, std::vector<int> ports,
 
-                int lb_weight = kDefaultLocalityWeight,
 
-                int priority = kDefaultLocalityPriority,
 
-                std::vector<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 std::string sub_zone;
 
-       std::vector<int> ports;
 
-       int lb_weight;
 
-       int priority;
 
-       std::vector<HealthStatus> health_statuses;
 
-     };
 
-     EdsResourceArgs() = default;
 
-     explicit EdsResourceArgs(std::vector<Locality> locality_list)
 
-         : locality_list(std::move(locality_list)) {}
 
-     std::vector<Locality> locality_list;
 
-     std::map<std::string, uint32_t> drop_categories;
 
-     FractionalPercent::DenominatorType drop_denominator =
 
-         FractionalPercent::MILLION;
 
-   };
 
-   explicit AdsServiceImpl(bool enable_load_reporting)
 
-       : v2_rpc_service_(this, /*is_v2=*/true),
 
-         v3_rpc_service_(this, /*is_v2=*/false) {
 
-     // Construct RDS response data.
 
-     default_route_config_.set_name(kDefaultRouteConfigurationName);
 
-     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(kDefaultClusterName);
 
-     SetRdsResource(default_route_config_);
 
-     // Construct LDS response data (with inlined RDS result).
 
-     default_listener_ = BuildListener(default_route_config_);
 
-     SetLdsResource(default_listener_);
 
-     // Construct CDS response data.
 
-     default_cluster_.set_name(kDefaultClusterName);
 
-     default_cluster_.set_type(Cluster::EDS);
 
-     auto* eds_config = default_cluster_.mutable_eds_cluster_config();
 
-     eds_config->mutable_eds_config()->mutable_ads();
 
-     eds_config->set_service_name(kDefaultEdsServiceName);
 
-     default_cluster_.set_lb_policy(Cluster::ROUND_ROBIN);
 
-     if (enable_load_reporting) {
 
-       default_cluster_.mutable_lrs_server()->mutable_self();
 
-     }
 
-     SetCdsResource(default_cluster_);
 
-   }
 
-   bool seen_v2_client() const { return seen_v2_client_; }
 
-   bool seen_v3_client() const { return seen_v3_client_; }
 
-   ::envoy::service::discovery::v2::AggregatedDiscoveryService::Service*
 
-   v2_rpc_service() {
 
-     return &v2_rpc_service_;
 
-   }
 
-   ::envoy::service::discovery::v3::AggregatedDiscoveryService::Service*
 
-   v3_rpc_service() {
 
-     return &v3_rpc_service_;
 
-   }
 
-   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 resource_type_response_state_[kLdsTypeUrl];
 
-   }
 
-   ResponseState rds_response_state() {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     return resource_type_response_state_[kRdsTypeUrl];
 
-   }
 
-   ResponseState cds_response_state() {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     return resource_type_response_state_[kCdsTypeUrl];
 
-   }
 
-   ResponseState eds_response_state() {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     return resource_type_response_state_[kEdsTypeUrl];
 
-   }
 
-   void SetResourceIgnore(const std::string& type_url) {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     resource_types_to_ignore_.emplace(type_url);
 
-   }
 
-   void UnsetResource(const std::string& type_url, const std::string& name) {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     ResourceState& state = resource_map_[type_url][name];
 
-     ++state.version;
 
-     state.resource.reset();
 
-     gpr_log(GPR_INFO, "ADS[%p]: Unsetting %s resource %s to version %u", this,
 
-             type_url.c_str(), name.c_str(), state.version);
 
-     for (SubscriptionState* subscription : state.subscriptions) {
 
-       subscription->update_queue->emplace_back(type_url, name);
 
-     }
 
-   }
 
-   void SetResource(google::protobuf::Any resource, const std::string& type_url,
 
-                    const std::string& name) {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     ResourceState& state = resource_map_[type_url][name];
 
-     ++state.version;
 
-     state.resource = std::move(resource);
 
-     gpr_log(GPR_INFO, "ADS[%p]: Updating %s resource %s to version %u", this,
 
-             type_url.c_str(), name.c_str(), state.version);
 
-     for (SubscriptionState* subscription : state.subscriptions) {
 
-       subscription->update_queue->emplace_back(type_url, name);
 
-     }
 
-   }
 
-   void SetLdsResource(const Listener& listener) {
 
-     google::protobuf::Any resource;
 
-     resource.PackFrom(listener);
 
-     SetResource(std::move(resource), kLdsTypeUrl, listener.name());
 
-   }
 
-   void SetRdsResource(const RouteConfiguration& route) {
 
-     google::protobuf::Any resource;
 
-     resource.PackFrom(route);
 
-     SetResource(std::move(resource), kRdsTypeUrl, route.name());
 
-   }
 
-   void SetCdsResource(const Cluster& cluster) {
 
-     google::protobuf::Any resource;
 
-     resource.PackFrom(cluster);
 
-     SetResource(std::move(resource), kCdsTypeUrl, cluster.name());
 
-   }
 
-   void SetEdsResource(const ClusterLoadAssignment& assignment) {
 
-     google::protobuf::Any resource;
 
-     resource.PackFrom(assignment);
 
-     SetResource(std::move(resource), kEdsTypeUrl, assignment.cluster_name());
 
-   }
 
-   void SetLdsToUseDynamicRds() {
 
-     auto listener = default_listener_;
 
-     HttpConnectionManager http_connection_manager;
 
-     auto* rds = http_connection_manager.mutable_rds();
 
-     rds->set_route_config_name(kDefaultRouteConfigurationName);
 
-     rds->mutable_config_source()->mutable_ads();
 
-     listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
 
-         http_connection_manager);
 
-     SetLdsResource(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(kServerName);
 
-     listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
 
-         http_connection_manager);
 
-     return listener;
 
-   }
 
-   static ClusterLoadAssignment BuildEdsResource(
 
-       const EdsResourceArgs& args,
 
-       const char* eds_service_name = kDefaultEdsServiceName) {
 
-     ClusterLoadAssignment assignment;
 
-     assignment.set_cluster_name(eds_service_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] != 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 std::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);
 
-       }
 
-     }
 
-     return assignment;
 
-   }
 
-   void Start() {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     ads_done_ = false;
 
-   }
 
-   void Shutdown() {
 
-     {
 
-       grpc_core::MutexLock lock(&ads_mu_);
 
-       NotifyDoneWithAdsCallLocked();
 
-       resource_type_response_state_.clear();
 
-     }
 
-     gpr_log(GPR_INFO, "ADS[%p]: shut down", this);
 
-   }
 
-   void NotifyDoneWithAdsCall() {
 
-     grpc_core::MutexLock lock(&ads_mu_);
 
-     NotifyDoneWithAdsCallLocked();
 
-   }
 
-   void NotifyDoneWithAdsCallLocked() {
 
-     if (!ads_done_) {
 
-       ads_done_ = true;
 
-       ads_cond_.Broadcast();
 
-     }
 
-   }
 
-  private:
 
-   // A queue of resource type/name pairs that have changed since the client
 
-   // subscribed to them.
 
-   using UpdateQueue = std::deque<
 
-       std::pair<std::string /* type url */, std::string /* resource name */>>;
 
-   // A struct representing a client's subscription to a particular resource.
 
-   struct SubscriptionState {
 
-     // Version that the client currently knows about.
 
-     int current_version = 0;
 
-     // The queue upon which to place updates when the resource is updated.
 
-     UpdateQueue* update_queue;
 
-   };
 
-   // A struct representing the a client's subscription to all the resources.
 
-   using SubscriptionNameMap =
 
-       std::map<std::string /* resource_name */, SubscriptionState>;
 
-   using SubscriptionMap =
 
-       std::map<std::string /* type_url */, SubscriptionNameMap>;
 
-   // A struct representing the current state for a resource:
 
-   // - the version of the resource that is set by the SetResource() methods.
 
-   // - a list of subscriptions interested in this resource.
 
-   struct ResourceState {
 
-     int version = 0;
 
-     absl::optional<google::protobuf::Any> resource;
 
-     std::set<SubscriptionState*> subscriptions;
 
-   };
 
-   // A struct representing the current state for all resources:
 
-   // LDS, CDS, EDS, and RDS for the class as a whole.
 
-   using ResourceNameMap =
 
-       std::map<std::string /* resource_name */, ResourceState>;
 
-   using ResourceMap = std::map<std::string /* type_url */, ResourceNameMap>;
 
-   template <class RpcApi, class DiscoveryRequest, class DiscoveryResponse>
 
-   class RpcService : public RpcApi::Service {
 
-    public:
 
-     using Stream = ServerReaderWriter<DiscoveryResponse, DiscoveryRequest>;
 
-     RpcService(AdsServiceImpl* parent, bool is_v2)
 
-         : parent_(parent), is_v2_(is_v2) {}
 
-     Status StreamAggregatedResources(ServerContext* context,
 
-                                      Stream* stream) override {
 
-       gpr_log(GPR_INFO, "ADS[%p]: StreamAggregatedResources starts", this);
 
-       if (is_v2_) {
 
-         parent_->seen_v2_client_ = true;
 
-       } else {
 
-         parent_->seen_v3_client_ = true;
 
-       }
 
-       // Resources (type/name pairs) that have changed since the client
 
-       // subscribed to them.
 
-       UpdateQueue update_queue;
 
-       // Resources that the client will be subscribed to keyed by resource type
 
-       // url.
 
-       SubscriptionMap subscription_map;
 
-       [&]() {
 
-         {
 
-           grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-           if (parent_->ads_done_) return;
 
-         }
 
-         // Balancer shouldn't receive the call credentials metadata.
 
-         EXPECT_EQ(context->client_metadata().find(g_kCallCredsMdKey),
 
-                   context->client_metadata().end());
 
-         // Current Version map keyed by resource type url.
 
-         std::map<std::string, int> resource_type_version;
 
-         // Creating blocking thread to read from stream.
 
-         std::deque<DiscoveryRequest> requests;
 
-         bool stream_closed = false;
 
-         // Take a reference of the AdsServiceImpl object, reference will go
 
-         // out of scope after the reader thread is joined.
 
-         std::shared_ptr<AdsServiceImpl> ads_service_impl =
 
-             parent_->shared_from_this();
 
-         std::thread reader(std::bind(&RpcService::BlockingRead, this, stream,
 
-                                      &requests, &stream_closed));
 
-         // Main loop to look for requests and updates.
 
-         while (true) {
 
-           // Look for new requests and and decide what to handle.
 
-           absl::optional<DiscoveryResponse> response;
 
-           // Boolean to keep track if the loop received any work to do: a
 
-           // request or an update; regardless whether a response was actually
 
-           // sent out.
 
-           bool did_work = false;
 
-           {
 
-             grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-             if (stream_closed) break;
 
-             if (!requests.empty()) {
 
-               DiscoveryRequest request = std::move(requests.front());
 
-               requests.pop_front();
 
-               did_work = true;
 
-               gpr_log(GPR_INFO,
 
-                       "ADS[%p]: Received request for type %s with content %s",
 
-                       this, request.type_url().c_str(),
 
-                       request.DebugString().c_str());
 
-               const std::string v3_resource_type =
 
-                   TypeUrlToV3(request.type_url());
 
-               // As long as we are not in shutdown, identify ACK and NACK by
 
-               // looking for version information and comparing it to nonce (this
 
-               // server ensures they are always set to the same in a response.)
 
-               auto it =
 
-                   parent_->resource_type_response_state_.find(v3_resource_type);
 
-               if (it != parent_->resource_type_response_state_.end()) {
 
-                 if (!request.response_nonce().empty()) {
 
-                   it->second.state =
 
-                       (!request.version_info().empty() &&
 
-                        request.version_info() == request.response_nonce())
 
-                           ? ResponseState::ACKED
 
-                           : ResponseState::NACKED;
 
-                 }
 
-                 if (request.has_error_detail()) {
 
-                   it->second.error_message = request.error_detail().message();
 
-                 }
 
-               }
 
-               // As long as the test did not tell us to ignore this type of
 
-               // request, look at all the resource names.
 
-               if (parent_->resource_types_to_ignore_.find(v3_resource_type) ==
 
-                   parent_->resource_types_to_ignore_.end()) {
 
-                 auto& subscription_name_map =
 
-                     subscription_map[v3_resource_type];
 
-                 auto& resource_name_map =
 
-                     parent_->resource_map_[v3_resource_type];
 
-                 std::set<std::string> resources_in_current_request;
 
-                 std::set<std::string> resources_added_to_response;
 
-                 for (const std::string& resource_name :
 
-                      request.resource_names()) {
 
-                   resources_in_current_request.emplace(resource_name);
 
-                   auto& subscription_state =
 
-                       subscription_name_map[resource_name];
 
-                   auto& resource_state = resource_name_map[resource_name];
 
-                   // Subscribe if needed.
 
-                   parent_->MaybeSubscribe(v3_resource_type, resource_name,
 
-                                           &subscription_state, &resource_state,
 
-                                           &update_queue);
 
-                   // Send update if needed.
 
-                   if (ClientNeedsResourceUpdate(resource_state,
 
-                                                 &subscription_state)) {
 
-                     gpr_log(GPR_INFO,
 
-                             "ADS[%p]: Sending update for type=%s name=%s "
 
-                             "version=%d",
 
-                             this, request.type_url().c_str(),
 
-                             resource_name.c_str(), resource_state.version);
 
-                     resources_added_to_response.emplace(resource_name);
 
-                     if (!response.has_value()) response.emplace();
 
-                     if (resource_state.resource.has_value()) {
 
-                       auto* resource = response->add_resources();
 
-                       resource->CopyFrom(resource_state.resource.value());
 
-                       if (is_v2_) {
 
-                         resource->set_type_url(request.type_url());
 
-                       }
 
-                     }
 
-                   }
 
-                 }
 
-                 // Process unsubscriptions for any resource no longer
 
-                 // present in the request's resource list.
 
-                 parent_->ProcessUnsubscriptions(
 
-                     v3_resource_type, resources_in_current_request,
 
-                     &subscription_name_map, &resource_name_map);
 
-                 // Send response if needed.
 
-                 if (!resources_added_to_response.empty()) {
 
-                   CompleteBuildingDiscoveryResponse(
 
-                       v3_resource_type, request.type_url(),
 
-                       ++resource_type_version[v3_resource_type],
 
-                       subscription_name_map, resources_added_to_response,
 
-                       &response.value());
 
-                 }
 
-               }
 
-             }
 
-           }
 
-           if (response.has_value()) {
 
-             gpr_log(GPR_INFO, "ADS[%p]: Sending response: %s", this,
 
-                     response->DebugString().c_str());
 
-             stream->Write(response.value());
 
-           }
 
-           response.reset();
 
-           // Look for updates and decide what to handle.
 
-           {
 
-             grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-             if (!update_queue.empty()) {
 
-               const std::string resource_type =
 
-                   std::move(update_queue.front().first);
 
-               const std::string resource_name =
 
-                   std::move(update_queue.front().second);
 
-               update_queue.pop_front();
 
-               const std::string v2_resource_type = TypeUrlToV2(resource_type);
 
-               did_work = true;
 
-               gpr_log(GPR_INFO, "ADS[%p]: Received update for type=%s name=%s",
 
-                       this, resource_type.c_str(), resource_name.c_str());
 
-               auto& subscription_name_map = subscription_map[resource_type];
 
-               auto& resource_name_map = parent_->resource_map_[resource_type];
 
-               auto it = subscription_name_map.find(resource_name);
 
-               if (it != subscription_name_map.end()) {
 
-                 SubscriptionState& subscription_state = it->second;
 
-                 ResourceState& resource_state =
 
-                     resource_name_map[resource_name];
 
-                 if (ClientNeedsResourceUpdate(resource_state,
 
-                                               &subscription_state)) {
 
-                   gpr_log(
 
-                       GPR_INFO,
 
-                       "ADS[%p]: Sending update for type=%s name=%s version=%d",
 
-                       this, resource_type.c_str(), resource_name.c_str(),
 
-                       resource_state.version);
 
-                   response.emplace();
 
-                   if (resource_state.resource.has_value()) {
 
-                     auto* resource = response->add_resources();
 
-                     resource->CopyFrom(resource_state.resource.value());
 
-                     if (is_v2_) {
 
-                       resource->set_type_url(v2_resource_type);
 
-                     }
 
-                   }
 
-                   CompleteBuildingDiscoveryResponse(
 
-                       resource_type, v2_resource_type,
 
-                       ++resource_type_version[resource_type],
 
-                       subscription_name_map, {resource_name},
 
-                       &response.value());
 
-                 }
 
-               }
 
-             }
 
-           }
 
-           if (response.has_value()) {
 
-             gpr_log(GPR_INFO, "ADS[%p]: Sending update response: %s", this,
 
-                     response->DebugString().c_str());
 
-             stream->Write(response.value());
 
-           }
 
-           // If we didn't find anything to do, delay before the next loop
 
-           // iteration; otherwise, check whether we should exit and then
 
-           // immediately continue.
 
-           gpr_timespec deadline =
 
-               grpc_timeout_milliseconds_to_deadline(did_work ? 0 : 10);
 
-           {
 
-             grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-             if (!parent_->ads_cond_.WaitUntil(
 
-                     &parent_->ads_mu_, [this] { return parent_->ads_done_; },
 
-                     deadline)) {
 
-               break;
 
-             }
 
-           }
 
-         }
 
-         reader.join();
 
-       }();
 
-       // Clean up any subscriptions that were still active when the call
 
-       // finished.
 
-       {
 
-         grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-         for (auto& p : subscription_map) {
 
-           const std::string& type_url = p.first;
 
-           SubscriptionNameMap& subscription_name_map = p.second;
 
-           for (auto& q : subscription_name_map) {
 
-             const std::string& resource_name = q.first;
 
-             SubscriptionState& subscription_state = q.second;
 
-             ResourceState& resource_state =
 
-                 parent_->resource_map_[type_url][resource_name];
 
-             resource_state.subscriptions.erase(&subscription_state);
 
-           }
 
-         }
 
-       }
 
-       gpr_log(GPR_INFO, "ADS[%p]: StreamAggregatedResources done", this);
 
-       return Status::OK;
 
-     }
 
-    private:
 
-     static std::string TypeUrlToV2(const std::string& resource_type) {
 
-       if (resource_type == kLdsTypeUrl) return kLdsV2TypeUrl;
 
-       if (resource_type == kRdsTypeUrl) return kRdsV2TypeUrl;
 
-       if (resource_type == kCdsTypeUrl) return kCdsV2TypeUrl;
 
-       if (resource_type == kEdsTypeUrl) return kEdsV2TypeUrl;
 
-       return resource_type;
 
-     }
 
-     static std::string TypeUrlToV3(const std::string& resource_type) {
 
-       if (resource_type == kLdsV2TypeUrl) return kLdsTypeUrl;
 
-       if (resource_type == kRdsV2TypeUrl) return kRdsTypeUrl;
 
-       if (resource_type == kCdsV2TypeUrl) return kCdsTypeUrl;
 
-       if (resource_type == kEdsV2TypeUrl) return kEdsTypeUrl;
 
-       return resource_type;
 
-     }
 
-     // Starting a thread to do blocking read on the stream until cancel.
 
-     void BlockingRead(Stream* stream, std::deque<DiscoveryRequest>* requests,
 
-                       bool* stream_closed) {
 
-       DiscoveryRequest request;
 
-       bool seen_first_request = false;
 
-       while (stream->Read(&request)) {
 
-         if (!seen_first_request) {
 
-           EXPECT_TRUE(request.has_node());
 
-           ASSERT_FALSE(request.node().client_features().empty());
 
-           EXPECT_EQ(request.node().client_features(0),
 
-                     "envoy.lb.does_not_support_overprovisioning");
 
-           CheckBuildVersion(request);
 
-           seen_first_request = true;
 
-         }
 
-         {
 
-           grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-           requests->emplace_back(std::move(request));
 
-         }
 
-       }
 
-       gpr_log(GPR_INFO, "ADS[%p]: Null read, stream closed", this);
 
-       grpc_core::MutexLock lock(&parent_->ads_mu_);
 
-       *stream_closed = true;
 
-     }
 
-     static void CheckBuildVersion(
 
-         const ::envoy::api::v2::DiscoveryRequest& request) {
 
-       EXPECT_FALSE(request.node().build_version().empty());
 
-     }
 
-     static void CheckBuildVersion(
 
-         const ::envoy::service::discovery::v3::DiscoveryRequest& request) {}
 
-     // Completing the building a DiscoveryResponse by adding common information
 
-     // for all resources and by adding all subscribed resources for LDS and CDS.
 
-     void CompleteBuildingDiscoveryResponse(
 
-         const std::string& resource_type, const std::string& v2_resource_type,
 
-         const int version, const SubscriptionNameMap& subscription_name_map,
 
-         const std::set<std::string>& resources_added_to_response,
 
-         DiscoveryResponse* response) {
 
-       auto& response_state =
 
-           parent_->resource_type_response_state_[resource_type];
 
-       if (response_state.state == ResponseState::NOT_SENT) {
 
-         response_state.state = ResponseState::SENT;
 
-       }
 
-       response->set_type_url(is_v2_ ? v2_resource_type : resource_type);
 
-       response->set_version_info(absl::StrCat(version));
 
-       response->set_nonce(absl::StrCat(version));
 
-       if (resource_type == kLdsTypeUrl || resource_type == kCdsTypeUrl) {
 
-         // For LDS and CDS we must send back all subscribed resources
 
-         // (even the unchanged ones)
 
-         for (const auto& p : subscription_name_map) {
 
-           const std::string& resource_name = p.first;
 
-           if (resources_added_to_response.find(resource_name) ==
 
-               resources_added_to_response.end()) {
 
-             const ResourceState& resource_state =
 
-                 parent_->resource_map_[resource_type][resource_name];
 
-             if (resource_state.resource.has_value()) {
 
-               auto* resource = response->add_resources();
 
-               resource->CopyFrom(resource_state.resource.value());
 
-               if (is_v2_) {
 
-                 resource->set_type_url(v2_resource_type);
 
-               }
 
-             }
 
-           }
 
-         }
 
-       }
 
-     }
 
-     AdsServiceImpl* parent_;
 
-     const bool is_v2_;
 
-   };
 
-   // Checks whether the client needs to receive a newer version of
 
-   // the resource.  If so, updates subscription_state->current_version and
 
-   // returns true.
 
-   static bool ClientNeedsResourceUpdate(const ResourceState& resource_state,
 
-                                         SubscriptionState* subscription_state) {
 
-     if (subscription_state->current_version < resource_state.version) {
 
-       subscription_state->current_version = resource_state.version;
 
-       return true;
 
-     }
 
-     return false;
 
-   }
 
-   // Subscribes to a resource if not already subscribed:
 
-   // 1. Sets the update_queue field in subscription_state.
 
-   // 2. Adds subscription_state to resource_state->subscriptions.
 
-   void MaybeSubscribe(const std::string& resource_type,
 
-                       const std::string& resource_name,
 
-                       SubscriptionState* subscription_state,
 
-                       ResourceState* resource_state,
 
-                       UpdateQueue* update_queue) {
 
-     // The update_queue will be null if we were not previously subscribed.
 
-     if (subscription_state->update_queue != nullptr) return;
 
-     subscription_state->update_queue = update_queue;
 
-     resource_state->subscriptions.emplace(subscription_state);
 
-     gpr_log(GPR_INFO, "ADS[%p]: subscribe to resource type %s name %s state %p",
 
-             this, resource_type.c_str(), resource_name.c_str(),
 
-             &subscription_state);
 
-   }
 
-   // Removes subscriptions for resources no longer present in the
 
-   // current request.
 
-   void ProcessUnsubscriptions(
 
-       const std::string& resource_type,
 
-       const std::set<std::string>& resources_in_current_request,
 
-       SubscriptionNameMap* subscription_name_map,
 
-       ResourceNameMap* resource_name_map) {
 
-     for (auto it = subscription_name_map->begin();
 
-          it != subscription_name_map->end();) {
 
-       const std::string& resource_name = it->first;
 
-       SubscriptionState& subscription_state = it->second;
 
-       if (resources_in_current_request.find(resource_name) !=
 
-           resources_in_current_request.end()) {
 
-         ++it;
 
-         continue;
 
-       }
 
-       gpr_log(GPR_INFO, "ADS[%p]: Unsubscribe to type=%s name=%s state=%p",
 
-               this, resource_type.c_str(), resource_name.c_str(),
 
-               &subscription_state);
 
-       auto resource_it = resource_name_map->find(resource_name);
 
-       GPR_ASSERT(resource_it != resource_name_map->end());
 
-       auto& resource_state = resource_it->second;
 
-       resource_state.subscriptions.erase(&subscription_state);
 
-       if (resource_state.subscriptions.empty() &&
 
-           !resource_state.resource.has_value()) {
 
-         resource_name_map->erase(resource_it);
 
-       }
 
-       it = subscription_name_map->erase(it);
 
-     }
 
-   }
 
-   RpcService<::envoy::service::discovery::v2::AggregatedDiscoveryService,
 
-              ::envoy::api::v2::DiscoveryRequest,
 
-              ::envoy::api::v2::DiscoveryResponse>
 
-       v2_rpc_service_;
 
-   RpcService<::envoy::service::discovery::v3::AggregatedDiscoveryService,
 
-              ::envoy::service::discovery::v3::DiscoveryRequest,
 
-              ::envoy::service::discovery::v3::DiscoveryResponse>
 
-       v3_rpc_service_;
 
-   std::atomic_bool seen_v2_client_{false};
 
-   std::atomic_bool seen_v3_client_{false};
 
-   grpc_core::CondVar ads_cond_;
 
-   // Protect the members below.
 
-   grpc_core::Mutex ads_mu_;
 
-   bool ads_done_ = false;
 
-   Listener default_listener_;
 
-   RouteConfiguration default_route_config_;
 
-   Cluster default_cluster_;
 
-   std::map<std::string /* type_url */, ResponseState>
 
-       resource_type_response_state_;
 
-   std::set<std::string /*resource_type*/> resource_types_to_ignore_;
 
-   // An instance data member containing the current state of all resources.
 
-   // Note that an entry will exist whenever either of the following is true:
 
-   // - The resource exists (i.e., has been created by SetResource() and has not
 
-   //   yet been destroyed by UnsetResource()).
 
-   // - There is at least one subscription for the resource.
 
-   ResourceMap resource_map_;
 
- };
 
- class LrsServiceImpl : public std::enable_shared_from_this<LrsServiceImpl> {
 
-  public:
 
-   explicit LrsServiceImpl(int client_load_reporting_interval_seconds)
 
-       : v2_rpc_service_(this),
 
-         v3_rpc_service_(this),
 
-         client_load_reporting_interval_seconds_(
 
-             client_load_reporting_interval_seconds),
 
-         cluster_names_({kDefaultClusterName}) {}
 
-   ::envoy::service::load_stats::v2::LoadReportingService::Service*
 
-   v2_rpc_service() {
 
-     return &v2_rpc_service_;
 
-   }
 
-   ::envoy::service::load_stats::v3::LoadReportingService::Service*
 
-   v3_rpc_service() {
 
-     return &v3_rpc_service_;
 
-   }
 
-   size_t request_count() {
 
-     return v2_rpc_service_.request_count() + v3_rpc_service_.request_count();
 
-   }
 
-   size_t response_count() {
 
-     return v2_rpc_service_.response_count() + v3_rpc_service_.response_count();
 
-   }
 
-   // Must be called before the LRS call is started.
 
-   void set_send_all_clusters(bool send_all_clusters) {
 
-     send_all_clusters_ = send_all_clusters;
 
-   }
 
-   void set_cluster_names(const std::set<std::string>& cluster_names) {
 
-     cluster_names_ = cluster_names;
 
-   }
 
-   void Start() {
 
-     lrs_done_ = false;
 
-     result_queue_.clear();
 
-   }
 
-   void Shutdown() {
 
-     {
 
-       grpc_core::MutexLock lock(&lrs_mu_);
 
-       NotifyDoneWithLrsCallLocked();
 
-     }
 
-     gpr_log(GPR_INFO, "LRS[%p]: shut down", this);
 
-   }
 
-   std::vector<ClientStats> WaitForLoadReport() {
 
-     grpc_core::MutexLock lock(&load_report_mu_);
 
-     grpc_core::CondVar cv;
 
-     if (result_queue_.empty()) {
 
-       load_report_cond_ = &cv;
 
-       load_report_cond_->WaitUntil(&load_report_mu_,
 
-                                    [this] { return !result_queue_.empty(); });
 
-       load_report_cond_ = nullptr;
 
-     }
 
-     std::vector<ClientStats> result = std::move(result_queue_.front());
 
-     result_queue_.pop_front();
 
-     return result;
 
-   }
 
-   void NotifyDoneWithLrsCall() {
 
-     grpc_core::MutexLock lock(&lrs_mu_);
 
-     NotifyDoneWithLrsCallLocked();
 
-   }
 
-  private:
 
-   template <class RpcApi, class LoadStatsRequest, class LoadStatsResponse>
 
-   class RpcService : public CountedService<typename RpcApi::Service> {
 
-    public:
 
-     using Stream = ServerReaderWriter<LoadStatsResponse, LoadStatsRequest>;
 
-     explicit RpcService(LrsServiceImpl* parent) : parent_(parent) {}
 
-     Status StreamLoadStats(ServerContext* /*context*/,
 
-                            Stream* stream) override {
 
-       gpr_log(GPR_INFO, "LRS[%p]: StreamLoadStats starts", this);
 
-       GPR_ASSERT(parent_->client_load_reporting_interval_seconds_ > 0);
 
-       // Take a reference of the LrsServiceImpl object, reference will go
 
-       // out of scope after this method exits.
 
-       std::shared_ptr<LrsServiceImpl> lrs_service_impl =
 
-           parent_->shared_from_this();
 
-       // Read initial request.
 
-       LoadStatsRequest request;
 
-       if (stream->Read(&request)) {
 
-         CountedService<typename RpcApi::Service>::IncreaseRequestCount();
 
-         // Verify client features.
 
-         EXPECT_THAT(
 
-             request.node().client_features(),
 
-             ::testing::Contains("envoy.lrs.supports_send_all_clusters"));
 
-         // Send initial response.
 
-         LoadStatsResponse response;
 
-         if (parent_->send_all_clusters_) {
 
-           response.set_send_all_clusters(true);
 
-         } else {
 
-           for (const std::string& cluster_name : parent_->cluster_names_) {
 
-             response.add_clusters(cluster_name);
 
-           }
 
-         }
 
-         response.mutable_load_reporting_interval()->set_seconds(
 
-             parent_->client_load_reporting_interval_seconds_);
 
-         stream->Write(response);
 
-         CountedService<typename RpcApi::Service>::IncreaseResponseCount();
 
-         // Wait for report.
 
-         request.Clear();
 
-         while (stream->Read(&request)) {
 
-           gpr_log(GPR_INFO, "LRS[%p]: received client load report message: %s",
 
-                   this, request.DebugString().c_str());
 
-           std::vector<ClientStats> stats;
 
-           for (const auto& cluster_stats : request.cluster_stats()) {
 
-             stats.emplace_back(cluster_stats);
 
-           }
 
-           grpc_core::MutexLock lock(&parent_->load_report_mu_);
 
-           parent_->result_queue_.emplace_back(std::move(stats));
 
-           if (parent_->load_report_cond_ != nullptr) {
 
-             parent_->load_report_cond_->Signal();
 
-           }
 
-         }
 
-         // Wait until notified done.
 
-         grpc_core::MutexLock lock(&parent_->lrs_mu_);
 
-         parent_->lrs_cv_.WaitUntil(&parent_->lrs_mu_,
 
-                                    [this] { return parent_->lrs_done_; });
 
-       }
 
-       gpr_log(GPR_INFO, "LRS[%p]: StreamLoadStats done", this);
 
-       return Status::OK;
 
-     }
 
-    private:
 
-     LrsServiceImpl* parent_;
 
-   };
 
-   void NotifyDoneWithLrsCallLocked() {
 
-     if (!lrs_done_) {
 
-       lrs_done_ = true;
 
-       lrs_cv_.Broadcast();
 
-     }
 
-   }
 
-   RpcService<::envoy::service::load_stats::v2::LoadReportingService,
 
-              ::envoy::service::load_stats::v2::LoadStatsRequest,
 
-              ::envoy::service::load_stats::v2::LoadStatsResponse>
 
-       v2_rpc_service_;
 
-   RpcService<::envoy::service::load_stats::v3::LoadReportingService,
 
-              ::envoy::service::load_stats::v3::LoadStatsRequest,
 
-              ::envoy::service::load_stats::v3::LoadStatsResponse>
 
-       v3_rpc_service_;
 
-   const int client_load_reporting_interval_seconds_;
 
-   bool send_all_clusters_ = false;
 
-   std::set<std::string> cluster_names_;
 
-   grpc_core::CondVar lrs_cv_;
 
-   grpc_core::Mutex lrs_mu_;  // Protects lrs_done_.
 
-   bool lrs_done_ = false;
 
-   grpc_core::Mutex load_report_mu_;  // Protects the members below.
 
-   grpc_core::CondVar* load_report_cond_ = nullptr;
 
-   std::deque<std::vector<ClientStats>> result_queue_;
 
- };
 
- class TestType {
 
-  public:
 
-   TestType(bool use_xds_resolver, bool enable_load_reporting,
 
-            bool enable_rds_testing = false, bool use_v2 = false)
 
-       : use_xds_resolver_(use_xds_resolver),
 
-         enable_load_reporting_(enable_load_reporting),
 
-         enable_rds_testing_(enable_rds_testing),
 
-         use_v2_(use_v2) {}
 
-   bool use_xds_resolver() const { return use_xds_resolver_; }
 
-   bool enable_load_reporting() const { return enable_load_reporting_; }
 
-   bool enable_rds_testing() const { return enable_rds_testing_; }
 
-   bool use_v2() const { return use_v2_; }
 
-   std::string AsString() const {
 
-     std::string retval = (use_xds_resolver_ ? "XdsResolver" : "FakeResolver");
 
-     retval += (use_v2_ ? "V2" : "V3");
 
-     if (enable_load_reporting_) retval += "WithLoadReporting";
 
-     if (enable_rds_testing_) retval += "Rds";
 
-     return retval;
 
-   }
 
-  private:
 
-   const bool use_xds_resolver_;
 
-   const bool enable_load_reporting_;
 
-   const bool enable_rds_testing_;
 
-   const bool use_v2_;
 
- };
 
- class XdsEnd2endTest : public ::testing::TestWithParam<TestType> {
 
-  protected:
 
-   XdsEnd2endTest(size_t num_backends, size_t num_balancers,
 
-                  int client_load_reporting_interval_seconds = 100)
 
-       : 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_EXPERIMENTAL_V3_SUPPORT", "true");
 
-     gpr_setenv("GRPC_XDS_BOOTSTRAP",
 
-                GetParam().use_v2() ? g_bootstrap_file_v2 : g_bootstrap_file_v3);
 
-     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();
 
-     }
 
-     // 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();
 
-       if (GetParam().enable_rds_testing()) {
 
-         balancers_[i]->ads_service()->SetLdsToUseDynamicRds();
 
-       }
 
-     }
 
-     ResetStub();
 
-   }
 
-   const char* DefaultEdsServiceName() const {
 
-     return GetParam().use_xds_resolver() ? kDefaultEdsServiceName : kServerName;
 
-   }
 
-   void TearDown() override {
 
-     ShutdownAllBackends();
 
-     for (auto& balancer : balancers_) balancer->Shutdown();
 
-   }
 
-   void StartAllBackends() {
 
-     for (auto& backend : backends_) backend->Start();
 
-   }
 
-   void StartBackend(size_t index) { backends_[index]->Start(); }
 
-   void ShutdownAllBackends() {
 
-     for (auto& backend : backends_) backend->Shutdown();
 
-   }
 
-   void ShutdownBackend(size_t index) { backends_[index]->Shutdown(); }
 
-   void ResetStub(int failover_timeout = 0,
 
-                  const std::string& expected_targets = "",
 
-                  int xds_resource_does_not_exist_timeout = 0) {
 
-     ChannelArguments args;
 
-     if (failover_timeout > 0) {
 
-       args.SetInt(GRPC_ARG_PRIORITY_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);
 
-     }
 
-     std::string uri = absl::StrCat(
 
-         GetParam().use_xds_resolver() ? "xds" : "fake", ":///", kServerName);
 
-     // 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, creds, args);
 
-     stub_ = grpc::testing::EchoTestService::NewStub(channel_);
 
-     stub1_ = grpc::testing::EchoTest1Service::NewStub(channel_);
 
-     stub2_ = grpc::testing::EchoTest2Service::NewStub(channel_);
 
-   }
 
-   enum RpcService {
 
-     SERVICE_ECHO,
 
-     SERVICE_ECHO1,
 
-     SERVICE_ECHO2,
 
-   };
 
-   enum RpcMethod {
 
-     METHOD_ECHO,
 
-     METHOD_ECHO1,
 
-     METHOD_ECHO2,
 
-   };
 
-   struct RpcOptions {
 
-     RpcService service = SERVICE_ECHO;
 
-     RpcMethod method = METHOD_ECHO;
 
-     int timeout_ms = 1000;
 
-     bool wait_for_ready = false;
 
-     bool server_fail = false;
 
-     std::vector<std::pair<std::string, std::string>> metadata;
 
-     RpcOptions() {}
 
-     RpcOptions& set_rpc_service(RpcService rpc_service) {
 
-       service = rpc_service;
 
-       return *this;
 
-     }
 
-     RpcOptions& set_rpc_method(RpcMethod rpc_method) {
 
-       method = rpc_method;
 
-       return *this;
 
-     }
 
-     RpcOptions& set_timeout_ms(int rpc_timeout_ms) {
 
-       timeout_ms = rpc_timeout_ms;
 
-       return *this;
 
-     }
 
-     RpcOptions& set_wait_for_ready(bool rpc_wait_for_ready) {
 
-       wait_for_ready = rpc_wait_for_ready;
 
-       return *this;
 
-     }
 
-     RpcOptions& set_server_fail(bool rpc_server_fail) {
 
-       server_fail = rpc_server_fail;
 
-       return *this;
 
-     }
 
-     RpcOptions& set_metadata(
 
-         std::vector<std::pair<std::string, std::string>> rpc_metadata) {
 
-       metadata = rpc_metadata;
 
-       return *this;
 
-     }
 
-   };
 
-   template <typename Stub>
 
-   Status SendRpcMethod(Stub* stub, const RpcOptions& rpc_options,
 
-                        ClientContext* context, EchoRequest& request,
 
-                        EchoResponse* response) {
 
-     switch (rpc_options.method) {
 
-       case METHOD_ECHO:
 
-         return (*stub)->Echo(context, request, response);
 
-       case METHOD_ECHO1:
 
-         return (*stub)->Echo1(context, request, response);
 
-       case METHOD_ECHO2:
 
-         return (*stub)->Echo2(context, request, response);
 
-     }
 
-   }
 
-   void ResetBackendCounters(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) {
 
-       backends_[i]->backend_service()->ResetCounters();
 
-       backends_[i]->backend_service1()->ResetCounters();
 
-       backends_[i]->backend_service2()->ResetCounters();
 
-     }
 
-   }
 
-   bool SeenAllBackends(size_t start_index = 0, size_t stop_index = 0,
 
-                        const RpcOptions& rpc_options = RpcOptions()) {
 
-     if (stop_index == 0) stop_index = backends_.size();
 
-     for (size_t i = start_index; i < stop_index; ++i) {
 
-       switch (rpc_options.service) {
 
-         case SERVICE_ECHO:
 
-           if (backends_[i]->backend_service()->request_count() == 0)
 
-             return false;
 
-           break;
 
-         case SERVICE_ECHO1:
 
-           if (backends_[i]->backend_service1()->request_count() == 0)
 
-             return false;
 
-           break;
 
-         case SERVICE_ECHO2:
 
-           if (backends_[i]->backend_service2()->request_count() == 0)
 
-             return false;
 
-           break;
 
-       }
 
-     }
 
-     return true;
 
-   }
 
-   void SendRpcAndCount(int* num_total, int* num_ok, int* num_failure,
 
-                        int* num_drops,
 
-                        const RpcOptions& rpc_options = RpcOptions()) {
 
-     const Status status = SendRpc(rpc_options);
 
-     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,
 
-       const RpcOptions& rpc_options = RpcOptions(),
 
-       bool allow_failures = false) {
 
-     int num_ok = 0;
 
-     int num_failure = 0;
 
-     int num_drops = 0;
 
-     int num_total = 0;
 
-     while (!SeenAllBackends(start_index, stop_index, rpc_options)) {
 
-       SendRpcAndCount(&num_total, &num_ok, &num_failure, &num_drops,
 
-                       rpc_options);
 
-     }
 
-     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);
 
-     if (!allow_failures) EXPECT_EQ(num_failure, 0);
 
-     return std::make_tuple(num_ok, num_failure, num_drops);
 
-   }
 
-   void WaitForBackend(size_t backend_idx, bool reset_counters = true,
 
-                       bool require_success = false) {
 
-     gpr_log(GPR_INFO, "========= WAITING FOR BACKEND %lu ==========",
 
-             static_cast<unsigned long>(backend_idx));
 
-     do {
 
-       Status status = SendRpc();
 
-       if (require_success) {
 
-         EXPECT_TRUE(status.ok()) << "code=" << status.error_code()
 
-                                  << " message=" << status.error_message();
 
-       }
 
-     } 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) {
 
-       std::string lb_uri_str = absl::StrCat("ipv4:127.0.0.1:", port);
 
-       grpc_uri* lb_uri = grpc_uri_parse(lb_uri_str.c_str(), true);
 
-       GPR_ASSERT(lb_uri != nullptr);
 
-       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);
 
-     }
 
-     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);
 
-     ASSERT_EQ(error, GRPC_ERROR_NONE) << grpc_error_string(error);
 
-     ASSERT_NE(result.service_config.get(), nullptr);
 
-     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);
 
-       ASSERT_NE(result.service_config.get(), nullptr);
 
-       ASSERT_EQ(error, GRPC_ERROR_NONE) << grpc_error_string(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;
 
-   }
 
-   Status SendRpc(const RpcOptions& rpc_options = RpcOptions(),
 
-                  EchoResponse* response = nullptr) {
 
-     const bool local_response = (response == nullptr);
 
-     if (local_response) response = new EchoResponse;
 
-     EchoRequest request;
 
-     ClientContext context;
 
-     for (const auto& metadata : rpc_options.metadata) {
 
-       context.AddMetadata(metadata.first, metadata.second);
 
-     }
 
-     if (rpc_options.timeout_ms != 0) {
 
-       context.set_deadline(
 
-           grpc_timeout_milliseconds_to_deadline(rpc_options.timeout_ms));
 
-     }
 
-     if (rpc_options.wait_for_ready) context.set_wait_for_ready(true);
 
-     request.set_message(kRequestMessage);
 
-     if (rpc_options.server_fail) {
 
-       request.mutable_param()->mutable_expected_error()->set_code(
 
-           GRPC_STATUS_FAILED_PRECONDITION);
 
-     }
 
-     Status status;
 
-     switch (rpc_options.service) {
 
-       case SERVICE_ECHO:
 
-         status =
 
-             SendRpcMethod(&stub_, rpc_options, &context, request, response);
 
-         break;
 
-       case SERVICE_ECHO1:
 
-         status =
 
-             SendRpcMethod(&stub1_, rpc_options, &context, request, response);
 
-         break;
 
-       case SERVICE_ECHO2:
 
-         status =
 
-             SendRpcMethod(&stub2_, rpc_options, &context, request, response);
 
-         break;
 
-     }
 
-     if (local_response) delete response;
 
-     return status;
 
-   }
 
-   void CheckRpcSendOk(const size_t times = 1,
 
-                       const RpcOptions& rpc_options = RpcOptions()) {
 
-     for (size_t i = 0; i < times; ++i) {
 
-       EchoResponse response;
 
-       const Status status = SendRpc(rpc_options, &response);
 
-       EXPECT_TRUE(status.ok()) << "code=" << status.error_code()
 
-                                << " message=" << status.error_message();
 
-       EXPECT_EQ(response.message(), kRequestMessage);
 
-     }
 
-   }
 
-   void CheckRpcSendFailure(const size_t times = 1, bool server_fail = false) {
 
-     for (size_t i = 0; i < times; ++i) {
 
-       const Status status = SendRpc(RpcOptions().set_server_fail(server_fail));
 
-       EXPECT_FALSE(status.ok());
 
-     }
 
-   }
 
-   void SetRouteConfiguration(int idx, const RouteConfiguration& route_config) {
 
-     if (GetParam().enable_rds_testing()) {
 
-       balancers_[idx]->ads_service()->SetRdsResource(route_config);
 
-     } else {
 
-       balancers_[idx]->ads_service()->SetLdsResource(
 
-           AdsServiceImpl::BuildListener(route_config));
 
-     }
 
-   }
 
-   AdsServiceImpl::ResponseState RouteConfigurationResponseState(int idx) const {
 
-     AdsServiceImpl* ads_service = balancers_[idx]->ads_service();
 
-     if (GetParam().enable_rds_testing()) {
 
-       return ads_service->rds_response_state();
 
-     }
 
-     return ads_service->lds_response_state();
 
-   }
 
-  public:
 
-   // This method could benefit test subclasses; to make it accessible
 
-   // via bind with a qualified name, it needs to be public.
 
-   void SetEdsResourceWithDelay(size_t i,
 
-                                const ClusterLoadAssignment& assignment,
 
-                                int delay_ms) {
 
-     GPR_ASSERT(delay_ms > 0);
 
-     gpr_sleep_until(grpc_timeout_milliseconds_to_deadline(delay_ms));
 
-     balancers_[i]->ads_service()->SetEdsResource(assignment);
 
-   }
 
-  protected:
 
-   class ServerThread {
 
-    public:
 
-     ServerThread() : port_(g_port_saver->GetPort()) {}
 
-     virtual ~ServerThread(){};
 
-     void Start() {
 
-       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, &mu, &cond)));
 
-       cond.Wait(&mu);
 
-       gpr_log(GPR_INFO, "%s server startup complete", Type());
 
-     }
 
-     void Serve(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 << "localhost:" << 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<::grpc::testing::EchoTestService::Service>*
 
-     backend_service() {
 
-       return &backend_service_;
 
-     }
 
-     BackendServiceImpl<::grpc::testing::EchoTest1Service::Service>*
 
-     backend_service1() {
 
-       return &backend_service1_;
 
-     }
 
-     BackendServiceImpl<::grpc::testing::EchoTest2Service::Service>*
 
-     backend_service2() {
 
-       return &backend_service2_;
 
-     }
 
-    private:
 
-     void RegisterAllServices(ServerBuilder* builder) override {
 
-       builder->RegisterService(&backend_service_);
 
-       builder->RegisterService(&backend_service1_);
 
-       builder->RegisterService(&backend_service2_);
 
-     }
 
-     void StartAllServices() override {
 
-       backend_service_.Start();
 
-       backend_service1_.Start();
 
-       backend_service2_.Start();
 
-     }
 
-     void ShutdownAllServices() override {
 
-       backend_service_.Shutdown();
 
-       backend_service1_.Shutdown();
 
-       backend_service2_.Shutdown();
 
-     }
 
-     const char* Type() override { return "Backend"; }
 
-     BackendServiceImpl<::grpc::testing::EchoTestService::Service>
 
-         backend_service_;
 
-     BackendServiceImpl<::grpc::testing::EchoTest1Service::Service>
 
-         backend_service1_;
 
-     BackendServiceImpl<::grpc::testing::EchoTest2Service::Service>
 
-         backend_service2_;
 
-   };
 
-   class BalancerServerThread : public ServerThread {
 
-    public:
 
-     explicit BalancerServerThread(int client_load_reporting_interval = 0)
 
-         : ads_service_(new AdsServiceImpl(client_load_reporting_interval > 0)),
 
-           lrs_service_(new LrsServiceImpl(client_load_reporting_interval)) {}
 
-     AdsServiceImpl* ads_service() { return ads_service_.get(); }
 
-     LrsServiceImpl* lrs_service() { return lrs_service_.get(); }
 
-    private:
 
-     void RegisterAllServices(ServerBuilder* builder) override {
 
-       builder->RegisterService(ads_service_->v2_rpc_service());
 
-       builder->RegisterService(ads_service_->v3_rpc_service());
 
-       builder->RegisterService(lrs_service_->v2_rpc_service());
 
-       builder->RegisterService(lrs_service_->v3_rpc_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"; }
 
-     std::shared_ptr<AdsServiceImpl> ads_service_;
 
-     std::shared_ptr<LrsServiceImpl> lrs_service_;
 
-   };
 
-   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::unique_ptr<grpc::testing::EchoTest1Service::Stub> stub1_;
 
-   std::unique_ptr<grpc::testing::EchoTest2Service::Stub> stub2_;
 
-   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_;
 
- };
 
- 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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());
 
-   }
 
-   // Check LB policy name for the channel.
 
-   EXPECT_EQ((GetParam().use_xds_resolver() ? "xds_cluster_manager_experimental"
 
-                                            : "eds_experimental"),
 
-             channel_->GetLoadBalancingPolicyName());
 
- }
 
- TEST_P(BasicTest, IgnoresUnhealthyEndpoints) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   const size_t kNumRpcsPerAddress = 100;
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0",
 
-        GetBackendPorts(),
 
-        kDefaultLocalityWeight,
 
-        kDefaultLocalityPriority,
 
-        {HealthStatus::DRAINING}},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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());
 
-   }
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", ports},
 
-   });
 
-   const size_t kNumRpcsPerAddress = 10;
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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::EdsResourceArgs::Locality empty_locality("locality0", {});
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       empty_locality,
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // Send non-empty serverlist only after kServerlistDelayMs.
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   std::thread delayed_resource_setter(
 
-       std::bind(&BasicTest::SetEdsResourceWithDelay, this, 0,
 
-                 AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()),
 
-                 kServerlistDelayMs));
 
-   const auto t0 = system_clock::now();
 
-   // Client will block: LB will initially send empty serverlist.
 
-   CheckRpcSendOk(
 
-       1, RpcOptions().set_timeout_ms(kCallDeadlineMs).set_wait_for_ready(true));
 
-   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);
 
-   delayed_resource_setter.join();
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", ports},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   const Status status = SendRpc();
 
-   // The error shouldn't be DEADLINE_EXCEEDED.
 
-   EXPECT_EQ(StatusCode::UNAVAILABLE, status.error_code());
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   WaitForAllBackends();
 
-   // Stop backends.  RPCs should fail.
 
-   ShutdownAllBackends();
 
-   // Sending multiple failed requests instead of just one to ensure that the
 
-   // client notices that all backends are down before we restart them. If we
 
-   // didn't do this, then a single RPC could fail here due to the race condition
 
-   // between the LB pick and the GOAWAY from the chosen backend being shut down,
 
-   // which would not actually prove that the client noticed that all of the
 
-   // backends are down. Then, when we send another request below (which we
 
-   // expect to succeed), if the callbacks happen in the wrong order, the same
 
-   // race condition could happen again due to the client not yet having noticed
 
-   // that the backends were all down.
 
-   CheckRpcSendFailure(num_backends_);
 
-   // Restart all backends.  RPCs should start succeeding again.
 
-   StartAllBackends();
 
-   CheckRpcSendOk(1, RpcOptions().set_timeout_ms(2000).set_wait_for_ready(true));
 
- }
 
- TEST_P(BasicTest, IgnoresDuplicateUpdates) {
 
-   const size_t kNumRpcsPerAddress = 100;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // Wait for all backends to come online.
 
-   WaitForAllBackends();
 
-   // Send kNumRpcsPerAddress RPCs per server, but send an EDS update in
 
-   // between.  If the update is not ignored, this will cause the
 
-   // round_robin policy to see an update, which will randomly reset its
 
-   // position in the address list.
 
-   for (size_t i = 0; i < kNumRpcsPerAddress; ++i) {
 
-     CheckRpcSendOk(2);
 
-     balancers_[0]->ads_service()->SetEdsResource(
 
-         AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-     CheckRpcSendOk(2);
 
-   }
 
-   // Each backend should have gotten the right number of requests.
 
-   for (size_t i = 1; i < backends_.size(); ++i) {
 
-     EXPECT_EQ(kNumRpcsPerAddress,
 
-               backends_[i]->backend_service()->request_count());
 
-   }
 
- }
 
- using XdsResolverOnlyTest = BasicTest;
 
- // Tests switching over from one cluster to another.
 
- TEST_P(XdsResolverOnlyTest, ChangeClusters) {
 
-   const char* kNewClusterName = "new_cluster_name";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   // We need to wait for all backends to come online.
 
-   WaitForAllBackends(0, 2);
 
-   // Populate new EDS resource.
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsServiceName));
 
-   // Populate new CDS resource.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Change RDS resource to point to new cluster.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   new_route_config.mutable_virtual_hosts(0)
 
-       ->mutable_routes(0)
 
-       ->mutable_route()
 
-       ->set_cluster(kNewClusterName);
 
-   Listener listener =
 
-       balancers_[0]->ads_service()->BuildListener(new_route_config);
 
-   balancers_[0]->ads_service()->SetLdsResource(listener);
 
-   // Wait for all new backends to be used.
 
-   std::tuple<int, int, int> counts = WaitForAllBackends(2, 4);
 
-   // Make sure no RPCs failed in the transition.
 
-   EXPECT_EQ(0, std::get<1>(counts));
 
- }
 
- // Tests that we go into TRANSIENT_FAILURE if the Cluster disappears.
 
- TEST_P(XdsResolverOnlyTest, ClusterRemoved) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   // We need to wait for all backends to come online.
 
-   WaitForAllBackends();
 
-   // Unset CDS resource.
 
-   balancers_[0]->ads_service()->UnsetResource(kCdsTypeUrl, kDefaultClusterName);
 
-   // Wait for RPCs to start failing.
 
-   do {
 
-   } while (SendRpc(RpcOptions(), nullptr).ok());
 
-   // Make sure RPCs are still failing.
 
-   CheckRpcSendFailure(1000);
 
-   // Make sure we ACK'ed the update.
 
-   EXPECT_EQ(balancers_[0]->ads_service()->cds_response_state().state,
 
-             AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- // Tests that we restart all xDS requests when we reestablish the ADS call.
 
- TEST_P(XdsResolverOnlyTest, RestartsRequestsUponReconnection) {
 
-   balancers_[0]->ads_service()->SetLdsToUseDynamicRds();
 
-   const char* kNewClusterName = "new_cluster_name";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   // We need to wait for all backends to come online.
 
-   WaitForAllBackends(0, 2);
 
-   // Now shut down and restart the balancer.  When the client
 
-   // reconnects, it should automatically restart the requests for all
 
-   // resource types.
 
-   balancers_[0]->Shutdown();
 
-   balancers_[0]->Start();
 
-   // Make sure things are still working.
 
-   CheckRpcSendOk(100);
 
-   // Populate new EDS resource.
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsServiceName));
 
-   // Populate new CDS resource.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Change RDS resource to point to new cluster.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   new_route_config.mutable_virtual_hosts(0)
 
-       ->mutable_routes(0)
 
-       ->mutable_route()
 
-       ->set_cluster(kNewClusterName);
 
-   balancers_[0]->ads_service()->SetRdsResource(new_route_config);
 
-   // Wait for all new backends to be used.
 
-   std::tuple<int, int, int> counts = WaitForAllBackends(2, 4);
 
-   // Make sure no RPCs failed in the transition.
 
-   EXPECT_EQ(0, std::get<1>(counts));
 
- }
 
- TEST_P(XdsResolverOnlyTest, DefaultRouteSpecifiesSlashPrefix) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   route_config.mutable_virtual_hosts(0)
 
-       ->mutable_routes(0)
 
-       ->mutable_match()
 
-       ->set_prefix("/");
 
-   balancers_[0]->ads_service()->SetLdsResource(
 
-       AdsServiceImpl::BuildListener(route_config));
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   // We need to wait for all backends to come online.
 
-   WaitForAllBackends();
 
- }
 
- class XdsResolverLoadReportingOnlyTest : public XdsEnd2endTest {
 
-  public:
 
-   XdsResolverLoadReportingOnlyTest() : XdsEnd2endTest(4, 1, 3) {}
 
- };
 
- // Tests load reporting when switching over from one cluster to another.
 
- TEST_P(XdsResolverLoadReportingOnlyTest, ChangeClusters) {
 
-   const char* kNewClusterName = "new_cluster_name";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   balancers_[0]->lrs_service()->set_cluster_names(
 
-       {kDefaultClusterName, kNewClusterName});
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // cluster kDefaultClusterName -> locality0 -> backends 0 and 1
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   // cluster kNewClusterName -> locality1 -> backends 2 and 3
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality1", GetBackendPorts(2, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsServiceName));
 
-   // CDS resource for kNewClusterName.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Wait for all backends to come online.
 
-   int num_ok = 0;
 
-   int num_failure = 0;
 
-   int num_drops = 0;
 
-   std::tie(num_ok, num_failure, num_drops) = WaitForAllBackends(0, 2);
 
-   // The load report received at the balancer should be correct.
 
-   std::vector<ClientStats> load_report =
 
-       balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   EXPECT_THAT(
 
-       load_report,
 
-       ::testing::ElementsAre(::testing::AllOf(
 
-           ::testing::Property(&ClientStats::cluster_name, kDefaultClusterName),
 
-           ::testing::Property(
 
-               &ClientStats::locality_stats,
 
-               ::testing::ElementsAre(::testing::Pair(
 
-                   "locality0",
 
-                   ::testing::AllOf(
 
-                       ::testing::Field(&ClientStats::LocalityStats::
 
-                                            total_successful_requests,
 
-                                        num_ok),
 
-                       ::testing::Field(&ClientStats::LocalityStats::
 
-                                            total_requests_in_progress,
 
-                                        0UL),
 
-                       ::testing::Field(
 
-                           &ClientStats::LocalityStats::total_error_requests,
 
-                           num_failure),
 
-                       ::testing::Field(
 
-                           &ClientStats::LocalityStats::total_issued_requests,
 
-                           num_failure + num_ok))))),
 
-           ::testing::Property(&ClientStats::total_dropped_requests,
 
-                               num_drops))));
 
-   // Change RDS resource to point to new cluster.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   new_route_config.mutable_virtual_hosts(0)
 
-       ->mutable_routes(0)
 
-       ->mutable_route()
 
-       ->set_cluster(kNewClusterName);
 
-   Listener listener =
 
-       balancers_[0]->ads_service()->BuildListener(new_route_config);
 
-   balancers_[0]->ads_service()->SetLdsResource(listener);
 
-   // Wait for all new backends to be used.
 
-   std::tie(num_ok, num_failure, num_drops) = WaitForAllBackends(2, 4);
 
-   // The load report received at the balancer should be correct.
 
-   load_report = balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   EXPECT_THAT(
 
-       load_report,
 
-       ::testing::ElementsAre(
 
-           ::testing::AllOf(
 
-               ::testing::Property(&ClientStats::cluster_name,
 
-                                   kDefaultClusterName),
 
-               ::testing::Property(
 
-                   &ClientStats::locality_stats,
 
-                   ::testing::ElementsAre(::testing::Pair(
 
-                       "locality0",
 
-                       ::testing::AllOf(
 
-                           ::testing::Field(&ClientStats::LocalityStats::
 
-                                                total_successful_requests,
 
-                                            ::testing::Lt(num_ok)),
 
-                           ::testing::Field(&ClientStats::LocalityStats::
 
-                                                total_requests_in_progress,
 
-                                            0UL),
 
-                           ::testing::Field(
 
-                               &ClientStats::LocalityStats::total_error_requests,
 
-                               ::testing::Le(num_failure)),
 
-                           ::testing::Field(
 
-                               &ClientStats::LocalityStats::
 
-                                   total_issued_requests,
 
-                               ::testing::Le(num_failure + num_ok)))))),
 
-               ::testing::Property(&ClientStats::total_dropped_requests,
 
-                                   num_drops)),
 
-           ::testing::AllOf(
 
-               ::testing::Property(&ClientStats::cluster_name, kNewClusterName),
 
-               ::testing::Property(
 
-                   &ClientStats::locality_stats,
 
-                   ::testing::ElementsAre(::testing::Pair(
 
-                       "locality1",
 
-                       ::testing::AllOf(
 
-                           ::testing::Field(&ClientStats::LocalityStats::
 
-                                                total_successful_requests,
 
-                                            ::testing::Le(num_ok)),
 
-                           ::testing::Field(&ClientStats::LocalityStats::
 
-                                                total_requests_in_progress,
 
-                                            0UL),
 
-                           ::testing::Field(
 
-                               &ClientStats::LocalityStats::total_error_requests,
 
-                               ::testing::Le(num_failure)),
 
-                           ::testing::Field(
 
-                               &ClientStats::LocalityStats::
 
-                                   total_issued_requests,
 
-                               ::testing::Le(num_failure + num_ok)))))),
 
-               ::testing::Property(&ClientStats::total_dropped_requests,
 
-                                   num_drops))));
 
-   int total_ok = 0;
 
-   int total_failure = 0;
 
-   for (const ClientStats& client_stats : load_report) {
 
-     total_ok += client_stats.total_successful_requests();
 
-     total_failure += client_stats.total_error_requests();
 
-   }
 
-   EXPECT_EQ(total_ok, num_ok);
 
-   EXPECT_EQ(total_failure, num_failure);
 
-   // 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());
 
- }
 
- 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, absl::StrCat(kServerName, ";lb"));
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannel({balancers_[0]->port()});
 
-   const size_t kNumRpcsPerAddress = 100;
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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());
 
-   }
 
- }
 
- // 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, absl::StrCat(kServerName, ";lb"));
 
-         SetNextResolution({});
 
-         SetNextResolutionForLbChannel({balancers_[0]->port()});
 
-         channel_->WaitForConnected(grpc_timeout_seconds_to_deadline(1));
 
-       },
 
-       "");
 
- }
 
- using LdsTest = BasicTest;
 
- // 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()->SetLdsResource(listener);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->lds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "Listener has no ApiListener.");
 
- }
 
- // 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()->SetLdsResource(listener);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->lds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "HttpConnectionManager neither has inlined route_config nor RDS.");
 
- }
 
- // Tests that LDS client should send a NACK if the rds message in the
 
- // http_connection_manager is missing the config_source field.
 
- TEST_P(LdsTest, RdsMissingConfigSource) {
 
-   auto listener = balancers_[0]->ads_service()->default_listener();
 
-   HttpConnectionManager http_connection_manager;
 
-   http_connection_manager.mutable_rds()->set_route_config_name(
 
-       kDefaultRouteConfigurationName);
 
-   listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
 
-       http_connection_manager);
 
-   balancers_[0]->ads_service()->SetLdsResource(listener);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->lds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "HttpConnectionManager missing config_source for RDS.");
 
- }
 
- // Tests that LDS client should send a NACK if the rds message in the
 
- // http_connection_manager has a config_source field that does not specify ADS.
 
- TEST_P(LdsTest, RdsConfigSourceDoesNotSpecifyAds) {
 
-   auto listener = balancers_[0]->ads_service()->default_listener();
 
-   HttpConnectionManager http_connection_manager;
 
-   auto* rds = http_connection_manager.mutable_rds();
 
-   rds->set_route_config_name(kDefaultRouteConfigurationName);
 
-   rds->mutable_config_source()->mutable_self();
 
-   listener.mutable_api_listener()->mutable_api_listener()->PackFrom(
 
-       http_connection_manager);
 
-   balancers_[0]->ads_service()->SetLdsResource(listener);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->lds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "HttpConnectionManager ConfigSource for RDS does not specify ADS.");
 
- }
 
- using LdsRdsTest = BasicTest;
 
- // Tests that LDS client should send an ACK upon correct LDS response (with
 
- // inlined RDS result).
 
- TEST_P(LdsRdsTest, Vanilla) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   (void)SendRpc();
 
-   EXPECT_EQ(RouteConfigurationResponseState(0).state,
 
-             AdsServiceImpl::ResponseState::ACKED);
 
-   // Make sure we actually used the RPC service for the right version of xDS.
 
-   EXPECT_EQ(balancers_[0]->ads_service()->seen_v2_client(),
 
-             GetParam().use_v2());
 
-   EXPECT_NE(balancers_[0]->ads_service()->seen_v3_client(),
 
-             GetParam().use_v2());
 
- }
 
- // Tests that we go into TRANSIENT_FAILURE if the Listener is removed.
 
- TEST_P(LdsRdsTest, ListenerRemoved) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   // We need to wait for all backends to come online.
 
-   WaitForAllBackends();
 
-   // Unset LDS resource.
 
-   balancers_[0]->ads_service()->UnsetResource(kLdsTypeUrl, kServerName);
 
-   // Wait for RPCs to start failing.
 
-   do {
 
-   } while (SendRpc(RpcOptions(), nullptr).ok());
 
-   // Make sure RPCs are still failing.
 
-   CheckRpcSendFailure(1000);
 
-   // Make sure we ACK'ed the update.
 
-   EXPECT_EQ(balancers_[0]->ads_service()->lds_response_state().state,
 
-             AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- // Tests that LDS client ACKs but fails if matching domain can't be found in
 
- // the LDS response.
 
- TEST_P(LdsRdsTest, 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");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   // Do a bit of polling, to allow the ACK to get to the ADS server.
 
-   channel_->WaitForConnected(grpc_timeout_milliseconds_to_deadline(100));
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- // Tests that LDS client should choose the virtual host with matching domain if
 
- // multiple virtual hosts exist in the LDS response.
 
- TEST_P(LdsRdsTest, 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");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   (void)SendRpc();
 
-   EXPECT_EQ(RouteConfigurationResponseState(0).state,
 
-             AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- // Tests that LDS client should choose the last route in the virtual host if
 
- // multiple routes exist in the LDS response.
 
- TEST_P(LdsRdsTest, 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();
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   (void)SendRpc();
 
-   EXPECT_EQ(RouteConfigurationResponseState(0).state,
 
-             AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- // Tests that LDS client should send a NACK if route match has a case_sensitive
 
- // set to false.
 
- TEST_P(LdsRdsTest, RouteMatchHasCaseSensitiveFalse) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->mutable_case_sensitive()->set_value(false);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "case_sensitive if set must be set to true.");
 
- }
 
- // Tests that LDS client should ignore route which has query_parameters.
 
- TEST_P(LdsRdsTest, RouteMatchHasQueryParameters) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   route1->mutable_match()->add_query_parameters();
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should send a ACK if route match has a prefix
 
- // that is either empty or a single slash
 
- TEST_P(LdsRdsTest, RouteMatchHasValidPrefixEmptyOrSingleSlash) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("");
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("/");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   (void)SendRpc();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- // Tests that LDS client should ignore route which has a path
 
- // prefix string does not start with "/".
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPrefixNoLeadingSlash) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("grpc.testing.EchoTest1Service/");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has a prefix
 
- // string with more than 2 slashes.
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPrefixExtraContent) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/Echo1/");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has a prefix
 
- // string "//".
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPrefixDoubleSlash) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("//");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has path
 
- // but it's empty.
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathEmptyPath) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has path
 
- // string does not start with "/".
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathNoLeadingSlash) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("grpc.testing.EchoTest1Service/Echo1");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has path
 
- // string that has too many slashes; for example, ends with "/".
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathTooManySlashes) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/Echo1/");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has path
 
- // string that has only 1 slash: missing "/" between service and method.
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathOnlyOneSlash) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service.Echo1");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has path
 
- // string that is missing service.
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathMissingService) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("//Echo1");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Tests that LDS client should ignore route which has path
 
- // string that is missing method.
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathMissingMethod) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/");
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No valid routes specified.");
 
- }
 
- // Test that LDS client should reject route which has invalid path regex.
 
- TEST_P(LdsRdsTest, RouteMatchHasInvalidPathRegex) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->mutable_safe_regex()->set_regex("a[z-a]");
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "Invalid regex string specified in path matcher.");
 
- }
 
- // Tests that LDS client should send a NACK if route has an action other than
 
- // RouteAction in the LDS response.
 
- TEST_P(LdsRdsTest, RouteHasNoRouteAction) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   route_config.mutable_virtual_hosts(0)->mutable_routes(0)->mutable_redirect();
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "No RouteAction found in route.");
 
- }
 
- TEST_P(LdsRdsTest, RouteActionClusterHasEmptyClusterName) {
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   route1->mutable_route()->set_cluster("");
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "RouteAction cluster contains empty cluster name.");
 
- }
 
- TEST_P(LdsRdsTest, RouteActionWeightedTargetHasIncorrectTotalWeightSet) {
 
-   const size_t kWeight75 = 75;
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name(kNewCluster1Name);
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75 + 1);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "RouteAction weighted_cluster has incorrect total weight");
 
- }
 
- TEST_P(LdsRdsTest, RouteActionWeightedTargetClusterHasEmptyClusterName) {
 
-   const size_t kWeight75 = 75;
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name("");
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(
 
-       response_state.error_message,
 
-       "RouteAction weighted_cluster cluster contains empty cluster name.");
 
- }
 
- TEST_P(LdsRdsTest, RouteActionWeightedTargetClusterHasNoWeight) {
 
-   const size_t kWeight75 = 75;
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name(kNewCluster1Name);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "RouteAction weighted_cluster cluster missing weight");
 
- }
 
- TEST_P(LdsRdsTest, RouteHeaderMatchInvalidRegex) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* header_matcher1 = route1->mutable_match()->add_headers();
 
-   header_matcher1->set_name("header1");
 
-   header_matcher1->mutable_safe_regex_match()->set_regex("a[z-a]");
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "Invalid regex string specified in header matcher.");
 
- }
 
- TEST_P(LdsRdsTest, RouteHeaderMatchInvalidRange) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* header_matcher1 = route1->mutable_match()->add_headers();
 
-   header_matcher1->set_name("header1");
 
-   header_matcher1->mutable_range_match()->set_start(1001);
 
-   header_matcher1->mutable_range_match()->set_end(1000);
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   SetRouteConfiguration(0, route_config);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "Invalid range header matcher specifier specified: end "
 
-             "cannot be smaller than start.");
 
- }
 
- // Tests that LDS client times out when no response received.
 
- TEST_P(LdsRdsTest, Timeout) {
 
-   ResetStub(0, "", 500);
 
-   if (GetParam().enable_rds_testing()) {
 
-     balancers_[0]->ads_service()->SetResourceIgnore(kRdsTypeUrl);
 
-   } else {
 
-     balancers_[0]->ads_service()->SetResourceIgnore(kLdsTypeUrl);
 
-   }
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
- }
 
- // Tests that LDS client should choose the default route (with no matching
 
- // specified) after unable to find a match with previous routes.
 
- TEST_P(LdsRdsTest, XdsRoutingPathMatching) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const size_t kNumEcho1Rpcs = 10;
 
-   const size_t kNumEcho2Rpcs = 20;
 
-   const size_t kNumEchoRpcs = 30;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(3, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_path("/grpc.testing.EchoTest1Service/Echo1");
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   route2->mutable_match()->set_path("/grpc.testing.EchoTest2Service/Echo2");
 
-   route2->mutable_route()->set_cluster(kNewCluster2Name);
 
-   auto* route3 = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   route3->mutable_match()->set_path("/grpc.testing.EchoTest3Service/Echo3");
 
-   route3->mutable_route()->set_cluster(kDefaultClusterName);
 
-   auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 2);
 
-   CheckRpcSendOk(kNumEchoRpcs, RpcOptions().set_wait_for_ready(true));
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions()
 
-                                     .set_rpc_service(SERVICE_ECHO1)
 
-                                     .set_rpc_method(METHOD_ECHO1)
 
-                                     .set_wait_for_ready(true));
 
-   CheckRpcSendOk(kNumEcho2Rpcs, RpcOptions()
 
-                                     .set_rpc_service(SERVICE_ECHO2)
 
-                                     .set_rpc_method(METHOD_ECHO2)
 
-                                     .set_wait_for_ready(true));
 
-   // Make sure RPCs all go to the correct backend.
 
-   for (size_t i = 0; i < 2; ++i) {
 
-     EXPECT_EQ(kNumEchoRpcs / 2,
 
-               backends_[i]->backend_service()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
 
-   }
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service2()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   EXPECT_EQ(kNumEcho2Rpcs, backends_[3]->backend_service2()->request_count());
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingPrefixMatching) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const size_t kNumEcho1Rpcs = 10;
 
-   const size_t kNumEcho2Rpcs = 20;
 
-   const size_t kNumEchoRpcs = 30;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(3, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   route2->mutable_match()->set_prefix("/grpc.testing.EchoTest2Service/");
 
-   route2->mutable_route()->set_cluster(kNewCluster2Name);
 
-   auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 2);
 
-   CheckRpcSendOk(kNumEchoRpcs, RpcOptions().set_wait_for_ready(true));
 
-   CheckRpcSendOk(
 
-       kNumEcho1Rpcs,
 
-       RpcOptions().set_rpc_service(SERVICE_ECHO1).set_wait_for_ready(true));
 
-   CheckRpcSendOk(
 
-       kNumEcho2Rpcs,
 
-       RpcOptions().set_rpc_service(SERVICE_ECHO2).set_wait_for_ready(true));
 
-   // Make sure RPCs all go to the correct backend.
 
-   for (size_t i = 0; i < 2; ++i) {
 
-     EXPECT_EQ(kNumEchoRpcs / 2,
 
-               backends_[i]->backend_service()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
 
-   }
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service2()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   EXPECT_EQ(kNumEcho2Rpcs, backends_[3]->backend_service2()->request_count());
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingPathRegexMatching) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const size_t kNumEcho1Rpcs = 10;
 
-   const size_t kNumEcho2Rpcs = 20;
 
-   const size_t kNumEchoRpcs = 30;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(3, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   // Will match "/grpc.testing.EchoTest1Service/"
 
-   route1->mutable_match()->mutable_safe_regex()->set_regex(".*1.*");
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   auto* route2 = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   // Will match "/grpc.testing.EchoTest2Service/"
 
-   route2->mutable_match()->mutable_safe_regex()->set_regex(".*2.*");
 
-   route2->mutable_route()->set_cluster(kNewCluster2Name);
 
-   auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 2);
 
-   CheckRpcSendOk(kNumEchoRpcs, RpcOptions().set_wait_for_ready(true));
 
-   CheckRpcSendOk(
 
-       kNumEcho1Rpcs,
 
-       RpcOptions().set_rpc_service(SERVICE_ECHO1).set_wait_for_ready(true));
 
-   CheckRpcSendOk(
 
-       kNumEcho2Rpcs,
 
-       RpcOptions().set_rpc_service(SERVICE_ECHO2).set_wait_for_ready(true));
 
-   // Make sure RPCs all go to the correct backend.
 
-   for (size_t i = 0; i < 2; ++i) {
 
-     EXPECT_EQ(kNumEchoRpcs / 2,
 
-               backends_[i]->backend_service()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
 
-   }
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   EXPECT_EQ(kNumEcho1Rpcs, backends_[2]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service2()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   EXPECT_EQ(kNumEcho2Rpcs, backends_[3]->backend_service2()->request_count());
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingWeightedCluster) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const size_t kNumEcho1Rpcs = 1000;
 
-   const size_t kNumEchoRpcs = 10;
 
-   const size_t kWeight75 = 75;
 
-   const size_t kWeight25 = 25;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name(kNewCluster1Name);
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   auto* weighted_cluster2 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster2->set_name(kNewCluster2Name);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight25);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75 + kWeight25);
 
-   auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 1);
 
-   WaitForAllBackends(1, 3, true, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   const int weight_75_request_count =
 
-       backends_[1]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   const int weight_25_request_count =
 
-       backends_[2]->backend_service1()->request_count();
 
-   const double kErrorTolerance = 0.2;
 
-   EXPECT_THAT(weight_75_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   // TODO: (@donnadionne) Reduce tolerance: increased the tolerance to keep the
 
-   // test from flaking while debugging potential root cause.
 
-   const double kErrorToleranceSmallLoad = 0.3;
 
-   gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
 
-           weight_75_request_count, weight_25_request_count);
 
-   EXPECT_THAT(weight_25_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 - kErrorToleranceSmallLoad)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 + kErrorToleranceSmallLoad))));
 
- }
 
- TEST_P(LdsRdsTest, RouteActionWeightedTargetDefaultRoute) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const size_t kNumEchoRpcs = 1000;
 
-   const size_t kWeight75 = 75;
 
-   const size_t kWeight25 = 25;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name(kNewCluster1Name);
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   auto* weighted_cluster2 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster2->set_name(kNewCluster2Name);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight25);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75 + kWeight25);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(1, 3);
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(0, backends_[0]->backend_service()->request_count());
 
-   const int weight_75_request_count =
 
-       backends_[1]->backend_service()->request_count();
 
-   const int weight_25_request_count =
 
-       backends_[2]->backend_service()->request_count();
 
-   const double kErrorTolerance = 0.2;
 
-   EXPECT_THAT(weight_75_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEchoRpcs * kWeight75 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEchoRpcs * kWeight75 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   // TODO: (@donnadionne) Reduce tolerance: increased the tolerance to keep the
 
-   // test from flaking while debugging potential root cause.
 
-   const double kErrorToleranceSmallLoad = 0.3;
 
-   gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
 
-           weight_75_request_count, weight_25_request_count);
 
-   EXPECT_THAT(weight_25_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEchoRpcs * kWeight25 / 100 *
 
-                                              (1 - kErrorToleranceSmallLoad)),
 
-                                ::testing::Le(kNumEchoRpcs * kWeight25 / 100 *
 
-                                              (1 + kErrorToleranceSmallLoad))));
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingWeightedClusterUpdateWeights) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const char* kNewCluster3Name = "new_cluster_3";
 
-   const char* kNewEdsService3Name = "new_eds_service_name_3";
 
-   const size_t kNumEcho1Rpcs = 1000;
 
-   const size_t kNumEchoRpcs = 10;
 
-   const size_t kWeight75 = 75;
 
-   const size_t kWeight25 = 25;
 
-   const size_t kWeight50 = 50;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args3({
 
-       {"locality0", GetBackendPorts(3, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args3, kNewEdsService3Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   Cluster new_cluster3 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster3.set_name(kNewCluster3Name);
 
-   new_cluster3.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService3Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster3);
 
-   // Populating Route Configurations.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name(kNewCluster1Name);
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   auto* weighted_cluster2 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster2->set_name(kNewCluster2Name);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight25);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75 + kWeight25);
 
-   auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 1);
 
-   WaitForAllBackends(1, 3, true, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   const int weight_75_request_count =
 
-       backends_[1]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[1]->backend_service2()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   const int weight_25_request_count =
 
-       backends_[2]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   const double kErrorTolerance = 0.2;
 
-   EXPECT_THAT(weight_75_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   // TODO: (@donnadionne) Reduce tolerance: increased the tolerance to keep the
 
-   // test from flaking while debugging potential root cause.
 
-   const double kErrorToleranceSmallLoad = 0.3;
 
-   gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
 
-           weight_75_request_count, weight_25_request_count);
 
-   EXPECT_THAT(weight_25_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 - kErrorToleranceSmallLoad)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 + kErrorToleranceSmallLoad))));
 
-   // Change Route Configurations: same clusters different weights.
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight50);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight50);
 
-   // Change default route to a new cluster to help to identify when new polices
 
-   // are seen by the client.
 
-   default_route->mutable_route()->set_cluster(kNewCluster3Name);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   ResetBackendCounters();
 
-   WaitForAllBackends(3, 4);
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(0, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   const int weight_50_request_count_1 =
 
-       backends_[1]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   const int weight_50_request_count_2 =
 
-       backends_[2]->backend_service1()->request_count();
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   EXPECT_THAT(weight_50_request_count_1,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   EXPECT_THAT(weight_50_request_count_2,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingWeightedClusterUpdateClusters) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const char* kNewCluster3Name = "new_cluster_3";
 
-   const char* kNewEdsService3Name = "new_eds_service_name_3";
 
-   const size_t kNumEcho1Rpcs = 1000;
 
-   const size_t kNumEchoRpcs = 10;
 
-   const size_t kWeight75 = 75;
 
-   const size_t kWeight25 = 25;
 
-   const size_t kWeight50 = 50;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args3({
 
-       {"locality0", GetBackendPorts(3, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args3, kNewEdsService3Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   Cluster new_cluster3 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster3.set_name(kNewCluster3Name);
 
-   new_cluster3.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService3Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster3);
 
-   // Populating Route Configurations.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* weighted_cluster1 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster1->set_name(kNewCluster1Name);
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   auto* weighted_cluster2 =
 
-       route1->mutable_route()->mutable_weighted_clusters()->add_clusters();
 
-   weighted_cluster2->set_name(kDefaultClusterName);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight25);
 
-   route1->mutable_route()
 
-       ->mutable_weighted_clusters()
 
-       ->mutable_total_weight()
 
-       ->set_value(kWeight75 + kWeight25);
 
-   auto* default_route = new_route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 1);
 
-   WaitForAllBackends(1, 2, true, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   int weight_25_request_count =
 
-       backends_[0]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   int weight_75_request_count =
 
-       backends_[1]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   const double kErrorTolerance = 0.2;
 
-   EXPECT_THAT(weight_75_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   // TODO: (@donnadionne) Reduce tolerance: increased the tolerance to keep the
 
-   // test from flaking while debugging potential root cause.
 
-   const double kErrorToleranceSmallLoad = 0.3;
 
-   gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
 
-           weight_75_request_count, weight_25_request_count);
 
-   EXPECT_THAT(weight_25_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 - kErrorToleranceSmallLoad)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 + kErrorToleranceSmallLoad))));
 
-   // Change Route Configurations: new set of clusters with different weights.
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight50);
 
-   weighted_cluster2->set_name(kNewCluster2Name);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight50);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   ResetBackendCounters();
 
-   WaitForAllBackends(2, 3, true, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   const int weight_50_request_count_1 =
 
-       backends_[1]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   const int weight_50_request_count_2 =
 
-       backends_[2]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service1()->request_count());
 
-   EXPECT_THAT(weight_50_request_count_1,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   EXPECT_THAT(weight_50_request_count_2,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight50 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   // Change Route Configurations.
 
-   weighted_cluster1->mutable_weight()->set_value(kWeight75);
 
-   weighted_cluster2->set_name(kNewCluster3Name);
 
-   weighted_cluster2->mutable_weight()->set_value(kWeight25);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   ResetBackendCounters();
 
-   WaitForAllBackends(3, 4, true, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   weight_75_request_count = backends_[1]->backend_service1()->request_count();
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[3]->backend_service()->request_count());
 
-   weight_25_request_count = backends_[3]->backend_service1()->request_count();
 
-   EXPECT_THAT(weight_75_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 - kErrorTolerance)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight75 / 100 *
 
-                                              (1 + kErrorTolerance))));
 
-   // TODO: (@donnadionne) Reduce tolerance: increased the tolerance to keep the
 
-   // test from flaking while debugging potential root cause.
 
-   gpr_log(GPR_INFO, "target_75 received %d rpcs and target_25 received %d rpcs",
 
-           weight_75_request_count, weight_25_request_count);
 
-   EXPECT_THAT(weight_25_request_count,
 
-               ::testing::AllOf(::testing::Ge(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 - kErrorToleranceSmallLoad)),
 
-                                ::testing::Le(kNumEcho1Rpcs * kWeight25 / 100 *
 
-                                              (1 + kErrorToleranceSmallLoad))));
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingClusterUpdateClusters) {
 
-   const char* kNewClusterName = "new_cluster";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   const size_t kNumEchoRpcs = 5;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsServiceName));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Send Route Configuration.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(0, 1);
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   // Change Route Configurations: new default cluster.
 
-   auto* default_route =
 
-       new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   default_route->mutable_route()->set_cluster(kNewClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   WaitForAllBackends(1, 2);
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   // Make sure RPCs all go to the correct backend.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[1]->backend_service()->request_count());
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingClusterUpdateClustersWithPickingDelays) {
 
-   const char* kNewClusterName = "new_cluster";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsServiceName));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Bring down the current backend: 0, this will delay route picking time,
 
-   // resulting in un-committed RPCs.
 
-   ShutdownBackend(0);
 
-   // Send a RouteConfiguration with a default route that points to
 
-   // backend 0.
 
-   RouteConfiguration new_route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   SetRouteConfiguration(0, new_route_config);
 
-   // Send exactly one RPC with no deadline and with wait_for_ready=true.
 
-   // This RPC will not complete until after backend 0 is started.
 
-   std::thread sending_rpc([this]() {
 
-     CheckRpcSendOk(1, RpcOptions().set_wait_for_ready(true).set_timeout_ms(0));
 
-   });
 
-   // Send a non-wait_for_ready RPC which should fail, this will tell us
 
-   // that the client has received the update and attempted to connect.
 
-   const Status status = SendRpc(RpcOptions().set_timeout_ms(0));
 
-   EXPECT_FALSE(status.ok());
 
-   // Send a update RouteConfiguration to use backend 1.
 
-   auto* default_route =
 
-       new_route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   default_route->mutable_route()->set_cluster(kNewClusterName);
 
-   SetRouteConfiguration(0, new_route_config);
 
-   // Wait for RPCs to go to the new backend: 1, this ensures that the client has
 
-   // processed the update.
 
-   WaitForAllBackends(1, 2, false, RpcOptions(), true);
 
-   // Bring up the previous backend: 0, this will allow the delayed RPC to
 
-   // finally call on_call_committed upon completion.
 
-   StartBackend(0);
 
-   sending_rpc.join();
 
-   // Make sure RPCs go to the correct backend:
 
-   EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(1, backends_[1]->backend_service()->request_count());
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingHeadersMatching) {
 
-   const char* kNewClusterName = "new_cluster";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   const size_t kNumEcho1Rpcs = 100;
 
-   const size_t kNumEchoRpcs = 5;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsServiceName));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* header_matcher1 = route1->mutable_match()->add_headers();
 
-   header_matcher1->set_name("header1");
 
-   header_matcher1->set_exact_match("POST,PUT,GET");
 
-   auto* header_matcher2 = route1->mutable_match()->add_headers();
 
-   header_matcher2->set_name("header2");
 
-   header_matcher2->mutable_safe_regex_match()->set_regex("[a-z]*");
 
-   auto* header_matcher3 = route1->mutable_match()->add_headers();
 
-   header_matcher3->set_name("header3");
 
-   header_matcher3->mutable_range_match()->set_start(1);
 
-   header_matcher3->mutable_range_match()->set_end(1000);
 
-   auto* header_matcher4 = route1->mutable_match()->add_headers();
 
-   header_matcher4->set_name("header4");
 
-   header_matcher4->set_present_match(false);
 
-   auto* header_matcher5 = route1->mutable_match()->add_headers();
 
-   header_matcher5->set_name("header5");
 
-   header_matcher5->set_prefix_match("/grpc");
 
-   auto* header_matcher6 = route1->mutable_match()->add_headers();
 
-   header_matcher6->set_name("header6");
 
-   header_matcher6->set_suffix_match(".cc");
 
-   header_matcher6->set_invert_match(true);
 
-   route1->mutable_route()->set_cluster(kNewClusterName);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   std::vector<std::pair<std::string, std::string>> metadata = {
 
-       {"header1", "POST"}, {"header2", "blah"},
 
-       {"header3", "1"},    {"header5", "/grpc.testing.EchoTest1Service/"},
 
-       {"header1", "PUT"},  {"header6", "grpc.java"},
 
-       {"header1", "GET"},
 
-   };
 
-   const auto header_match_rpc_options = RpcOptions()
 
-                                             .set_rpc_service(SERVICE_ECHO1)
 
-                                             .set_rpc_method(METHOD_ECHO1)
 
-                                             .set_metadata(std::move(metadata));
 
-   // Make sure all backends are up.
 
-   WaitForAllBackends(0, 1);
 
-   WaitForAllBackends(1, 2, true, header_match_rpc_options);
 
-   // Send RPCs.
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   CheckRpcSendOk(kNumEcho1Rpcs, header_match_rpc_options);
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service2()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   EXPECT_EQ(kNumEcho1Rpcs, backends_[1]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service2()->request_count());
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingHeadersMatchingSpecialHeaderContentType) {
 
-   const char* kNewClusterName = "new_cluster";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   const size_t kNumEchoRpcs = 100;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsServiceName));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("");
 
-   auto* header_matcher1 = route1->mutable_match()->add_headers();
 
-   header_matcher1->set_name("content-type");
 
-   header_matcher1->set_exact_match("notapplication/grpc");
 
-   route1->mutable_route()->set_cluster(kNewClusterName);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   auto* header_matcher2 = default_route->mutable_match()->add_headers();
 
-   header_matcher2->set_name("content-type");
 
-   header_matcher2->set_exact_match("application/grpc");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   // Make sure the backend is up.
 
-   WaitForAllBackends(0, 1);
 
-   // Send RPCs.
 
-   CheckRpcSendOk(kNumEchoRpcs);
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingHeadersMatchingSpecialCasesToIgnore) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const size_t kNumEchoRpcs = 100;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("");
 
-   auto* header_matcher1 = route1->mutable_match()->add_headers();
 
-   header_matcher1->set_name("grpc-foo-bin");
 
-   header_matcher1->set_present_match(true);
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   auto route2 = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   route2->mutable_match()->set_prefix("");
 
-   auto* header_matcher2 = route2->mutable_match()->add_headers();
 
-   header_matcher2->set_name("grpc-previous-rpc-attempts");
 
-   header_matcher2->set_present_match(true);
 
-   route2->mutable_route()->set_cluster(kNewCluster2Name);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   // Send headers which will mismatch each route
 
-   std::vector<std::pair<std::string, std::string>> metadata = {
 
-       {"grpc-foo-bin", "grpc-foo-bin"},
 
-       {"grpc-previous-rpc-attempts", "grpc-previous-rpc-attempts"},
 
-   };
 
-   WaitForAllBackends(0, 1);
 
-   CheckRpcSendOk(kNumEchoRpcs, RpcOptions().set_metadata(metadata));
 
-   // Verify that only the default backend got RPCs since all previous routes
 
-   // were mismatched.
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[2]->backend_service()->request_count());
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingRuntimeFractionMatching) {
 
-   const char* kNewClusterName = "new_cluster";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   const size_t kNumRpcs = 1000;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsServiceName));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()
 
-       ->mutable_runtime_fraction()
 
-       ->mutable_default_value()
 
-       ->set_numerator(25);
 
-   route1->mutable_route()->set_cluster(kNewClusterName);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   WaitForAllBackends(0, 2);
 
-   CheckRpcSendOk(kNumRpcs);
 
-   const int default_backend_count =
 
-       backends_[0]->backend_service()->request_count();
 
-   const int matched_backend_count =
 
-       backends_[1]->backend_service()->request_count();
 
-   const double kErrorTolerance = 0.2;
 
-   EXPECT_THAT(default_backend_count,
 
-               ::testing::AllOf(
 
-                   ::testing::Ge(kNumRpcs * 75 / 100 * (1 - kErrorTolerance)),
 
-                   ::testing::Le(kNumRpcs * 75 / 100 * (1 + kErrorTolerance))));
 
-   EXPECT_THAT(matched_backend_count,
 
-               ::testing::AllOf(
 
-                   ::testing::Ge(kNumRpcs * 25 / 100 * (1 - kErrorTolerance)),
 
-                   ::testing::Le(kNumRpcs * 25 / 100 * (1 + kErrorTolerance))));
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingHeadersMatchingUnmatchCases) {
 
-   const char* kNewCluster1Name = "new_cluster_1";
 
-   const char* kNewEdsService1Name = "new_eds_service_name_1";
 
-   const char* kNewCluster2Name = "new_cluster_2";
 
-   const char* kNewEdsService2Name = "new_eds_service_name_2";
 
-   const char* kNewCluster3Name = "new_cluster_3";
 
-   const char* kNewEdsService3Name = "new_eds_service_name_3";
 
-   const size_t kNumEcho1Rpcs = 100;
 
-   const size_t kNumEchoRpcs = 5;
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args2({
 
-       {"locality0", GetBackendPorts(2, 3)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args3({
 
-       {"locality0", GetBackendPorts(3, 4)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsService1Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args2, kNewEdsService2Name));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args3, kNewEdsService3Name));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster1 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster1.set_name(kNewCluster1Name);
 
-   new_cluster1.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService1Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster1);
 
-   Cluster new_cluster2 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster2.set_name(kNewCluster2Name);
 
-   new_cluster2.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService2Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster2);
 
-   Cluster new_cluster3 = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster3.set_name(kNewCluster3Name);
 
-   new_cluster3.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsService3Name);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster3);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* header_matcher1 = route1->mutable_match()->add_headers();
 
-   header_matcher1->set_name("header1");
 
-   header_matcher1->set_exact_match("POST");
 
-   route1->mutable_route()->set_cluster(kNewCluster1Name);
 
-   auto route2 = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   route2->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* header_matcher2 = route2->mutable_match()->add_headers();
 
-   header_matcher2->set_name("header2");
 
-   header_matcher2->mutable_range_match()->set_start(1);
 
-   header_matcher2->mutable_range_match()->set_end(1000);
 
-   route2->mutable_route()->set_cluster(kNewCluster2Name);
 
-   auto route3 = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   route3->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   auto* header_matcher3 = route3->mutable_match()->add_headers();
 
-   header_matcher3->set_name("header3");
 
-   header_matcher3->mutable_safe_regex_match()->set_regex("[a-z]*");
 
-   route3->mutable_route()->set_cluster(kNewCluster3Name);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   // Send headers which will mismatch each route
 
-   std::vector<std::pair<std::string, std::string>> metadata = {
 
-       {"header1", "POST"},
 
-       {"header2", "1000"},
 
-       {"header3", "123"},
 
-       {"header1", "GET"},
 
-   };
 
-   WaitForAllBackends(0, 1);
 
-   CheckRpcSendOk(kNumEchoRpcs, RpcOptions().set_metadata(metadata));
 
-   CheckRpcSendOk(kNumEcho1Rpcs, RpcOptions()
 
-                                     .set_rpc_service(SERVICE_ECHO1)
 
-                                     .set_rpc_method(METHOD_ECHO1)
 
-                                     .set_metadata(metadata));
 
-   // Verify that only the default backend got RPCs since all previous routes
 
-   // were mismatched.
 
-   for (size_t i = 1; i < 4; ++i) {
 
-     EXPECT_EQ(0, backends_[i]->backend_service()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service1()->request_count());
 
-     EXPECT_EQ(0, backends_[i]->backend_service2()->request_count());
 
-   }
 
-   EXPECT_EQ(kNumEchoRpcs, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(kNumEcho1Rpcs, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service2()->request_count());
 
-   const auto& response_state = RouteConfigurationResponseState(0);
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::ACKED);
 
- }
 
- TEST_P(LdsRdsTest, XdsRoutingChangeRoutesWithoutChangingClusters) {
 
-   const char* kNewClusterName = "new_cluster";
 
-   const char* kNewEdsServiceName = "new_eds_service_name";
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   // Populate new EDS resources.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   AdsServiceImpl::EdsResourceArgs args1({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args1, kNewEdsServiceName));
 
-   // Populate new CDS resources.
 
-   Cluster new_cluster = balancers_[0]->ads_service()->default_cluster();
 
-   new_cluster.set_name(kNewClusterName);
 
-   new_cluster.mutable_eds_cluster_config()->set_service_name(
 
-       kNewEdsServiceName);
 
-   balancers_[0]->ads_service()->SetCdsResource(new_cluster);
 
-   // Populating Route Configurations for LDS.
 
-   RouteConfiguration route_config =
 
-       balancers_[0]->ads_service()->default_route_config();
 
-   auto* route1 = route_config.mutable_virtual_hosts(0)->mutable_routes(0);
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest1Service/");
 
-   route1->mutable_route()->set_cluster(kNewClusterName);
 
-   auto* default_route = route_config.mutable_virtual_hosts(0)->add_routes();
 
-   default_route->mutable_match()->set_prefix("");
 
-   default_route->mutable_route()->set_cluster(kDefaultClusterName);
 
-   SetRouteConfiguration(0, route_config);
 
-   // Make sure all backends are up and that requests for each RPC
 
-   // service go to the right backends.
 
-   WaitForAllBackends(0, 1, false);
 
-   WaitForAllBackends(1, 2, false, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   WaitForAllBackends(0, 1, false, RpcOptions().set_rpc_service(SERVICE_ECHO2));
 
-   // Requests for services Echo and Echo2 should have gone to backend 0.
 
-   EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(1, backends_[0]->backend_service2()->request_count());
 
-   // Requests for service Echo1 should have gone to backend 1.
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   EXPECT_EQ(1, backends_[1]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service2()->request_count());
 
-   // Now send an update that changes the first route to match a
 
-   // different RPC service, and wait for the client to make the change.
 
-   route1->mutable_match()->set_prefix("/grpc.testing.EchoTest2Service/");
 
-   SetRouteConfiguration(0, route_config);
 
-   WaitForAllBackends(1, 2, true, RpcOptions().set_rpc_service(SERVICE_ECHO2));
 
-   // Now repeat the earlier test, making sure all traffic goes to the
 
-   // right place.
 
-   WaitForAllBackends(0, 1, false);
 
-   WaitForAllBackends(0, 1, false, RpcOptions().set_rpc_service(SERVICE_ECHO1));
 
-   WaitForAllBackends(1, 2, false, RpcOptions().set_rpc_service(SERVICE_ECHO2));
 
-   // Requests for services Echo and Echo1 should have gone to backend 0.
 
-   EXPECT_EQ(1, backends_[0]->backend_service()->request_count());
 
-   EXPECT_EQ(1, backends_[0]->backend_service1()->request_count());
 
-   EXPECT_EQ(0, backends_[0]->backend_service2()->request_count());
 
-   // Requests for service Echo2 should have gone to backend 1.
 
-   EXPECT_EQ(0, backends_[1]->backend_service()->request_count());
 
-   EXPECT_EQ(0, backends_[1]->backend_service1()->request_count());
 
-   EXPECT_EQ(1, backends_[1]->backend_service2()->request_count());
 
- }
 
- 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().state,
 
-             AdsServiceImpl::ResponseState::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(Cluster::STATIC);
 
-   balancers_[0]->ads_service()->SetCdsResource(cluster);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->cds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "DiscoveryType is not EDS.");
 
- }
 
- // 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()->SetCdsResource(cluster);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->cds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "EDS ConfigSource is not ADS.");
 
- }
 
- // 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(Cluster::LEAST_REQUEST);
 
-   balancers_[0]->ads_service()->SetCdsResource(cluster);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->cds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "LB policy is not ROUND_ROBIN.");
 
- }
 
- // 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()->SetCdsResource(cluster);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->cds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message, "LRS ConfigSource is not self.");
 
- }
 
- // Tests that CDS client times out when no response received.
 
- TEST_P(CdsTest, Timeout) {
 
-   ResetStub(0, "", 500);
 
-   balancers_[0]->ads_service()->SetResourceIgnore(kCdsTypeUrl);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
- }
 
- using EdsTest = BasicTest;
 
- TEST_P(EdsTest, Timeout) {
 
-   ResetStub(0, "", 500);
 
-   balancers_[0]->ads_service()->SetResourceIgnore(kEdsTypeUrl);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendFailure();
 
- }
 
- // Tests that EDS client should send a NACK if the EDS update contains
 
- // sparse priorities.
 
- TEST_P(EdsTest, NacksSparsePriorityList) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(), kDefaultLocalityWeight, 1},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args));
 
-   CheckRpcSendFailure();
 
-   const auto& response_state =
 
-       balancers_[0]->ads_service()->eds_response_state();
 
-   EXPECT_EQ(response_state.state, AdsServiceImpl::ResponseState::NACKED);
 
-   EXPECT_EQ(response_state.error_message,
 
-             "EDS update includes sparse priority list");
 
- }
 
- // In most of our tests, we use different names for different resource
 
- // types, to make sure that there are no cut-and-paste errors in the code
 
- // that cause us to look at data for the wrong resource type.  So we add
 
- // this test to make sure that the EDS resource name defaults to the
 
- // cluster name if not specified in the CDS resource.
 
- TEST_P(EdsTest, EdsServiceNameDefaultsToClusterName) {
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, kDefaultClusterName));
 
-   Cluster cluster = balancers_[0]->ads_service()->default_cluster();
 
-   cluster.mutable_eds_cluster_config()->clear_service_name();
 
-   balancers_[0]->ads_service()->SetCdsResource(cluster);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   CheckRpcSendOk();
 
- }
 
- 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1), kLocalityWeight0},
 
-       {"locality1", GetBackendPorts(1, 2), kLocalityWeight1},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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))));
 
- }
 
- // Tests that we correctly handle a locality containing no endpoints.
 
- TEST_P(LocalityMapTest, LocalityContainingNoEndpoints) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   const size_t kNumRpcs = 5000;
 
-   // EDS response contains 2 localities, one with no endpoints.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-       {"locality1", {}},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // Wait for both backends to be ready.
 
-   WaitForAllBackends();
 
-   // Send kNumRpcs RPCs.
 
-   CheckRpcSendOk(kNumRpcs);
 
-   // All traffic should go to the reachable locality.
 
-   EXPECT_EQ(backends_[0]->backend_service()->request_count(),
 
-             kNumRpcs / backends_.size());
 
-   EXPECT_EQ(backends_[1]->backend_service()->request_count(),
 
-             kNumRpcs / backends_.size());
 
-   EXPECT_EQ(backends_[2]->backend_service()->request_count(),
 
-             kNumRpcs / backends_.size());
 
-   EXPECT_EQ(backends_[3]->backend_service()->request_count(),
 
-             kNumRpcs / backends_.size());
 
- }
 
- // EDS update with no localities.
 
- TEST_P(LocalityMapTest, NoLocalities) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource({}, DefaultEdsServiceName()));
 
-   Status status = SendRpc();
 
-   EXPECT_FALSE(status.ok());
 
-   EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE);
 
- }
 
- // 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::EdsResourceArgs args;
 
-   for (size_t i = 0; i < kNumLocalities; ++i) {
 
-     std::string name = absl::StrCat("locality", i);
 
-     AdsServiceImpl::EdsResourceArgs::Locality locality(name,
 
-                                                        {backends_[0]->port()});
 
-     args.locality_list.emplace_back(std::move(locality));
 
-   }
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // The second ADS response contains 1 locality, which contains backend 1.
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", GetBackendPorts(1, 2)},
 
-   });
 
-   std::thread delayed_resource_setter(
 
-       std::bind(&BasicTest::SetEdsResourceWithDelay, this, 0,
 
-                 AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()),
 
-                 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);
 
-   delayed_resource_setter.join();
 
- }
 
- // 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 = 3000;
 
-   // 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1), 2},
 
-       {"locality1", GetBackendPorts(1, 2), 3},
 
-       {"locality2", GetBackendPorts(2, 3), 4},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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) {
 
-     gpr_log(GPR_INFO, "Locality %" PRIuPTR " rate %f", i,
 
-             locality_picked_rates[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))));
 
-   }
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality1", GetBackendPorts(1, 2), 3},
 
-       {"locality2", GetBackendPorts(2, 3), 2},
 
-       {"locality3", GetBackendPorts(3, 4), 6},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // Backend 3 hasn't received any request.
 
-   EXPECT_EQ(0U, backends_[3]->backend_service()->request_count());
 
-   // Wait until the locality update has been processed, as signaled by backend 3
 
-   // receiving a request.
 
-   WaitForAllBackends(3, 4);
 
-   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) {
 
-     gpr_log(GPR_INFO, "Locality %" PRIuPTR " rate %f", i,
 
-             locality_picked_rates[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))));
 
-   }
 
- }
 
- // Tests that we don't fail RPCs when replacing all of the localities in
 
- // a given priority.
 
- TEST_P(LocalityMapTest, ReplaceAllLocalitiesInPriority) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality1", GetBackendPorts(1, 2)},
 
-   });
 
-   std::thread delayed_resource_setter(std::bind(
 
-       &BasicTest::SetEdsResourceWithDelay, this, 0,
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()), 5000));
 
-   // Wait for the first backend to be ready.
 
-   WaitForBackend(0);
 
-   // Keep sending RPCs until we switch over to backend 1, which tells us
 
-   // that we received the update.  No RPCs should fail during this
 
-   // transition.
 
-   WaitForBackend(1, /*reset_counters=*/true, /*require_success=*/true);
 
-   delayed_resource_setter.join();
 
- }
 
- class FailoverTest : public BasicTest {
 
-  public:
 
-   void SetUp() override {
 
-     BasicTest::SetUp();
 
-     ResetStub(500, "");
 
-   }
 
- };
 
- // Localities with the highest priority are used when multiple priority exist.
 
- TEST_P(FailoverTest, ChooseHighestPriority) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs 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},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   WaitForBackend(3, false);
 
-   for (size_t i = 0; i < 3; ++i) {
 
-     EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());
 
-   }
 
- }
 
- // Does not choose priority with no endpoints.
 
- TEST_P(FailoverTest, DoesNotUsePriorityWithNoEndpoints) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 1},
 
-       {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 2},
 
-       {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 3},
 
-       {"locality3", {}, kDefaultLocalityWeight, 0},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   WaitForBackend(0, false);
 
-   for (size_t i = 1; i < 3; ++i) {
 
-     EXPECT_EQ(0U, backends_[i]->backend_service()->request_count());
 
-   }
 
- }
 
- // Does not choose locality with no endpoints.
 
- TEST_P(FailoverTest, DoesNotUseLocalityWithNoEndpoints) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", {}, kDefaultLocalityWeight, 0},
 
-       {"locality1", GetBackendPorts(), kDefaultLocalityWeight, 0},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // Wait for all backends to be used.
 
-   std::tuple<int, int, int> counts = WaitForAllBackends();
 
-   // Make sure no RPCs failed in the transition.
 
-   EXPECT_EQ(0, std::get<1>(counts));
 
- }
 
- // If the higher priority localities are not reachable, failover to the highest
 
- // priority among the rest.
 
- TEST_P(FailoverTest, Failover) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs 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);
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   WaitForBackend(1, false);
 
-   for (size_t i = 0; i < 4; ++i) {
 
-     if (i == 1) continue;
 
-     EXPECT_EQ(0U, backends_[i]->backend_service()->request_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::EdsResourceArgs 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);
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   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 first update only contains unavailable priorities. The second update
 
- // contains available priorities.
 
- TEST_P(FailoverTest, UpdateInitialUnavailable) {
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannelAllBalancers();
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},
 
-       {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 1},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"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);
 
-   std::thread delayed_resource_setter(std::bind(
 
-       &BasicTest::SetEdsResourceWithDelay, this, 0,
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()), 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());
 
-   }
 
-   delayed_resource_setter.join();
 
- }
 
- // 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::EdsResourceArgs 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},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 2},
 
-       {"locality1", GetBackendPorts(1, 2), kDefaultLocalityWeight, 0},
 
-       {"locality2", GetBackendPorts(2, 3), kDefaultLocalityWeight, 1},
 
-       {"locality3", GetBackendPorts(3, 4), kDefaultLocalityWeight, 3},
 
-   });
 
-   std::thread delayed_resource_setter(std::bind(
 
-       &BasicTest::SetEdsResourceWithDelay, this, 0,
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()), 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());
 
-   delayed_resource_setter.join();
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},
 
-       {"locality1", GetBackendPorts(1, 3), kDefaultLocalityWeight, 1},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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::EdsResourceArgs({
 
-       {"locality0", GetBackendPorts(0, 1), kDefaultLocalityWeight, 0},
 
-       {"locality1", GetBackendPorts(1, 4), kDefaultLocalityWeight, 0},
 
-   });
 
-   std::thread delayed_resource_setter(std::bind(
 
-       &BasicTest::SetEdsResourceWithDelay, this, 0,
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()), 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 of balancer 0 got at least 1 response.
 
-   EXPECT_GT(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT);
 
-   delayed_resource_setter.join();
 
- }
 
- 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   args.drop_categories = {{kLbDropType, kDropPerMillionForLb},
 
-                           {kThrottleDropType, kDropPerMillionForThrottle}};
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   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(RpcOptions(), &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))));
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   args.drop_categories = {{kLbDropType, kDropPerHundredForLb}};
 
-   args.drop_denominator = FractionalPercent::HUNDRED;
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   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(RpcOptions(), &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))));
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   args.drop_categories = {{kLbDropType, kDropPerTenThousandForLb}};
 
-   args.drop_denominator = FractionalPercent::TEN_THOUSAND;
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   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(RpcOptions(), &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))));
 
- }
 
- // Tests that drop is working correctly after update.
 
- TEST_P(DropTest, Update) {
 
-   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 first ADS response contains one drop category.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   args.drop_categories = {{kLbDropType, kDropPerMillionForLb}};
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   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(RpcOptions(), &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;
 
-   gpr_log(GPR_INFO, "First batch drop rate %f", seen_drop_rate);
 
-   const double kErrorTolerance = 0.3;
 
-   EXPECT_THAT(
 
-       seen_drop_rate,
 
-       ::testing::AllOf(::testing::Ge(kDropRateForLb * (1 - kErrorTolerance)),
 
-                        ::testing::Le(kDropRateForLb * (1 + kErrorTolerance))));
 
-   // The second ADS response contains two drop categories, send an update EDS
 
-   // response.
 
-   args.drop_categories = {{kLbDropType, kDropPerMillionForLb},
 
-                           {kThrottleDropType, kDropPerMillionForThrottle}};
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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(RpcOptions(), &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(RpcOptions(), &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;
 
-   gpr_log(GPR_INFO, "Second batch drop rate %f", seen_drop_rate);
 
-   EXPECT_THAT(
 
-       seen_drop_rate,
 
-       ::testing::AllOf(
 
-           ::testing::Ge(KDropRateForLbAndThrottle * (1 - kErrorTolerance)),
 
-           ::testing::Le(KDropRateForLbAndThrottle * (1 + kErrorTolerance))));
 
- }
 
- // 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::EdsResourceArgs args;
 
-   args.drop_categories = {{kLbDropType, kDropPerMillionForLb},
 
-                           {kThrottleDropType, kDropPerMillionForThrottle}};
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // Send kNumRpcs RPCs and all of them are dropped.
 
-   for (size_t i = 0; i < kNumRpcs; ++i) {
 
-     EchoResponse response;
 
-     const Status status = SendRpc(RpcOptions(), &response);
 
-     EXPECT_EQ(status.error_code(), StatusCode::UNAVAILABLE);
 
-     EXPECT_EQ(status.error_message(), "Call dropped by load balancing policy");
 
-   }
 
- }
 
- 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::EdsResourceArgs args({
 
-       {"locality0", {backends_[0]->port()}},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", {backends_[1]->port()}},
 
-   });
 
-   balancers_[1]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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 sent at least 1 response.
 
-   EXPECT_GT(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT);
 
-   EXPECT_EQ(balancers_[1]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[1]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_EQ(balancers_[2]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[2]->ads_service()->eds_response_state().error_message;
 
-   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());
 
-   // The ADS service of balancer 0 sent at least 1 response.
 
-   EXPECT_GT(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT);
 
-   EXPECT_EQ(balancers_[1]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[1]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_EQ(balancers_[2]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[2]->ads_service()->eds_response_state().error_message;
 
- }
 
- // 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::EdsResourceArgs args({
 
-       {"locality0", {backends_[0]->port()}},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", {backends_[1]->port()}},
 
-   });
 
-   balancers_[1]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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 sent at least 1 response.
 
-   EXPECT_GT(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT);
 
-   EXPECT_EQ(balancers_[1]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[1]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_EQ(balancers_[2]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[2]->ads_service()->eds_response_state().error_message;
 
-   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::EdsResourceArgs args({
 
-       {"locality0", {backends_[0]->port()}},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", {backends_[1]->port()}},
 
-   });
 
-   balancers_[1]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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());
 
-   // The ADS service of balancer 0 sent at least 1 response.
 
-   EXPECT_GT(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT);
 
-   EXPECT_EQ(balancers_[1]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[1]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_EQ(balancers_[2]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[2]->ads_service()->eds_response_state().error_message;
 
-   // 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 no balancers sent anything
 
-   EXPECT_EQ(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[0]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_EQ(balancers_[1]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[1]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_EQ(balancers_[2]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[2]->ads_service()->eds_response_state().error_message;
 
-   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());
 
-   // The ADS service of balancer 1 sent at least 1 response.
 
-   EXPECT_EQ(balancers_[0]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[0]->ads_service()->eds_response_state().error_message;
 
-   EXPECT_GT(balancers_[1]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT);
 
-   EXPECT_EQ(balancers_[2]->ads_service()->eds_response_state().state,
 
-             AdsServiceImpl::ResponseState::NOT_SENT)
 
-       << "Error Message:"
 
-       << balancers_[2]->ads_service()->eds_response_state().error_message;
 
- }
 
- // 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) {
 
-   if (!GetParam().use_xds_resolver()) {
 
-     balancers_[0]->lrs_service()->set_cluster_names({kServerName});
 
-   }
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannel({balancers_[0]->port()});
 
-   const size_t kNumRpcsPerAddress = 10;
 
-   const size_t kNumFailuresPerAddress = 3;
 
-   // TODO(juanlishen): Partition the backends after multiple localities is
 
-   // tested.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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_);
 
-   CheckRpcSendFailure(kNumFailuresPerAddress * num_backends_,
 
-                       /*server_fail=*/true);
 
-   // Check that each backend got the right number of requests.
 
-   for (size_t i = 0; i < backends_.size(); ++i) {
 
-     EXPECT_EQ(kNumRpcsPerAddress + kNumFailuresPerAddress,
 
-               backends_[i]->backend_service()->request_count());
 
-   }
 
-   // The load report received at the balancer should be correct.
 
-   std::vector<ClientStats> load_report =
 
-       balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   ASSERT_EQ(load_report.size(), 1UL);
 
-   ClientStats& client_stats = load_report.front();
 
-   EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok,
 
-             client_stats.total_successful_requests());
 
-   EXPECT_EQ(0U, client_stats.total_requests_in_progress());
 
-   EXPECT_EQ((kNumRpcsPerAddress + kNumFailuresPerAddress) * num_backends_ +
 
-                 num_ok + num_failure,
 
-             client_stats.total_issued_requests());
 
-   EXPECT_EQ(kNumFailuresPerAddress * num_backends_ + num_failure,
 
-             client_stats.total_error_requests());
 
-   EXPECT_EQ(0U, client_stats.total_dropped_requests());
 
-   // 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());
 
- }
 
- // Tests send_all_clusters.
 
- TEST_P(ClientLoadReportingTest, SendAllClusters) {
 
-   balancers_[0]->lrs_service()->set_send_all_clusters(true);
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannel({balancers_[0]->port()});
 
-   const size_t kNumRpcsPerAddress = 10;
 
-   const size_t kNumFailuresPerAddress = 3;
 
-   // TODO(juanlishen): Partition the backends after multiple localities is
 
-   // tested.
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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_);
 
-   CheckRpcSendFailure(kNumFailuresPerAddress * num_backends_,
 
-                       /*server_fail=*/true);
 
-   // Check that each backend got the right number of requests.
 
-   for (size_t i = 0; i < backends_.size(); ++i) {
 
-     EXPECT_EQ(kNumRpcsPerAddress + kNumFailuresPerAddress,
 
-               backends_[i]->backend_service()->request_count());
 
-   }
 
-   // The load report received at the balancer should be correct.
 
-   std::vector<ClientStats> load_report =
 
-       balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   ASSERT_EQ(load_report.size(), 1UL);
 
-   ClientStats& client_stats = load_report.front();
 
-   EXPECT_EQ(kNumRpcsPerAddress * num_backends_ + num_ok,
 
-             client_stats.total_successful_requests());
 
-   EXPECT_EQ(0U, client_stats.total_requests_in_progress());
 
-   EXPECT_EQ((kNumRpcsPerAddress + kNumFailuresPerAddress) * num_backends_ +
 
-                 num_ok + num_failure,
 
-             client_stats.total_issued_requests());
 
-   EXPECT_EQ(kNumFailuresPerAddress * num_backends_ + num_failure,
 
-             client_stats.total_error_requests());
 
-   EXPECT_EQ(0U, client_stats.total_dropped_requests());
 
-   // 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());
 
- }
 
- // Tests that we don't include stats for clusters that are not requested
 
- // by the LRS server.
 
- TEST_P(ClientLoadReportingTest, HonorsClustersRequestedByLrsServer) {
 
-   balancers_[0]->lrs_service()->set_cluster_names({"bogus"});
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannel({balancers_[0]->port()});
 
-   const size_t kNumRpcsPerAddress = 100;
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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 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.
 
-   std::vector<ClientStats> load_report =
 
-       balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   ASSERT_EQ(load_report.size(), 0UL);
 
- }
 
- // Tests that if the balancer restarts, the client load report contains the
 
- // stats before and after the restart correctly.
 
- TEST_P(ClientLoadReportingTest, BalancerRestart) {
 
-   if (!GetParam().use_xds_resolver()) {
 
-     balancers_[0]->lrs_service()->set_cluster_names({kServerName});
 
-   }
 
-   SetNextResolution({});
 
-   SetNextResolutionForLbChannel({balancers_[0]->port()});
 
-   const size_t kNumBackendsFirstPass = backends_.size() / 2;
 
-   const size_t kNumBackendsSecondPass =
 
-       backends_.size() - kNumBackendsFirstPass;
 
-   AdsServiceImpl::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts(0, kNumBackendsFirstPass)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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);
 
-   std::vector<ClientStats> load_report =
 
-       balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   ASSERT_EQ(load_report.size(), 1UL);
 
-   ClientStats client_stats = std::move(load_report.front());
 
-   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();
 
-   args = AdsServiceImpl::EdsResourceArgs({
 
-       {"locality0", GetBackendPorts(kNumBackendsFirstPass)},
 
-   });
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   // 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.
 
-   load_report = balancers_[0]->lrs_service()->WaitForLoadReport();
 
-   ASSERT_EQ(load_report.size(), 1UL);
 
-   client_stats = std::move(load_report.front());
 
-   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) {
 
-   if (!GetParam().use_xds_resolver()) {
 
-     balancers_[0]->lrs_service()->set_cluster_names({kServerName});
 
-   }
 
-   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::EdsResourceArgs args({
 
-       {"locality0", GetBackendPorts()},
 
-   });
 
-   args.drop_categories = {{kLbDropType, kDropPerMillionForLb},
 
-                           {kThrottleDropType, kDropPerMillionForThrottle}};
 
-   balancers_[0]->ads_service()->SetEdsResource(
 
-       AdsServiceImpl::BuildEdsResource(args, DefaultEdsServiceName()));
 
-   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(RpcOptions(), &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.
 
-   const size_t total_rpc = num_warmup + kNumRpcs;
 
-   ClientStats client_stats;
 
-   do {
 
-     std::vector<ClientStats> load_reports =
 
-         balancers_[0]->lrs_service()->WaitForLoadReport();
 
-     for (const auto& load_report : load_reports) {
 
-       client_stats += load_report;
 
-     }
 
-   } while (client_stats.total_issued_requests() +
 
-                client_stats.total_dropped_requests() <
 
-            total_rpc);
 
-   EXPECT_EQ(num_drops, client_stats.total_dropped_requests());
 
-   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))));
 
- }
 
- std::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);
 
- // LDS/RDS commmon tests depend on XdsResolver.
 
- INSTANTIATE_TEST_SUITE_P(XdsTest, LdsRdsTest,
 
-                          ::testing::Values(TestType(true, false),
 
-                                            TestType(true, true),
 
-                                            TestType(true, false, true),
 
-                                            TestType(true, true, true),
 
-                                            // Also test with xDS v2.
 
-                                            TestType(true, true, 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);
 
- // XdsResolverOnlyTest depends on XdsResolver.
 
- INSTANTIATE_TEST_SUITE_P(XdsTest, XdsResolverOnlyTest,
 
-                          ::testing::Values(TestType(true, false),
 
-                                            TestType(true, true)),
 
-                          &TestTypeName);
 
- // XdsResolverLoadReprtingOnlyTest depends on XdsResolver and load reporting.
 
- INSTANTIATE_TEST_SUITE_P(XdsTest, XdsResolverLoadReportingOnlyTest,
 
-                          ::testing::Values(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);
 
- 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 grpc
 
- int 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;
 
- }
 
 
  |