1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201 |
- /*
- *
- * 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.
- *
- */
- //go:generate protoc --go_out=plugins=grpc:. codec_perf/perf.proto
- //go:generate protoc --go_out=plugins=grpc:. grpc_testing/test.proto
- package test
- import (
- "bytes"
- "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"
- spb "google.golang.org/genproto/googleapis/rpc/status"
- "google.golang.org/grpc"
- "google.golang.org/grpc/balancer/roundrobin"
- "google.golang.org/grpc/codes"
- "google.golang.org/grpc/connectivity"
- "google.golang.org/grpc/credentials"
- _ "google.golang.org/grpc/encoding/gzip"
- _ "google.golang.org/grpc/grpclog/glogger"
- "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/leakcheck"
- "google.golang.org/grpc/internal/testutils"
- "google.golang.org/grpc/keepalive"
- "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/resolver/passthrough"
- "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"
- )
- func init() {
- channelz.TurnOn()
- }
- 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 {
- 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", "v1", 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", balancer: "v1"}
- tcpTLSEnv = env{name: "tcp-tls-v1-balancer", network: "tcp", security: "tls", balancer: "v1"}
- 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 {
- t *testing.T
- e env
- ctx context.Context // valid for life of test, before tearDown
- cancel context.CancelFunc
- // Configurable knobs, after newTest returns:
- testServer testpb.TestServiceServer // nil means none
- healthServer *health.Server // nil means disabled
- maxStream uint32
- tapHandle tap.ServerInHandle
- maxMsgSize *int
- maxClientReceiveMsgSize *int
- maxClientSendMsgSize *int
- maxServerReceiveMsgSize *int
- maxServerSendMsgSize *int
- maxClientHeaderListSize *uint32
- maxServerHeaderListSize *uint32
- userAgent string
- // clientCompression and serverCompression are set to test the deprecated API
- // WithCompressor and WithDecompressor.
- clientCompression bool
- serverCompression bool
- // clientUseCompression is set 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
- unaryServerInt grpc.UnaryServerInterceptor
- streamServerInt grpc.StreamServerInterceptor
- unknownHandler grpc.StreamHandler
- sc <-chan grpc.ServiceConfig
- customCodec grpc.Codec
- serverInitialWindowSize int32
- serverInitialConnWindowSize int32
- clientInitialWindowSize int32
- clientInitialConnWindowSize int32
- perRPCCreds credentials.PerRPCCredentials
- customDialOptions []grpc.DialOption
- customServerOptions []grpc.ServerOption
- resolverScheme string
- cliKeepAlive *keepalive.ClientParameters
- svrKeepAlive *keepalive.ServerParameters
- // All test dialing is blocking by default. Set this to true if dial
- // should be non-blocking.
- nonBlockingDial bool
- // srv and srvAddr are set once startServer is called.
- srv stopper
- srvAddr string
- // srvs and srvAddrs are set once startServers is called.
- srvs []*grpc.Server
- 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()
- }
- if len(te.srvs) != 0 {
- 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.testServer = ts
- te.t.Logf("Running test in %s environment...", te.e.name)
- sopts := []grpc.ServerOption{grpc.MaxConcurrentStreams(te.maxStream)}
- if te.maxMsgSize != nil {
- sopts = append(sopts, grpc.MaxMsgSize(*te.maxMsgSize))
- }
- 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)
- }
- switch te.e.security {
- case "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))
- case "clientTimeoutCreds":
- sopts = append(sopts, grpc.Creds(&clientTimeoutCreds{}))
- }
- if te.customCodec != nil {
- sopts = append(sopts, grpc.CustomCodec(te.customCodec))
- }
- if te.svrKeepAlive != nil {
- sopts = append(sopts, grpc.KeepaliveParams(*te.svrKeepAlive))
- }
- sopts = append(sopts, te.customServerOptions...)
- s := grpc.NewServer(sopts...)
- te.srv = s
- if te.healthServer != nil {
- healthgrpc.RegisterHealthServer(s, te.healthServer)
- }
- if te.testServer != nil {
- testpb.RegisterTestServiceServer(s, te.testServer)
- }
- 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.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("Error creating TLS certificate: ", err)
- }
- hs := &http.Server{
- Handler: s,
- }
- err = http2.ConfigureServer(hs, &http2.Server{
- MaxConcurrentStreams: te.maxStream,
- })
- if err != nil {
- te.t.Fatal("error starting http2 server: ", err)
- }
- hs.TLSConfig.Certificates = []tls.Certificate{cert}
- tlsListener := tls.NewListener(lis, hs.TLSConfig)
- whs := &wrapHS{Listener: tlsListener, s: hs, conns: make(map[net.Conn]bool)}
- te.srv = whs
- go hs.Serve(whs)
- return lis
- }
- go s.Serve(lis)
- return lis
- }
- // TODO: delete wrapHS and wrapConn when Go1.6 and Go1.7 support are gone and
- // call s.Close and s.Shutdown instead.
- type wrapHS struct {
- sync.Mutex
- net.Listener
- s *http.Server
- conns map[net.Conn]bool
- }
- func (w *wrapHS) Accept() (net.Conn, error) {
- c, err := w.Listener.Accept()
- if err != nil {
- return nil, err
- }
- w.Lock()
- if w.conns == nil {
- w.Unlock()
- c.Close()
- return nil, errors.New("connection after listener closed")
- }
- w.conns[&wrapConn{Conn: c, hs: w}] = true
- w.Unlock()
- return c, nil
- }
- func (w *wrapHS) Stop() {
- w.Listener.Close()
- w.Lock()
- conns := w.conns
- w.conns = nil
- w.Unlock()
- for c := range conns {
- c.Close()
- }
- }
- // Poll for now..
- func (w *wrapHS) GracefulStop() {
- w.Listener.Close()
- for {
- w.Lock()
- l := len(w.conns)
- w.Unlock()
- if l == 0 {
- return
- }
- time.Sleep(50 * time.Millisecond)
- }
- }
- type wrapConn struct {
- net.Conn
- hs *wrapHS
- }
- func (w *wrapConn) Close() error {
- w.hs.Lock()
- delete(w.hs.conns, w.Conn)
- w.hs.Unlock()
- return w.Conn.Close()
- }
- func (te *test) startServerWithConnControl(ts testpb.TestServiceServer) *listenerWrapper {
- l := te.listenAndServe(ts, listenWithConnControl)
- return l.(*listenerWrapper)
- }
- // startServer starts a gRPC server listening. Callers should defer a
- // call to te.tearDown to clean up.
- func (te *test) startServer(ts testpb.TestServiceServer) {
- te.listenAndServe(ts, net.Listen)
- }
- 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.maxMsgSize != nil {
- opts = append(opts, grpc.WithMaxMsgSize(*te.maxMsgSize))
- }
- 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 "clientTimeoutCreds":
- opts = append(opts, grpc.WithTransportCredentials(&clientTimeoutCreds{}))
- 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 + ":///"
- }
- switch te.e.balancer {
- case "v1":
- opts = append(opts, grpc.WithBalancer(grpc.RoundRobin(nil)))
- case "round_robin":
- opts = append(opts, grpc.WithBalancerName(roundrobin.Name))
- }
- 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.CallCustomCodec(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"
- }
- if te.cliKeepAlive != nil {
- opts = append(opts, grpc.WithKeepaliveParams(*te.cliKeepAlive))
- }
- 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 TestContextDeadlineNotIgnored(t *testing.T) {
- defer leakcheck.Check(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 TestTimeoutOnDeadServer(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testTimeoutOnDeadServer(t, e)
- }
- }
- func testTimeoutOnDeadServer(t *testing.T, e env) {
- te := newTest(t, e)
- te.customDialOptions = []grpc.DialOption{grpc.WithWaitForHandshake()}
- 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.FailFast(false)); 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.FailFast(false))
- 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 TestServerGracefulStopIdempotent(t *testing.T) {
- defer leakcheck.Check(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 TestServerGoAway(t *testing.T) {
- defer leakcheck.Check(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.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.FailFast(false)); 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.
- if _, err := tc.EmptyCall(context.Background(), &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 TestServerGoAwayPendingRPC(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false))
- 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.FailFast(false)); 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.FailFast(false))
- 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 TestServerMultipleGoAwayPendingRPC(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false))
- 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.FailFast(false)); 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.FailFast(false)); 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 TestConcurrentClientConnCloseAndServerGoAway(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestConcurrentServerStopAndGoAway(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false))
- 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.FailFast(false)); 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.FailFast(false)); 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 TestClientConnCloseAfterGoAwayWithActiveStream(t *testing.T) {
- defer leakcheck.Check(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 TestFailFast(t *testing.T) {
- defer leakcheck.Check(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 TestGetMethodConfig(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- defer te.tearDown()
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- te.resolverScheme = r.Scheme()
- cc := te.clientConn()
- r.NewAddress([]resolver.Address{{Addr: te.srvAddr}})
- r.NewServiceConfig(`{
- "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.NewServiceConfig(`{
- "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 TestServiceConfigWaitForReady(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- defer te.tearDown()
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- // 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()
- r.NewAddress([]resolver.Address{{Addr: te.srvAddr}})
- r.NewServiceConfig(`{
- "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.FailFast(false)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- if _, err := tc.FullDuplexCall(context.Background(), grpc.FailFast(false)); 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.NewServiceConfig(`{
- "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 TestServiceConfigTimeout(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- defer te.tearDown()
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- // 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()
- r.NewAddress([]resolver.Address{{Addr: te.srvAddr}})
- r.NewServiceConfig(`{
- "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.FailFast(false)); 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.FailFast(false)); 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.NewServiceConfig(`{
- "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.FailFast(false)); 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.FailFast(false)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- cancel()
- }
- func TestServiceConfigMaxMsgSize(t *testing.T) {
- e := tcpClearRREnv
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- // 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)
- }
- scjs := `{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "UnaryCall"
- },
- {
- "service": "grpc.testing.TestService",
- "method": "FullDuplexCall"
- }
- ],
- "maxRequestMessageBytes": 2048,
- "maxResponseMessageBytes": 2048
- }
- ]
- }`
- // 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()
- r.NewAddress([]resolver.Address{{Addr: te1.srvAddr}})
- r.NewServiceConfig(scjs)
- 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.FailFast(false)); 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()
- r.NewAddress([]resolver.Address{{Addr: te2.srvAddr}})
- r.NewServiceConfig(scjs)
- 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.FailFast(false)); 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()
- r.NewAddress([]resolver.Address{{Addr: te3.srvAddr}})
- r.NewServiceConfig(scjs)
- 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.FailFast(false)); 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 TestStreamingRPCWithTimeoutInServiceConfigRecv(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- te.startServer(&testServer{security: tcpClearRREnv.security})
- defer te.tearDown()
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- te.resolverScheme = r.Scheme()
- te.nonBlockingDial = true
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- r.NewAddress([]resolver.Address{{Addr: te.srvAddr}})
- r.NewServiceConfig(`{
- "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.FailFast(false))
- 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 TestMaxMsgSizeClientDefault(t *testing.T) {
- defer leakcheck.Check(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 TestMaxMsgSizeClientAPI(t *testing.T) {
- defer leakcheck.Check(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 TestMaxMsgSizeServerAPI(t *testing.T) {
- defer leakcheck.Check(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 TestTap(t *testing.T) {
- defer leakcheck.Check(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)
- }
- }
- func healthCheck(d time.Duration, cc *grpc.ClientConn, serviceName string) (*healthpb.HealthCheckResponse, error) {
- ctx, cancel := context.WithTimeout(context.Background(), d)
- defer cancel()
- hc := healthgrpc.NewHealthClient(cc)
- req := &healthpb.HealthCheckRequest{
- Service: serviceName,
- }
- return hc.Check(ctx, req)
- }
- func TestHealthCheckOnSuccess(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthCheckOnSuccess(t, e)
- }
- }
- func testHealthCheckOnSuccess(t *testing.T, e env) {
- te := newTest(t, e)
- hs := health.NewServer()
- hs.SetServingStatus("grpc.health.v1.Health", 1)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- if _, err := healthCheck(1*time.Second, cc, "grpc.health.v1.Health"); err != nil {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, <nil>", err)
- }
- }
- func TestHealthCheckOnFailure(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthCheckOnFailure(t, e)
- }
- }
- func testHealthCheckOnFailure(t *testing.T, e env) {
- defer leakcheck.Check(t)
- te := newTest(t, e)
- te.declareLogNoise(
- "Failed to dial ",
- "grpc: the client connection is closing; please retry",
- )
- hs := health.NewServer()
- hs.SetServingStatus("grpc.health.v1.HealthCheck", 1)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- wantErr := status.Error(codes.DeadlineExceeded, "context deadline exceeded")
- if _, err := healthCheck(0*time.Second, cc, "grpc.health.v1.Health"); !reflect.DeepEqual(err, wantErr) {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, error code %s", err, codes.DeadlineExceeded)
- }
- awaitNewConnLogOutput()
- }
- func TestHealthCheckOff(t *testing.T) {
- defer leakcheck.Check(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.startServer(&testServer{security: e.security})
- defer te.tearDown()
- want := status.Error(codes.Unimplemented, "unknown service grpc.health.v1.Health")
- if _, err := healthCheck(1*time.Second, te.clientConn(), ""); !reflect.DeepEqual(err, want) {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, %v", err, want)
- }
- }
- func TestHealthWatchMultipleClients(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthWatchMultipleClients(t, e)
- }
- }
- func testHealthWatchMultipleClients(t *testing.T, e env) {
- const service = "grpc.health.v1.Health1"
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- hc := healthgrpc.NewHealthClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- req := &healthpb.HealthCheckRequest{
- Service: service,
- }
- stream1, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- stream2, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream2, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream2, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- func TestHealthWatchSameStatus(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthWatchSameStatus(t, e)
- }
- }
- func testHealthWatchSameStatus(t *testing.T, e env) {
- const service = "grpc.health.v1.Health1"
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- hc := healthgrpc.NewHealthClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- req := &healthpb.HealthCheckRequest{
- Service: service,
- }
- stream1, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_SERVING)
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_SERVING)
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_SERVING)
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream1, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- func TestHealthWatchServiceStatusSetBeforeStartingServer(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthWatchSetServiceStatusBeforeStartingServer(t, e)
- }
- }
- func testHealthWatchSetServiceStatusBeforeStartingServer(t *testing.T, e env) {
- const service = "grpc.health.v1.Health1"
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_SERVING)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- hc := healthgrpc.NewHealthClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- req := &healthpb.HealthCheckRequest{
- Service: service,
- }
- stream, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- }
- func TestHealthWatchDefaultStatusChange(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthWatchDefaultStatusChange(t, e)
- }
- }
- func testHealthWatchDefaultStatusChange(t *testing.T, e env) {
- const service = "grpc.health.v1.Health1"
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- hc := healthgrpc.NewHealthClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- req := &healthpb.HealthCheckRequest{
- Service: service,
- }
- stream, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVICE_UNKNOWN)
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_SERVING)
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- }
- func TestHealthWatchSetServiceStatusBeforeClientCallsWatch(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthWatchSetServiceStatusBeforeClientCallsWatch(t, e)
- }
- }
- func testHealthWatchSetServiceStatusBeforeClientCallsWatch(t *testing.T, e env) {
- const service = "grpc.health.v1.Health1"
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- hc := healthgrpc.NewHealthClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- req := &healthpb.HealthCheckRequest{
- Service: service,
- }
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_SERVING)
- stream, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- }
- func TestHealthWatchOverallServerHealthChange(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthWatchOverallServerHealthChange(t, e)
- }
- }
- func testHealthWatchOverallServerHealthChange(t *testing.T, e env) {
- const service = ""
- hs := health.NewServer()
- te := newTest(t, e)
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- hc := healthgrpc.NewHealthClient(cc)
- ctx, cancel := context.WithCancel(context.Background())
- defer cancel()
- req := &healthpb.HealthCheckRequest{
- Service: service,
- }
- stream, err := hc.Watch(ctx, req)
- if err != nil {
- t.Fatalf("error: %v", err)
- }
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_SERVING)
- hs.SetServingStatus(service, healthpb.HealthCheckResponse_NOT_SERVING)
- healthWatchChecker(t, stream, healthpb.HealthCheckResponse_NOT_SERVING)
- }
- func healthWatchChecker(t *testing.T, stream healthgrpc.Health_WatchClient, expectedServingStatus healthpb.HealthCheckResponse_ServingStatus) {
- response, err := stream.Recv()
- if err != nil {
- t.Fatalf("error on %v.Recv(): %v", stream, err)
- }
- if response.Status != expectedServingStatus {
- t.Fatalf("response.Status is %v (%v expected)", response.Status, expectedServingStatus)
- }
- }
- func TestUnknownHandler(t *testing.T) {
- defer leakcheck.Check(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()
- want := status.Error(codes.Unauthenticated, "user unauthenticated")
- if _, err := healthCheck(1*time.Second, te.clientConn(), ""); !reflect.DeepEqual(err, want) {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, %v", err, want)
- }
- }
- func TestHealthCheckServingStatus(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testHealthCheckServingStatus(t, e)
- }
- }
- func testHealthCheckServingStatus(t *testing.T, e env) {
- te := newTest(t, e)
- hs := health.NewServer()
- te.healthServer = hs
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- cc := te.clientConn()
- out, err := healthCheck(1*time.Second, cc, "")
- if err != nil {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, <nil>", err)
- }
- if out.Status != healthpb.HealthCheckResponse_SERVING {
- t.Fatalf("Got the serving status %v, want SERVING", out.Status)
- }
- wantErr := status.Error(codes.NotFound, "unknown service")
- if _, err := healthCheck(1*time.Second, cc, "grpc.health.v1.Health"); !reflect.DeepEqual(err, wantErr) {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, error code %s", err, codes.NotFound)
- }
- hs.SetServingStatus("grpc.health.v1.Health", healthpb.HealthCheckResponse_SERVING)
- out, err = healthCheck(1*time.Second, cc, "grpc.health.v1.Health")
- if err != nil {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, <nil>", err)
- }
- if out.Status != healthpb.HealthCheckResponse_SERVING {
- t.Fatalf("Got the serving status %v, want SERVING", out.Status)
- }
- hs.SetServingStatus("grpc.health.v1.Health", healthpb.HealthCheckResponse_NOT_SERVING)
- out, err = healthCheck(1*time.Second, cc, "grpc.health.v1.Health")
- if err != nil {
- t.Fatalf("Health/Check(_, _) = _, %v, want _, <nil>", err)
- }
- if out.Status != healthpb.HealthCheckResponse_NOT_SERVING {
- t.Fatalf("Got the serving status %v, want NOT_SERVING", out.Status)
- }
- }
- func TestEmptyUnaryWithUserAgent(t *testing.T) {
- defer leakcheck.Check(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 TestFailedEmptyUnary(t *testing.T) {
- defer leakcheck.Check(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{}); !reflect.DeepEqual(err, wantErr) {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %v", err, wantErr)
- }
- }
- func TestLargeUnary(t *testing.T) {
- defer leakcheck.Check(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 TestExceedMsgLimit(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testExceedMsgLimit(t, e)
- }
- }
- func testExceedMsgLimit(t *testing.T, e env) {
- te := newTest(t, e)
- te.maxMsgSize = newInt(1024)
- te.startServer(&testServer{security: e.security})
- defer te.tearDown()
- tc := testpb.NewTestServiceClient(te.clientConn())
- argSize := int32(*te.maxMsgSize + 1)
- const smallSize = 1
- payload, err := newPayload(testpb.PayloadType_COMPRESSABLE, argSize)
- if err != nil {
- t.Fatal(err)
- }
- smallPayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, smallSize)
- if err != nil {
- t.Fatal(err)
- }
- // Test on server side for unary RPC.
- req := &testpb.SimpleRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseSize: smallSize,
- Payload: payload,
- }
- 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 on client side for unary RPC.
- req.ResponseSize = int32(*te.maxMsgSize) + 1
- 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)
- }
- // Test on server side for streaming RPC.
- stream, err := tc.FullDuplexCall(te.ctx)
- if err != nil {
- t.Fatalf("%v.FullDuplexCall(_) = _, %v, want <nil>", tc, err)
- }
- respParam := []*testpb.ResponseParameters{
- {
- Size: 1,
- },
- }
- spayload, err := newPayload(testpb.PayloadType_COMPRESSABLE, int32(*te.maxMsgSize+1))
- if err != nil {
- t.Fatal(err)
- }
- sreq := &testpb.StreamingOutputCallRequest{
- ResponseType: testpb.PayloadType_COMPRESSABLE,
- ResponseParameters: respParam,
- Payload: spayload,
- }
- 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 = int32(*te.maxMsgSize) + 1
- 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 TestPeerClientSide(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestPeerNegative(t *testing.T) {
- defer leakcheck.Check(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 TestPeerFailedRPC(t *testing.T) {
- defer leakcheck.Check(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 TestMetadataUnaryRPC(t *testing.T) {
- defer leakcheck.Check(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 TestMultipleSetTrailerUnaryRPC(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestMultipleSetTrailerStreamingRPC(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false))
- 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 TestSetAndSendHeaderUnaryRPC(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestMultipleSetHeaderUnaryRPC(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestMultipleSetHeaderUnaryRPCError(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestSetAndSendHeaderStreamingRPC(t *testing.T) {
- defer leakcheck.Check(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 TestMultipleSetHeaderStreamingRPC(t *testing.T) {
- defer leakcheck.Check(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 TestMultipleSetHeaderStreamingRPCError(t *testing.T) {
- defer leakcheck.Check(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)
- }
- }
- // TestMalformedHTTP2Metedata verfies the returned error when the client
- // sends an illegal metadata.
- func TestMalformedHTTP2Metadata(t *testing.T) {
- defer leakcheck.Check(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 TestTransparentRetry(t *testing.T) {
- defer leakcheck.Check(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,
- errCode: codes.Unavailable, // We won't retry on fail fast.
- }}
- for _, tc := range testCases {
- attempts = 0
- successAttempt = tc.successAttempt
- ctx, cancel := context.WithTimeout(context.Background(), time.Second)
- _, err := tsc.EmptyCall(ctx, &testpb.Empty{}, grpc.FailFast(tc.failFast))
- cancel()
- if status.Code(err) != tc.errCode {
- t.Errorf("%+v: tsc.EmptyCall(_, _) = _, %v, want _, Code=%v", tc, err, tc.errCode)
- }
- }
- }
- func TestCancel(t *testing.T) {
- defer leakcheck.Check(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 TestCancelNoIO(t *testing.T) {
- defer leakcheck.Check(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 TestNoService(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false))
- 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 TestPingPong(t *testing.T) {
- defer leakcheck.Check(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 TestMetadataStreamingRPC(t *testing.T) {
- defer leakcheck.Check(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 TestServerStreaming(t *testing.T) {
- defer leakcheck.Check(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 TestFailedServerStreaming(t *testing.T) {
- defer leakcheck.Check(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(); !reflect.DeepEqual(err, wantErr) {
- t.Fatalf("%v.Recv() = _, %v, want _, %v", stream, err, wantErr)
- }
- }
- // 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 TestServerStreamingConcurrent(t *testing.T) {
- defer leakcheck.Check(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 TestClientStreaming(t *testing.T) {
- defer leakcheck.Check(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 TestClientStreamingError(t *testing.T) {
- defer leakcheck.Check(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 TestExceedMaxStreamsLimit(t *testing.T) {
- defer leakcheck.Check(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 TestStreamsQuotaRecovery(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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 TestCompressServerHasNoSupport(t *testing.T) {
- defer leakcheck.Check(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 TestCompressOK(t *testing.T) {
- defer leakcheck.Check(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 TestIdentityEncoding(t *testing.T) {
- defer leakcheck.Check(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 TestUnaryClientInterceptor(t *testing.T) {
- defer leakcheck.Check(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 TestStreamClientInterceptor(t *testing.T) {
- defer leakcheck.Check(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 TestUnaryServerInterceptor(t *testing.T) {
- defer leakcheck.Check(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 TestStreamServerInterceptor(t *testing.T) {
- defer leakcheck.Check(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 TestClientRequestBodyErrorUnexpectedEOF(t *testing.T) {
- defer leakcheck.Check(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 TestClientRequestBodyErrorCloseAfterLength(t *testing.T) {
- defer leakcheck.Check(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 TestClientRequestBodyErrorCancel(t *testing.T) {
- defer leakcheck.Check(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 TestClientRequestBodyErrorCancelStreamingInput(t *testing.T) {
- defer leakcheck.Check(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 TestClientResourceExhaustedCancelFullDuplex(t *testing.T) {
- defer leakcheck.Check(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)
- 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 clientTimeoutCreds struct {
- timeoutReturned bool
- }
- func (c *clientTimeoutCreds) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- if !c.timeoutReturned {
- c.timeoutReturned = true
- return nil, nil, context.DeadlineExceeded
- }
- return rawConn, nil, nil
- }
- func (c *clientTimeoutCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return rawConn, nil, nil
- }
- func (c *clientTimeoutCreds) Info() credentials.ProtocolInfo {
- return credentials.ProtocolInfo{}
- }
- func (c *clientTimeoutCreds) Clone() credentials.TransportCredentials {
- return nil
- }
- func (c *clientTimeoutCreds) OverrideServerName(s string) error {
- return nil
- }
- func TestNonFailFastRPCSucceedOnTimeoutCreds(t *testing.T) {
- te := newTest(t, env{name: "timeout-cred", network: "tcp", security: "clientTimeoutCreds", balancer: "v1"})
- te.userAgent = testAppUA
- te.startServer(&testServer{security: te.e.security})
- defer te.tearDown()
- cc := te.clientConn()
- tc := testpb.NewTestServiceClient(cc)
- // This unary call should succeed, because ClientHandshake will succeed for the second time.
- if _, err := tc.EmptyCall(context.Background(), &testpb.Empty{}, grpc.FailFast(false)); err != nil {
- te.t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want <nil>", err)
- }
- }
- type serverDispatchCred struct {
- rawConnCh chan net.Conn
- }
- func newServerDispatchCred() *serverDispatchCred {
- return &serverDispatchCred{
- rawConnCh: make(chan net.Conn, 1),
- }
- }
- func (c *serverDispatchCred) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return rawConn, nil, nil
- }
- func (c *serverDispatchCred) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- select {
- case c.rawConnCh <- rawConn:
- default:
- }
- return nil, nil, credentials.ErrConnDispatched
- }
- func (c *serverDispatchCred) Info() credentials.ProtocolInfo {
- return credentials.ProtocolInfo{}
- }
- func (c *serverDispatchCred) Clone() credentials.TransportCredentials {
- return nil
- }
- func (c *serverDispatchCred) OverrideServerName(s string) error {
- return nil
- }
- func (c *serverDispatchCred) getRawConn() net.Conn {
- return <-c.rawConnCh
- }
- func TestServerCredsDispatch(t *testing.T) {
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to listen: %v", err)
- }
- cred := newServerDispatchCred()
- s := grpc.NewServer(grpc.Creds(cred))
- go s.Serve(lis)
- defer s.Stop()
- cc, err := grpc.Dial(lis.Addr().String(), grpc.WithTransportCredentials(cred))
- if err != nil {
- t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
- }
- defer cc.Close()
- rawConn := cred.getRawConn()
- // Give grpc a chance to see the error and potentially close the connection.
- // And check that connection is not closed after that.
- time.Sleep(100 * time.Millisecond)
- // Check rawConn is not closed.
- if n, err := rawConn.Write([]byte{0}); n <= 0 || err != nil {
- t.Errorf("Read() = %v, %v; want n>0, <nil>", n, err)
- }
- }
- type authorityCheckCreds struct {
- got string
- }
- func (c *authorityCheckCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return rawConn, nil, nil
- }
- func (c *authorityCheckCreds) ClientHandshake(ctx context.Context, authority string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- c.got = authority
- return rawConn, nil, nil
- }
- func (c *authorityCheckCreds) Info() credentials.ProtocolInfo {
- return credentials.ProtocolInfo{}
- }
- func (c *authorityCheckCreds) Clone() credentials.TransportCredentials {
- return c
- }
- func (c *authorityCheckCreds) OverrideServerName(s string) error {
- return nil
- }
- // This test makes sure that the authority client handshake gets is the endpoint
- // in dial target, not the resolved ip address.
- func TestCredsHandshakeAuthority(t *testing.T) {
- const testAuthority = "test.auth.ori.ty"
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- t.Fatalf("Failed to listen: %v", err)
- }
- cred := &authorityCheckCreds{}
- s := grpc.NewServer()
- go s.Serve(lis)
- defer s.Stop()
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- cc, err := grpc.Dial(r.Scheme()+":///"+testAuthority, grpc.WithTransportCredentials(cred))
- if err != nil {
- t.Fatalf("grpc.Dial(%q) = %v", lis.Addr().String(), err)
- }
- defer cc.Close()
- r.NewAddress([]resolver.Address{{Addr: lis.Addr().String()}})
- ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
- defer cancel()
- for {
- s := cc.GetState()
- if s == connectivity.Ready {
- break
- }
- if !cc.WaitForStateChange(ctx, s) {
- // ctx got timeout or canceled.
- t.Fatalf("ClientConn is not ready after 100 ms")
- }
- }
- if cred.got != testAuthority {
- t.Fatalf("client creds got authority: %q, want: %q", cred.got, testAuthority)
- }
- }
- 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 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.FailFast(true)); status.Code(err) != codes.Unavailable {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want <Unavailable>", err)
- }
- }
- func TestFlowControlLogicalRace(t *testing.T) {
- // Test for a regression of https://github.com/grpc/grpc-go/issues/632,
- // and other flow control bugs.
- defer leakcheck.Check(t)
- 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)
- 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
- 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) 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 {
- r, cleanup := manual.GenerateAndRegisterManualResolver()
- ss.r = r
- ss.cleanups = append(ss.cleanups, cleanup)
- lis, err := net.Listen("tcp", "localhost:0")
- if err != nil {
- return fmt.Errorf(`net.Listen("tcp", "localhost:0") = %v`, err)
- }
- 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)
- target := ss.r.Scheme() + ":///" + lis.Addr().String()
- opts := append([]grpc.DialOption{grpc.WithInsecure()}, dopts...)
- cc, err := grpc.Dial(target, opts...)
- if err != nil {
- return fmt.Errorf("grpc.Dial(%q) = %v", target, err)
- }
- ss.cc = cc
- ss.r.NewAddress([]resolver.Address{{Addr: lis.Addr().String()}})
- 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) 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 TestGRPCMethod(t *testing.T) {
- defer leakcheck.Check(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 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 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 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 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 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 TestConfigurableWindowSizeWithLargeWindow(t *testing.T) {
- defer leakcheck.Check(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 TestConfigurableWindowSizeWithSmallWindow(t *testing.T) {
- defer leakcheck.Check(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)
- }
- }
- var (
- // test authdata
- authdata = map[string]string{
- "test-key": "test-value",
- "test-key2-bin": string([]byte{1, 2, 3}),
- }
- )
- type testPerRPCCredentials struct{}
- func (cr testPerRPCCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
- return authdata, nil
- }
- func (cr testPerRPCCredentials) RequireTransportSecurity() bool {
- return false
- }
- func authHandle(ctx context.Context, info *tap.Info) (context.Context, error) {
- md, ok := metadata.FromIncomingContext(ctx)
- if !ok {
- return ctx, fmt.Errorf("didn't find metadata in context")
- }
- for k, vwant := range authdata {
- vgot, ok := md[k]
- if !ok {
- return ctx, fmt.Errorf("didn't find authdata key %v in context", k)
- }
- if vgot[0] != vwant {
- return ctx, fmt.Errorf("for key %v, got value %v, want %v", k, vgot, vwant)
- }
- }
- return ctx, nil
- }
- func TestPerRPCCredentialsViaDialOptions(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testPerRPCCredentialsViaDialOptions(t, e)
- }
- }
- func testPerRPCCredentialsViaDialOptions(t *testing.T, e env) {
- te := newTest(t, e)
- te.tapHandle = authHandle
- te.perRPCCreds = testPerRPCCredentials{}
- 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("Test failed. Reason: %v", err)
- }
- }
- func TestPerRPCCredentialsViaCallOptions(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testPerRPCCredentialsViaCallOptions(t, e)
- }
- }
- func testPerRPCCredentialsViaCallOptions(t *testing.T, e env) {
- te := newTest(t, e)
- te.tapHandle = authHandle
- 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.PerRPCCredentials(testPerRPCCredentials{})); err != nil {
- t.Fatalf("Test failed. Reason: %v", err)
- }
- }
- func TestPerRPCCredentialsViaDialOptionsAndCallOptions(t *testing.T) {
- defer leakcheck.Check(t)
- for _, e := range listTestEnv() {
- testPerRPCCredentialsViaDialOptionsAndCallOptions(t, e)
- }
- }
- func testPerRPCCredentialsViaDialOptionsAndCallOptions(t *testing.T, e env) {
- te := newTest(t, e)
- te.perRPCCreds = testPerRPCCredentials{}
- // When credentials are provided via both dial options and call options,
- // we apply both sets.
- te.tapHandle = func(ctx context.Context, _ *tap.Info) (context.Context, error) {
- md, ok := metadata.FromIncomingContext(ctx)
- if !ok {
- return ctx, fmt.Errorf("couldn't find metadata in context")
- }
- for k, vwant := range authdata {
- vgot, ok := md[k]
- if !ok {
- return ctx, fmt.Errorf("couldn't find metadata for key %v", k)
- }
- if len(vgot) != 2 {
- return ctx, fmt.Errorf("len of value for key %v was %v, want 2", k, len(vgot))
- }
- if vgot[0] != vwant || vgot[1] != vwant {
- return ctx, fmt.Errorf("value for %v was %v, want [%v, %v]", k, vgot, vwant, vwant)
- }
- }
- return ctx, nil
- }
- 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.PerRPCCredentials(testPerRPCCredentials{})); err != nil {
- t.Fatalf("Test failed. Reason: %v", err)
- }
- }
- func TestWaitForReadyConnection(t *testing.T) {
- defer leakcheck.Check(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) String() string {
- return "Fermat's near-miss."
- }
- func TestEncodeDoesntPanic(t *testing.T) {
- defer leakcheck.Check(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 TestSvrWriteStatusEarlyWrite(t *testing.T) {
- defer leakcheck.Check(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 TestServiceConfigGetMethodConfigTD(t *testing.T) {
- defer leakcheck.Check(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 TestServiceConfigWaitForReadyTD(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want _, %s", err, codes.DeadlineExceeded)
- }
- if _, err := tc.FullDuplexCall(context.Background(), grpc.FailFast(false)); 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 TestServiceConfigTimeoutTD(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false)); 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.FailFast(false)); 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.FailFast(false)); 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.FailFast(false)); status.Code(err) != codes.DeadlineExceeded {
- t.Fatalf("TestService/FullDuplexCall(_) = _, %v, want %s", err, codes.DeadlineExceeded)
- }
- cancel()
- }
- func TestServiceConfigMaxMsgSizeTD(t *testing.T) {
- defer leakcheck.Check(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 TestMethodFromServerStream(t *testing.T) {
- defer leakcheck.Check(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 TestInterceptorCanAccessCallOptions(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false),
- 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.FailFast(true),
- 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 TestCompressorRegister(t *testing.T) {
- defer leakcheck.Check(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 TestServeExitsWhenListenerClosed(t *testing.T) {
- defer leakcheck.Check(t)
- ss := &stubServer{
- emptyCall: func(context.Context, *testpb.Empty) (*testpb.Empty, error) {
- return &testpb.Empty{}, nil
- },
- }
- s := grpc.NewServer()
- 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 TestStatusInvalidUTF8Message(t *testing.T) {
- defer leakcheck.Check(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 TestStatusInvalidUTF8Details(t *testing.T) {
- defer leakcheck.Check(t)
- 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 TestClientDoesntDeadlockWhileWritingErrornousLargeMessages(t *testing.T) {
- defer leakcheck.Check(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()
- }
- const clientAlwaysFailCredErrorMsg = "clientAlwaysFailCred always fails"
- var errClientAlwaysFailCred = errors.New(clientAlwaysFailCredErrorMsg)
- type clientAlwaysFailCred struct{}
- func (c clientAlwaysFailCred) ClientHandshake(ctx context.Context, addr string, rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return nil, nil, errClientAlwaysFailCred
- }
- func (c clientAlwaysFailCred) ServerHandshake(rawConn net.Conn) (net.Conn, credentials.AuthInfo, error) {
- return rawConn, nil, nil
- }
- func (c clientAlwaysFailCred) Info() credentials.ProtocolInfo {
- return credentials.ProtocolInfo{}
- }
- func (c clientAlwaysFailCred) Clone() credentials.TransportCredentials {
- return nil
- }
- func (c clientAlwaysFailCred) OverrideServerName(s string) error {
- return nil
- }
- func TestFailFastRPCErrorOnBadCertificates(t *testing.T) {
- te := newTest(t, env{name: "bad-cred", network: "tcp", security: "clientAlwaysFailCred", balancer: "round_robin"})
- te.startServer(&testServer{security: te.e.security})
- defer te.tearDown()
- opts := []grpc.DialOption{grpc.WithTransportCredentials(clientAlwaysFailCred{})}
- ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
- defer cancel()
- cc, err := grpc.DialContext(ctx, te.srvAddr, opts...)
- if err != nil {
- t.Fatalf("Dial(_) = %v, want %v", err, nil)
- }
- defer cc.Close()
- tc := testpb.NewTestServiceClient(cc)
- for i := 0; i < 1000; i++ {
- // This loop runs for at most 1 second. The first several RPCs will fail
- // with Unavailable because the connection hasn't started. When the
- // first connection failed with creds error, the next RPC should also
- // fail with the expected error.
- if _, err = tc.EmptyCall(context.Background(), &testpb.Empty{}); strings.Contains(err.Error(), clientAlwaysFailCredErrorMsg) {
- return
- }
- time.Sleep(time.Millisecond)
- }
- te.t.Fatalf("TestService/EmptyCall(_, _) = _, %v, want err.Error() contains %q", err, clientAlwaysFailCredErrorMsg)
- }
- func TestRPCTimeout(t *testing.T) {
- defer leakcheck.Check(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 TestDisabledIOBuffers(t *testing.T) {
- defer leakcheck.Check(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.FailFast(false))
- 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 TestServerMaxHeaderListSizeClientUserViolation(t *testing.T) {
- defer leakcheck.Check(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 TestClientMaxHeaderListSizeServerUserViolation(t *testing.T) {
- defer leakcheck.Check(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 TestServerMaxHeaderListSizeClientIntentionalViolation(t *testing.T) {
- defer leakcheck.Check(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 TestClientMaxHeaderListSizeServerIntentionalViolation(t *testing.T) {
- defer leakcheck.Check(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.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 TestNetPipeConn(t *testing.T) {
- // This test will block indefinitely if grpc writes both client and server
- // prefaces without either reading from the Conn.
- defer leakcheck.Check(t)
- 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 TestLargeTimeout(t *testing.T) {
- defer leakcheck.Check(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 := deadline.Sub(time.Now())
- 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 TestGoAwayThenClose(t *testing.T) {
- defer leakcheck.Check(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()
- ts1 := &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, ts1)
- 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()
- conn2Ready := grpcsync.NewEvent()
- ts2 := &funcServer{unaryCall: func(ctx context.Context, in *testpb.SimpleRequest) (*testpb.SimpleResponse, error) {
- conn2Ready.Fire()
- return &testpb.SimpleResponse{}, nil
- }}
- testpb.RegisterTestServiceServer(s2, ts2)
- go s2.Serve(lis2)
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- r.InitialAddrs([]resolver.Address{
- {Addr: lis1.Addr().String()},
- {Addr: lis2.Addr().String()},
- })
- cc, err := grpc.DialContext(ctx, r.Scheme()+":///", 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)
- }
- // 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 TestRPCWaitsForResolver(t *testing.T) {
- te := testServiceConfigSetup(t, tcpClearRREnv)
- te.startServer(&testServer{security: tcpClearRREnv.security})
- defer te.tearDown()
- r, rcleanup := manual.GenerateAndRegisterManualResolver()
- defer rcleanup()
- te.resolverScheme = r.Scheme()
- te.nonBlockingDial = true
- cc := te.clientConn()
- 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.NewServiceConfig(`{
- "methodConfig": [
- {
- "name": [
- {
- "service": "grpc.testing.TestService",
- "method": "UnaryCall"
- }
- ],
- "maxRequestMessageBytes": 0
- }
- ]
- }`)
- r.NewAddress([]resolver.Address{{Addr: te.srvAddr}})
- }()
- // 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)
- }
- }
|