12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199 |
- /*
- *
- * Copyright 2014 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.
- *
- */
- package test
- import (
- "bufio"
- "bytes"
- "compress/gzip"
- "context"
- "crypto/tls"
- "errors"
- "flag"
- "fmt"
- "io"
- "math"
- "net"
- "net/http"
- "os"
- "reflect"
- "runtime"
- "strings"
- "sync"
- "sync/atomic"
- "syscall"
- "testing"
- "time"
- "github.com/golang/protobuf/proto"
- anypb "github.com/golang/protobuf/ptypes/any"
- "golang.org/x/net/http2"
- "golang.org/x/net/http2/hpack"
- spb "google.golang.org/genproto/googleapis/rpc/status"
- "google.golang.org/grpc"
- "google.golang.org/grpc/codes"
- "google.golang.org/grpc/connectivity"
- "google.golang.org/grpc/credentials"
- "google.golang.org/grpc/encoding"
- _ "google.golang.org/grpc/encoding/gzip"
- "google.golang.org/grpc/health"
- healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
- healthpb "google.golang.org/grpc/health/grpc_health_v1"
- "google.golang.org/grpc/internal/channelz"
- "google.golang.org/grpc/internal/grpcsync"
- "google.golang.org/grpc/internal/grpctest"
- "google.golang.org/grpc/internal/testutils"
- "google.golang.org/grpc/internal/transport"
- "google.golang.org/grpc/metadata"
- "google.golang.org/grpc/peer"
- "google.golang.org/grpc/resolver"
- "google.golang.org/grpc/resolver/manual"
- "google.golang.org/grpc/serviceconfig"
- "google.golang.org/grpc/stats"
- "google.golang.org/grpc/status"
- "google.golang.org/grpc/tap"
- testpb "google.golang.org/grpc/test/grpc_testing"
- "google.golang.org/grpc/testdata"
- )
- const defaultHealthService = "grpc.health.v1.Health"
- func init() {
- channelz.TurnOn()
- }
- type s struct {
- grpctest.Tester
- }
- func Test(t *testing.T) {
- grpctest.RunSubTests(t, s{})
- }
- var (
- // For headers:
- testMetadata = metadata.MD{
- "key1": []string{"value1"},
- "key2": []string{"value2"},
- "key3-bin": []string{"binvalue1", string([]byte{1, 2, 3})},
- }
- testMetadata2 = metadata.MD{
- "key1": []string{"value12"},
- "key2": []string{"value22"},
- }
- // For trailers:
- testTrailerMetadata = metadata.MD{
- "tkey1": []string{"trailerValue1"},
- "tkey2": []string{"trailerValue2"},
- "tkey3-bin": []string{"trailerbinvalue1", string([]byte{3, 2, 1})},
- }
- testTrailerMetadata2 = metadata.MD{
- "tkey1": []string{"trailerValue12"},
- "tkey2": []string{"trailerValue22"},
- }
- // capital "Key" is illegal in HTTP/2.
- malformedHTTP2Metadata = metadata.MD{
- "Key": []string{"foo"},
- }
- testAppUA = "myApp1/1.0 myApp2/0.9"
- failAppUA = "fail-this-RPC"
- detailedError = status.ErrorProto(&spb.Status{
- Code: int32(codes.DataLoss),
- Message: "error for testing: " + failAppUA,
- Details: []*anypb.Any{{
- TypeUrl: "url",
- Value: []byte{6, 0, 0, 6, 1, 3},
- }},
- })
- )
- var raceMode bool // set by race.go in race mode
- type testServer struct {
- testpb.UnimplementedTestServiceServer
- security string // indicate the authentication protocol used by this server.
- earlyFail bool // whether to error out the execution of a service handler prematurely.
- setAndSendHeader bool // whether to call setHeader and sendHeader.
- setHeaderOnly bool // whether to only call setHeader, not sendHeader.
- multipleSetTrailer bool // whether to call setTrailer multiple times.
- unaryCallSleepTime time.Duration
- }
- func (s *testServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- if md, ok := metadata.FromIncomingContext(ctx); ok {
- // For testing purpose, returns an error if user-agent is failAppUA.
- // To test that client gets the correct error.
- if ua, ok := md["user-agent"]; !ok || strings.HasPrefix(ua[0], failAppUA) {
- return nil, detailedError
- }
- var str []string
- for _, entry := range md["user-agent"] {
- str = append(str, "ua", entry)
- }
- grpc.SendHeader(ctx, metadata.Pairs(str...))
- }
- return new(testpb.Empty), nil
- }
- func newPayload(t testpb.PayloadType, size int32) (*testpb.Payload, error) {
- if size < 0 {
- return nil, fmt.Errorf("requested a response with invalid length %d", size)
- }
- body := make([]byte, size)
- switch t {
- case testpb.PayloadType_COMPRESSABLE:
- case testpb.PayloadType_UNCOMPRESSABLE:
- return nil, fmt.Errorf("PayloadType UNCOMPRESSABLE is not supported")
- default:
- return nil, fmt.Errorf("unsupported payload type: %d", t)
- }
- return &testpb.Payload{
- Type: t,
- Body: body,
- }, nil
- }
- func (s *testServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- md, ok := metadata.FromIncomingContext(ctx)
- if ok {
- if _, exists := md[":authority"]; !exists {
- return nil, status.Errorf(codes.DataLoss, "expected an :authority metadata: %v", md)
- }
- if s.setAndSendHeader {
- if err := grpc.SetHeader(ctx, md); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SetHeader(_, %v) = %v, want <nil>", md, err)
- }
- if err := grpc.SendHeader(ctx, testMetadata2); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SendHeader(_, %v) = %v, want <nil>", testMetadata2, err)
- }
- } else if s.setHeaderOnly {
- if err := grpc.SetHeader(ctx, md); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SetHeader(_, %v) = %v, want <nil>", md, err)
- }
- if err := grpc.SetHeader(ctx, testMetadata2); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SetHeader(_, %v) = %v, want <nil>", testMetadata2, err)
- }
- } else {
- if err := grpc.SendHeader(ctx, md); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SendHeader(_, %v) = %v, want <nil>", md, err)
- }
- }
- if err := grpc.SetTrailer(ctx, testTrailerMetadata); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SetTrailer(_, %v) = %v, want <nil>", testTrailerMetadata, err)
- }
- if s.multipleSetTrailer {
- if err := grpc.SetTrailer(ctx, testTrailerMetadata2); err != nil {
- return nil, status.Errorf(status.Code(err), "grpc.SetTrailer(_, %v) = %v, want <nil>", testTrailerMetadata2, err)
- }
- }
- }
- pr, ok := peer.FromContext(ctx)
- if !ok {
- return nil, status.Error(codes.DataLoss, "failed to get peer from ctx")
- }
- if pr.Addr == net.Addr(nil) {
- return nil, status.Error(codes.DataLoss, "failed to get peer address")
- }
- if s.security != "" {
- // Check Auth info
- var authType, serverName string
- switch info := pr.AuthInfo.(type) {
- case credentials.TLSInfo:
- authType = info.AuthType()
- serverName = info.State.ServerName
- default:
- return nil, status.Error(codes.Unauthenticated, "Unknown AuthInfo type")
- }
- if authType != s.security {
- return nil, status.Errorf(codes.Unauthenticated, "Wrong auth type: got %q, want %q", authType, s.security)
- }
- if serverName != "x.test.youtube.com" {
- return nil, status.Errorf(codes.Unauthenticated, "Unknown server name %q", serverName)
- }
- }
- // Simulate some service delay.
- time.Sleep(s.unaryCallSleepTime)
- payload, err := newPayload(in.GetResponseType(), in.GetResponseSize())
- if err != nil {
- return nil, err
- }
- return &testpb.SimpleResponse{
- Payload: payload,
- }, nil
- }
- func (s *testServer) StreamingOutputCall(args *testpb.StreamingOutputCallRequest, stream testpb.TestService_StreamingOutputCallServer) error {
- if md, ok := metadata.FromIncomingContext(stream.Context()); ok {
- if _, exists := md[":authority"]; !exists {
- return status.Errorf(codes.DataLoss, "expected an :authority metadata: %v", md)
- }
- // For testing purpose, returns an error if user-agent is failAppUA.
- // To test that client gets the correct error.
- if ua, ok := md["user-agent"]; !ok || strings.HasPrefix(ua[0], failAppUA) {
- return status.Error(codes.DataLoss, "error for testing: "+failAppUA)
- }
- }
- cs := args.GetResponseParameters()
- for _, c := range cs {
- if us := c.GetIntervalUs(); us > 0 {
- time.Sleep(time.Duration(us) * time.Microsecond)
- }
- payload, err := newPayload(args.GetResponseType(), c.GetSize())
- if err != nil {
- return err
- }
- if err := stream.Send(&testpb.StreamingOutputCallResponse{
- Payload: payload,
- }); err != nil {
- return err
- }
- }
- return nil
- }
- func (s *testServer) StreamingInputCall(stream testpb.TestService_StreamingInputCallServer) error {
- var sum int
- for {
- in, err := stream.Recv()
- if err == io.EOF {
- return stream.SendAndClose(&testpb.StreamingInputCallResponse{
- AggregatedPayloadSize: int32(sum),
- })
- }
- if err != nil {
- return err
- }
- p := in.GetPayload().GetBody()
- sum += len(p)
- if s.earlyFail {
- return status.Error(codes.NotFound, "not found")
- }
- }
- }
- func (s *testServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServer) error {
- md, ok := metadata.FromIncomingContext(stream.Context())
- if ok {
- if s.setAndSendHeader {
- if err := stream.SetHeader(md); err != nil {
- return status.Errorf(status.Code(err), "%v.SetHeader(_, %v) = %v, want <nil>", stream, md, err)
- }
- if err := stream.SendHeader(testMetadata2); err != nil {
- return status.Errorf(status.Code(err), "%v.SendHeader(_, %v) = %v, want <nil>", stream, testMetadata2, err)
- }
- } else if s.setHeaderOnly {
- if err := stream.SetHeader(md); err != nil {
- return status.Errorf(status.Code(err), "%v.SetHeader(_, %v) = %v, want <nil>", stream, md, err)
- }
- if err := stream.SetHeader(testMetadata2); err != nil {
- return status.Errorf(status.Code(err), "%v.SetHeader(_, %v) = %v, want <nil>", stream, testMetadata2, err)
- }
- } else {
- if err := stream.SendHeader(md); err != nil {
- return status.Errorf(status.Code(err), "%v.SendHeader(%v) = %v, want %v", stream, md, err, nil)
- }
- }
- stream.SetTrailer(testTrailerMetadata)
- if s.multipleSetTrailer {
- stream.SetTrailer(testTrailerMetadata2)
- }
- }
- for {
- in, err := stream.Recv()
- if err == io.EOF {
- // read done.
- return nil
- }
- if err != nil {
- // to facilitate testSvrWriteStatusEarlyWrite
- if status.Code(err) == codes.ResourceExhausted {
- return status.Errorf(codes.Internal, "fake error for test testSvrWriteStatusEarlyWrite. true error: %s", err.Error())
- }
- return err
- }
- cs := in.GetResponseParameters()
- for _, c := range cs {
- if us := c.GetIntervalUs(); us > 0 {
- time.Sleep(time.Duration(us) * time.Microsecond)
- }
- payload, err := newPayload(in.GetResponseType(), c.GetSize())
- if err != nil {
- return err
- }
- if err := stream.Send(&testpb.StreamingOutputCallResponse{
- Payload: payload,
- }); err != nil {
- // to facilitate testSvrWriteStatusEarlyWrite
- if status.Code(err) == codes.ResourceExhausted {
- return status.Errorf(codes.Internal, "fake error for test testSvrWriteStatusEarlyWrite. true error: %s", err.Error())
- }
- return err
- }
- }
- }
- }
- func (s *testServer) HalfDuplexCall(stream testpb.TestService_HalfDuplexCallServer) error {
- var msgBuf []*testpb.StreamingOutputCallRequest
- for {
- in, err := stream.Recv()
- if err == io.EOF {
- // read done.
- break
- }
- if err != nil {
- return err
- }
- msgBuf = append(msgBuf, in)
- }
- for _, m := range msgBuf {
- cs := m.GetResponseParameters()
- for _, c := range cs {
- if us := c.GetIntervalUs(); us > 0 {
- time.Sleep(time.Duration(us) * time.Microsecond)
- }
- payload, err := newPayload(m.GetResponseType(), c.GetSize())
- if err != nil {
- return err
- }
- if err := stream.Send(&testpb.StreamingOutputCallResponse{
- Payload: payload,
- }); err != nil {
- return err
- }
- }
- }
- return nil
- }
- type env struct {
- name string
- network string // The type of network such as tcp, unix, etc.
- security string // The security protocol such as TLS, SSH, etc.
- httpHandler bool // whether to use the http.Handler ServerTransport; requires TLS
- balancer string // One of "round_robin", "pick_first", or "".
- customDialer func(string, string, time.Duration) (net.Conn, error)
- }
- func (e env) runnable() bool {
- if runtime.GOOS == "windows" && e.network == "unix" {
- return false
- }
- return true
- }
- func (e env) dialer(addr string, timeout time.Duration) (net.Conn, error) {
- if e.customDialer != nil {
- return e.customDialer(e.network, addr, timeout)
- }
- return net.DialTimeout(e.network, addr, timeout)
- }
- var (
- tcpClearEnv = env{name: "tcp-clear-v1-balancer", network: "tcp"}
- tcpTLSEnv = env{name: "tcp-tls-v1-balancer", network: "tcp", security: "tls"}
- tcpClearRREnv = env{name: "tcp-clear", network: "tcp", balancer: "round_robin"}
- tcpTLSRREnv = env{name: "tcp-tls", network: "tcp", security: "tls", balancer: "round_robin"}
- handlerEnv = env{name: "handler-tls", network: "tcp", security: "tls", httpHandler: true, balancer: "round_robin"}
- noBalancerEnv = env{name: "no-balancer", network: "tcp", security: "tls"}
- allEnv = []env{tcpClearEnv, tcpTLSEnv, tcpClearRREnv, tcpTLSRREnv, handlerEnv, noBalancerEnv}
- )
- var onlyEnv = flag.String("only_env", "", "If non-empty, one of 'tcp-clear', 'tcp-tls', 'unix-clear', 'unix-tls', or 'handler-tls' to only run the tests for that environment. Empty means all.")
- func listTestEnv() (envs []env) {
- if *onlyEnv != "" {
- for _, e := range allEnv {
- if e.name == *onlyEnv {
- if !e.runnable() {
- panic(fmt.Sprintf("--only_env environment %q does not run on %s", *onlyEnv, runtime.GOOS))
- }
- return []env{e}
- }
- }
- panic(fmt.Sprintf("invalid --only_env value %q", *onlyEnv))
- }
- for _, e := range allEnv {
- if e.runnable() {
- envs = append(envs, e)
- }
- }
- return envs
- }
- // test is an end-to-end test. It should be created with the newTest
- // func, modified as needed, and then started with its startServer method.
- // It should be cleaned up with the tearDown method.
- type test struct {
- // The following are setup in newTest().
- t *testing.T
- e env
- ctx context.Context // valid for life of test, before tearDown
- cancel context.CancelFunc
- // The following knobs are for the server-side, and should be set after
- // calling newTest() and before calling startServer().
- // whether or not to expose the server's health via the default health
- // service implementation.
- enableHealthServer bool
- // In almost all cases, one should set the 'enableHealthServer' flag above to
- // expose the server's health using the default health service
- // implementation. This should only be used when a non-default health service
- // implementation is required.
- healthServer healthpb.HealthServer
- maxStream uint32
- tapHandle tap.ServerInHandle
- maxServerMsgSize *int
- maxServerReceiveMsgSize *int
- maxServerSendMsgSize *int
- maxServerHeaderListSize *uint32
- // Used to test the deprecated API WithCompressor and WithDecompressor.
- serverCompression bool
- unknownHandler grpc.StreamHandler
- unaryServerInt grpc.UnaryServerInterceptor
- streamServerInt grpc.StreamServerInterceptor
- serverInitialWindowSize int32
- serverInitialConnWindowSize int32
- customServerOptions []grpc.ServerOption
- // The following knobs are for the client-side, and should be set after
- // calling newTest() and before calling clientConn().
- maxClientMsgSize *int
- maxClientReceiveMsgSize *int
- maxClientSendMsgSize *int
- maxClientHeaderListSize *uint32
- userAgent string
- // Used to test the deprecated API WithCompressor and WithDecompressor.
- clientCompression bool
- // Used to test the new compressor registration API UseCompressor.
- clientUseCompression bool
- // clientNopCompression is set to create a compressor whose type is not supported.
- clientNopCompression bool
- unaryClientInt grpc.UnaryClientInterceptor
- streamClientInt grpc.StreamClientInterceptor
- sc <-chan grpc.ServiceConfig
- customCodec encoding.Codec
- clientInitialWindowSize int32
- clientInitialConnWindowSize int32
- perRPCCreds credentials.PerRPCCredentials
- customDialOptions []grpc.DialOption
- resolverScheme string
- // All test dialing is blocking by default. Set this to true if dial
- // should be non-blocking.
- nonBlockingDial bool
- // These are are set once startServer is called. The common case is to have
- // only one testServer.
- srv stopper
- hSrv healthpb.HealthServer
- srvAddr string
- // These are are set once startServers is called.
- srvs []stopper
- hSrvs []healthpb.HealthServer
- srvAddrs []string
- cc *grpc.ClientConn // nil until requested via clientConn
- restoreLogs func() // nil unless declareLogNoise is used
- }
- type stopper interface {
- Stop()
- GracefulStop()
- }
- func (te *test) tearDown() {
- if te.cancel != nil {
- te.cancel()
- te.cancel = nil
- }
- if te.cc != nil {
- te.cc.Close()
- te.cc = nil
- }
- if te.restoreLogs != nil {
- te.restoreLogs()
- te.restoreLogs = nil
- }
- if te.srv != nil {
- te.srv.Stop()
- }
- for _, s := range te.srvs {
- s.Stop()
- }
- }
- // newTest returns a new test using the provided testing.T and
- // environment. It is returned with default values. Tests should
- // modify it before calling its startServer and clientConn methods.
- func newTest(t *testing.T, e env) *test {
- te := &test{
- t: t,
- e: e,
- maxStream: math.MaxUint32,
- }
- te.ctx, te.cancel = context.WithCancel(context.Background())
- return te
- }
- func (te *test) listenAndServe(ts testpb.TestServiceServer, listen func(network, address string) (net.Listener, error)) net.Listener {
- te.t.Helper()
- te.t.Logf("Running test in %s environment...", te.e.name)
- sopts := []grpc.ServerOption{grpc.MaxConcurrentStreams(te.maxStream)}
- if te.maxServerMsgSize != nil {
- sopts = append(sopts, grpc.MaxMsgSize(*te.maxServerMsgSize))
- }
- if te.maxServerReceiveMsgSize != nil {
- sopts = append(sopts, grpc.MaxRecvMsgSize(*te.maxServerReceiveMsgSize))
- }
- if te.maxServerSendMsgSize != nil {
- sopts = append(sopts, grpc.MaxSendMsgSize(*te.maxServerSendMsgSize))
- }
- if te.maxServerHeaderListSize != nil {
- sopts = append(sopts, grpc.MaxHeaderListSize(*te.maxServerHeaderListSize))
- }
- if te.tapHandle != nil {
- sopts = append(sopts, grpc.InTapHandle(te.tapHandle))
- }
- if te.serverCompression {
- sopts = append(sopts,
- grpc.RPCCompressor(grpc.NewGZIPCompressor()),
- grpc.RPCDecompressor(grpc.NewGZIPDecompressor()),
- )
- }
- if te.unaryServerInt != nil {
- sopts = append(sopts, grpc.UnaryInterceptor(te.unaryServerInt))
- }
- if te.streamServerInt != nil {
- sopts = append(sopts, grpc.StreamInterceptor(te.streamServerInt))
- }
- if te.unknownHandler != nil {
- sopts = append(sopts, grpc.UnknownServiceHandler(te.unknownHandler))
- }
- if te.serverInitialWindowSize > 0 {
- sopts = append(sopts, grpc.InitialWindowSize(te.serverInitialWindowSize))
- }
- if te.serverInitialConnWindowSize > 0 {
- sopts = append(sopts, grpc.InitialConnWindowSize(te.serverInitialConnWindowSize))
- }
- la := "localhost:0"
- switch te.e.network {
- case "unix":
- la = "/tmp/testsock" + fmt.Sprintf("%d", time.Now().UnixNano())
- syscall.Unlink(la)
- }
- lis, err := listen(te.e.network, la)
- if err != nil {
- te.t.Fatalf("Failed to listen: %v", err)
- }
- if te.e.security == "tls" {
- creds, err := credentials.NewServerTLSFromFile(testdata.Path("server1.pem"), testdata.Path("server1.key"))
- if err != nil {
- te.t.Fatalf("Failed to generate credentials %v", err)
- }
- sopts = append(sopts, grpc.Creds(creds))
- }
- sopts = append(sopts, te.customServerOptions...)
- s := grpc.NewServer(sopts...)
- if ts != nil {
- testpb.RegisterTestServiceServer(s, ts)
- }
- // Create a new default health server if enableHealthServer is set, or use
- // the provided one.
- hs := te.healthServer
- if te.enableHealthServer {
- hs = health.NewServer()
- }
- if hs != nil {
- healthgrpc.RegisterHealthServer(s, hs)
- }
- addr := la
- switch te.e.network {
- case "unix":
- default:
- _, port, err := net.SplitHostPort(lis.Addr().String())
- if err != nil {
- te.t.Fatalf("Failed to parse listener address: %v", err)
- }
- addr = "localhost:" + port
- }
- te.srv = s
- te.hSrv = hs
- te.srvAddr = addr
- if te.e.httpHandler {
- if te.e.security != "tls" {
- te.t.Fatalf("unsupported environment settings")
- }
- cert, err := tls.LoadX509KeyPair(testdata.Path("server1.pem"), testdata.Path("server1.key"))
- if err != nil {
- te.t.Fatal("tls.LoadX509KeyPair(server1.pem, server1.key) failed: ", err)
- }
- hs := &http.Server{
- Handler: s,
- TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}},
- }
- if err := http2.ConfigureServer(hs, &http2.Server{MaxConcurrentStreams: te.maxStream}); err != nil {
- te.t.Fatal("http2.ConfigureServer(_, _) failed: ", err)
- }
- te.srv = wrapHS{hs}
- tlsListener := tls.NewListener(lis, hs.TLSConfig)
- go hs.Serve(tlsListener)
- return lis
- }
- go s.Serve(lis)
- return lis
- }
- type wrapHS struct {
- s *http.Server
- }
- func (w wrapHS) GracefulStop() {
- w.s.Shutdown(context.Background())
- }
- func (w wrapHS) Stop() {
- w.s.Close()
- }
- func (te *test) startServerWithConnControl(ts testpb.TestServiceServer) *listenerWrapper {
- l := te.listenAndServe(ts, listenWithConnControl)
- return l.(*listenerWrapper)
- }
- // startServer starts a gRPC server exposing the provided TestService
- // implementation. Callers should defer a call to te.tearDown to clean up
- func (te *test) startServer(ts testpb.TestServiceServer) {
- te.t.Helper()
- te.listenAndServe(ts, net.Listen)
- }
- // startServers starts 'num' gRPC servers exposing the provided TestService.
- func (te *test) startServers(ts testpb.TestServiceServer, num int) {
- for i := 0; i < num; i++ {
- te.startServer(ts)
- te.srvs = append(te.srvs, te.srv.(*grpc.Server))
- te.hSrvs = append(te.hSrvs, te.hSrv)
- te.srvAddrs = append(te.srvAddrs, te.srvAddr)
- te.srv = nil
- te.hSrv = nil
- te.srvAddr = ""
- }
- }
- // setHealthServingStatus is a helper function to set the health status.
- func (te *test) setHealthServingStatus(service string, status healthpb.HealthCheckResponse_ServingStatus) {
- hs, ok := te.hSrv.(*health.Server)
- if !ok {
- panic(fmt.Sprintf("SetServingStatus(%v, %v) called for health server of type %T", service, status, hs))
- }
- hs.SetServingStatus(service, status)
- }
- type nopCompressor struct {
- grpc.Compressor
- }
- // NewNopCompressor creates a compressor to test the case that type is not supported.
- func NewNopCompressor() grpc.Compressor {
- return &nopCompressor{grpc.NewGZIPCompressor()}
- }
- func (c *nopCompressor) Type() string {
- return "nop"
- }
- type nopDecompressor struct {
- grpc.Decompressor
- }
- // NewNopDecompressor creates a decompressor to test the case that type is not supported.
- func NewNopDecompressor() grpc.Decompressor {
- return &nopDecompressor{grpc.NewGZIPDecompressor()}
- }
- func (d *nopDecompressor) Type() string {
- return "nop"
- }
- func (te *test) configDial(opts ...grpc.DialOption) ([]grpc.DialOption, string) {
- opts = append(opts, grpc.WithDialer(te.e.dialer), grpc.WithUserAgent(te.userAgent))
- if te.sc != nil {
- opts = append(opts, grpc.WithServiceConfig(te.sc))
- }
- if te.clientCompression {
- opts = append(opts,
- grpc.WithCompressor(grpc.NewGZIPCompressor()),
- grpc.WithDecompressor(grpc.NewGZIPDecompressor()),
- )
- }
- if te.clientUseCompression {
- opts = append(opts, grpc.WithDefaultCallOptions(grpc.UseCompressor("gzip")))
- }
- if te.clientNopCompression {
- opts = append(opts,
- grpc.WithCompressor(NewNopCompressor()),
- grpc.WithDecompressor(NewNopDecompressor()),
- )
- }
- if te.unaryClientInt != nil {
- opts = append(opts, grpc.WithUnaryInterceptor(te.unaryClientInt))
- }
- if te.streamClientInt != nil {
- opts = append(opts, grpc.WithStreamInterceptor(te.streamClientInt))
- }
- if te.maxClientMsgSize != nil {
- opts = append(opts, grpc.WithMaxMsgSize(*te.maxClientMsgSize))
- }
- if te.maxClientReceiveMsgSize != nil {
- opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(*te.maxClientReceiveMsgSize)))
- }
- if te.maxClientSendMsgSize != nil {
- opts = append(opts, grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(*te.maxClientSendMsgSize)))
- }
- if te.maxClientHeaderListSize != nil {
- opts = append(opts, grpc.WithMaxHeaderListSize(*te.maxClientHeaderListSize))
- }
- switch te.e.security {
- case "tls":
- creds, err := credentials.NewClientTLSFromFile(testdata.Path("ca.pem"), "x.test.youtube.com")
- if err != nil {
- te.t.Fatalf("Failed to load credentials: %v", err)
- }
- opts = append(opts, grpc.WithTransportCredentials(creds))
- case "empty":
- // Don't add any transport creds option.
- default:
- opts = append(opts, grpc.WithInsecure())
- }
- // TODO(bar) switch balancer case "pick_first".
- var scheme string
- if te.resolverScheme == "" {
- scheme = "passthrough:///"
- } else {
- scheme = te.resolverScheme + ":///"
- }
- if te.e.balancer != "" {
- opts = append(opts, grpc.WithBalancerName(te.e.balancer))
- }
- if te.clientInitialWindowSize > 0 {
- opts = append(opts, grpc.WithInitialWindowSize(te.clientInitialWindowSize))
- }
- if te.clientInitialConnWindowSize > 0 {
- opts = append(opts, grpc.WithInitialConnWindowSize(te.clientInitialConnWindowSize))
- }
- if te.perRPCCreds != nil {
- opts = append(opts, grpc.WithPerRPCCredentials(te.perRPCCreds))
- }
- if te.customCodec != nil {
- opts = append(opts, grpc.WithDefaultCallOptions(grpc.ForceCodec(te.customCodec)))
- }
- if !te.nonBlockingDial && te.srvAddr != "" {
- // Only do a blocking dial if server is up.
- opts = append(opts, grpc.WithBlock())
- }
- if te.srvAddr == "" {
- te.srvAddr = "client.side.only.test"
- }
- opts = append(opts, te.customDialOptions...)
- return opts, scheme
- }
- func (te *test) clientConnWithConnControl() (*grpc.ClientConn, *dialerWrapper) {
- if te.cc != nil {
- return te.cc, nil
- }
- opts, scheme := te.configDial()
- dw := &dialerWrapper{}
- // overwrite the dialer before
- opts = append(opts, grpc.WithDialer(dw.dialer))
- var err error
- te.cc, err = grpc.Dial(scheme+te.srvAddr, opts...)
- if err != nil {
- te.t.Fatalf("Dial(%q) = %v", scheme+te.srvAddr, err)
- }
- return te.cc, dw
- }
- func (te *test) clientConn(opts ...grpc.DialOption) *grpc.ClientConn {
- if te.cc != nil {
- return te.cc
- }
- var scheme string
- opts, scheme = te.configDial(opts...)
- var err error
- te.cc, err = grpc.Dial(scheme+te.srvAddr, opts...)
- if err != nil {
- te.t.Fatalf("Dial(%q) = %v", scheme+te.srvAddr, err)
- }
- return te.cc
- }
- func (te *test) declareLogNoise(phrases ...string) {
- te.restoreLogs = declareLogNoise(te.t, phrases...)
- }
- func (te *test) withServerTester(fn func(st *serverTester)) {
- c, err := te.e.dialer(te.srvAddr, 10*time.Second)
- if err != nil {
- te.t.Fatal(err)
- }
- defer c.Close()
- if te.e.security == "tls" {
- c = tls.Client(c, &tls.Config{
- InsecureSkipVerify: true,
- NextProtos: []string{http2.NextProtoTLS},
- })
- }
- st := newServerTesterFromConn(te.t, c)
- st.greet()
- fn(st)
- }
- type lazyConn struct {
- net.Conn
- beLazy int32
- }
- func (l *lazyConn) Write(b []byte) (int, error) {
- if atomic.LoadInt32(&(l.beLazy)) == 1 {
- time.Sleep(time.Second)
- }
- return l.Conn.Write(b)
- }
- func (s) TestContextDeadlineNotIgnored(t *testing.T) {
- e := noBalancerEnv
- var lc *lazyConn
- e.customDialer = func(network, addr string, timeout time.Duration) (net.Conn, error) {
- conn, err := net.DialTimeout(network, addr, timeout)
- if err != nil {
- return nil, err
- }
- lc = &lazyConn{Conn: conn}
- return lc, nil
- }
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- atomic.StoreInt32(&(lc.beLazy), 1)
- ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
- defer cancel()
- t1 := time.Now()
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, context.DeadlineExceeded", err)
- }
- if time.Since(t1) > 2*time.Second {
- t.Fatalf("TestService/EmptyCall(_, _) ran over the deadline")
- }
- }
- func (s) TestTimeoutOnDeadServer(t *testing.T) {
- for _, e := range listTestEnv() {
- testTimeoutOnDeadServer(t, e)
- }
- }
- func testTimeoutOnDeadServer(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- te.srv.Stop()
- // Wait for the client to notice the connection is gone.
- ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
- state := cc.GetState()
- for ; state == connectivity.Ready && cc.WaitForStateChange(ctx, state); state = cc.GetState() {
- }
- cancel()
- if state == connectivity.Ready {
- t.Fatalf("Timed out waiting for non-ready state")
- }
- ctx, cancel = context.WithTimeout(context.Background(), time.Millisecond)
- _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true))
- cancel()
- if e.balancer != "" && status.Code(err) != codes.DeadlineExceeded {
- // If e.balancer == nil, the ac will stop reconnecting because the dialer returns non-temp error,
- // the error will be an internal error.
- t.Fatalf("TestService/EmptyCall(%v, _) = _, %v, want _, error code: %s", ctx, err, codes.DeadlineExceeded)
- }
- awaitNewConnLogOutput()
- }
- func (s) TestServerGracefulStopIdempotent(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testServerGracefulStopIdempotent(t, e)
- }
- }
- func testServerGracefulStopIdempotent(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- for i := 0; i < 3; i++ {
- te.srv.GracefulStop()
- }
- }
- func (s) TestServerGoAway(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testServerGoAway(t, e)
- }
- }
- func testServerGoAway(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- // Finish an RPC to make sure the connection is good.
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- ch := make(chan struct{})
- go func() {
- te.srv.GracefulStop()
- close(ch)
- }()
- // Loop until the server side GoAway signal is propagated to the client.
- for {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil && status.Code(err) != codes.DeadlineExceeded {
- cancel()
- break
- }
- cancel()
- }
- // A new RPC should fail.
- ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.Unavailable && status.Code(err) != codes.Internal {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s or %s", err, codes.Unavailable, codes.Internal)
- }
- <-ch
- awaitNewConnLogOutput()
- }
- func (s) TestServerGoAwayPendingRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testServerGoAwayPendingRPC(t, e)
- }
- }
- func testServerGoAwayPendingRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- stream, err := tc.FullDuplexCall(ctx, grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- // Finish an RPC to make sure the connection is good.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("%v.EmptyCall(_, _, _) = _, %v, want _, <nil>", tc, err)
- }
- ch := make(chan struct{})
- go func() {
- te.srv.GracefulStop()
- close(ch)
- }()
- // Loop until the server side GoAway signal is propagated to the client.
- start := time.Now()
- errored := false
- for time.Since(start) < time.Second {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
- _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true))
- cancel()
- if err != nil {
- errored = true
- break
- }
- }
- if !errored {
- t.Fatalf("GoAway never received by client")
- }
- respParam := []*testpb.ResponseParameters{{Size: 1}}
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(100))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- // The existing RPC should be still good to proceed.
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(_) = %v, want <nil>", stream, err)
- }
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = _, %v, want _, <nil>", stream, err)
- }
- // The RPC will run until canceled.
- cancel()
- <-ch
- awaitNewConnLogOutput()
- }
- func (s) TestServerMultipleGoAwayPendingRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testServerMultipleGoAwayPendingRPC(t, e)
- }
- }
- func testServerMultipleGoAwayPendingRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- stream, err := tc.FullDuplexCall(ctx, grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- // Finish an RPC to make sure the connection is good.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("%v.EmptyCall(_, _, _) = _, %v, want _, <nil>", tc, err)
- }
- ch1 := make(chan struct{})
- go func() {
- te.srv.GracefulStop()
- close(ch1)
- }()
- ch2 := make(chan struct{})
- go func() {
- te.srv.GracefulStop()
- close(ch2)
- }()
- // Loop until the server side GoAway signal is propagated to the client.
- for {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- cancel()
- break
- }
- cancel()
- }
- select {
- case <-ch1:
- t.Fatal("GracefulStop() terminated early")
- case <-ch2:
- t.Fatal("GracefulStop() terminated early")
- default:
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: 1,
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(100))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- // The existing RPC should be still good to proceed.
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = _, %v, want _, <nil>", stream, err)
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() = %v, want <nil>", stream, err)
- }
- <-ch1
- <-ch2
- cancel()
- awaitNewConnLogOutput()
- }
- func (s) TestConcurrentClientConnCloseAndServerGoAway(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testConcurrentClientConnCloseAndServerGoAway(t, e)
- }
- }
- func testConcurrentClientConnCloseAndServerGoAway(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("%v.EmptyCall(_, _, _) = _, %v, want _, <nil>", tc, err)
- }
- ch := make(chan struct{})
- // Close ClientConn and Server concurrently.
- go func() {
- te.srv.GracefulStop()
- close(ch)
- }()
- go func() {
- cc.Close()
- }()
- <-ch
- }
- func (s) TestConcurrentServerStopAndGoAway(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testConcurrentServerStopAndGoAway(t, e)
- }
- }
- func testConcurrentServerStopAndGoAway(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- stream, err := tc.FullDuplexCall(context.Background(), grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- // Finish an RPC to make sure the connection is good.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("%v.EmptyCall(_, _, _) = _, %v, want _, <nil>", tc, err)
- }
- ch := make(chan struct{})
- go func() {
- te.srv.GracefulStop()
- close(ch)
- }()
- // Loop until the server side GoAway signal is propagated to the client.
- for {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); err != nil {
- cancel()
- break
- }
- cancel()
- }
- // Stop the server and close all the connections.
- te.srv.Stop()
- respParam := []*testpb.ResponseParameters{
- {
- Size: 1,
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(100))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- sendStart := time.Now()
- for {
- if err := stream.Send(req); err == io.EOF {
- // stream.Send should eventually send io.EOF
- break
- } else if err != nil {
- // Send should never return a transport-level error.
- t.Fatalf("stream.Send(%v) = %v; want <nil or io.EOF>", req, err)
- }
- if time.Since(sendStart) > 2*time.Second {
- t.Fatalf("stream.Send(_) did not return io.EOF after 2s")
- }
- time.Sleep(time.Millisecond)
- }
- if _, err := stream.Recv(); err == nil || err == io.EOF {
- t.Fatalf("%v.Recv() = _, %v, want _, <non-nil, non-EOF>", stream, err)
- }
- <-ch
- awaitNewConnLogOutput()
- }
- func (s) TestClientConnCloseAfterGoAwayWithActiveStream(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testClientConnCloseAfterGoAwayWithActiveStream(t, e)
- }
- }
- func testClientConnCloseAfterGoAwayWithActiveStream(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- if _, err := tc.FullDuplexCall(ctx); err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want _, <nil>", tc, err)
- }
- done := make(chan struct{})
- go func() {
- te.srv.GracefulStop()
- close(done)
- }()
- time.Sleep(50 * time.Millisecond)
- cc.Close()
- timeout := time.NewTimer(time.Second)
- select {
- case <-done:
- case <-timeout.C:
- t.Fatalf("Test timed-out.")
- }
- }
- func (s) TestFailFast(t *testing.T) {
- for _, e := range listTestEnv() {
- testFailFast(t, e)
- }
- }
- func testFailFast(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- // Stop the server and tear down all the existing connections.
- te.srv.Stop()
- // Loop until the server teardown is propagated to the client.
- for {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- _, err := tc.EmptyCall(ctx, &testpb.Empty{})
- cancel()
- if status.Code(err) == codes.Unavailable {
- break
- }
- t.Logf("%v.EmptyCall(_, _) = _, %v", tc, err)
- time.Sleep(10 * time.Millisecond)
- }
- // The client keeps reconnecting and ongoing fail-fast RPCs should fail with code.Unavailable.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/EmptyCall(_, _, _) = _, %v, want _, error code: %s", err, codes.Unavailable)
- }
- if _, err := tc.StreamingInputCall(context.Background()); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/StreamingInputCall(_) = _, %v, want _, error code: %s", err, codes.Unavailable)
- }
- awaitNewConnLogOutput()
- }
- func testServiceConfigSetup(t *testing.T, e env) *test {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- "Failed to dial : context canceled; please retry.",
- )
- return te
- }
- func newBool(b bool) (a *bool) {
- return &b
- }
- func newInt(b int) (a *int) {
- return &b
- }
- func newDuration(b time.Duration) (a *time.Duration) {
- a = new(time.Duration)
- *a = b
- return
- }
- func (s) TestGetMethodConfig(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- defer te.tearDown()
- r := manual.NewBuilderWithScheme("whatever")
- te.resolverScheme = r.Scheme()
- cc := te.clientConn(grpc.WithResolvers(r))
- addrs := []resolver.Address{{Addr: te.srvAddr}}
- r.UpdateState(resolver.State{
- Addresses: addrs,
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "EmptyCall"
- }
- ],
- "waitForReady": true,
- "timeout": ".001s"
- },
- {
- "name": [
- {
- "service": "grpc.testing.TestService"
- }
- ],
- "waitForReady": false
- }
- ]
- }`)})
- tc := testpb.NewTestServiceClient(cc)
- // Make sure service config has been processed by grpc.
- for {
- if cc.GetMethodConfig("/grpc.testing.TestService/EmptyCall").WaitForReady != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // The following RPCs are expected to become non-fail-fast ones with 1ms deadline.
- var err error
- if _, err = tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- r.UpdateState(resolver.State{Addresses: addrs, ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "UnaryCall"
- }
- ],
- "waitForReady": true,
- "timeout": ".001s"
- },
- {
- "name": [
- {
- "service": "grpc.testing.TestService"
- }
- ],
- "waitForReady": false
- }
- ]
- }`)})
- // Make sure service config has been processed by grpc.
- for {
- if mc := cc.GetMethodConfig("/grpc.testing.TestService/EmptyCall"); mc.WaitForReady != nil && !*mc.WaitForReady {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // The following RPCs are expected to become fail-fast.
- if _, err = tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.Unavailable)
- }
- }
- func (s) TestServiceConfigWaitForReady(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- defer te.tearDown()
- r := manual.NewBuilderWithScheme("whatever")
- // Case1: Client API set failfast to be false, and service config set wait_for_ready to be false, Client API should win, and the rpc will wait until deadline exceeds.
- te.resolverScheme = r.Scheme()
- cc := te.clientConn(grpc.WithResolvers(r))
- addrs := []resolver.Address{{Addr: te.srvAddr}}
- r.UpdateState(resolver.State{
- Addresses: addrs,
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "EmptyCall"
- },
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "waitForReady": false,
- "timeout": ".001s"
- }
- ]
- }`)})
- tc := testpb.NewTestServiceClient(cc)
- // Make sure service config has been processed by grpc.
- for {
- if cc.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall").WaitForReady != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // The following RPCs are expected to become non-fail-fast ones with 1ms deadline.
- var err error
- if _, err = tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- if _, err := tc.FullDuplexCall(context.Background(), grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- // Generate a service config update.
- // Case2:Client API set failfast to be false, and service config set wait_for_ready to be true, and the rpc will wait until deadline exceeds.
- r.UpdateState(resolver.State{
- Addresses: addrs,
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "EmptyCall"
- },
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "waitForReady": true,
- "timeout": ".001s"
- }
- ]
- }`)})
- // Wait for the new service config to take effect.
- for {
- if mc := cc.GetMethodConfig("/grpc.testing.TestService/EmptyCall"); mc.WaitForReady != nil && *mc.WaitForReady {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // The following RPCs are expected to become non-fail-fast ones with 1ms deadline.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- if _, err := tc.FullDuplexCall(context.Background()); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- }
- func (s) TestServiceConfigTimeout(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- defer te.tearDown()
- r := manual.NewBuilderWithScheme("whatever")
- // Case1: Client API sets timeout to be 1ns and ServiceConfig sets timeout to be 1hr. Timeout should be 1ns (min of 1ns and 1hr) and the rpc will wait until deadline exceeds.
- te.resolverScheme = r.Scheme()
- cc := te.clientConn(grpc.WithResolvers(r))
- addrs := []resolver.Address{{Addr: te.srvAddr}}
- r.UpdateState(resolver.State{
- Addresses: addrs,
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "EmptyCall"
- },
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "waitForReady": true,
- "timeout": "3600s"
- }
- ]
- }`)})
- tc := testpb.NewTestServiceClient(cc)
- // Make sure service config has been processed by grpc.
- for {
- if cc.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall").Timeout != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // The following RPCs are expected to become non-fail-fast ones with 1ns deadline.
- var err error
- ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
- if _, err = tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- cancel()
- ctx, cancel = context.WithTimeout(context.Background(), time.Nanosecond)
- if _, err = tc.FullDuplexCall(ctx, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- cancel()
- // Generate a service config update.
- // Case2: Client API sets timeout to be 1hr and ServiceConfig sets timeout to be 1ns. Timeout should be 1ns (min of 1ns and 1hr) and the rpc will wait until deadline exceeds.
- r.UpdateState(resolver.State{
- Addresses: addrs,
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "EmptyCall"
- },
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "waitForReady": true,
- "timeout": ".000000001s"
- }
- ]
- }`)})
- // Wait for the new service config to take effect.
- for {
- if mc := cc.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall"); mc.Timeout != nil && *mc.Timeout == time.Nanosecond {
- break
- }
- time.Sleep(time.Millisecond)
- }
- ctx, cancel = context.WithTimeout(context.Background(), time.Hour)
- if _, err = tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- cancel()
- ctx, cancel = context.WithTimeout(context.Background(), time.Hour)
- if _, err = tc.FullDuplexCall(ctx, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- cancel()
- }
- func (s) TestServiceConfigMaxMsgSize(t *testing.T) {
- e := tcpClearRREnv
- r := manual.NewBuilderWithScheme("whatever")
- // Setting up values and objects shared across all test cases.
- const smallSize = 1
- const largeSize = 1024
- const extraLargeSize = 2048
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- largePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, largeSize)
- if err != nil {
- t.Fatal(err)
- }
- extraLargePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, extraLargeSize)
- if err != nil {
- t.Fatal(err)
- }
- // Case1: sc set maxReqSize to 2048 (send), maxRespSize to 2048 (recv).
- te1 := testServiceConfigSetup(t, e)
- defer te1.tearDown()
- te1.resolverScheme = r.Scheme()
- te1.nonBlockingDial = true
- te1.startServer(&testServer{security: e.security})
- cc1 := te1.clientConn(grpc.WithResolvers(r))
- addrs := []resolver.Address{{Addr: te1.srvAddr}}
- sc := parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "UnaryCall"
- },
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "maxRequestMessageBytes": 2048,
- "maxResponseMessageBytes": 2048
- }
- ]
- }`)
- r.UpdateState(resolver.State{Addresses: addrs, ServiceConfig: sc})
- tc := testpb.NewTestServiceClient(cc1)
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: int32(extraLargeSize),
- Payload: smallPayload,
- }
- for {
- if cc1.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall").MaxReqSize != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // Test for unary RPC recv.
- if _, err = tc.UnaryCall(context.Background(), req, grpc.WaitForReady(true)); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = extraLargePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(extraLargeSize),
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: smallPayload,
- }
- stream, err := tc.FullDuplexCall(te1.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err = stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err = stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = extraLargePayload
- stream, err = tc.FullDuplexCall(te1.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err = stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- // Case2: Client API set maxReqSize to 1024 (send), maxRespSize to 1024 (recv). Sc sets maxReqSize to 2048 (send), maxRespSize to 2048 (recv).
- te2 := testServiceConfigSetup(t, e)
- te2.resolverScheme = r.Scheme()
- te2.nonBlockingDial = true
- te2.maxClientReceiveMsgSize = newInt(1024)
- te2.maxClientSendMsgSize = newInt(1024)
- te2.startServer(&testServer{security: e.security})
- defer te2.tearDown()
- cc2 := te2.clientConn(grpc.WithResolvers(r))
- r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: te2.srvAddr}}, ServiceConfig: sc})
- tc = testpb.NewTestServiceClient(cc2)
- for {
- if cc2.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall").MaxReqSize != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // Test for unary RPC recv.
- req.Payload = smallPayload
- req.ResponseSize = int32(largeSize)
- if _, err = tc.UnaryCall(context.Background(), req, grpc.WaitForReady(true)); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = largePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- stream, err = tc.FullDuplexCall(te2.ctx)
- respParam[0].Size = int32(largeSize)
- sreq.Payload = smallPayload
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err = stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err = stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = largePayload
- stream, err = tc.FullDuplexCall(te2.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err = stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- // Case3: Client API set maxReqSize to 4096 (send), maxRespSize to 4096 (recv). Sc sets maxReqSize to 2048 (send), maxRespSize to 2048 (recv).
- te3 := testServiceConfigSetup(t, e)
- te3.resolverScheme = r.Scheme()
- te3.nonBlockingDial = true
- te3.maxClientReceiveMsgSize = newInt(4096)
- te3.maxClientSendMsgSize = newInt(4096)
- te3.startServer(&testServer{security: e.security})
- defer te3.tearDown()
- cc3 := te3.clientConn(grpc.WithResolvers(r))
- r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: te3.srvAddr}}, ServiceConfig: sc})
- tc = testpb.NewTestServiceClient(cc3)
- for {
- if cc3.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall").MaxReqSize != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- // Test for unary RPC recv.
- req.Payload = smallPayload
- req.ResponseSize = int32(largeSize)
- if _, err = tc.UnaryCall(context.Background(), req, grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want <nil>", err)
- }
- req.ResponseSize = int32(extraLargeSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = largePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want <nil>", err)
- }
- req.Payload = extraLargePayload
- if _, err = tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- stream, err = tc.FullDuplexCall(te3.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam[0].Size = int32(largeSize)
- sreq.Payload = smallPayload
- if err = stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err = stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = _, %v, want <nil>", stream, err)
- }
- respParam[0].Size = int32(extraLargeSize)
- if err = stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err = stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = largePayload
- stream, err = tc.FullDuplexCall(te3.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- sreq.Payload = extraLargePayload
- if err := stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- }
- // Reading from a streaming RPC may fail with context canceled if timeout was
- // set by service config (https://github.com/grpc/grpc-go/issues/1818). This
- // test makes sure read from streaming RPC doesn't fail in this case.
- func (s) TestStreamingRPCWithTimeoutInServiceConfigRecv(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- te.startServer(&testServer{security: tcpClearRREnv.security})
- defer te.tearDown()
- r := manual.NewBuilderWithScheme("whatever")
- te.resolverScheme = r.Scheme()
- te.nonBlockingDial = true
- cc := te.clientConn(grpc.WithResolvers(r))
- tc := testpb.NewTestServiceClient(cc)
- r.UpdateState(resolver.State{
- Addresses: []resolver.Address{{Addr: te.srvAddr}},
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "waitForReady": true,
- "timeout": "10s"
- }
- ]
- }`)})
- // Make sure service config has been processed by grpc.
- for {
- if cc.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall").Timeout != nil {
- break
- }
- time.Sleep(time.Millisecond)
- }
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- stream, err := tc.FullDuplexCall(ctx, grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want <nil>", err)
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 0)
- if err != nil {
- t.Fatalf("failed to newPayload: %v", err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: []*testpb.ResponseParameters{{Size: 0}},
- Payload: payload,
- }
- if err := stream.Send(req); err != nil {
- t.Fatalf("stream.Send(%v) = %v, want <nil>", req, err)
- }
- stream.CloseSend()
- time.Sleep(time.Second)
- // Sleep 1 second before recv to make sure the final status is received
- // before the recv.
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("stream.Recv = _, %v, want _, <nil>", err)
- }
- // Keep reading to drain the stream.
- for {
- if _, err := stream.Recv(); err != nil {
- break
- }
- }
- }
- func (s) TestPreloaderClientSend(t *testing.T) {
- for _, e := range listTestEnv() {
- testPreloaderClientSend(t, e)
- }
- }
- func testPreloaderClientSend(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- "Failed to dial : context canceled; please retry.",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- // Test for streaming RPC recv.
- // Set context for send with proper RPC Information
- stream, err := tc.FullDuplexCall(te.ctx, grpc.UseCompressor("gzip"))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- var index int
- for index < len(reqSizes) {
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(respSizes[index]),
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(reqSizes[index]))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- preparedMsg := &grpc.PreparedMsg{}
- err = preparedMsg.Encode(stream, req)
- if err != nil {
- t.Fatalf("PrepareMsg failed for size %d : %v", reqSizes[index], err)
- }
- if err := stream.SendMsg(preparedMsg); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- reply, err := stream.Recv()
- if err != nil {
- t.Fatalf("%v.Recv() = %v, want <nil>", stream, err)
- }
- pt := reply.GetPayload().GetType()
- if pt != testpb.PayloadType_COMPRESSABLE {
- t.Fatalf("Got the reply of type %d, want %d", pt, testpb.PayloadType_COMPRESSABLE)
- }
- size := len(reply.GetPayload().GetBody())
- if size != int(respSizes[index]) {
- t.Fatalf("Got reply body of length %d, want %d", size, respSizes[index])
- }
- index++
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v failed to complele the ping pong test: %v", stream, err)
- }
- }
- func (s) TestMaxMsgSizeClientDefault(t *testing.T) {
- for _, e := range listTestEnv() {
- testMaxMsgSizeClientDefault(t, e)
- }
- }
- func testMaxMsgSizeClientDefault(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- "Failed to dial : context canceled; please retry.",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const smallSize = 1
- const largeSize = 4 * 1024 * 1024
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: int32(largeSize),
- Payload: smallPayload,
- }
- // Test for unary RPC recv.
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(largeSize),
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: smallPayload,
- }
- // Test for streaming RPC recv.
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- }
- func (s) TestMaxMsgSizeClientAPI(t *testing.T) {
- for _, e := range listTestEnv() {
- testMaxMsgSizeClientAPI(t, e)
- }
- }
- func testMaxMsgSizeClientAPI(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- // To avoid error on server side.
- te.maxServerSendMsgSize = newInt(5 * 1024 * 1024)
- te.maxClientReceiveMsgSize = newInt(1024)
- te.maxClientSendMsgSize = newInt(1024)
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- "Failed to dial : context canceled; please retry.",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const smallSize = 1
- const largeSize = 1024
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- largePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, largeSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: int32(largeSize),
- Payload: smallPayload,
- }
- // Test for unary RPC recv.
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = largePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(largeSize),
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: smallPayload,
- }
- // Test for streaming RPC recv.
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = largePayload
- stream, err = tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- }
- func (s) TestMaxMsgSizeServerAPI(t *testing.T) {
- for _, e := range listTestEnv() {
- testMaxMsgSizeServerAPI(t, e)
- }
- }
- func testMaxMsgSizeServerAPI(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.maxServerReceiveMsgSize = newInt(1024)
- te.maxServerSendMsgSize = newInt(1024)
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- "Failed to dial : context canceled; please retry.",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const smallSize = 1
- const largeSize = 1024
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- largePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, largeSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: int32(largeSize),
- Payload: smallPayload,
- }
- // Test for unary RPC send.
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC recv.
- req.Payload = largePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(largeSize),
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: smallPayload,
- }
- // Test for streaming RPC send.
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = largePayload
- stream, err = tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- }
- func (s) TestTap(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testTap(t, e)
- }
- }
- type myTap struct {
- cnt int
- }
- func (t *myTap) handle(ctx context.Context, info *tap.Info) (context.Context, error) {
- if info != nil {
- if info.FullMethodName == "/grpc.testing.TestService/EmptyCall" {
- t.cnt++
- } else if info.FullMethodName == "/grpc.testing.TestService/UnaryCall" {
- return nil, fmt.Errorf("tap error")
- }
- }
- return ctx, nil
- }
- func testTap(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- ttap := &myTap{}
- te.tapHandle = ttap.handle
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- )
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- if ttap.cnt != 1 {
- t.Fatalf("Get the count in ttap %d, want 1", ttap.cnt)
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 31)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: 45,
- Payload: payload,
- }
- if _, err := tc.UnaryCall(context.Background(), req); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, %s", err, codes.Unavailable)
- }
- }
- // healthCheck is a helper function to make a unary health check RPC and return
- // the response.
- func healthCheck(d time.Duration, cc *grpc.ClientConn, service string) (*healthpb.HealthCheckResponse, error) {
- ctx, cancel := context.WithTimeout(context.Background(), d)
- defer cancel()
- hc := healthgrpc.NewHealthClient(cc)
- return hc.Check(ctx, &healthpb.HealthCheckRequest{Service: service})
- }
- // verifyHealthCheckStatus is a helper function to verify that the current
- // health status of the service matches the one passed in 'wantStatus'.
- func verifyHealthCheckStatus(t *testing.T, d time.Duration, cc *grpc.ClientConn, service string, wantStatus healthpb.HealthCheckResponse_ServingStatus) {
- t.Helper()
- resp, err := healthCheck(d, cc, service)
- if err != nil {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, <nil>", err)
- }
- if resp.Status != wantStatus {
- t.Fatalf("Got the serving status %v, want %v", resp.Status, wantStatus)
- }
- }
- // verifyHealthCheckErrCode is a helper function to verify that a unary health
- // check RPC returns an error with a code set to 'wantCode'.
- func verifyHealthCheckErrCode(t *testing.T, d time.Duration, cc *grpc.ClientConn, service string, wantCode codes.Code) {
- t.Helper()
- if _, err := healthCheck(d, cc, service); status.Code(err) != wantCode {
- t.Fatalf("Health/Check() got errCode %v, want %v", status.Code(err), wantCode)
- }
- }
- // newHealthCheckStream is a helper function to start a health check streaming
- // RPC, and returns the stream.
- func newHealthCheckStream(t *testing.T, cc *grpc.ClientConn, service string) (healthgrpc.Health_WatchClient, context.CancelFunc) {
- t.Helper()
- ctx, cancel := context.WithCancel(context.Background())
- hc := healthgrpc.NewHealthClient(cc)
- stream, err := hc.Watch(ctx, &healthpb.HealthCheckRequest{Service: service})
- if err != nil {
- t.Fatalf("hc.Watch(_, %v) failed: %v", service, err)
- }
- return stream, cancel
- }
- // healthWatchChecker is a helper function to verify that the next health
- // status returned on the given stream matches the one passed in 'wantStatus'.
- func healthWatchChecker(t *testing.T, stream healthgrpc.Health_WatchClient, wantStatus healthpb.HealthCheckResponse_ServingStatus) {
- t.Helper()
- response, err := stream.Recv()
- if err != nil {
- t.Fatalf("stream.Recv() failed: %v", err)
- }
- if response.Status != wantStatus {
- t.Fatalf("got servingStatus %v, want %v", response.Status, wantStatus)
- }
- }
- // TestHealthCheckSuccess invokes the unary Check() RPC on the health server in
- // a successful case.
- func (s) TestHealthCheckSuccess(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthCheckSuccess(t, e)
- }
- }
- func testHealthCheckSuccess(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- defer te.tearDown()
- verifyHealthCheckErrCode(t, 1*time.Second, te.clientConn(), defaultHealthService, codes.OK)
- }
- // TestHealthCheckFailure invokes the unary Check() RPC on the health server
- // with an expired context and expects the RPC to fail.
- func (s) TestHealthCheckFailure(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthCheckFailure(t, e)
- }
- }
- func testHealthCheckFailure(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise(
- "Failed to dial ",
- "grpc: the client connection is closing; please retry",
- )
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- defer te.tearDown()
- verifyHealthCheckErrCode(t, 0*time.Second, te.clientConn(), defaultHealthService, codes.DeadlineExceeded)
- awaitNewConnLogOutput()
- }
- // TestHealthCheckOff makes a unary Check() RPC on the health server where the
- // health status of the defaultHealthService is not set, and therefore expects
- // an error code 'codes.NotFound'.
- func (s) TestHealthCheckOff(t *testing.T) {
- for _, e := range listTestEnv() {
- // TODO(bradfitz): Temporarily skip this env due to #619.
- if e.name == "handler-tls" {
- continue
- }
- testHealthCheckOff(t, e)
- }
- }
- func testHealthCheckOff(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- verifyHealthCheckErrCode(t, 1*time.Second, te.clientConn(), defaultHealthService, codes.NotFound)
- }
- // TestHealthWatchMultipleClients makes a streaming Watch() RPC on the health
- // server with multiple clients and expects the same status on both streams.
- func (s) TestHealthWatchMultipleClients(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthWatchMultipleClients(t, e)
- }
- }
- func testHealthWatchMultipleClients(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- stream1, cf1 := newHealthCheckStream(t, cc, defaultHealthService)
- defer cf1()
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- stream2, cf2 := newHealthCheckStream(t, cc, defaultHealthService)
- defer cf2()
- healthWatchChecker(t, stream2, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream2, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- // TestHealthWatchSameStatusmakes a streaming Watch() RPC on the health server
- // and makes sure that the health status of the server is as expected after
- // multiple calls to SetServingStatus with the same status.
- func (s) TestHealthWatchSameStatus(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthWatchSameStatus(t, e)
- }
- }
- func testHealthWatchSameStatus(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- stream, cf := newHealthCheckStream(t, te.clientConn(), defaultHealthService)
- defer cf()
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- // TestHealthWatchServiceStatusSetBeforeStartingServer starts a health server
- // on which the health status for the defaultService is set before the gRPC
- // server is started, and expects the correct health status to be returned.
- func (s) TestHealthWatchServiceStatusSetBeforeStartingServer(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthWatchSetServiceStatusBeforeStartingServer(t, e)
- }
- }
- func testHealthWatchSetServiceStatusBeforeStartingServer(t *testing.T, e env) {
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- hs.SetServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- stream, cf := newHealthCheckStream(t, te.clientConn(), defaultHealthService)
- defer cf()
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- }
- // TestHealthWatchDefaultStatusChange verifies the simple case where the
- // service starts off with a SERVICE_UNKNOWN status (because SetServingStatus
- // hasn't been called yet) and then moves to SERVING after SetServingStatus is
- // called.
- func (s) TestHealthWatchDefaultStatusChange(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthWatchDefaultStatusChange(t, e)
- }
- }
- func testHealthWatchDefaultStatusChange(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- stream, cf := newHealthCheckStream(t, te.clientConn(), defaultHealthService)
- defer cf()
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- }
- // TestHealthWatchSetServiceStatusBeforeClientCallsWatch verifies the case
- // where the health status is set to SERVING before the client calls Watch().
- func (s) TestHealthWatchSetServiceStatusBeforeClientCallsWatch(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthWatchSetServiceStatusBeforeClientCallsWatch(t, e)
- }
- }
- func testHealthWatchSetServiceStatusBeforeClientCallsWatch(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- defer te.tearDown()
- stream, cf := newHealthCheckStream(t, te.clientConn(), defaultHealthService)
- defer cf()
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- }
- // TestHealthWatchOverallServerHealthChange verifies setting the overall status
- // of the server by using the empty service name.
- func (s) TestHealthWatchOverallServerHealthChange(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthWatchOverallServerHealthChange(t, e)
- }
- }
- func testHealthWatchOverallServerHealthChange(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- stream, cf := newHealthCheckStream(t, te.clientConn(), "")
- defer cf()
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- te.setHealthServingStatus("", healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- // TestUnknownHandler verifies that an expected error is returned (by setting
- // the unknownHandler on the server) for a service which is not exposed to the
- // client.
- func (s) TestUnknownHandler(t *testing.T) {
- // An example unknownHandler that returns a different code and a different
- // method, making sure that we do not expose what methods are implemented to
- // a client that is not authenticated.
- unknownHandler := func(srv interface{}, stream grpc.ServerStream) error {
- return status.Error(codes.Unauthenticated, "user unauthenticated")
- }
- for _, e := range listTestEnv() {
- // TODO(bradfitz): Temporarily skip this env due to #619.
- if e.name == "handler-tls" {
- continue
- }
- testUnknownHandler(t, e, unknownHandler)
- }
- }
- func testUnknownHandler(t *testing.T, e env, unknownHandler grpc.StreamHandler) {
- te := newTest(t, e)
- te.unknownHandler = unknownHandler
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- verifyHealthCheckErrCode(t, 1*time.Second, te.clientConn(), "", codes.Unauthenticated)
- }
- // TestHealthCheckServingStatus makes a streaming Watch() RPC on the health
- // server and verifies a bunch of health status transitions.
- func (s) TestHealthCheckServingStatus(t *testing.T) {
- for _, e := range listTestEnv() {
- testHealthCheckServingStatus(t, e)
- }
- }
- func testHealthCheckServingStatus(t *testing.T, e env) {
- te := newTest(t, e)
- te.enableHealthServer = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- verifyHealthCheckStatus(t, 1*time.Second, cc, "", healthpb.HealthCheckResponse_SERVING)
- verifyHealthCheckErrCode(t, 1*time.Second, cc, defaultHealthService, codes.NotFound)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- verifyHealthCheckStatus(t, 1*time.Second, cc, defaultHealthService, healthpb.HealthCheckResponse_SERVING)
- te.setHealthServingStatus(defaultHealthService, healthpb.HealthCheckResponse_NOT_SERVING)
- verifyHealthCheckStatus(t, 1*time.Second, cc, defaultHealthService, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- func (s) TestEmptyUnaryWithUserAgent(t *testing.T) {
- for _, e := range listTestEnv() {
- testEmptyUnaryWithUserAgent(t, e)
- }
- }
- func testEmptyUnaryWithUserAgent(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- var header metadata.MD
- reply, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.Header(&header))
- if err != nil || !proto.Equal(&testpb.Empty{}, reply) {
- t.Fatalf("TestService/EmptyCall(_, _) = %v, %v, want %v, <nil>", reply, err, &testpb.Empty{})
- }
- if v, ok := header["ua"]; !ok || !strings.HasPrefix(v[0], testAppUA) {
- t.Fatalf("header[\"ua\"] = %q, %t, want string with prefix %q, true", v, ok, testAppUA)
- }
- te.srv.Stop()
- }
- func (s) TestFailedEmptyUnary(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- // This test covers status details, but
- // Grpc-Status-Details-Bin is not support in handler_server.
- continue
- }
- testFailedEmptyUnary(t, e)
- }
- }
- func testFailedEmptyUnary(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = failAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- wantErr := detailedError
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); !testutils.StatusErrEqual(err, wantErr) {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, wantErr)
- }
- }
- func (s) TestLargeUnary(t *testing.T) {
- for _, e := range listTestEnv() {
- testLargeUnary(t, e)
- }
- }
- func testLargeUnary(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const argSize = 271828
- const respSize = 314159
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- reply, err := tc.UnaryCall(context.Background(), req)
- if err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, <nil>", err)
- }
- pt := reply.GetPayload().GetType()
- ps := len(reply.GetPayload().GetBody())
- if pt != testpb.PayloadType_COMPRESSABLE || ps != respSize {
- t.Fatalf("Got the reply with type %d len %d; want %d, %d", pt, ps, testpb.PayloadType_COMPRESSABLE, respSize)
- }
- }
- // Test backward-compatibility API for setting msg size limit.
- func (s) TestExceedMsgLimit(t *testing.T) {
- for _, e := range listTestEnv() {
- testExceedMsgLimit(t, e)
- }
- }
- func testExceedMsgLimit(t *testing.T, e env) {
- te := newTest(t, e)
- maxMsgSize := 1024
- te.maxServerMsgSize, te.maxClientMsgSize = newInt(maxMsgSize), newInt(maxMsgSize)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- largeSize := int32(maxMsgSize + 1)
- const smallSize = 1
- largePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, largeSize)
- if err != nil {
- t.Fatal(err)
- }
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- // Make sure the server cannot receive a unary RPC of largeSize.
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: smallSize,
- Payload: largePayload,
- }
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Make sure the client cannot receive a unary RPC of largeSize.
- req.ResponseSize = largeSize
- req.Payload = smallPayload
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Make sure the server cannot receive a streaming RPC of largeSize.
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: 1,
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: largePayload,
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test on client side for streaming RPC.
- stream, err = tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam[0].Size = largeSize
- sreq.Payload = smallPayload
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- }
- func (s) TestPeerClientSide(t *testing.T) {
- for _, e := range listTestEnv() {
- testPeerClientSide(t, e)
- }
- }
- func testPeerClientSide(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- peer := new(peer.Peer)
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.Peer(peer), grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- pa := peer.Addr.String()
- if e.network == "unix" {
- if pa != te.srvAddr {
- t.Fatalf("peer.Addr = %v, want %v", pa, te.srvAddr)
- }
- return
- }
- _, pp, err := net.SplitHostPort(pa)
- if err != nil {
- t.Fatalf("Failed to parse address from peer.")
- }
- _, sp, err := net.SplitHostPort(te.srvAddr)
- if err != nil {
- t.Fatalf("Failed to parse address of test server.")
- }
- if pp != sp {
- t.Fatalf("peer.Addr = localhost:%v, want localhost:%v", pp, sp)
- }
- }
- // TestPeerNegative tests that if call fails setting peer
- // doesn't cause a segmentation fault.
- // issue#1141 https://github.com/grpc/grpc-go/issues/1141
- func (s) TestPeerNegative(t *testing.T) {
- for _, e := range listTestEnv() {
- testPeerNegative(t, e)
- }
- }
- func testPeerNegative(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- peer := new(peer.Peer)
- ctx, cancel := context.WithCancel(context.Background())
- cancel()
- tc.EmptyCall(ctx, &testpb.Empty{}, grpc.Peer(peer))
- }
- func (s) TestPeerFailedRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- testPeerFailedRPC(t, e)
- }
- }
- func testPeerFailedRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.maxServerReceiveMsgSize = newInt(1 * 1024)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- // first make a successful request to the server
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- // make a second request that will be rejected by the server
- const largeSize = 5 * 1024
- largePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, largeSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- Payload: largePayload,
- }
- peer := new(peer.Peer)
- if _, err := tc.UnaryCall(context.Background(), req, grpc.Peer(peer)); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- } else {
- pa := peer.Addr.String()
- if e.network == "unix" {
- if pa != te.srvAddr {
- t.Fatalf("peer.Addr = %v, want %v", pa, te.srvAddr)
- }
- return
- }
- _, pp, err := net.SplitHostPort(pa)
- if err != nil {
- t.Fatalf("Failed to parse address from peer.")
- }
- _, sp, err := net.SplitHostPort(te.srvAddr)
- if err != nil {
- t.Fatalf("Failed to parse address of test server.")
- }
- if pp != sp {
- t.Fatalf("peer.Addr = localhost:%v, want localhost:%v", pp, sp)
- }
- }
- }
- func (s) TestMetadataUnaryRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- testMetadataUnaryRPC(t, e)
- }
- }
- func testMetadataUnaryRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const argSize = 2718
- const respSize = 314
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- var header, trailer metadata.MD
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- if _, err := tc.UnaryCall(ctx, req, grpc.Header(&header), grpc.Trailer(&trailer)); err != nil {
- t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
- }
- // Ignore optional response headers that Servers may set:
- if header != nil {
- delete(header, "trailer") // RFC 2616 says server SHOULD (but optional) declare trailers
- delete(header, "date") // the Date header is also optional
- delete(header, "user-agent")
- delete(header, "content-type")
- }
- if !reflect.DeepEqual(header, testMetadata) {
- t.Fatalf("Received header metadata %v, want %v", header, testMetadata)
- }
- if !reflect.DeepEqual(trailer, testTrailerMetadata) {
- t.Fatalf("Received trailer metadata %v, want %v", trailer, testTrailerMetadata)
- }
- }
- func (s) TestMetadataOrderUnaryRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- testMetadataOrderUnaryRPC(t, e)
- }
- }
- func testMetadataOrderUnaryRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- ctx = metadata.AppendToOutgoingContext(ctx, "key1", "value2")
- ctx = metadata.AppendToOutgoingContext(ctx, "key1", "value3")
- // using Join to built expected metadata instead of FromOutgoingContext
- newMetadata := metadata.Join(testMetadata, metadata.Pairs("key1", "value2", "key1", "value3"))
- var header metadata.MD
- if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}, grpc.Header(&header)); err != nil {
- t.Fatal(err)
- }
- // Ignore optional response headers that Servers may set:
- if header != nil {
- delete(header, "trailer") // RFC 2616 says server SHOULD (but optional) declare trailers
- delete(header, "date") // the Date header is also optional
- delete(header, "user-agent")
- delete(header, "content-type")
- }
- if !reflect.DeepEqual(header, newMetadata) {
- t.Fatalf("Received header metadata %v, want %v", header, newMetadata)
- }
- }
- func (s) TestMultipleSetTrailerUnaryRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- testMultipleSetTrailerUnaryRPC(t, e)
- }
- }
- func testMultipleSetTrailerUnaryRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, multipleSetTrailer: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const (
- argSize = 1
- respSize = 1
- )
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- var trailer metadata.MD
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- if _, err := tc.UnaryCall(ctx, req, grpc.Trailer(&trailer), grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
- }
- expectedTrailer := metadata.Join(testTrailerMetadata, testTrailerMetadata2)
- if !reflect.DeepEqual(trailer, expectedTrailer) {
- t.Fatalf("Received trailer metadata %v, want %v", trailer, expectedTrailer)
- }
- }
- func (s) TestMultipleSetTrailerStreamingRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- testMultipleSetTrailerStreamingRPC(t, e)
- }
- }
- func testMultipleSetTrailerStreamingRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, multipleSetTrailer: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- stream, err := tc.FullDuplexCall(ctx, grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v failed to complele the FullDuplexCall: %v", stream, err)
- }
- trailer := stream.Trailer()
- expectedTrailer := metadata.Join(testTrailerMetadata, testTrailerMetadata2)
- if !reflect.DeepEqual(trailer, expectedTrailer) {
- t.Fatalf("Received trailer metadata %v, want %v", trailer, expectedTrailer)
- }
- }
- func (s) TestSetAndSendHeaderUnaryRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testSetAndSendHeaderUnaryRPC(t, e)
- }
- }
- // To test header metadata is sent on SendHeader().
- func testSetAndSendHeaderUnaryRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, setAndSendHeader: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const (
- argSize = 1
- respSize = 1
- )
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- var header metadata.MD
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- if _, err := tc.UnaryCall(ctx, req, grpc.Header(&header), grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
- }
- delete(header, "user-agent")
- delete(header, "content-type")
- expectedHeader := metadata.Join(testMetadata, testMetadata2)
- if !reflect.DeepEqual(header, expectedHeader) {
- t.Fatalf("Received header metadata %v, want %v", header, expectedHeader)
- }
- }
- func (s) TestMultipleSetHeaderUnaryRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testMultipleSetHeaderUnaryRPC(t, e)
- }
- }
- // To test header metadata is sent when sending response.
- func testMultipleSetHeaderUnaryRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, setHeaderOnly: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const (
- argSize = 1
- respSize = 1
- )
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- var header metadata.MD
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- if _, err := tc.UnaryCall(ctx, req, grpc.Header(&header), grpc.WaitForReady(true)); err != nil {
- t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <nil>", ctx, err)
- }
- delete(header, "user-agent")
- delete(header, "content-type")
- expectedHeader := metadata.Join(testMetadata, testMetadata2)
- if !reflect.DeepEqual(header, expectedHeader) {
- t.Fatalf("Received header metadata %v, want %v", header, expectedHeader)
- }
- }
- func (s) TestMultipleSetHeaderUnaryRPCError(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testMultipleSetHeaderUnaryRPCError(t, e)
- }
- }
- // To test header metadata is sent when sending status.
- func testMultipleSetHeaderUnaryRPCError(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, setHeaderOnly: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const (
- argSize = 1
- respSize = -1 // Invalid respSize to make RPC fail.
- )
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- var header metadata.MD
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- if _, err := tc.UnaryCall(ctx, req, grpc.Header(&header), grpc.WaitForReady(true)); err == nil {
- t.Fatalf("TestService.UnaryCall(%v, _, _, _) = _, %v; want _, <non-nil>", ctx, err)
- }
- delete(header, "user-agent")
- delete(header, "content-type")
- expectedHeader := metadata.Join(testMetadata, testMetadata2)
- if !reflect.DeepEqual(header, expectedHeader) {
- t.Fatalf("Received header metadata %v, want %v", header, expectedHeader)
- }
- }
- func (s) TestSetAndSendHeaderStreamingRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testSetAndSendHeaderStreamingRPC(t, e)
- }
- }
- // To test header metadata is sent on SendHeader().
- func testSetAndSendHeaderStreamingRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, setAndSendHeader: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v failed to complele the FullDuplexCall: %v", stream, err)
- }
- header, err := stream.Header()
- if err != nil {
- t.Fatalf("%v.Header() = _, %v, want _, <nil>", stream, err)
- }
- delete(header, "user-agent")
- delete(header, "content-type")
- expectedHeader := metadata.Join(testMetadata, testMetadata2)
- if !reflect.DeepEqual(header, expectedHeader) {
- t.Fatalf("Received header metadata %v, want %v", header, expectedHeader)
- }
- }
- func (s) TestMultipleSetHeaderStreamingRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testMultipleSetHeaderStreamingRPC(t, e)
- }
- }
- // To test header metadata is sent when sending response.
- func testMultipleSetHeaderStreamingRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, setHeaderOnly: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const (
- argSize = 1
- respSize = 1
- )
- ctx := metadata.NewOutgoingContext(context.Background(), testMetadata)
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: []*testpb.ResponseParameters{
- {Size: respSize},
- },
- Payload: payload,
- }
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = %v, want <nil>", stream, err)
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v failed to complele the FullDuplexCall: %v", stream, err)
- }
- header, err := stream.Header()
- if err != nil {
- t.Fatalf("%v.Header() = _, %v, want _, <nil>", stream, err)
- }
- delete(header, "user-agent")
- delete(header, "content-type")
- expectedHeader := metadata.Join(testMetadata, testMetadata2)
- if !reflect.DeepEqual(header, expectedHeader) {
- t.Fatalf("Received header metadata %v, want %v", header, expectedHeader)
- }
- }
- func (s) TestMultipleSetHeaderStreamingRPCError(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testMultipleSetHeaderStreamingRPCError(t, e)
- }
- }
- // To test header metadata is sent when sending status.
- func testMultipleSetHeaderStreamingRPCError(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, setHeaderOnly: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const (
- argSize = 1
- respSize = -1
- )
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- ctx = metadata.NewOutgoingContext(ctx, testMetadata)
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: []*testpb.ResponseParameters{
- {Size: respSize},
- },
- Payload: payload,
- }
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- if _, err := stream.Recv(); err == nil {
- t.Fatalf("%v.Recv() = %v, want <non-nil>", stream, err)
- }
- header, err := stream.Header()
- if err != nil {
- t.Fatalf("%v.Header() = _, %v, want _, <nil>", stream, err)
- }
- delete(header, "user-agent")
- delete(header, "content-type")
- expectedHeader := metadata.Join(testMetadata, testMetadata2)
- if !reflect.DeepEqual(header, expectedHeader) {
- t.Fatalf("Received header metadata %v, want %v", header, expectedHeader)
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
- }
- }
- // TestMalformedHTTP2Metadata verfies the returned error when the client
- // sends an illegal metadata.
- func (s) TestMalformedHTTP2Metadata(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- // Failed with "server stops accepting new RPCs".
- // Server stops accepting new RPCs when the client sends an illegal http2 header.
- continue
- }
- testMalformedHTTP2Metadata(t, e)
- }
- }
- func testMalformedHTTP2Metadata(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 2718)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: 314,
- Payload: payload,
- }
- ctx := metadata.NewOutgoingContext(context.Background(), malformedHTTP2Metadata)
- if _, err := tc.UnaryCall(ctx, req); status.Code(err) != codes.Internal {
- t.Fatalf("TestService.UnaryCall(%v, _) = _, %v; want _, %s", ctx, err, codes.Internal)
- }
- }
- func (s) TestTransparentRetry(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- // Fails with RST_STREAM / FLOW_CONTROL_ERROR
- continue
- }
- testTransparentRetry(t, e)
- }
- }
- // This test makes sure RPCs are retried times when they receive a RST_STREAM
- // with the REFUSED_STREAM error code, which the InTapHandle provokes.
- func testTransparentRetry(t *testing.T, e env) {
- te := newTest(t, e)
- attempts := 0
- successAttempt := 2
- te.tapHandle = func(ctx context.Context, _ *tap.Info) (context.Context, error) {
- attempts++
- if attempts < successAttempt {
- return nil, errors.New("not now")
- }
- return ctx, nil
- }
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tsc := testpb.NewTestServiceClient(cc)
- testCases := []struct {
- successAttempt int
- failFast bool
- errCode codes.Code
- }{{
- successAttempt: 1,
- }, {
- successAttempt: 2,
- }, {
- successAttempt: 3,
- errCode: codes.Unavailable,
- }, {
- successAttempt: 1,
- failFast: true,
- }, {
- successAttempt: 2,
- failFast: true,
- }, {
- successAttempt: 3,
- failFast: true,
- errCode: codes.Unavailable,
- }}
- for _, tc := range testCases {
- attempts = 0
- successAttempt = tc.successAttempt
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- _, err := tsc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(!tc.failFast))
- cancel()
- if status.Code(err) != tc.errCode {
- t.Errorf("%+v: tsc.EmptyCall(_, _) = _, %v, want _, Code=%v", tc, err, tc.errCode)
- }
- }
- }
- func (s) TestCancel(t *testing.T) {
- for _, e := range listTestEnv() {
- testCancel(t, e)
- }
- }
- func testCancel(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise("grpc: the client connection is closing; please retry")
- te.startServer(&testServer{security: e.security, unaryCallSleepTime: time.Second})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- const argSize = 2718
- const respSize = 314
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- ctx, cancel := context.WithCancel(context.Background())
- time.AfterFunc(1*time.Millisecond, cancel)
- if r, err := tc.UnaryCall(ctx, req); status.Code(err) != codes.Canceled {
- t.Fatalf("TestService/UnaryCall(_, _) = %v, %v; want _, error code: %s", r, err, codes.Canceled)
- }
- awaitNewConnLogOutput()
- }
- func (s) TestCancelNoIO(t *testing.T) {
- for _, e := range listTestEnv() {
- testCancelNoIO(t, e)
- }
- }
- func testCancelNoIO(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise("http2Client.notifyError got notified that the client transport was broken")
- te.maxStream = 1 // Only allows 1 live stream per server transport.
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- // Start one blocked RPC for which we'll never send streaming
- // input. This will consume the 1 maximum concurrent streams,
- // causing future RPCs to hang.
- ctx, cancelFirst := context.WithCancel(context.Background())
- _, err := tc.StreamingInputCall(ctx)
- if err != nil {
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want _, <nil>", tc, err)
- }
- // Loop until the ClientConn receives the initial settings
- // frame from the server, notifying it about the maximum
- // concurrent streams. We know when it's received it because
- // an RPC will fail with codes.DeadlineExceeded instead of
- // succeeding.
- // TODO(bradfitz): add internal test hook for this (Issue 534)
- for {
- ctx, cancelSecond := context.WithTimeout(context.Background(), 50*time.Millisecond)
- _, err := tc.StreamingInputCall(ctx)
- cancelSecond()
- if err == nil {
- continue
- }
- if status.Code(err) == codes.DeadlineExceeded {
- break
- }
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want _, %s", tc, err, codes.DeadlineExceeded)
- }
- // If there are any RPCs in flight before the client receives
- // the max streams setting, let them be expired.
- // TODO(bradfitz): add internal test hook for this (Issue 534)
- time.Sleep(50 * time.Millisecond)
- go func() {
- time.Sleep(50 * time.Millisecond)
- cancelFirst()
- }()
- // This should be blocked until the 1st is canceled, then succeed.
- ctx, cancelThird := context.WithTimeout(context.Background(), 500*time.Millisecond)
- if _, err := tc.StreamingInputCall(ctx); err != nil {
- t.Errorf("%v.StreamingInputCall(_) = _, %v, want _, <nil>", tc, err)
- }
- cancelThird()
- }
- // The following tests the gRPC streaming RPC implementations.
- // TODO(zhaoq): Have better coverage on error cases.
- var (
- reqSizes = []int{27182, 8, 1828, 45904}
- respSizes = []int{31415, 9, 2653, 58979}
- )
- func (s) TestNoService(t *testing.T) {
- for _, e := range listTestEnv() {
- testNoService(t, e)
- }
- }
- func testNoService(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(nil)
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- stream, err := tc.FullDuplexCall(te.ctx, grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if _, err := stream.Recv(); status.Code(err) != codes.Unimplemented {
- t.Fatalf("stream.Recv() = _, %v, want _, error code %s", err, codes.Unimplemented)
- }
- }
- func (s) TestPingPong(t *testing.T) {
- for _, e := range listTestEnv() {
- testPingPong(t, e)
- }
- }
- func testPingPong(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- var index int
- for index < len(reqSizes) {
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(respSizes[index]),
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(reqSizes[index]))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- reply, err := stream.Recv()
- if err != nil {
- t.Fatalf("%v.Recv() = %v, want <nil>", stream, err)
- }
- pt := reply.GetPayload().GetType()
- if pt != testpb.PayloadType_COMPRESSABLE {
- t.Fatalf("Got the reply of type %d, want %d", pt, testpb.PayloadType_COMPRESSABLE)
- }
- size := len(reply.GetPayload().GetBody())
- if size != int(respSizes[index]) {
- t.Fatalf("Got reply body of length %d, want %d", size, respSizes[index])
- }
- index++
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() got %v, want %v", stream, err, nil)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v failed to complele the ping pong test: %v", stream, err)
- }
- }
- func (s) TestMetadataStreamingRPC(t *testing.T) {
- for _, e := range listTestEnv() {
- testMetadataStreamingRPC(t, e)
- }
- }
- func testMetadataStreamingRPC(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- ctx := metadata.NewOutgoingContext(te.ctx, testMetadata)
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- go func() {
- headerMD, err := stream.Header()
- if e.security == "tls" {
- delete(headerMD, "transport_security_type")
- }
- delete(headerMD, "trailer") // ignore if present
- delete(headerMD, "user-agent")
- delete(headerMD, "content-type")
- if err != nil || !reflect.DeepEqual(testMetadata, headerMD) {
- t.Errorf("#1 %v.Header() = %v, %v, want %v, <nil>", stream, headerMD, err, testMetadata)
- }
- // test the cached value.
- headerMD, err = stream.Header()
- delete(headerMD, "trailer") // ignore if present
- delete(headerMD, "user-agent")
- delete(headerMD, "content-type")
- if err != nil || !reflect.DeepEqual(testMetadata, headerMD) {
- t.Errorf("#2 %v.Header() = %v, %v, want %v, <nil>", stream, headerMD, err, testMetadata)
- }
- err = func() error {
- for index := 0; index < len(reqSizes); index++ {
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(respSizes[index]),
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(reqSizes[index]))
- if err != nil {
- return err
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- if err := stream.Send(req); err != nil {
- return fmt.Errorf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- }
- return nil
- }()
- // Tell the server we're done sending args.
- stream.CloseSend()
- if err != nil {
- t.Error(err)
- }
- }()
- for {
- if _, err := stream.Recv(); err != nil {
- break
- }
- }
- trailerMD := stream.Trailer()
- if !reflect.DeepEqual(testTrailerMetadata, trailerMD) {
- t.Fatalf("%v.Trailer() = %v, want %v", stream, trailerMD, testTrailerMetadata)
- }
- }
- func (s) TestServerStreaming(t *testing.T) {
- for _, e := range listTestEnv() {
- testServerStreaming(t, e)
- }
- }
- func testServerStreaming(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- respParam := make([]*testpb.ResponseParameters, len(respSizes))
- for i, s := range respSizes {
- respParam[i] = &testpb.ResponseParameters{
- Size: int32(s),
- }
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- }
- stream, err := tc.StreamingOutputCall(context.Background(), req)
- if err != nil {
- t.Fatalf("%v.StreamingOutputCall(_) = _, %v, want <nil>", tc, err)
- }
- var rpcStatus error
- var respCnt int
- var index int
- for {
- reply, err := stream.Recv()
- if err != nil {
- rpcStatus = err
- break
- }
- pt := reply.GetPayload().GetType()
- if pt != testpb.PayloadType_COMPRESSABLE {
- t.Fatalf("Got the reply of type %d, want %d", pt, testpb.PayloadType_COMPRESSABLE)
- }
- size := len(reply.GetPayload().GetBody())
- if size != int(respSizes[index]) {
- t.Fatalf("Got reply body of length %d, want %d", size, respSizes[index])
- }
- index++
- respCnt++
- }
- if rpcStatus != io.EOF {
- t.Fatalf("Failed to finish the server streaming rpc: %v, want <EOF>", rpcStatus)
- }
- if respCnt != len(respSizes) {
- t.Fatalf("Got %d reply, want %d", len(respSizes), respCnt)
- }
- }
- func (s) TestFailedServerStreaming(t *testing.T) {
- for _, e := range listTestEnv() {
- testFailedServerStreaming(t, e)
- }
- }
- func testFailedServerStreaming(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = failAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- respParam := make([]*testpb.ResponseParameters, len(respSizes))
- for i, s := range respSizes {
- respParam[i] = &testpb.ResponseParameters{
- Size: int32(s),
- }
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- }
- ctx := metadata.NewOutgoingContext(te.ctx, testMetadata)
- stream, err := tc.StreamingOutputCall(ctx, req)
- if err != nil {
- t.Fatalf("%v.StreamingOutputCall(_) = _, %v, want <nil>", tc, err)
- }
- wantErr := status.Error(codes.DataLoss, "error for testing: "+failAppUA)
- if _, err := stream.Recv(); !equalError(err, wantErr) {
- t.Fatalf("%v.Recv() = _, %v, want _, %v", stream, err, wantErr)
- }
- }
- func equalError(x, y error) bool {
- return x == y || (x != nil && y != nil && x.Error() == y.Error())
- }
- // concurrentSendServer is a TestServiceServer whose
- // StreamingOutputCall makes ten serial Send calls, sending payloads
- // "0".."9", inclusive. TestServerStreamingConcurrent verifies they
- // were received in the correct order, and that there were no races.
- //
- // All other TestServiceServer methods crash if called.
- type concurrentSendServer struct {
- testpb.TestServiceServer
- }
- func (s concurrentSendServer) StreamingOutputCall(args *testpb.StreamingOutputCallRequest, stream testpb.TestService_StreamingOutputCallServer) error {
- for i := 0; i < 10; i++ {
- stream.Send(&testpb.StreamingOutputCallResponse{
- Payload: &testpb.Payload{
- Body: []byte{'0' + uint8(i)},
- },
- })
- }
- return nil
- }
- // Tests doing a bunch of concurrent streaming output calls.
- func (s) TestServerStreamingConcurrent(t *testing.T) {
- for _, e := range listTestEnv() {
- testServerStreamingConcurrent(t, e)
- }
- }
- func testServerStreamingConcurrent(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(concurrentSendServer{})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- doStreamingCall := func() {
- req := &testpb.StreamingOutputCallRequest{}
- stream, err := tc.StreamingOutputCall(context.Background(), req)
- if err != nil {
- t.Errorf("%v.StreamingOutputCall(_) = _, %v, want <nil>", tc, err)
- return
- }
- var ngot int
- var buf bytes.Buffer
- for {
- reply, err := stream.Recv()
- if err == io.EOF {
- break
- }
- if err != nil {
- t.Fatal(err)
- }
- ngot++
- if buf.Len() > 0 {
- buf.WriteByte(',')
- }
- buf.Write(reply.GetPayload().GetBody())
- }
- if want := 10; ngot != want {
- t.Errorf("Got %d replies, want %d", ngot, want)
- }
- if got, want := buf.String(), "0,1,2,3,4,5,6,7,8,9"; got != want {
- t.Errorf("Got replies %q; want %q", got, want)
- }
- }
- var wg sync.WaitGroup
- for i := 0; i < 20; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- doStreamingCall()
- }()
- }
- wg.Wait()
- }
- func generatePayloadSizes() [][]int {
- reqSizes := [][]int{
- {27182, 8, 1828, 45904},
- }
- num8KPayloads := 1024
- eightKPayloads := []int{}
- for i := 0; i < num8KPayloads; i++ {
- eightKPayloads = append(eightKPayloads, (1 << 13))
- }
- reqSizes = append(reqSizes, eightKPayloads)
- num2MPayloads := 8
- twoMPayloads := []int{}
- for i := 0; i < num2MPayloads; i++ {
- twoMPayloads = append(twoMPayloads, (1 << 21))
- }
- reqSizes = append(reqSizes, twoMPayloads)
- return reqSizes
- }
- func (s) TestClientStreaming(t *testing.T) {
- for _, s := range generatePayloadSizes() {
- for _, e := range listTestEnv() {
- testClientStreaming(t, e, s)
- }
- }
- }
- func testClientStreaming(t *testing.T, e env, sizes []int) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- ctx, cancel := context.WithTimeout(te.ctx, time.Second*30)
- defer cancel()
- stream, err := tc.StreamingInputCall(ctx)
- if err != nil {
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want <nil>", tc, err)
- }
- var sum int
- for _, s := range sizes {
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(s))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingInputCallRequest{
- Payload: payload,
- }
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(_) = %v, want <nil>", stream, err)
- }
- sum += s
- }
- reply, err := stream.CloseAndRecv()
- if err != nil {
- t.Fatalf("%v.CloseAndRecv() got error %v, want %v", stream, err, nil)
- }
- if reply.GetAggregatedPayloadSize() != int32(sum) {
- t.Fatalf("%v.CloseAndRecv().GetAggregatePayloadSize() = %v; want %v", stream, reply.GetAggregatedPayloadSize(), sum)
- }
- }
- func (s) TestClientStreamingError(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.name == "handler-tls" {
- continue
- }
- testClientStreamingError(t, e)
- }
- }
- func testClientStreamingError(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, earlyFail: true})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- stream, err := tc.StreamingInputCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want <nil>", tc, err)
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 1)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingInputCallRequest{
- Payload: payload,
- }
- // The 1st request should go through.
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- for {
- if err := stream.Send(req); err != io.EOF {
- continue
- }
- if _, err := stream.CloseAndRecv(); status.Code(err) != codes.NotFound {
- t.Fatalf("%v.CloseAndRecv() = %v, want error %s", stream, err, codes.NotFound)
- }
- break
- }
- }
- func (s) TestExceedMaxStreamsLimit(t *testing.T) {
- for _, e := range listTestEnv() {
- testExceedMaxStreamsLimit(t, e)
- }
- }
- func testExceedMaxStreamsLimit(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise(
- "http2Client.notifyError got notified that the client transport was broken",
- "Conn.resetTransport failed to create client transport",
- "grpc: the connection is closing",
- )
- te.maxStream = 1 // Only allows 1 live stream per server transport.
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- _, err := tc.StreamingInputCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want _, <nil>", tc, err)
- }
- // Loop until receiving the new max stream setting from the server.
- for {
- ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
- defer cancel()
- _, err := tc.StreamingInputCall(ctx)
- if err == nil {
- time.Sleep(50 * time.Millisecond)
- continue
- }
- if status.Code(err) == codes.DeadlineExceeded {
- break
- }
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want _, %s", tc, err, codes.DeadlineExceeded)
- }
- }
- func (s) TestStreamsQuotaRecovery(t *testing.T) {
- for _, e := range listTestEnv() {
- testStreamsQuotaRecovery(t, e)
- }
- }
- func testStreamsQuotaRecovery(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise(
- "http2Client.notifyError got notified that the client transport was broken",
- "Conn.resetTransport failed to create client transport",
- "grpc: the connection is closing",
- )
- te.maxStream = 1 // Allows 1 live stream.
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- if _, err := tc.StreamingInputCall(ctx); err != nil {
- t.Fatalf("tc.StreamingInputCall(_) = _, %v, want _, <nil>", err)
- }
- // Loop until the new max stream setting is effective.
- for {
- ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
- _, err := tc.StreamingInputCall(ctx)
- cancel()
- if err == nil {
- time.Sleep(5 * time.Millisecond)
- continue
- }
- if status.Code(err) == codes.DeadlineExceeded {
- break
- }
- t.Fatalf("tc.StreamingInputCall(_) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- var wg sync.WaitGroup
- for i := 0; i < 10; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 314)
- if err != nil {
- t.Error(err)
- return
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: 1592,
- Payload: payload,
- }
- // No rpc should go through due to the max streams limit.
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
- defer cancel()
- if _, err := tc.UnaryCall(ctx, req, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Errorf("tc.UnaryCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- }()
- }
- wg.Wait()
- cancel()
- // A new stream should be allowed after canceling the first one.
- ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- if _, err := tc.StreamingInputCall(ctx); err != nil {
- t.Fatalf("tc.StreamingInputCall(_) = _, %v, want _, %v", err, nil)
- }
- }
- func (s) TestCompressServerHasNoSupport(t *testing.T) {
- for _, e := range listTestEnv() {
- testCompressServerHasNoSupport(t, e)
- }
- }
- func testCompressServerHasNoSupport(t *testing.T, e env) {
- te := newTest(t, e)
- te.serverCompression = false
- te.clientCompression = false
- te.clientNopCompression = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- const argSize = 271828
- const respSize = 314159
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.Unimplemented {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code %s", err, codes.Unimplemented)
- }
- // Streaming RPC
- stream, err := tc.FullDuplexCall(context.Background())
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.Unimplemented {
- t.Fatalf("%v.Recv() = %v, want error code %s", stream, err, codes.Unimplemented)
- }
- }
- func (s) TestCompressOK(t *testing.T) {
- for _, e := range listTestEnv() {
- testCompressOK(t, e)
- }
- }
- func testCompressOK(t *testing.T, e env) {
- te := newTest(t, e)
- te.serverCompression = true
- te.clientCompression = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- // Unary call
- const argSize = 271828
- const respSize = 314159
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("something", "something"))
- if _, err := tc.UnaryCall(ctx, req); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, <nil>", err)
- }
- // Streaming RPC
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: 31415,
- },
- }
- payload, err = newPayload(testpb.PayloadType_COMPRESSABLE, int32(31415))
- if err != nil {
- t.Fatal(err)
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- stream.CloseSend()
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = %v, want <nil>", stream, err)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v.Recv() = %v, want io.EOF", stream, err)
- }
- }
- func (s) TestIdentityEncoding(t *testing.T) {
- for _, e := range listTestEnv() {
- testIdentityEncoding(t, e)
- }
- }
- func testIdentityEncoding(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- // Unary call
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 5)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: 10,
- Payload: payload,
- }
- ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("something", "something"))
- if _, err := tc.UnaryCall(ctx, req); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, <nil>", err)
- }
- // Streaming RPC
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- stream, err := tc.FullDuplexCall(ctx, grpc.UseCompressor("identity"))
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- payload, err = newPayload(testpb.PayloadType_COMPRESSABLE, int32(31415))
- if err != nil {
- t.Fatal(err)
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: []*testpb.ResponseParameters{{Size: 10}},
- Payload: payload,
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- stream.CloseSend()
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = %v, want <nil>", stream, err)
- }
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("%v.Recv() = %v, want io.EOF", stream, err)
- }
- }
- func (s) TestUnaryClientInterceptor(t *testing.T) {
- for _, e := range listTestEnv() {
- testUnaryClientInterceptor(t, e)
- }
- }
- func failOkayRPC(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
- err := invoker(ctx, method, req, reply, cc, opts...)
- if err == nil {
- return status.Error(codes.NotFound, "")
- }
- return err
- }
- func testUnaryClientInterceptor(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.unaryClientInt = failOkayRPC
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.NotFound {
- t.Fatalf("%v.EmptyCall(_, _) = _, %v, want _, error code %s", tc, err, codes.NotFound)
- }
- }
- func (s) TestStreamClientInterceptor(t *testing.T) {
- for _, e := range listTestEnv() {
- testStreamClientInterceptor(t, e)
- }
- }
- func failOkayStream(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
- s, err := streamer(ctx, desc, cc, method, opts...)
- if err == nil {
- return nil, status.Error(codes.NotFound, "")
- }
- return s, nil
- }
- func testStreamClientInterceptor(t *testing.T, e env) {
- te := newTest(t, e)
- te.streamClientInt = failOkayStream
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(1),
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(1))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- if _, err := tc.StreamingOutputCall(context.Background(), req); status.Code(err) != codes.NotFound {
- t.Fatalf("%v.StreamingOutputCall(_) = _, %v, want _, error code %s", tc, err, codes.NotFound)
- }
- }
- func (s) TestUnaryServerInterceptor(t *testing.T) {
- for _, e := range listTestEnv() {
- testUnaryServerInterceptor(t, e)
- }
- }
- func errInjector(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
- return nil, status.Error(codes.PermissionDenied, "")
- }
- func testUnaryServerInterceptor(t *testing.T, e env) {
- te := newTest(t, e)
- te.unaryServerInt = errInjector
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.PermissionDenied {
- t.Fatalf("%v.EmptyCall(_, _) = _, %v, want _, error code %s", tc, err, codes.PermissionDenied)
- }
- }
- func (s) TestStreamServerInterceptor(t *testing.T) {
- for _, e := range listTestEnv() {
- // TODO(bradfitz): Temporarily skip this env due to #619.
- if e.name == "handler-tls" {
- continue
- }
- testStreamServerInterceptor(t, e)
- }
- }
- func fullDuplexOnly(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
- if info.FullMethod == "/grpc.testing.TestService/FullDuplexCall" {
- return handler(srv, ss)
- }
- // Reject the other methods.
- return status.Error(codes.PermissionDenied, "")
- }
- func testStreamServerInterceptor(t *testing.T, e env) {
- te := newTest(t, e)
- te.streamServerInt = fullDuplexOnly
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(1),
- },
- }
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(1))
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- s1, err := tc.StreamingOutputCall(context.Background(), req)
- if err != nil {
- t.Fatalf("%v.StreamingOutputCall(_) = _, %v, want _, <nil>", tc, err)
- }
- if _, err := s1.Recv(); status.Code(err) != codes.PermissionDenied {
- t.Fatalf("%v.StreamingInputCall(_) = _, %v, want _, error code %s", tc, err, codes.PermissionDenied)
- }
- s2, err := tc.FullDuplexCall(context.Background())
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := s2.Send(req); err != nil {
- t.Fatalf("%v.Send(_) = %v, want <nil>", s2, err)
- }
- if _, err := s2.Recv(); err != nil {
- t.Fatalf("%v.Recv() = _, %v, want _, <nil>", s2, err)
- }
- }
- // funcServer implements methods of TestServiceServer using funcs,
- // similar to an http.HandlerFunc.
- // Any unimplemented method will crash. Tests implement the method(s)
- // they need.
- type funcServer struct {
- testpb.TestServiceServer
- unaryCall func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error)
- streamingInputCall func(stream testpb.TestService_StreamingInputCallServer) error
- fullDuplexCall func(stream testpb.TestService_FullDuplexCallServer) error
- }
- func (s *funcServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- return s.unaryCall(ctx, in)
- }
- func (s *funcServer) StreamingInputCall(stream testpb.TestService_StreamingInputCallServer) error {
- return s.streamingInputCall(stream)
- }
- func (s *funcServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServer) error {
- return s.fullDuplexCall(stream)
- }
- func (s) TestClientRequestBodyErrorUnexpectedEOF(t *testing.T) {
- for _, e := range listTestEnv() {
- testClientRequestBodyErrorUnexpectedEOF(t, e)
- }
- }
- func testClientRequestBodyErrorUnexpectedEOF(t *testing.T, e env) {
- te := newTest(t, e)
- ts := &funcServer{unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- errUnexpectedCall := errors.New("unexpected call func server method")
- t.Error(errUnexpectedCall)
- return nil, errUnexpectedCall
- }}
- te.startServer(ts)
- defer te.tearDown()
- te.withServerTester(func(st *serverTester) {
- st.writeHeadersGRPC(1, "/grpc.testing.TestService/UnaryCall")
- // Say we have 5 bytes coming, but set END_STREAM flag:
- st.writeData(1, true, []byte{0, 0, 0, 0, 5})
- st.wantAnyFrame() // wait for server to crash (it used to crash)
- })
- }
- func (s) TestClientRequestBodyErrorCloseAfterLength(t *testing.T) {
- for _, e := range listTestEnv() {
- testClientRequestBodyErrorCloseAfterLength(t, e)
- }
- }
- func testClientRequestBodyErrorCloseAfterLength(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise("Server.processUnaryRPC failed to write status")
- ts := &funcServer{unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- errUnexpectedCall := errors.New("unexpected call func server method")
- t.Error(errUnexpectedCall)
- return nil, errUnexpectedCall
- }}
- te.startServer(ts)
- defer te.tearDown()
- te.withServerTester(func(st *serverTester) {
- st.writeHeadersGRPC(1, "/grpc.testing.TestService/UnaryCall")
- // say we're sending 5 bytes, but then close the connection instead.
- st.writeData(1, false, []byte{0, 0, 0, 0, 5})
- st.cc.Close()
- })
- }
- func (s) TestClientRequestBodyErrorCancel(t *testing.T) {
- for _, e := range listTestEnv() {
- testClientRequestBodyErrorCancel(t, e)
- }
- }
- func testClientRequestBodyErrorCancel(t *testing.T, e env) {
- te := newTest(t, e)
- gotCall := make(chan bool, 1)
- ts := &funcServer{unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- gotCall <- true
- return new(testpb.SimpleResponse), nil
- }}
- te.startServer(ts)
- defer te.tearDown()
- te.withServerTester(func(st *serverTester) {
- st.writeHeadersGRPC(1, "/grpc.testing.TestService/UnaryCall")
- // Say we have 5 bytes coming, but cancel it instead.
- st.writeRSTStream(1, http2.ErrCodeCancel)
- st.writeData(1, false, []byte{0, 0, 0, 0, 5})
- // Verify we didn't a call yet.
- select {
- case <-gotCall:
- t.Fatal("unexpected call")
- default:
- }
- // And now send an uncanceled (but still invalid), just to get a response.
- st.writeHeadersGRPC(3, "/grpc.testing.TestService/UnaryCall")
- st.writeData(3, true, []byte{0, 0, 0, 0, 0})
- <-gotCall
- st.wantAnyFrame()
- })
- }
- func (s) TestClientRequestBodyErrorCancelStreamingInput(t *testing.T) {
- for _, e := range listTestEnv() {
- testClientRequestBodyErrorCancelStreamingInput(t, e)
- }
- }
- func testClientRequestBodyErrorCancelStreamingInput(t *testing.T, e env) {
- te := newTest(t, e)
- recvErr := make(chan error, 1)
- ts := &funcServer{streamingInputCall: func(stream testpb.TestService_StreamingInputCallServer) error {
- _, err := stream.Recv()
- recvErr <- err
- return nil
- }}
- te.startServer(ts)
- defer te.tearDown()
- te.withServerTester(func(st *serverTester) {
- st.writeHeadersGRPC(1, "/grpc.testing.TestService/StreamingInputCall")
- // Say we have 5 bytes coming, but cancel it instead.
- st.writeData(1, false, []byte{0, 0, 0, 0, 5})
- st.writeRSTStream(1, http2.ErrCodeCancel)
- var got error
- select {
- case got = <-recvErr:
- case <-time.After(3 * time.Second):
- t.Fatal("timeout waiting for error")
- }
- if grpc.Code(got) != codes.Canceled {
- t.Errorf("error = %#v; want error code %s", got, codes.Canceled)
- }
- })
- }
- func (s) TestClientResourceExhaustedCancelFullDuplex(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.httpHandler {
- // httpHandler write won't be blocked on flow control window.
- continue
- }
- testClientResourceExhaustedCancelFullDuplex(t, e)
- }
- }
- func testClientResourceExhaustedCancelFullDuplex(t *testing.T, e env) {
- te := newTest(t, e)
- recvErr := make(chan error, 1)
- ts := &funcServer{fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- defer close(recvErr)
- _, err := stream.Recv()
- if err != nil {
- return status.Errorf(codes.Internal, "stream.Recv() got error: %v, want <nil>", err)
- }
- // create a payload that's larger than the default flow control window.
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 10)
- if err != nil {
- return err
- }
- resp := &testpb.StreamingOutputCallResponse{
- Payload: payload,
- }
- ce := make(chan error, 1)
- go func() {
- var err error
- for {
- if err = stream.Send(resp); err != nil {
- break
- }
- }
- ce <- err
- }()
- select {
- case err = <-ce:
- case <-time.After(10 * time.Second):
- err = errors.New("10s timeout reached")
- }
- recvErr <- err
- return err
- }}
- te.startServer(ts)
- defer te.tearDown()
- // set a low limit on receive message size to error with Resource Exhausted on
- // client side when server send a large message.
- te.maxClientReceiveMsgSize = newInt(10)
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- stream, err := tc.FullDuplexCall(context.Background())
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- req := &testpb.StreamingOutputCallRequest{}
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- if _, err := stream.Recv(); status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- err = <-recvErr
- if status.Code(err) != codes.Canceled {
- t.Fatalf("server got error %v, want error code: %s", err, codes.Canceled)
- }
- }
- type clientFailCreds struct{}
- func (c *clientFailCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return rawConn, nil, nil
- }
- func (c *clientFailCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return nil, nil, fmt.Errorf("client handshake fails with fatal error")
- }
- func (c *clientFailCreds) Info() credentials.ProtocolInfo {
- return credentials.ProtocolInfo{}
- }
- func (c *clientFailCreds) Clone() credentials.TransportCredentials {
- return c
- }
- func (c *clientFailCreds) OverrideServerName(s string) error {
- return nil
- }
- // This test makes sure that failfast RPCs fail if client handshake fails with
- // fatal errors.
- func (s) TestFailfastRPCFailOnFatalHandshakeError(t *testing.T) {
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to listen: %v", err)
- }
- defer lis.Close()
- cc, err := grpc.Dial("passthrough:///"+lis.Addr().String(), grpc.WithTransportCredentials(&clientFailCreds{}))
- if err != nil {
- t.Fatalf("grpc.Dial(_) = %v", err)
- }
- defer cc.Close()
- tc := testpb.NewTestServiceClient(cc)
- // This unary call should fail, but not timeout.
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(false)); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want <Unavailable>", err)
- }
- }
- func (s) TestFlowControlLogicalRace(t *testing.T) {
- // Test for a regression of https://github.com/grpc/grpc-go/issues/632,
- // and other flow control bugs.
- const (
- itemCount = 100
- itemSize = 1 << 10
- recvCount = 2
- maxFailures = 3
- requestTimeout = time.Second * 5
- )
- requestCount := 10000
- if raceMode {
- requestCount = 1000
- }
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to listen: %v", err)
- }
- defer lis.Close()
- s := grpc.NewServer()
- testpb.RegisterTestServiceServer(s, &flowControlLogicalRaceServer{
- itemCount: itemCount,
- itemSize: itemSize,
- })
- defer s.Stop()
- go s.Serve(lis)
- ctx := context.Background()
- cc, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure(), grpc.WithBlock())
- if err != nil {
- t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
- }
- defer cc.Close()
- cl := testpb.NewTestServiceClient(cc)
- failures := 0
- for i := 0; i < requestCount; i++ {
- ctx, cancel := context.WithTimeout(ctx, requestTimeout)
- output, err := cl.StreamingOutputCall(ctx, &testpb.StreamingOutputCallRequest{})
- if err != nil {
- t.Fatalf("StreamingOutputCall; err = %q", err)
- }
- j := 0
- loop:
- for ; j < recvCount; j++ {
- _, err := output.Recv()
- if err != nil {
- if err == io.EOF {
- break loop
- }
- switch status.Code(err) {
- case codes.DeadlineExceeded:
- break loop
- default:
- t.Fatalf("Recv; err = %q", err)
- }
- }
- }
- cancel()
- <-ctx.Done()
- if j < recvCount {
- t.Errorf("got %d responses to request %d", j, i)
- failures++
- if failures >= maxFailures {
- // Continue past the first failure to see if the connection is
- // entirely broken, or if only a single RPC was affected
- break
- }
- }
- }
- }
- type flowControlLogicalRaceServer struct {
- testpb.TestServiceServer
- itemSize int
- itemCount int
- }
- func (s *flowControlLogicalRaceServer) StreamingOutputCall(req *testpb.StreamingOutputCallRequest, srv testpb.TestService_StreamingOutputCallServer) error {
- for i := 0; i < s.itemCount; i++ {
- err := srv.Send(&testpb.StreamingOutputCallResponse{
- Payload: &testpb.Payload{
- // Sending a large stream of data which the client reject
- // helps to trigger some types of flow control bugs.
- //
- // Reallocating memory here is inefficient, but the stress it
- // puts on the GC leads to more frequent flow control
- // failures. The GC likely causes more variety in the
- // goroutine scheduling orders.
- Body: bytes.Repeat([]byte("a"), s.itemSize),
- },
- })
- if err != nil {
- return err
- }
- }
- return nil
- }
- type lockingWriter struct {
- mu sync.Mutex
- w io.Writer
- }
- func (lw *lockingWriter) Write(p []byte) (n int, err error) {
- lw.mu.Lock()
- defer lw.mu.Unlock()
- return lw.w.Write(p)
- }
- func (lw *lockingWriter) setWriter(w io.Writer) {
- lw.mu.Lock()
- defer lw.mu.Unlock()
- lw.w = w
- }
- var testLogOutput = &lockingWriter{w: os.Stderr}
- // awaitNewConnLogOutput waits for any of grpc.NewConn's goroutines to
- // terminate, if they're still running. It spams logs with this
- // message. We wait for it so our log filter is still
- // active. Otherwise the "defer restore()" at the top of various test
- // functions restores our log filter and then the goroutine spams.
- func awaitNewConnLogOutput() {
- awaitLogOutput(50*time.Millisecond, "grpc: the client connection is closing; please retry")
- }
- func awaitLogOutput(maxWait time.Duration, phrase string) {
- pb := []byte(phrase)
- timer := time.NewTimer(maxWait)
- defer timer.Stop()
- wakeup := make(chan bool, 1)
- for {
- if logOutputHasContents(pb, wakeup) {
- return
- }
- select {
- case <-timer.C:
- // Too slow. Oh well.
- return
- case <-wakeup:
- }
- }
- }
- func logOutputHasContents(v []byte, wakeup chan<- bool) bool {
- testLogOutput.mu.Lock()
- defer testLogOutput.mu.Unlock()
- fw, ok := testLogOutput.w.(*filterWriter)
- if !ok {
- return false
- }
- fw.mu.Lock()
- defer fw.mu.Unlock()
- if bytes.Contains(fw.buf.Bytes(), v) {
- return true
- }
- fw.wakeup = wakeup
- return false
- }
- var verboseLogs = flag.Bool("verbose_logs", false, "show all grpclog output, without filtering")
- func noop() {}
- // declareLogNoise declares that t is expected to emit the following noisy phrases,
- // even on success. Those phrases will be filtered from grpclog output
- // and only be shown if *verbose_logs or t ends up failing.
- // The returned restore function should be called with defer to be run
- // before the test ends.
- func declareLogNoise(t *testing.T, phrases ...string) (restore func()) {
- if *verboseLogs {
- return noop
- }
- fw := &filterWriter{dst: os.Stderr, filter: phrases}
- testLogOutput.setWriter(fw)
- return func() {
- if t.Failed() {
- fw.mu.Lock()
- defer fw.mu.Unlock()
- if fw.buf.Len() > 0 {
- t.Logf("Complete log output:\n%s", fw.buf.Bytes())
- }
- }
- testLogOutput.setWriter(os.Stderr)
- }
- }
- type filterWriter struct {
- dst io.Writer
- filter []string
- mu sync.Mutex
- buf bytes.Buffer
- wakeup chan<- bool // if non-nil, gets true on write
- }
- func (fw *filterWriter) Write(p []byte) (n int, err error) {
- fw.mu.Lock()
- fw.buf.Write(p)
- if fw.wakeup != nil {
- select {
- case fw.wakeup <- true:
- default:
- }
- }
- fw.mu.Unlock()
- ps := string(p)
- for _, f := range fw.filter {
- if strings.Contains(ps, f) {
- return len(p), nil
- }
- }
- return fw.dst.Write(p)
- }
- // stubServer is a server that is easy to customize within individual test
- // cases.
- type stubServer struct {
- // Guarantees we satisfy this interface; panics if unimplemented methods are called.
- testpb.TestServiceServer
- // Customizable implementations of server handlers.
- emptyCall func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error)
- unaryCall func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error)
- fullDuplexCall func(stream testpb.TestService_FullDuplexCallServer) error
- // A client connected to this service the test may use. Created in Start().
- client testpb.TestServiceClient
- cc *grpc.ClientConn
- s *grpc.Server
- // Parameters for Listen and Dial. Defaults will be used if these are empty
- // before Start.
- network string
- address string
- target string
- cleanups []func() // Lambdas executed in Stop(); populated by Start().
- r *manual.Resolver
- }
- func (ss *stubServer) EmptyCall(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- return ss.emptyCall(ctx, in)
- }
- func (ss *stubServer) UnaryCall(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- return ss.unaryCall(ctx, in)
- }
- func (ss *stubServer) FullDuplexCall(stream testpb.TestService_FullDuplexCallServer) error {
- return ss.fullDuplexCall(stream)
- }
- // Start starts the server and creates a client connected to it.
- func (ss *stubServer) Start(sopts []grpc.ServerOption, dopts ...grpc.DialOption) error {
- if ss.network == "" {
- ss.network = "tcp"
- }
- if ss.address == "" {
- ss.address = "localhost:0"
- }
- if ss.target == "" {
- ss.r = manual.NewBuilderWithScheme("whatever")
- }
- lis, err := net.Listen(ss.network, ss.address)
- if err != nil {
- return fmt.Errorf("net.Listen(%q, %q) = %v", ss.network, ss.address, err)
- }
- ss.address = lis.Addr().String()
- ss.cleanups = append(ss.cleanups, func() { lis.Close() })
- s := grpc.NewServer(sopts...)
- testpb.RegisterTestServiceServer(s, ss)
- go s.Serve(lis)
- ss.cleanups = append(ss.cleanups, s.Stop)
- ss.s = s
- opts := append([]grpc.DialOption{grpc.WithInsecure()}, dopts...)
- if ss.r != nil {
- ss.target = ss.r.Scheme() + ":///" + ss.address
- opts = append(opts, grpc.WithResolvers(ss.r))
- }
- cc, err := grpc.Dial(ss.target, opts...)
- if err != nil {
- return fmt.Errorf("grpc.Dial(%q) = %v", ss.target, err)
- }
- ss.cc = cc
- if ss.r != nil {
- ss.r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: ss.address}}})
- }
- if err := ss.waitForReady(cc); err != nil {
- return err
- }
- ss.cleanups = append(ss.cleanups, func() { cc.Close() })
- ss.client = testpb.NewTestServiceClient(cc)
- return nil
- }
- func (ss *stubServer) newServiceConfig(sc string) {
- if ss.r != nil {
- ss.r.UpdateState(resolver.State{Addresses: []resolver.Address{{Addr: ss.address}}, ServiceConfig: parseCfg(ss.r, sc)})
- }
- }
- func (ss *stubServer) waitForReady(cc *grpc.ClientConn) error {
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- for {
- s := cc.GetState()
- if s == connectivity.Ready {
- return nil
- }
- if !cc.WaitForStateChange(ctx, s) {
- // ctx got timeout or canceled.
- return ctx.Err()
- }
- }
- }
- func (ss *stubServer) Stop() {
- for i := len(ss.cleanups) - 1; i >= 0; i-- {
- ss.cleanups[i]()
- }
- }
- func (s) TestGRPCMethod(t *testing.T) {
- var method string
- var ok bool
- ss := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- method, ok = grpc.Method(ctx)
- return &testpb.Empty{}, nil
- },
- }
- if err := ss.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- if _, err := ss.client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
- t.Fatalf("ss.client.EmptyCall(_, _) = _, %v; want _, nil", err)
- }
- if want := "/grpc.testing.TestService/EmptyCall"; !ok || method != want {
- t.Fatalf("grpc.Method(_) = %q, %v; want %q, true", method, ok, want)
- }
- }
- func (s) TestUnaryProxyDoesNotForwardMetadata(t *testing.T) {
- const mdkey = "somedata"
- // endpoint ensures mdkey is NOT in metadata and returns an error if it is.
- endpoint := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- if md, ok := metadata.FromIncomingContext(ctx); !ok || md[mdkey] != nil {
- return nil, status.Errorf(codes.Internal, "endpoint: md=%v; want !contains(%q)", md, mdkey)
- }
- return &testpb.Empty{}, nil
- },
- }
- if err := endpoint.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer endpoint.Stop()
- // proxy ensures mdkey IS in metadata, then forwards the RPC to endpoint
- // without explicitly copying the metadata.
- proxy := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- if md, ok := metadata.FromIncomingContext(ctx); !ok || md[mdkey] == nil {
- return nil, status.Errorf(codes.Internal, "proxy: md=%v; want contains(%q)", md, mdkey)
- }
- return endpoint.client.EmptyCall(ctx, in)
- },
- }
- if err := proxy.Start(nil); err != nil {
- t.Fatalf("Error starting proxy server: %v", err)
- }
- defer proxy.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- md := metadata.Pairs(mdkey, "val")
- ctx = metadata.NewOutgoingContext(ctx, md)
- // Sanity check that endpoint properly errors when it sees mdkey.
- _, err := endpoint.client.EmptyCall(ctx, &testpb.Empty{})
- if s, ok := status.FromError(err); !ok || s.Code() != codes.Internal {
- t.Fatalf("endpoint.client.EmptyCall(_, _) = _, %v; want _, <status with Code()=Internal>", err)
- }
- if _, err := proxy.client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
- t.Fatal(err.Error())
- }
- }
- func (s) TestStreamingProxyDoesNotForwardMetadata(t *testing.T) {
- const mdkey = "somedata"
- // doFDC performs a FullDuplexCall with client and returns the error from the
- // first stream.Recv call, or nil if that error is io.EOF. Calls t.Fatal if
- // the stream cannot be established.
- doFDC := func(ctx context.Context, client testpb.TestServiceClient) error {
- stream, err := client.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("Unwanted error: %v", err)
- }
- if _, err := stream.Recv(); err != io.EOF {
- return err
- }
- return nil
- }
- // endpoint ensures mdkey is NOT in metadata and returns an error if it is.
- endpoint := &stubServer{
- fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- ctx := stream.Context()
- if md, ok := metadata.FromIncomingContext(ctx); !ok || md[mdkey] != nil {
- return status.Errorf(codes.Internal, "endpoint: md=%v; want !contains(%q)", md, mdkey)
- }
- return nil
- },
- }
- if err := endpoint.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer endpoint.Stop()
- // proxy ensures mdkey IS in metadata, then forwards the RPC to endpoint
- // without explicitly copying the metadata.
- proxy := &stubServer{
- fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- ctx := stream.Context()
- if md, ok := metadata.FromIncomingContext(ctx); !ok || md[mdkey] == nil {
- return status.Errorf(codes.Internal, "endpoint: md=%v; want !contains(%q)", md, mdkey)
- }
- return doFDC(ctx, endpoint.client)
- },
- }
- if err := proxy.Start(nil); err != nil {
- t.Fatalf("Error starting proxy server: %v", err)
- }
- defer proxy.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- md := metadata.Pairs(mdkey, "val")
- ctx = metadata.NewOutgoingContext(ctx, md)
- // Sanity check that endpoint properly errors when it sees mdkey in ctx.
- err := doFDC(ctx, endpoint.client)
- if s, ok := status.FromError(err); !ok || s.Code() != codes.Internal {
- t.Fatalf("stream.Recv() = _, %v; want _, <status with Code()=Internal>", err)
- }
- if err := doFDC(ctx, proxy.client); err != nil {
- t.Fatalf("doFDC(_, proxy.client) = %v; want nil", err)
- }
- }
- func (s) TestStatsTagsAndTrace(t *testing.T) {
- // Data added to context by client (typically in a stats handler).
- tags := []byte{1, 5, 2, 4, 3}
- trace := []byte{5, 2, 1, 3, 4}
- // endpoint ensures Tags() and Trace() in context match those that were added
- // by the client and returns an error if not.
- endpoint := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- md, _ := metadata.FromIncomingContext(ctx)
- if tg := stats.Tags(ctx); !reflect.DeepEqual(tg, tags) {
- return nil, status.Errorf(codes.Internal, "stats.Tags(%v)=%v; want %v", ctx, tg, tags)
- }
- if !reflect.DeepEqual(md["grpc-tags-bin"], []string{string(tags)}) {
- return nil, status.Errorf(codes.Internal, "md['grpc-tags-bin']=%v; want %v", md["grpc-tags-bin"], tags)
- }
- if tr := stats.Trace(ctx); !reflect.DeepEqual(tr, trace) {
- return nil, status.Errorf(codes.Internal, "stats.Trace(%v)=%v; want %v", ctx, tr, trace)
- }
- if !reflect.DeepEqual(md["grpc-trace-bin"], []string{string(trace)}) {
- return nil, status.Errorf(codes.Internal, "md['grpc-trace-bin']=%v; want %v", md["grpc-trace-bin"], trace)
- }
- return &testpb.Empty{}, nil
- },
- }
- if err := endpoint.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer endpoint.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- testCases := []struct {
- ctx context.Context
- want codes.Code
- }{
- {ctx: ctx, want: codes.Internal},
- {ctx: stats.SetTags(ctx, tags), want: codes.Internal},
- {ctx: stats.SetTrace(ctx, trace), want: codes.Internal},
- {ctx: stats.SetTags(stats.SetTrace(ctx, tags), tags), want: codes.Internal},
- {ctx: stats.SetTags(stats.SetTrace(ctx, trace), tags), want: codes.OK},
- }
- for _, tc := range testCases {
- _, err := endpoint.client.EmptyCall(tc.ctx, &testpb.Empty{})
- if tc.want == codes.OK && err != nil {
- t.Fatalf("endpoint.client.EmptyCall(%v, _) = _, %v; want _, nil", tc.ctx, err)
- }
- if s, ok := status.FromError(err); !ok || s.Code() != tc.want {
- t.Fatalf("endpoint.client.EmptyCall(%v, _) = _, %v; want _, <status with Code()=%v>", tc.ctx, err, tc.want)
- }
- }
- }
- func (s) TestTapTimeout(t *testing.T) {
- sopts := []grpc.ServerOption{
- grpc.InTapHandle(func(ctx context.Context, _ *tap.Info) (context.Context, error) {
- c, cancel := context.WithCancel(ctx)
- // Call cancel instead of setting a deadline so we can detect which error
- // occurred -- this cancellation (desired) or the client's deadline
- // expired (indicating this cancellation did not affect the RPC).
- time.AfterFunc(10*time.Millisecond, cancel)
- return c, nil
- }),
- }
- ss := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- <-ctx.Done()
- return nil, status.Errorf(codes.Canceled, ctx.Err().Error())
- },
- }
- if err := ss.Start(sopts); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- // This was known to be flaky; test several times.
- for i := 0; i < 10; i++ {
- // Set our own deadline in case the server hangs.
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- res, err := ss.client.EmptyCall(ctx, &testpb.Empty{})
- cancel()
- if s, ok := status.FromError(err); !ok || s.Code() != codes.Canceled {
- t.Fatalf("ss.client.EmptyCall(context.Background(), _) = %v, %v; want nil, <status with Code()=Canceled>", res, err)
- }
- }
- }
- func (s) TestClientWriteFailsAfterServerClosesStream(t *testing.T) {
- ss := &stubServer{
- fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- return status.Errorf(codes.Internal, "")
- },
- }
- sopts := []grpc.ServerOption{}
- if err := ss.Start(sopts); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- stream, err := ss.client.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("Error while creating stream: %v", err)
- }
- for {
- if err := stream.Send(&testpb.StreamingOutputCallRequest{}); err == nil {
- time.Sleep(5 * time.Millisecond)
- } else if err == io.EOF {
- break // Success.
- } else {
- t.Fatalf("stream.Send(_) = %v, want io.EOF", err)
- }
- }
- }
- type windowSizeConfig struct {
- serverStream int32
- serverConn int32
- clientStream int32
- clientConn int32
- }
- func max(a, b int32) int32 {
- if a > b {
- return a
- }
- return b
- }
- func (s) TestConfigurableWindowSizeWithLargeWindow(t *testing.T) {
- wc := windowSizeConfig{
- serverStream: 8 * 1024 * 1024,
- serverConn: 12 * 1024 * 1024,
- clientStream: 6 * 1024 * 1024,
- clientConn: 8 * 1024 * 1024,
- }
- for _, e := range listTestEnv() {
- testConfigurableWindowSize(t, e, wc)
- }
- }
- func (s) TestConfigurableWindowSizeWithSmallWindow(t *testing.T) {
- wc := windowSizeConfig{
- serverStream: 1,
- serverConn: 1,
- clientStream: 1,
- clientConn: 1,
- }
- for _, e := range listTestEnv() {
- testConfigurableWindowSize(t, e, wc)
- }
- }
- func testConfigurableWindowSize(t *testing.T, e env, wc windowSizeConfig) {
- te := newTest(t, e)
- te.serverInitialWindowSize = wc.serverStream
- te.serverInitialConnWindowSize = wc.serverConn
- te.clientInitialWindowSize = wc.clientStream
- te.clientInitialConnWindowSize = wc.clientConn
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- stream, err := tc.FullDuplexCall(context.Background())
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- numOfIter := 11
- // Set message size to exhaust largest of window sizes.
- messageSize := max(max(wc.serverStream, wc.serverConn), max(wc.clientStream, wc.clientConn)) / int32(numOfIter-1)
- messageSize = max(messageSize, 64*1024)
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, messageSize)
- if err != nil {
- t.Fatal(err)
- }
- respParams := []*testpb.ResponseParameters{
- {
- Size: messageSize,
- },
- }
- req := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParams,
- Payload: payload,
- }
- for i := 0; i < numOfIter; i++ {
- if err := stream.Send(req); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, req, err)
- }
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = _, %v, want _, <nil>", stream, err)
- }
- }
- if err := stream.CloseSend(); err != nil {
- t.Fatalf("%v.CloseSend() = %v, want <nil>", stream, err)
- }
- }
- func (s) TestWaitForReadyConnection(t *testing.T) {
- for _, e := range listTestEnv() {
- testWaitForReadyConnection(t, e)
- }
- }
- func testWaitForReadyConnection(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn() // Non-blocking dial.
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- state := cc.GetState()
- // Wait for connection to be Ready.
- for ; state != connectivity.Ready && cc.WaitForStateChange(ctx, state); state = cc.GetState() {
- }
- if state != connectivity.Ready {
- t.Fatalf("Want connection state to be Ready, got %v", state)
- }
- ctx, cancel = context.WithTimeout(context.Background(), time.Second)
- defer cancel()
- // Make a fail-fast RPC.
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); err != nil {
- t.Fatalf("TestService/EmptyCall(_,_) = _, %v, want _, nil", err)
- }
- }
- type errCodec struct {
- noError bool
- }
- func (c *errCodec) Marshal(v interface{}) ([]byte, error) {
- if c.noError {
- return []byte{}, nil
- }
- return nil, fmt.Errorf("3987^12 + 4365^12 = 4472^12")
- }
- func (c *errCodec) Unmarshal(data []byte, v interface{}) error {
- return nil
- }
- func (c *errCodec) Name() string {
- return "Fermat's near-miss."
- }
- func (s) TestEncodeDoesntPanic(t *testing.T) {
- for _, e := range listTestEnv() {
- testEncodeDoesntPanic(t, e)
- }
- }
- func testEncodeDoesntPanic(t *testing.T, e env) {
- te := newTest(t, e)
- erc := &errCodec{}
- te.customCodec = erc
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- te.customCodec = nil
- tc := testpb.NewTestServiceClient(te.clientConn())
- // Failure case, should not panic.
- tc.EmptyCall(context.Background(), &testpb.Empty{})
- erc.noError = true
- // Passing case.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); err != nil {
- t.Fatalf("EmptyCall(_, _) = _, %v, want _, <nil>", err)
- }
- }
- func (s) TestSvrWriteStatusEarlyWrite(t *testing.T) {
- for _, e := range listTestEnv() {
- testSvrWriteStatusEarlyWrite(t, e)
- }
- }
- func testSvrWriteStatusEarlyWrite(t *testing.T, e env) {
- te := newTest(t, e)
- const smallSize = 1024
- const largeSize = 2048
- const extraLargeSize = 4096
- te.maxServerReceiveMsgSize = newInt(largeSize)
- te.maxServerSendMsgSize = newInt(largeSize)
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- extraLargePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, extraLargeSize)
- if err != nil {
- t.Fatal(err)
- }
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(smallSize),
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: extraLargePayload,
- }
- // Test recv case: server receives a message larger than maxServerReceiveMsgSize.
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err = stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send() = _, %v, want <nil>", stream, err)
- }
- if _, err = stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test send case: server sends a message larger than maxServerSendMsgSize.
- sreq.Payload = smallPayload
- respParam[0].Size = int32(extraLargeSize)
- stream, err = tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err = stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err = stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- }
- // The following functions with function name ending with TD indicates that they
- // should be deleted after old service config API is deprecated and deleted.
- func testServiceConfigSetupTD(t *testing.T, e env) (*test, chan grpc.ServiceConfig) {
- te := newTest(t, e)
- // We write before read.
- ch := make(chan grpc.ServiceConfig, 1)
- te.sc = ch
- te.userAgent = testAppUA
- te.declareLogNoise(
- "transport: http2Client.notifyError got notified that the client transport was broken EOF",
- "grpc: addrConn.transportMonitor exits due to: grpc: the connection is closing",
- "grpc: addrConn.resetTransport failed to create client transport: connection error",
- "Failed to dial : context canceled; please retry.",
- )
- return te, ch
- }
- func (s) TestServiceConfigGetMethodConfigTD(t *testing.T) {
- for _, e := range listTestEnv() {
- testGetMethodConfigTD(t, e)
- }
- }
- func testGetMethodConfigTD(t *testing.T, e env) {
- te, ch := testServiceConfigSetupTD(t, e)
- defer te.tearDown()
- mc1 := grpc.MethodConfig{
- WaitForReady: newBool(true),
- Timeout: newDuration(time.Millisecond),
- }
- mc2 := grpc.MethodConfig{WaitForReady: newBool(false)}
- m := make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/EmptyCall"] = mc1
- m["/grpc.testing.TestService/"] = mc2
- sc := grpc.ServiceConfig{
- Methods: m,
- }
- ch <- sc
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- // The following RPCs are expected to become non-fail-fast ones with 1ms deadline.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- m = make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/UnaryCall"] = mc1
- m["/grpc.testing.TestService/"] = mc2
- sc = grpc.ServiceConfig{
- Methods: m,
- }
- ch <- sc
- // Wait for the new service config to propagate.
- for {
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) == codes.DeadlineExceeded {
- continue
- }
- break
- }
- // The following RPCs are expected to become fail-fast.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.Unavailable)
- }
- }
- func (s) TestServiceConfigWaitForReadyTD(t *testing.T) {
- for _, e := range listTestEnv() {
- testServiceConfigWaitForReadyTD(t, e)
- }
- }
- func testServiceConfigWaitForReadyTD(t *testing.T, e env) {
- te, ch := testServiceConfigSetupTD(t, e)
- defer te.tearDown()
- // Case1: Client API set failfast to be false, and service config set wait_for_ready to be false, Client API should win, and the rpc will wait until deadline exceeds.
- mc := grpc.MethodConfig{
- WaitForReady: newBool(false),
- Timeout: newDuration(time.Millisecond),
- }
- m := make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/EmptyCall"] = mc
- m["/grpc.testing.TestService/FullDuplexCall"] = mc
- sc := grpc.ServiceConfig{
- Methods: m,
- }
- ch <- sc
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- // The following RPCs are expected to become non-fail-fast ones with 1ms deadline.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- if _, err := tc.FullDuplexCall(context.Background(), grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- // Generate a service config update.
- // Case2: Client API does not set failfast, and service config set wait_for_ready to be true, and the rpc will wait until deadline exceeds.
- mc.WaitForReady = newBool(true)
- m = make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/EmptyCall"] = mc
- m["/grpc.testing.TestService/FullDuplexCall"] = mc
- sc = grpc.ServiceConfig{
- Methods: m,
- }
- ch <- sc
- // Wait for the new service config to take effect.
- mc = cc.GetMethodConfig("/grpc.testing.TestService/EmptyCall")
- for {
- if !*mc.WaitForReady {
- time.Sleep(100 * time.Millisecond)
- mc = cc.GetMethodConfig("/grpc.testing.TestService/EmptyCall")
- continue
- }
- break
- }
- // The following RPCs are expected to become non-fail-fast ones with 1ms deadline.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- if _, err := tc.FullDuplexCall(context.Background()); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- }
- func (s) TestServiceConfigTimeoutTD(t *testing.T) {
- for _, e := range listTestEnv() {
- testServiceConfigTimeoutTD(t, e)
- }
- }
- func testServiceConfigTimeoutTD(t *testing.T, e env) {
- te, ch := testServiceConfigSetupTD(t, e)
- defer te.tearDown()
- // Case1: Client API sets timeout to be 1ns and ServiceConfig sets timeout to be 1hr. Timeout should be 1ns (min of 1ns and 1hr) and the rpc will wait until deadline exceeds.
- mc := grpc.MethodConfig{
- Timeout: newDuration(time.Hour),
- }
- m := make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/EmptyCall"] = mc
- m["/grpc.testing.TestService/FullDuplexCall"] = mc
- sc := grpc.ServiceConfig{
- Methods: m,
- }
- ch <- sc
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- // The following RPCs are expected to become non-fail-fast ones with 1ns deadline.
- ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- cancel()
- ctx, cancel = context.WithTimeout(context.Background(), time.Nanosecond)
- if _, err := tc.FullDuplexCall(ctx, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- cancel()
- // Generate a service config update.
- // Case2: Client API sets timeout to be 1hr and ServiceConfig sets timeout to be 1ns. Timeout should be 1ns (min of 1ns and 1hr) and the rpc will wait until deadline exceeds.
- mc.Timeout = newDuration(time.Nanosecond)
- m = make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/EmptyCall"] = mc
- m["/grpc.testing.TestService/FullDuplexCall"] = mc
- sc = grpc.ServiceConfig{
- Methods: m,
- }
- ch <- sc
- // Wait for the new service config to take effect.
- mc = cc.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall")
- for {
- if *mc.Timeout != time.Nanosecond {
- time.Sleep(100 * time.Millisecond)
- mc = cc.GetMethodConfig("/grpc.testing.TestService/FullDuplexCall")
- continue
- }
- break
- }
- ctx, cancel = context.WithTimeout(context.Background(), time.Hour)
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- cancel()
- ctx, cancel = context.WithTimeout(context.Background(), time.Hour)
- if _, err := tc.FullDuplexCall(ctx, grpc.WaitForReady(true)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- cancel()
- }
- func (s) TestServiceConfigMaxMsgSizeTD(t *testing.T) {
- for _, e := range listTestEnv() {
- testServiceConfigMaxMsgSizeTD(t, e)
- }
- }
- func testServiceConfigMaxMsgSizeTD(t *testing.T, e env) {
- // Setting up values and objects shared across all test cases.
- const smallSize = 1
- const largeSize = 1024
- const extraLargeSize = 2048
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- largePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, largeSize)
- if err != nil {
- t.Fatal(err)
- }
- extraLargePayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, extraLargeSize)
- if err != nil {
- t.Fatal(err)
- }
- mc := grpc.MethodConfig{
- MaxReqSize: newInt(extraLargeSize),
- MaxRespSize: newInt(extraLargeSize),
- }
- m := make(map[string]grpc.MethodConfig)
- m["/grpc.testing.TestService/UnaryCall"] = mc
- m["/grpc.testing.TestService/FullDuplexCall"] = mc
- sc := grpc.ServiceConfig{
- Methods: m,
- }
- // Case1: sc set maxReqSize to 2048 (send), maxRespSize to 2048 (recv).
- te1, ch1 := testServiceConfigSetupTD(t, e)
- te1.startServer(&testServer{security: e.security})
- defer te1.tearDown()
- ch1 <- sc
- tc := testpb.NewTestServiceClient(te1.clientConn())
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: int32(extraLargeSize),
- Payload: smallPayload,
- }
- // Test for unary RPC recv.
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = extraLargePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- respParam := []*testpb.ResponseParameters{
- {
- Size: int32(extraLargeSize),
- },
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: smallPayload,
- }
- stream, err := tc.FullDuplexCall(te1.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = extraLargePayload
- stream, err = tc.FullDuplexCall(te1.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- // Case2: Client API set maxReqSize to 1024 (send), maxRespSize to 1024 (recv). Sc sets maxReqSize to 2048 (send), maxRespSize to 2048 (recv).
- te2, ch2 := testServiceConfigSetupTD(t, e)
- te2.maxClientReceiveMsgSize = newInt(1024)
- te2.maxClientSendMsgSize = newInt(1024)
- te2.startServer(&testServer{security: e.security})
- defer te2.tearDown()
- ch2 <- sc
- tc = testpb.NewTestServiceClient(te2.clientConn())
- // Test for unary RPC recv.
- req.Payload = smallPayload
- req.ResponseSize = int32(largeSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = largePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- stream, err = tc.FullDuplexCall(te2.ctx)
- respParam[0].Size = int32(largeSize)
- sreq.Payload = smallPayload
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = largePayload
- stream, err = tc.FullDuplexCall(te2.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- // Case3: Client API set maxReqSize to 4096 (send), maxRespSize to 4096 (recv). Sc sets maxReqSize to 2048 (send), maxRespSize to 2048 (recv).
- te3, ch3 := testServiceConfigSetupTD(t, e)
- te3.maxClientReceiveMsgSize = newInt(4096)
- te3.maxClientSendMsgSize = newInt(4096)
- te3.startServer(&testServer{security: e.security})
- defer te3.tearDown()
- ch3 <- sc
- tc = testpb.NewTestServiceClient(te3.clientConn())
- // Test for unary RPC recv.
- req.Payload = smallPayload
- req.ResponseSize = int32(largeSize)
- if _, err := tc.UnaryCall(context.Background(), req); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want <nil>", err)
- }
- req.ResponseSize = int32(extraLargeSize)
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for unary RPC send.
- req.Payload = largePayload
- req.ResponseSize = int32(smallSize)
- if _, err := tc.UnaryCall(context.Background(), req); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want <nil>", err)
- }
- req.Payload = extraLargePayload
- if _, err := tc.UnaryCall(context.Background(), req); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, error code: %s", err, codes.ResourceExhausted)
- }
- // Test for streaming RPC recv.
- stream, err = tc.FullDuplexCall(te3.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam[0].Size = int32(largeSize)
- sreq.Payload = smallPayload
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = _, %v, want <nil>", stream, err)
- }
- respParam[0].Size = int32(extraLargeSize)
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Recv() = _, %v, want _, error code: %s", stream, err, codes.ResourceExhausted)
- }
- // Test for streaming RPC send.
- respParam[0].Size = int32(smallSize)
- sreq.Payload = largePayload
- stream, err = tc.FullDuplexCall(te3.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- sreq.Payload = extraLargePayload
- if err := stream.Send(sreq); err == nil || status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("%v.Send(%v) = %v, want _, error code: %s", stream, sreq, err, codes.ResourceExhausted)
- }
- }
- func (s) TestMethodFromServerStream(t *testing.T) {
- const testMethod = "/package.service/method"
- e := tcpClearRREnv
- te := newTest(t, e)
- var method string
- var ok bool
- te.unknownHandler = func(srv interface{}, stream grpc.ServerStream) error {
- method, ok = grpc.MethodFromServerStream(stream)
- return nil
- }
- te.startServer(nil)
- defer te.tearDown()
- _ = te.clientConn().Invoke(context.Background(), testMethod, nil, nil)
- if !ok || method != testMethod {
- t.Fatalf("Invoke with method %q, got %q, %v, want %q, true", testMethod, method, ok, testMethod)
- }
- }
- func (s) TestInterceptorCanAccessCallOptions(t *testing.T) {
- e := tcpClearRREnv
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- type observedOptions struct {
- headers []*metadata.MD
- trailers []*metadata.MD
- peer []*peer.Peer
- creds []credentials.PerRPCCredentials
- failFast []bool
- maxRecvSize []int
- maxSendSize []int
- compressor []string
- subtype []string
- }
- var observedOpts observedOptions
- populateOpts := func(opts []grpc.CallOption) {
- for _, o := range opts {
- switch o := o.(type) {
- case grpc.HeaderCallOption:
- observedOpts.headers = append(observedOpts.headers, o.HeaderAddr)
- case grpc.TrailerCallOption:
- observedOpts.trailers = append(observedOpts.trailers, o.TrailerAddr)
- case grpc.PeerCallOption:
- observedOpts.peer = append(observedOpts.peer, o.PeerAddr)
- case grpc.PerRPCCredsCallOption:
- observedOpts.creds = append(observedOpts.creds, o.Creds)
- case grpc.FailFastCallOption:
- observedOpts.failFast = append(observedOpts.failFast, o.FailFast)
- case grpc.MaxRecvMsgSizeCallOption:
- observedOpts.maxRecvSize = append(observedOpts.maxRecvSize, o.MaxRecvMsgSize)
- case grpc.MaxSendMsgSizeCallOption:
- observedOpts.maxSendSize = append(observedOpts.maxSendSize, o.MaxSendMsgSize)
- case grpc.CompressorCallOption:
- observedOpts.compressor = append(observedOpts.compressor, o.CompressorType)
- case grpc.ContentSubtypeCallOption:
- observedOpts.subtype = append(observedOpts.subtype, o.ContentSubtype)
- }
- }
- }
- te.unaryClientInt = func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
- populateOpts(opts)
- return nil
- }
- te.streamClientInt = func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string, streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream, error) {
- populateOpts(opts)
- return nil, nil
- }
- defaults := []grpc.CallOption{
- grpc.WaitForReady(true),
- grpc.MaxCallRecvMsgSize(1010),
- }
- tc := testpb.NewTestServiceClient(te.clientConn(grpc.WithDefaultCallOptions(defaults...)))
- var headers metadata.MD
- var trailers metadata.MD
- var pr peer.Peer
- tc.UnaryCall(context.Background(), &testpb.SimpleRequest{},
- grpc.MaxCallRecvMsgSize(100),
- grpc.MaxCallSendMsgSize(200),
- grpc.PerRPCCredentials(testPerRPCCredentials{}),
- grpc.Header(&headers),
- grpc.Trailer(&trailers),
- grpc.Peer(&pr))
- expected := observedOptions{
- failFast: []bool{false},
- maxRecvSize: []int{1010, 100},
- maxSendSize: []int{200},
- creds: []credentials.PerRPCCredentials{testPerRPCCredentials{}},
- headers: []*metadata.MD{&headers},
- trailers: []*metadata.MD{&trailers},
- peer: []*peer.Peer{&pr},
- }
- if !reflect.DeepEqual(expected, observedOpts) {
- t.Errorf("unary call did not observe expected options: expected %#v, got %#v", expected, observedOpts)
- }
- observedOpts = observedOptions{} // reset
- tc.StreamingInputCall(context.Background(),
- grpc.WaitForReady(false),
- grpc.MaxCallSendMsgSize(2020),
- grpc.UseCompressor("comp-type"),
- grpc.CallContentSubtype("json"))
- expected = observedOptions{
- failFast: []bool{false, true},
- maxRecvSize: []int{1010},
- maxSendSize: []int{2020},
- compressor: []string{"comp-type"},
- subtype: []string{"json"},
- }
- if !reflect.DeepEqual(expected, observedOpts) {
- t.Errorf("streaming call did not observe expected options: expected %#v, got %#v", expected, observedOpts)
- }
- }
- func (s) TestCompressorRegister(t *testing.T) {
- for _, e := range listTestEnv() {
- testCompressorRegister(t, e)
- }
- }
- func testCompressorRegister(t *testing.T, e env) {
- te := newTest(t, e)
- te.clientCompression = false
- te.serverCompression = false
- te.clientUseCompression = true
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- // Unary call
- const argSize = 271828
- const respSize = 314159
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- ctx := metadata.NewOutgoingContext(context.Background(), metadata.Pairs("something", "something"))
- if _, err := tc.UnaryCall(ctx, req); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, <nil>", err)
- }
- // Streaming RPC
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: 31415,
- },
- }
- payload, err = newPayload(testpb.PayloadType_COMPRESSABLE, int32(31415))
- if err != nil {
- t.Fatal(err)
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: payload,
- }
- if err := stream.Send(sreq); err != nil {
- t.Fatalf("%v.Send(%v) = %v, want <nil>", stream, sreq, err)
- }
- if _, err := stream.Recv(); err != nil {
- t.Fatalf("%v.Recv() = %v, want <nil>", stream, err)
- }
- }
- func (s) TestServeExitsWhenListenerClosed(t *testing.T) {
- ss := &stubServer{
- emptyCall: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
- return &testpb.Empty{}, nil
- },
- }
- s := grpc.NewServer()
- defer s.Stop()
- testpb.RegisterTestServiceServer(s, ss)
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to create listener: %v", err)
- }
- done := make(chan struct{})
- go func() {
- s.Serve(lis)
- close(done)
- }()
- cc, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure(), grpc.WithBlock())
- if err != nil {
- t.Fatalf("Failed to dial server: %v", err)
- }
- defer cc.Close()
- c := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- if _, err := c.EmptyCall(ctx, &testpb.Empty{}); err != nil {
- t.Fatalf("Failed to send test RPC to server: %v", err)
- }
- if err := lis.Close(); err != nil {
- t.Fatalf("Failed to close listener: %v", err)
- }
- const timeout = 5 * time.Second
- timer := time.NewTimer(timeout)
- select {
- case <-done:
- return
- case <-timer.C:
- t.Fatalf("Serve did not return after %v", timeout)
- }
- }
- // Service handler returns status with invalid utf8 message.
- func (s) TestStatusInvalidUTF8Message(t *testing.T) {
- var (
- origMsg = string([]byte{0xff, 0xfe, 0xfd})
- wantMsg = "���"
- )
- ss := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- return nil, status.Errorf(codes.Internal, origMsg)
- },
- }
- if err := ss.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- if _, err := ss.client.EmptyCall(ctx, &testpb.Empty{}); status.Convert(err).Message() != wantMsg {
- t.Fatalf("ss.client.EmptyCall(_, _) = _, %v (msg %q); want _, err with msg %q", err, status.Convert(err).Message(), wantMsg)
- }
- }
- // Service handler returns status with details and invalid utf8 message. Proto
- // will fail to marshal the status because of the invalid utf8 message. Details
- // will be dropped when sending.
- func (s) TestStatusInvalidUTF8Details(t *testing.T) {
- grpctest.TLogger.ExpectError("transport: failed to marshal rpc status")
- var (
- origMsg = string([]byte{0xff, 0xfe, 0xfd})
- wantMsg = "���"
- )
- ss := &stubServer{
- emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
- st := status.New(codes.Internal, origMsg)
- st, err := st.WithDetails(&testpb.Empty{})
- if err != nil {
- return nil, err
- }
- return nil, st.Err()
- },
- }
- if err := ss.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
- defer cancel()
- _, err := ss.client.EmptyCall(ctx, &testpb.Empty{})
- st := status.Convert(err)
- if st.Message() != wantMsg {
- t.Fatalf("ss.client.EmptyCall(_, _) = _, %v (msg %q); want _, err with msg %q", err, st.Message(), wantMsg)
- }
- if len(st.Details()) != 0 {
- // Details should be dropped on the server side.
- t.Fatalf("RPC status contain details: %v, want no details", st.Details())
- }
- }
- func (s) TestClientDoesntDeadlockWhileWritingErrornousLargeMessages(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.httpHandler {
- continue
- }
- testClientDoesntDeadlockWhileWritingErrornousLargeMessages(t, e)
- }
- }
- func testClientDoesntDeadlockWhileWritingErrornousLargeMessages(t *testing.T, e env) {
- te := newTest(t, e)
- te.userAgent = testAppUA
- smallSize := 1024
- te.maxServerReceiveMsgSize = &smallSize
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, 1048576)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- Payload: payload,
- }
- var wg sync.WaitGroup
- for i := 0; i < 10; i++ {
- wg.Add(1)
- go func() {
- defer wg.Done()
- for j := 0; j < 100; j++ {
- ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(time.Second*10))
- defer cancel()
- if _, err := tc.UnaryCall(ctx, req); status.Code(err) != codes.ResourceExhausted {
- t.Errorf("TestService/UnaryCall(_,_) = _. %v, want code: %s", err, codes.ResourceExhausted)
- return
- }
- }
- }()
- }
- wg.Wait()
- }
- func (s) TestRPCTimeout(t *testing.T) {
- for _, e := range listTestEnv() {
- testRPCTimeout(t, e)
- }
- }
- func testRPCTimeout(t *testing.T, e env) {
- te := newTest(t, e)
- te.startServer(&testServer{security: e.security, unaryCallSleepTime: 500 * time.Millisecond})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- const argSize = 2718
- const respSize = 314
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: respSize,
- Payload: payload,
- }
- for i := -1; i <= 10; i++ {
- ctx, cancel := context.WithTimeout(context.Background(), time.Duration(i)*time.Millisecond)
- if _, err := tc.UnaryCall(ctx, req); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/UnaryCallv(_, _) = _, %v; want <nil>, error code: %s", err, codes.DeadlineExceeded)
- }
- cancel()
- }
- }
- func (s) TestDisabledIOBuffers(t *testing.T) {
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(60000))
- if err != nil {
- t.Fatalf("Failed to create payload: %v", err)
- }
- req := &testpb.StreamingOutputCallRequest{
- Payload: payload,
- }
- resp := &testpb.StreamingOutputCallResponse{
- Payload: payload,
- }
- ss := &stubServer{
- fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- for {
- in, err := stream.Recv()
- if err == io.EOF {
- return nil
- }
- if err != nil {
- t.Errorf("stream.Recv() = _, %v, want _, <nil>", err)
- return err
- }
- if !reflect.DeepEqual(in.Payload.Body, payload.Body) {
- t.Errorf("Received message(len: %v) on server not what was expected(len: %v).", len(in.Payload.Body), len(payload.Body))
- return err
- }
- if err := stream.Send(resp); err != nil {
- t.Errorf("stream.Send(_)= %v, want <nil>", err)
- return err
- }
- }
- },
- }
- s := grpc.NewServer(grpc.WriteBufferSize(0), grpc.ReadBufferSize(0))
- testpb.RegisterTestServiceServer(s, ss)
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to create listener: %v", err)
- }
- done := make(chan struct{})
- go func() {
- s.Serve(lis)
- close(done)
- }()
- defer s.Stop()
- dctx, dcancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer dcancel()
- cc, err := grpc.DialContext(dctx, lis.Addr().String(), grpc.WithInsecure(), grpc.WithBlock(), grpc.WithWriteBufferSize(0), grpc.WithReadBufferSize(0))
- if err != nil {
- t.Fatalf("Failed to dial server")
- }
- defer cc.Close()
- c := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- stream, err := c.FullDuplexCall(ctx, grpc.WaitForReady(true))
- if err != nil {
- t.Fatalf("Failed to send test RPC to server")
- }
- for i := 0; i < 10; i++ {
- if err := stream.Send(req); err != nil {
- t.Fatalf("stream.Send(_) = %v, want <nil>", err)
- }
- in, err := stream.Recv()
- if err != nil {
- t.Fatalf("stream.Recv() = _, %v, want _, <nil>", err)
- }
- if !reflect.DeepEqual(in.Payload.Body, payload.Body) {
- t.Fatalf("Received message(len: %v) on client not what was expected(len: %v).", len(in.Payload.Body), len(payload.Body))
- }
- }
- stream.CloseSend()
- if _, err := stream.Recv(); err != io.EOF {
- t.Fatalf("stream.Recv() = _, %v, want _, io.EOF", err)
- }
- }
- func (s) TestServerMaxHeaderListSizeClientUserViolation(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.httpHandler {
- continue
- }
- testServerMaxHeaderListSizeClientUserViolation(t, e)
- }
- }
- func testServerMaxHeaderListSizeClientUserViolation(t *testing.T, e env) {
- te := newTest(t, e)
- te.maxServerHeaderListSize = new(uint32)
- *te.maxServerHeaderListSize = 216
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- metadata.AppendToOutgoingContext(ctx, "oversize", string(make([]byte, 216)))
- var err error
- if err = verifyResultWithDelay(func() (bool, error) {
- if _, err = tc.EmptyCall(ctx, &testpb.Empty{}); err != nil && status.Code(err) == codes.Internal {
- return true, nil
- }
- return false, fmt.Errorf("tc.EmptyCall() = _, err: %v, want _, error code: %v", err, codes.Internal)
- }); err != nil {
- t.Fatal(err)
- }
- }
- func (s) TestClientMaxHeaderListSizeServerUserViolation(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.httpHandler {
- continue
- }
- testClientMaxHeaderListSizeServerUserViolation(t, e)
- }
- }
- func testClientMaxHeaderListSizeServerUserViolation(t *testing.T, e env) {
- te := newTest(t, e)
- te.maxClientHeaderListSize = new(uint32)
- *te.maxClientHeaderListSize = 1 // any header server sends will violate
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- var err error
- if err = verifyResultWithDelay(func() (bool, error) {
- if _, err = tc.EmptyCall(ctx, &testpb.Empty{}); err != nil && status.Code(err) == codes.Internal {
- return true, nil
- }
- return false, fmt.Errorf("tc.EmptyCall() = _, err: %v, want _, error code: %v", err, codes.Internal)
- }); err != nil {
- t.Fatal(err)
- }
- }
- func (s) TestServerMaxHeaderListSizeClientIntentionalViolation(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.httpHandler || e.security == "tls" {
- continue
- }
- testServerMaxHeaderListSizeClientIntentionalViolation(t, e)
- }
- }
- func testServerMaxHeaderListSizeClientIntentionalViolation(t *testing.T, e env) {
- te := newTest(t, e)
- te.maxServerHeaderListSize = new(uint32)
- *te.maxServerHeaderListSize = 512
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc, dw := te.clientConnWithConnControl()
- tc := &testServiceClientWrapper{TestServiceClient: testpb.NewTestServiceClient(cc)}
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want _, <nil>", tc, err)
- }
- rcw := dw.getRawConnWrapper()
- val := make([]string, 512)
- for i := range val {
- val[i] = "a"
- }
- // allow for client to send the initial header
- time.Sleep(100 * time.Millisecond)
- rcw.writeHeaders(http2.HeadersFrameParam{
- StreamID: tc.getCurrentStreamID(),
- BlockFragment: rcw.encodeHeader("oversize", strings.Join(val, "")),
- EndStream: false,
- EndHeaders: true,
- })
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.Internal {
- t.Fatalf("stream.Recv() = _, %v, want _, error code: %v", err, codes.Internal)
- }
- }
- func (s) TestClientMaxHeaderListSizeServerIntentionalViolation(t *testing.T) {
- for _, e := range listTestEnv() {
- if e.httpHandler || e.security == "tls" {
- continue
- }
- testClientMaxHeaderListSizeServerIntentionalViolation(t, e)
- }
- }
- func testClientMaxHeaderListSizeServerIntentionalViolation(t *testing.T, e env) {
- te := newTest(t, e)
- te.maxClientHeaderListSize = new(uint32)
- *te.maxClientHeaderListSize = 200
- lw := te.startServerWithConnControl(&testServer{security: e.security, setHeaderOnly: true})
- defer te.tearDown()
- cc, _ := te.clientConnWithConnControl()
- tc := &testServiceClientWrapper{TestServiceClient: testpb.NewTestServiceClient(cc)}
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- stream, err := tc.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want _, <nil>", tc, err)
- }
- var i int
- var rcw *rawConnWrapper
- for i = 0; i < 100; i++ {
- rcw = lw.getLastConn()
- if rcw != nil {
- break
- }
- time.Sleep(10 * time.Millisecond)
- continue
- }
- if i == 100 {
- t.Fatalf("failed to create server transport after 1s")
- }
- val := make([]string, 200)
- for i := range val {
- val[i] = "a"
- }
- // allow for client to send the initial header.
- time.Sleep(100 * time.Millisecond)
- rcw.writeHeaders(http2.HeadersFrameParam{
- StreamID: tc.getCurrentStreamID(),
- BlockFragment: rcw.encodeRawHeader("oversize", strings.Join(val, "")),
- EndStream: false,
- EndHeaders: true,
- })
- if _, err := stream.Recv(); err == nil || status.Code(err) != codes.Internal {
- t.Fatalf("stream.Recv() = _, %v, want _, error code: %v", err, codes.Internal)
- }
- }
- func (s) TestNetPipeConn(t *testing.T) {
- // This test will block indefinitely if grpc writes both client and server
- // prefaces without either reading from the Conn.
- pl := testutils.NewPipeListener()
- s := grpc.NewServer()
- defer s.Stop()
- ts := &funcServer{unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- return &testpb.SimpleResponse{}, nil
- }}
- testpb.RegisterTestServiceServer(s, ts)
- go s.Serve(pl)
- ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
- defer cancel()
- cc, err := grpc.DialContext(ctx, "", grpc.WithInsecure(), grpc.WithDialer(pl.Dialer()))
- if err != nil {
- t.Fatalf("Error creating client: %v", err)
- }
- defer cc.Close()
- client := testpb.NewTestServiceClient(cc)
- if _, err := client.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
- t.Fatalf("UnaryCall(_) = _, %v; want _, nil", err)
- }
- }
- func (s) TestLargeTimeout(t *testing.T) {
- for _, e := range listTestEnv() {
- testLargeTimeout(t, e)
- }
- }
- func testLargeTimeout(t *testing.T, e env) {
- te := newTest(t, e)
- te.declareLogNoise("Server.processUnaryRPC failed to write status")
- ts := &funcServer{}
- te.startServer(ts)
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- timeouts := []time.Duration{
- time.Duration(math.MaxInt64), // will be (correctly) converted to
- // 2562048 hours, which overflows upon converting back to an int64
- 2562047 * time.Hour, // the largest timeout that does not overflow
- }
- for i, maxTimeout := range timeouts {
- ts.unaryCall = func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- deadline, ok := ctx.Deadline()
- timeout := time.Until(deadline)
- minTimeout := maxTimeout - 5*time.Second
- if !ok || timeout < minTimeout || timeout > maxTimeout {
- t.Errorf("ctx.Deadline() = (now+%v), %v; want [%v, %v], true", timeout, ok, minTimeout, maxTimeout)
- return nil, status.Error(codes.OutOfRange, "deadline error")
- }
- return &testpb.SimpleResponse{}, nil
- }
- ctx, cancel := context.WithTimeout(context.Background(), maxTimeout)
- defer cancel()
- if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
- t.Errorf("case %v: UnaryCall(_) = _, %v; want _, nil", i, err)
- }
- }
- }
- // Proxies typically send GO_AWAY followed by connection closure a minute or so later. This
- // test ensures that the connection is re-created after GO_AWAY and not affected by the
- // subsequent (old) connection closure.
- func (s) TestGoAwayThenClose(t *testing.T) {
- ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
- defer cancel()
- lis1, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- s1 := grpc.NewServer()
- defer s1.Stop()
- ts := &funcServer{
- unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- return &testpb.SimpleResponse{}, nil
- },
- fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- // Wait forever.
- _, err := stream.Recv()
- if err == nil {
- t.Error("expected to never receive any message")
- }
- return err
- },
- }
- testpb.RegisterTestServiceServer(s1, ts)
- go s1.Serve(lis1)
- conn2Established := grpcsync.NewEvent()
- lis2, err := listenWithNotifyingListener("tcp", "localhost:0", conn2Established)
- if err != nil {
- t.Fatalf("Error while listening. Err: %v", err)
- }
- s2 := grpc.NewServer()
- defer s2.Stop()
- testpb.RegisterTestServiceServer(s2, ts)
- go s2.Serve(lis2)
- r := manual.NewBuilderWithScheme("whatever")
- r.InitialState(resolver.State{Addresses: []resolver.Address{
- {Addr: lis1.Addr().String()},
- }})
- cc, err := grpc.DialContext(ctx, r.Scheme()+":///", grpc.WithResolvers(r), grpc.WithInsecure())
- if err != nil {
- t.Fatalf("Error creating client: %v", err)
- }
- defer cc.Close()
- client := testpb.NewTestServiceClient(cc)
- // Should go on connection 1. We use a long-lived RPC because it will cause GracefulStop to send GO_AWAY, but the
- // connection doesn't get closed until the server stops and the client receives.
- stream, err := client.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("FullDuplexCall(_) = _, %v; want _, nil", err)
- }
- r.UpdateState(resolver.State{Addresses: []resolver.Address{
- {Addr: lis1.Addr().String()},
- {Addr: lis2.Addr().String()},
- }})
- // Send GO_AWAY to connection 1.
- go s1.GracefulStop()
- // Wait for connection 2 to be established.
- <-conn2Established.Done()
- // Close connection 1.
- s1.Stop()
- // Wait for client to close.
- _, err = stream.Recv()
- if err == nil {
- t.Fatal("expected the stream to die, but got a successful Recv")
- }
- // Do a bunch of RPCs, make sure it stays stable. These should go to connection 2.
- for i := 0; i < 10; i++ {
- if _, err := client.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
- t.Fatalf("UnaryCall(_) = _, %v; want _, nil", err)
- }
- }
- }
- func listenWithNotifyingListener(network, address string, event *grpcsync.Event) (net.Listener, error) {
- lis, err := net.Listen(network, address)
- if err != nil {
- return nil, err
- }
- return notifyingListener{connEstablished: event, Listener: lis}, nil
- }
- type notifyingListener struct {
- connEstablished *grpcsync.Event
- net.Listener
- }
- func (lis notifyingListener) Accept() (net.Conn, error) {
- defer lis.connEstablished.Fire()
- return lis.Listener.Accept()
- }
- func (s) TestRPCWaitsForResolver(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- te.startServer(&testServer{security: tcpClearRREnv.security})
- defer te.tearDown()
- r := manual.NewBuilderWithScheme("whatever")
- te.resolverScheme = r.Scheme()
- te.nonBlockingDial = true
- cc := te.clientConn(grpc.WithResolvers(r))
- tc := testpb.NewTestServiceClient(cc)
- ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
- defer cancel()
- // With no resolved addresses yet, this will timeout.
- if _, err := tc.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- go func() {
- time.Sleep(time.Second)
- r.UpdateState(resolver.State{
- Addresses: []resolver.Address{{Addr: te.srvAddr}},
- ServiceConfig: parseCfg(r, `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "UnaryCall"
- }
- ],
- "maxRequestMessageBytes": 0
- }
- ]
- }`)})
- }()
- // We wait a second before providing a service config and resolving
- // addresses. So this will wait for that and then honor the
- // maxRequestMessageBytes it contains.
- if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{ResponseType: testpb.PayloadType_UNCOMPRESSABLE}); status.Code(err) != codes.ResourceExhausted {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, nil", err)
- }
- if got := ctx.Err(); got != nil {
- t.Fatalf("ctx.Err() = %v; want nil (deadline should be set short by service config)", got)
- }
- if _, err := tc.UnaryCall(ctx, &testpb.SimpleRequest{}); err != nil {
- t.Fatalf("TestService/UnaryCall(_, _) = _, %v, want _, nil", err)
- }
- }
- func (s) TestHTTPHeaderFrameErrorHandlingHTTPMode(t *testing.T) {
- // Non-gRPC content-type fallback path.
- for httpCode := range transport.HTTPStatusConvTab {
- doHTTPHeaderTest(t, transport.HTTPStatusConvTab[int(httpCode)], []string{
- ":status", fmt.Sprintf("%d", httpCode),
- "content-type", "text/html", // non-gRPC content type to switch to HTTP mode.
- "grpc-status", "1", // Make up a gRPC status error
- "grpc-status-details-bin", "???", // Make up a gRPC field parsing error
- })
- }
- // Missing content-type fallback path.
- for httpCode := range transport.HTTPStatusConvTab {
- doHTTPHeaderTest(t, transport.HTTPStatusConvTab[int(httpCode)], []string{
- ":status", fmt.Sprintf("%d", httpCode),
- // Omitting content type to switch to HTTP mode.
- "grpc-status", "1", // Make up a gRPC status error
- "grpc-status-details-bin", "???", // Make up a gRPC field parsing error
- })
- }
- // Malformed HTTP status when fallback.
- doHTTPHeaderTest(t, codes.Internal, []string{
- ":status", "abc",
- // Omitting content type to switch to HTTP mode.
- "grpc-status", "1", // Make up a gRPC status error
- "grpc-status-details-bin", "???", // Make up a gRPC field parsing error
- })
- }
- // Testing erroneous ResponseHeader or Trailers-only (delivered in the first HEADERS frame).
- func (s) TestHTTPHeaderFrameErrorHandlingInitialHeader(t *testing.T) {
- for _, test := range []struct {
- header []string
- errCode codes.Code
- }{
- {
- // missing gRPC status.
- header: []string{
- ":status", "403",
- "content-type", "application/grpc",
- },
- errCode: codes.Unknown,
- },
- {
- // malformed grpc-status.
- header: []string{
- ":status", "502",
- "content-type", "application/grpc",
- "grpc-status", "abc",
- },
- errCode: codes.Internal,
- },
- {
- // Malformed grpc-tags-bin field.
- header: []string{
- ":status", "502",
- "content-type", "application/grpc",
- "grpc-status", "0",
- "grpc-tags-bin", "???",
- },
- errCode: codes.Internal,
- },
- {
- // gRPC status error.
- header: []string{
- ":status", "502",
- "content-type", "application/grpc",
- "grpc-status", "3",
- },
- errCode: codes.InvalidArgument,
- },
- } {
- doHTTPHeaderTest(t, test.errCode, test.header)
- }
- }
- // Testing non-Trailers-only Trailers (delievered in second HEADERS frame)
- func (s) TestHTTPHeaderFrameErrorHandlingNormalTrailer(t *testing.T) {
- for _, test := range []struct {
- responseHeader []string
- trailer []string
- errCode codes.Code
- }{
- {
- responseHeader: []string{
- ":status", "200",
- "content-type", "application/grpc",
- },
- trailer: []string{
- // trailer missing grpc-status
- ":status", "502",
- },
- errCode: codes.Unknown,
- },
- {
- responseHeader: []string{
- ":status", "404",
- "content-type", "application/grpc",
- },
- trailer: []string{
- // malformed grpc-status-details-bin field
- "grpc-status", "0",
- "grpc-status-details-bin", "????",
- },
- errCode: codes.Internal,
- },
- } {
- doHTTPHeaderTest(t, test.errCode, test.responseHeader, test.trailer)
- }
- }
- func (s) TestHTTPHeaderFrameErrorHandlingMoreThanTwoHeaders(t *testing.T) {
- header := []string{
- ":status", "200",
- "content-type", "application/grpc",
- }
- doHTTPHeaderTest(t, codes.Internal, header, header, header)
- }
- type httpServer struct {
- headerFields [][]string
- }
- func (s *httpServer) writeHeader(framer *http2.Framer, sid uint32, headerFields []string, endStream bool) error {
- if len(headerFields)%2 == 1 {
- panic("odd number of kv args")
- }
- var buf bytes.Buffer
- henc := hpack.NewEncoder(&buf)
- for len(headerFields) > 0 {
- k, v := headerFields[0], headerFields[1]
- headerFields = headerFields[2:]
- henc.WriteField(hpack.HeaderField{Name: k, Value: v})
- }
- return framer.WriteHeaders(http2.HeadersFrameParam{
- StreamID: sid,
- BlockFragment: buf.Bytes(),
- EndStream: endStream,
- EndHeaders: true,
- })
- }
- func (s *httpServer) start(t *testing.T, lis net.Listener) {
- // Launch an HTTP server to send back header.
- go func() {
- conn, err := lis.Accept()
- if err != nil {
- t.Errorf("Error accepting connection: %v", err)
- return
- }
- defer conn.Close()
- // Read preface sent by client.
- if _, err = io.ReadFull(conn, make([]byte, len(http2.ClientPreface))); err != nil {
- t.Errorf("Error at server-side while reading preface from client. Err: %v", err)
- return
- }
- reader := bufio.NewReader(conn)
- writer := bufio.NewWriter(conn)
- framer := http2.NewFramer(writer, reader)
- if err = framer.WriteSettingsAck(); err != nil {
- t.Errorf("Error at server-side while sending Settings ack. Err: %v", err)
- return
- }
- writer.Flush() // necessary since client is expecting preface before declaring connection fully setup.
- var sid uint32
- // Read frames until a header is received.
- for {
- frame, err := framer.ReadFrame()
- if err != nil {
- t.Errorf("Error at server-side while reading frame. Err: %v", err)
- return
- }
- if hframe, ok := frame.(*http2.HeadersFrame); ok {
- sid = hframe.Header().StreamID
- break
- }
- }
- for i, headers := range s.headerFields {
- if err = s.writeHeader(framer, sid, headers, i == len(s.headerFields)-1); err != nil {
- t.Errorf("Error at server-side while writing headers. Err: %v", err)
- return
- }
- writer.Flush()
- }
- }()
- }
- func doHTTPHeaderTest(t *testing.T, errCode codes.Code, headerFields ...[]string) {
- t.Helper()
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to listen. Err: %v", err)
- }
- defer lis.Close()
- server := &httpServer{
- headerFields: headerFields,
- }
- server.start(t, lis)
- cc, err := grpc.Dial(lis.Addr().String(), grpc.WithInsecure())
- if err != nil {
- t.Fatalf("failed to dial due to err: %v", err)
- }
- defer cc.Close()
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- client := testpb.NewTestServiceClient(cc)
- stream, err := client.FullDuplexCall(ctx)
- if err != nil {
- t.Fatalf("error creating stream due to err: %v", err)
- }
- if _, err := stream.Recv(); err == nil || status.Code(err) != errCode {
- t.Fatalf("stream.Recv() = _, %v, want error code: %v", err, errCode)
- }
- }
- func parseCfg(r *manual.Resolver, s string) *serviceconfig.ParseResult {
- g := r.CC.ParseServiceConfig(s)
- if g.Err != nil {
- panic(fmt.Sprintf("Error parsing config %q: %v", s, g.Err))
- }
- return g
- }
- func (s) TestClientCancellationPropagatesUnary(t *testing.T) {
- wg := &sync.WaitGroup{}
- called, done := make(chan struct{}), make(chan struct{})
- ss := &stubServer{
- emptyCall: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
- close(called)
- <-ctx.Done()
- err := ctx.Err()
- if err != context.Canceled {
- t.Errorf("ctx.Err() = %v; want context.Canceled", err)
- }
- close(done)
- return nil, err
- },
- }
- if err := ss.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- wg.Add(1)
- go func() {
- if _, err := ss.client.EmptyCall(ctx, &testpb.Empty{}); status.Code(err) != codes.Canceled {
- t.Errorf("ss.client.EmptyCall() = _, %v; want _, Code()=codes.Canceled", err)
- }
- wg.Done()
- }()
- select {
- case <-called:
- case <-time.After(5 * time.Second):
- t.Fatalf("failed to perform EmptyCall after 10s")
- }
- cancel()
- select {
- case <-done:
- case <-time.After(5 * time.Second):
- t.Fatalf("server failed to close done chan due to cancellation propagation")
- }
- wg.Wait()
- }
- type badGzipCompressor struct{}
- func (badGzipCompressor) Do(w io.Writer, p []byte) error {
- buf := &bytes.Buffer{}
- gzw := gzip.NewWriter(buf)
- if _, err := gzw.Write(p); err != nil {
- return err
- }
- err := gzw.Close()
- bs := buf.Bytes()
- if len(bs) >= 6 {
- bs[len(bs)-6] ^= 1 // modify checksum at end by 1 byte
- }
- w.Write(bs)
- return err
- }
- func (badGzipCompressor) Type() string {
- return "gzip"
- }
- func (s) TestGzipBadChecksum(t *testing.T) {
- ss := &stubServer{
- unaryCall: func(ctx context.Context, _ *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- return &testpb.SimpleResponse{}, nil
- },
- }
- if err := ss.Start(nil, grpc.WithCompressor(badGzipCompressor{})); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- p, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(1024))
- if err != nil {
- t.Fatalf("Unexpected error from newPayload: %v", err)
- }
- if _, err := ss.client.UnaryCall(ctx, &testpb.SimpleRequest{Payload: p}); err == nil ||
- status.Code(err) != codes.Internal ||
- !strings.Contains(status.Convert(err).Message(), gzip.ErrChecksum.Error()) {
- t.Errorf("ss.client.UnaryCall(_) = _, %v\n\twant: _, status(codes.Internal, contains %q)", err, gzip.ErrChecksum)
- }
- }
- // When an RPC is canceled, it's possible that the last Recv() returns before
- // all call options' after are executed.
- func (s) TestCanceledRPCCallOptionRace(t *testing.T) {
- ss := &stubServer{
- fullDuplexCall: func(stream testpb.TestService_FullDuplexCallServer) error {
- err := stream.Send(&testpb.StreamingOutputCallResponse{})
- if err != nil {
- return err
- }
- <-stream.Context().Done()
- return nil
- },
- }
- if err := ss.Start(nil); err != nil {
- t.Fatalf("Error starting endpoint server: %v", err)
- }
- defer ss.Stop()
- const count = 1000
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- var wg sync.WaitGroup
- wg.Add(count)
- for i := 0; i < count; i++ {
- go func() {
- defer wg.Done()
- var p peer.Peer
- ctx, cancel := context.WithCancel(ctx)
- defer cancel()
- stream, err := ss.client.FullDuplexCall(ctx, grpc.Peer(&p))
- if err != nil {
- t.Errorf("_.FullDuplexCall(_) = _, %v", err)
- return
- }
- if err := stream.Send(&testpb.StreamingOutputCallRequest{}); err != nil {
- t.Errorf("_ has error %v while sending", err)
- return
- }
- if _, err := stream.Recv(); err != nil {
- t.Errorf("%v.Recv() = %v", stream, err)
- return
- }
- cancel()
- if _, err := stream.Recv(); status.Code(err) != codes.Canceled {
- t.Errorf("%v compleled with error %v, want %s", stream, err, codes.Canceled)
- return
- }
- // If recv returns before call options are executed, peer.Addr is not set,
- // fail the test.
- if p.Addr == nil {
- t.Errorf("peer.Addr is nil, want non-nil")
- return
- }
- }()
- }
- wg.Wait()
- }
|